Skip to content

Commit a5e1786

Browse files
committed
Expose Swift search paths in validateSerializedAST
This is for the benefit of LLDB, which currently does an expensive import of all modules to get to the same information. rdar://40097459
1 parent c790171 commit a5e1786

File tree

5 files changed

+66
-43
lines changed

5 files changed

+66
-43
lines changed

Diff for: include/swift/Serialization/Validation.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ class ExtendedValidationInfo {
187187
}
188188
};
189189

190+
struct SearchPath {
191+
std::string Path;
192+
bool IsFramework;
193+
bool IsSystem;
194+
};
195+
190196
/// Returns info about the serialized AST in the given data.
191197
///
192198
/// If the returned status is anything but Status::Valid, the serialized data
@@ -216,7 +222,8 @@ ValidationInfo validateSerializedAST(
216222
bool requiresRevisionMatch = true,
217223
ExtendedValidationInfo *extendedInfo = nullptr,
218224
SmallVectorImpl<SerializationOptions::FileDependency> *dependencies =
219-
nullptr);
225+
nullptr,
226+
SmallVectorImpl<SearchPath> *searchPaths = nullptr);
220227

221228
/// Emit diagnostics explaining a failure to load a serialized AST.
222229
///

Diff for: lib/Serialization/ModuleFileSharedCore.cpp

+39-28
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ static ValidationInfo validateControlBlock(
401401

402402
static bool validateInputBlock(
403403
llvm::BitstreamCursor &cursor, SmallVectorImpl<uint64_t> &scratch,
404-
SmallVectorImpl<SerializationOptions::FileDependency> &dependencies) {
404+
SmallVectorImpl<SerializationOptions::FileDependency> *dependencies,
405+
SmallVectorImpl<SearchPath> *searchPaths) {
405406
SmallVector<StringRef, 4> dependencyDirectories;
406407
SmallString<256> dependencyFullPathBuffer;
407408

@@ -430,33 +431,43 @@ static bool validateInputBlock(
430431
}
431432
unsigned kind = maybeKind.get();
432433
switch (kind) {
433-
case input_block::FILE_DEPENDENCY: {
434-
bool isHashBased = scratch[2] != 0;
435-
bool isSDKRelative = scratch[3] != 0;
436-
437-
StringRef path = blobData;
438-
size_t directoryIndex = scratch[4];
439-
if (directoryIndex != 0) {
440-
if (directoryIndex > dependencyDirectories.size())
441-
return true;
442-
dependencyFullPathBuffer = dependencyDirectories[directoryIndex-1];
443-
llvm::sys::path::append(dependencyFullPathBuffer, blobData);
444-
path = dependencyFullPathBuffer;
445-
}
434+
case input_block::FILE_DEPENDENCY:
435+
if (dependencies) {
436+
bool isHashBased = scratch[2] != 0;
437+
bool isSDKRelative = scratch[3] != 0;
438+
439+
StringRef path = blobData;
440+
size_t directoryIndex = scratch[4];
441+
if (directoryIndex != 0) {
442+
if (directoryIndex > dependencyDirectories.size())
443+
return true;
444+
dependencyFullPathBuffer = dependencyDirectories[directoryIndex - 1];
445+
llvm::sys::path::append(dependencyFullPathBuffer, blobData);
446+
path = dependencyFullPathBuffer;
447+
}
446448

447-
if (isHashBased) {
448-
dependencies.push_back(
449-
SerializationOptions::FileDependency::hashBased(
450-
path, isSDKRelative, scratch[0], scratch[1]));
451-
} else {
452-
dependencies.push_back(
453-
SerializationOptions::FileDependency::modTimeBased(
454-
path, isSDKRelative, scratch[0], scratch[1]));
449+
if (isHashBased)
450+
dependencies->push_back(
451+
SerializationOptions::FileDependency::hashBased(
452+
path, isSDKRelative, scratch[0], scratch[1]));
453+
else
454+
dependencies->push_back(
455+
SerializationOptions::FileDependency::modTimeBased(
456+
path, isSDKRelative, scratch[0], scratch[1]));
455457
}
456458
break;
457-
}
458459
case input_block::DEPENDENCY_DIRECTORY:
459-
dependencyDirectories.push_back(blobData);
460+
if (dependencies)
461+
dependencyDirectories.push_back(blobData);
462+
break;
463+
case input_block::SEARCH_PATH:
464+
if (searchPaths) {
465+
bool isFramework;
466+
bool isSystem;
467+
input_block::SearchPathLayout::readRecord(scratch, isFramework,
468+
isSystem);
469+
searchPaths->push_back({std::string(blobData), isFramework, isSystem});
470+
}
460471
break;
461472
default:
462473
// Unknown metadata record, possibly for use by a future version of the
@@ -467,7 +478,6 @@ static bool validateInputBlock(
467478
return false;
468479
}
469480

470-
471481
bool serialization::isSerializedAST(StringRef data) {
472482
StringRef signatureStr(reinterpret_cast<const char *>(SWIFTMODULE_SIGNATURE),
473483
llvm::array_lengthof(SWIFTMODULE_SIGNATURE));
@@ -477,7 +487,8 @@ bool serialization::isSerializedAST(StringRef data) {
477487
ValidationInfo serialization::validateSerializedAST(
478488
StringRef data, bool requiresOSSAModules, StringRef requiredSDK,
479489
bool requiresRevisionMatch, ExtendedValidationInfo *extendedInfo,
480-
SmallVectorImpl<SerializationOptions::FileDependency> *dependencies) {
490+
SmallVectorImpl<SerializationOptions::FileDependency> *dependencies,
491+
SmallVectorImpl<SearchPath> *searchPaths) {
481492
ValidationInfo result;
482493

483494
// Check 32-bit alignment.
@@ -523,7 +534,7 @@ ValidationInfo serialization::validateSerializedAST(
523534
extendedInfo, localObfuscator);
524535
if (result.status == Status::Malformed)
525536
return result;
526-
} else if (dependencies &&
537+
} else if ((dependencies || searchPaths) &&
527538
result.status == Status::Valid &&
528539
topLevelEntry.ID == INPUT_BLOCK_ID) {
529540
if (llvm::Error Err = cursor.EnterSubBlock(INPUT_BLOCK_ID)) {
@@ -532,7 +543,7 @@ ValidationInfo serialization::validateSerializedAST(
532543
result.status = Status::Malformed;
533544
return result;
534545
}
535-
if (validateInputBlock(cursor, scratch, *dependencies)) {
546+
if (validateInputBlock(cursor, scratch, dependencies, searchPaths)) {
536547
result.status = Status::Malformed;
537548
return result;
538549
}

Diff for: lib/Serialization/ModuleFileSharedCore.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,11 @@ class ModuleFileSharedCore {
151151
/// All modules this module depends on.
152152
SmallVector<Dependency, 8> Dependencies;
153153

154-
struct SearchPath {
155-
std::string Path;
156-
bool IsFramework;
157-
bool IsSystem;
158-
};
159154
/// Search paths this module may provide.
160155
///
161156
/// This is not intended for use by frameworks, but may show up in debug
162157
/// modules.
163-
std::vector<SearchPath> SearchPaths;
158+
std::vector<serialization::SearchPath> SearchPaths;
164159

165160
/// Info for the (lone) imported header for this module.
166161
struct {

Diff for: test/DebugInfo/ASTSection-single.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
// RUN: %empty-directory(%t)
77

88
// RUN: echo "public let a0 = 0" >%t/a0.swift
9-
// RUN: %target-build-swift %t/a0.swift -emit-module -emit-module-path %t/a0.swiftmodule
9+
// RUN: %target-build-swift %t/a0.swift -emit-module -emit-module-path %t/a0.swiftmodule -I %s/Inputs
1010
// RUN: %target-swift-modulewrap %t/a0.swiftmodule -o %t/a0-mod.o
1111

1212
// RUN: %lldb-moduleimport-test -verbose %t/a0-mod.o | %FileCheck %s
13+
// CHECK: Path: {{.*}}/Inputs, framework=false, system=false
1314
// CHECK: Importing a0... ok!
14-

Diff for: tools/lldb-moduleimport-test/lldb-moduleimport-test.cpp

+16-6
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@ void anchorForGetMainExecutable() {}
4444

4545
using namespace llvm::MachO;
4646

47-
static bool
48-
validateModule(llvm::StringRef data, bool Verbose, bool requiresOSSAModules,
49-
swift::serialization::ValidationInfo &info,
50-
swift::serialization::ExtendedValidationInfo &extendedInfo) {
47+
static bool validateModule(
48+
llvm::StringRef data, bool Verbose, bool requiresOSSAModules,
49+
swift::serialization::ValidationInfo &info,
50+
swift::serialization::ExtendedValidationInfo &extendedInfo,
51+
llvm::SmallVectorImpl<swift::serialization::SearchPath> &searchPaths) {
5152
info = swift::serialization::validateSerializedAST(
5253
data, requiresOSSAModules,
5354
/*requiredSDK*/ StringRef(), /*requiresRevisionMatch*/ false,
54-
&extendedInfo);
55+
&extendedInfo, /* dependencies*/ nullptr, &searchPaths);
5556
if (info.status != swift::serialization::Status::Valid) {
5657
llvm::outs() << "error: validateSerializedAST() failed\n";
5758
return false;
@@ -79,6 +80,14 @@ validateModule(llvm::StringRef data, bool Verbose, bool requiresOSSAModules,
7980
llvm::outs() << " " << option;
8081
llvm::outs() << "\n";
8182
}
83+
llvm::outs() << "- Search Paths:\n";
84+
for (auto searchPath : searchPaths) {
85+
llvm::outs() << " Path: " << searchPath.Path;
86+
llvm::outs() << ", framework="
87+
<< (searchPath.IsFramework ? "true" : "false");
88+
llvm::outs() << ", system=" << (searchPath.IsSystem ? "true" : "false")
89+
<< "\n";
90+
}
8291
}
8392

8493
return true;
@@ -285,11 +294,12 @@ int main(int argc, char **argv) {
285294

286295
swift::serialization::ValidationInfo info;
287296
swift::serialization::ExtendedValidationInfo extendedInfo;
297+
llvm::SmallVector<swift::serialization::SearchPath> searchPaths;
288298
for (auto &Module : Modules) {
289299
info = {};
290300
extendedInfo = {};
291301
if (!validateModule(StringRef(Module.first, Module.second), Verbose,
292-
EnableOSSAModules, info, extendedInfo)) {
302+
EnableOSSAModules, info, extendedInfo, searchPaths)) {
293303
llvm::errs() << "Malformed module!\n";
294304
return 1;
295305
}

0 commit comments

Comments
 (0)