Add -prefix-serialized-debugging-options

This commit adds the `-prefix-serialized-debugging-options` flag,
which is used to apply the debug prefix map to serialized debugging
options embedded in the swiftmodule files.
This commit is contained in:
Richard Howell
2021-09-21 11:09:23 -07:00
parent 58d67be645
commit ce6b4b3ee0
7 changed files with 121 additions and 28 deletions

View File

@@ -182,6 +182,11 @@ public:
/// module appears to not be a public module. /// module appears to not be a public module.
Optional<bool> SerializeOptionsForDebugging; Optional<bool> SerializeOptionsForDebugging;
/// When true the debug prefix map entries will be applied to debugging
/// options before serialization. These can be reconstructed at debug time by
/// applying the inverse map in SearchPathOptions.SearchPathRemapper.
bool DebugPrefixSerializedDebuggingOptions = false;
/// When true, check if all required SwiftOnoneSupport symbols are present in /// When true, check if all required SwiftOnoneSupport symbols are present in
/// the module. /// the module.
bool CheckOnoneSupportCompleteness = false; bool CheckOnoneSupportCompleteness = false;

View File

@@ -559,7 +559,7 @@ def disable_bridging_pch : Flag<["-"], "disable-bridging-pch">,
def lto : Joined<["-"], "lto=">, def lto : Joined<["-"], "lto=">,
Flags<[FrontendOption, NoInteractiveOption]>, Flags<[FrontendOption, NoInteractiveOption]>,
HelpText<"Specify the LTO type to either 'llvm-thin' or 'llvm-full'">; HelpText<"Specify the LTO type to either 'llvm-thin' or 'llvm-full'">;
def lto_library : Separate<["-"], "lto-library">, def lto_library : Separate<["-"], "lto-library">,
Flags<[FrontendOption, ArgumentIsPath, NoInteractiveOption]>, Flags<[FrontendOption, ArgumentIsPath, NoInteractiveOption]>,
HelpText<"Perform LTO with <lto-library>">, MetaVarName<"<lto-library>">; HelpText<"Perform LTO with <lto-library>">, MetaVarName<"<lto-library>">;
@@ -815,6 +815,10 @@ def debug_info_format : Joined<["-"], "debug-info-format=">,
Flags<[FrontendOption]>, Flags<[FrontendOption]>,
HelpText<"Specify the debug info format type to either 'dwarf' or 'codeview'">; HelpText<"Specify the debug info format type to either 'dwarf' or 'codeview'">;
def prefix_serialized_debugging_options : Flag<["-"], "prefix-serialized-debugging-options">,
Flags<[FrontendOption]>,
HelpText<"Apply debug prefix mappings to serialized debug info in Swiftmodule files">;
// Verify debug info // Verify debug info
def verify_debug_info : Flag<["-"], "verify-debug-info">, def verify_debug_info : Flag<["-"], "verify-debug-info">,
Flags<[NoInteractiveOption, DoesNotAffectIncrementalBuild]>, Flags<[NoInteractiveOption, DoesNotAffectIncrementalBuild]>,

View File

@@ -14,6 +14,7 @@
#define SWIFT_SERIALIZATION_SERIALIZATIONOPTIONS_H #define SWIFT_SERIALIZATION_SERIALIZATIONOPTIONS_H
#include "swift/Basic/LLVM.h" #include "swift/Basic/LLVM.h"
#include "swift/Basic/PathRemapper.h"
#include "llvm/Support/VersionTuple.h" #include "llvm/Support/VersionTuple.h"
namespace swift { namespace swift {
@@ -42,7 +43,10 @@ namespace swift {
StringRef ImportedHeader; StringRef ImportedHeader;
StringRef ModuleLinkName; StringRef ModuleLinkName;
StringRef ModuleInterface; StringRef ModuleInterface;
ArrayRef<std::string> ExtraClangOptions; std::vector<std::string> ExtraClangOptions;
/// Path prefixes that should be rewritten in debug info.
PathRemapper DebuggingOptionsPrefixMap;
/// Describes a single-file dependency for this module, along with the /// Describes a single-file dependency for this module, along with the
/// appropriate strategy for how to verify if it's up-to-date. /// appropriate strategy for how to verify if it's up-to-date.

View File

@@ -258,6 +258,8 @@ bool ArgsToFrontendOptionsConverter::convert(
A->getOption().matches(OPT_serialize_debugging_options); A->getOption().matches(OPT_serialize_debugging_options);
} }
Opts.DebugPrefixSerializedDebuggingOptions |=
Args.hasArg(OPT_prefix_serialized_debugging_options);
Opts.EnableSourceImport |= Args.hasArg(OPT_enable_source_import); Opts.EnableSourceImport |= Args.hasArg(OPT_enable_source_import);
Opts.ImportUnderlyingModule |= Args.hasArg(OPT_import_underlying_module); Opts.ImportUnderlyingModule |= Args.hasArg(OPT_import_underlying_module);
Opts.EnableIncrementalDependencyVerifier |= Args.hasArg(OPT_verify_incremental_dependencies); Opts.EnableIncrementalDependencyVerifier |= Args.hasArg(OPT_verify_incremental_dependencies);

View File

@@ -147,7 +147,7 @@ SerializationOptions CompilerInvocation::computeSerializationOptions(
serializationOpts.ImportedHeader = opts.ImplicitObjCHeaderPath; serializationOpts.ImportedHeader = opts.ImplicitObjCHeaderPath;
serializationOpts.ModuleLinkName = opts.ModuleLinkName; serializationOpts.ModuleLinkName = opts.ModuleLinkName;
serializationOpts.UserModuleVersion = opts.UserModuleVersion; serializationOpts.UserModuleVersion = opts.UserModuleVersion;
serializationOpts.ExtraClangOptions = getClangImporterOptions().ExtraArgs;
serializationOpts.PublicDependentLibraries = serializationOpts.PublicDependentLibraries =
getIRGenOptions().PublicLinkLibraries; getIRGenOptions().PublicLinkLibraries;
serializationOpts.SDKName = getLangOptions().SDKName; serializationOpts.SDKName = getLangOptions().SDKName;
@@ -176,6 +176,20 @@ SerializationOptions CompilerInvocation::computeSerializationOptions(
opts.SerializeOptionsForDebugging.getValueOr( opts.SerializeOptionsForDebugging.getValueOr(
!module->isExternallyConsumed()); !module->isExternallyConsumed());
if (serializationOpts.SerializeOptionsForDebugging &&
opts.DebugPrefixSerializedDebuggingOptions) {
serializationOpts.DebuggingOptionsPrefixMap =
getIRGenOptions().DebugPrefixMap;
auto &remapper = serializationOpts.DebuggingOptionsPrefixMap;
auto remapClangPaths = [&remapper](StringRef path) {
return remapper.remapPath(path);
};
serializationOpts.ExtraClangOptions =
getClangImporterOptions().getRemappedExtraArgs(remapClangPaths);
} else {
serializationOpts.ExtraClangOptions = getClangImporterOptions().ExtraArgs;
}
serializationOpts.DisableCrossModuleIncrementalInfo = serializationOpts.DisableCrossModuleIncrementalInfo =
opts.DisableCrossModuleIncrementalBuild; opts.DisableCrossModuleIncrementalBuild;

View File

@@ -40,6 +40,7 @@
#include "swift/Basic/Defer.h" #include "swift/Basic/Defer.h"
#include "swift/Basic/Dwarf.h" #include "swift/Basic/Dwarf.h"
#include "swift/Basic/FileSystem.h" #include "swift/Basic/FileSystem.h"
#include "swift/Basic/PathRemapper.h"
#include "swift/Basic/STLExtras.h" #include "swift/Basic/STLExtras.h"
#include "swift/Basic/Version.h" #include "swift/Basic/Version.h"
#include "swift/ClangImporter/ClangImporter.h" #include "swift/ClangImporter/ClangImporter.h"
@@ -48,9 +49,9 @@
#include "swift/Demangling/ManglingMacros.h" #include "swift/Demangling/ManglingMacros.h"
#include "swift/Serialization/SerializationOptions.h" #include "swift/Serialization/SerializationOptions.h"
#include "swift/Strings.h" #include "swift/Strings.h"
#include "clang/AST/DeclTemplate.h"
#include "swift/SymbolGraphGen/SymbolGraphOptions.h"
#include "swift/SymbolGraphGen/SymbolGraphGen.h" #include "swift/SymbolGraphGen/SymbolGraphGen.h"
#include "swift/SymbolGraphGen/SymbolGraphOptions.h"
#include "clang/AST/DeclTemplate.h"
#include "llvm/ADT/SmallSet.h" #include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringExtras.h"
@@ -511,7 +512,7 @@ static uint8_t getRawOpaqueReadOwnership(swift::OpaqueReadOwnership ownership) {
CASE(OwnedOrBorrowed) CASE(OwnedOrBorrowed)
#undef CASE #undef CASE
} }
llvm_unreachable("bad kind"); llvm_unreachable("bad kind");
} }
static uint8_t getRawReadImplKind(swift::ReadImplKind kind) { static uint8_t getRawReadImplKind(swift::ReadImplKind kind) {
@@ -1058,25 +1059,35 @@ void Serializer::writeHeader(const SerializationOptions &options) {
options_block::SDKPathLayout SDKPath(Out); options_block::SDKPathLayout SDKPath(Out);
options_block::XCCLayout XCC(Out); options_block::XCCLayout XCC(Out);
SDKPath.emit(ScratchRecord, M->getASTContext().SearchPathOpts.SDKPath); const auto &PathRemapper = options.DebuggingOptionsPrefixMap;
SDKPath.emit(
ScratchRecord,
PathRemapper.remapPath(M->getASTContext().SearchPathOpts.SDKPath));
auto &Opts = options.ExtraClangOptions; auto &Opts = options.ExtraClangOptions;
for (auto Arg = Opts.begin(), E = Opts.end(); Arg != E; ++Arg) { for (auto Arg = Opts.begin(), E = Opts.end(); Arg != E; ++Arg) {
// FIXME: This is a hack and calls for a better design. StringRef arg(*Arg);
// if (arg.startswith("-ivfsoverlay")) {
// Filter out any -ivfsoverlay options that include an // FIXME: This is a hack and calls for a better design.
// unextended-module-overlay.yaml overlay. By convention the Xcode //
// buildsystem uses these while *building* mixed Objective-C and Swift // Filter out any -ivfsoverlay options that include an
// frameworks; but they should never be used to *import* the module // unextended-module-overlay.yaml overlay. By convention the Xcode
// defined in the framework. // buildsystem uses these while *building* mixed Objective-C and
if (StringRef(*Arg).startswith("-ivfsoverlay")) { // Swift frameworks; but they should never be used to *import* the
// module defined in the framework.
auto Next = std::next(Arg); auto Next = std::next(Arg);
if (Next != E && if (Next != E &&
StringRef(*Next).endswith("unextended-module-overlay.yaml")) { StringRef(*Next).endswith("unextended-module-overlay.yaml")) {
++Arg; ++Arg;
continue; continue;
} }
} else if (arg.startswith("-fdebug-prefix-map=")) {
// We don't serialize the debug prefix map flags as these
// contain absoute paths that are not usable on different
// machines. These flags are not necessary to compile the
// clang modules again so are safe to remove.
continue;
} }
XCC.emit(ScratchRecord, *Arg); XCC.emit(ScratchRecord, arg);
} }
} }
} }
@@ -1127,14 +1138,16 @@ void Serializer::writeInputBlock(const SerializationOptions &options) {
input_block::ModuleInterfaceLayout ModuleInterface(Out); input_block::ModuleInterfaceLayout ModuleInterface(Out);
if (options.SerializeOptionsForDebugging) { if (options.SerializeOptionsForDebugging) {
const auto &PathMapper = options.DebuggingOptionsPrefixMap;
const SearchPathOptions &searchPathOpts = M->getASTContext().SearchPathOpts; const SearchPathOptions &searchPathOpts = M->getASTContext().SearchPathOpts;
// Put the framework search paths first so that they'll be preferred upon // Put the framework search paths first so that they'll be preferred upon
// deserialization. // deserialization.
for (auto &framepath : searchPathOpts.FrameworkSearchPaths) for (auto &framepath : searchPathOpts.FrameworkSearchPaths)
SearchPath.emit(ScratchRecord, /*framework=*/true, framepath.IsSystem, SearchPath.emit(ScratchRecord, /*framework=*/true, framepath.IsSystem,
framepath.Path); PathMapper.remapPath(framepath.Path));
for (auto &path : searchPathOpts.ImportSearchPaths) for (auto &path : searchPathOpts.ImportSearchPaths)
SearchPath.emit(ScratchRecord, /*framework=*/false, /*system=*/false, path); SearchPath.emit(ScratchRecord, /*framework=*/false, /*system=*/false,
PathMapper.remapPath(path));
} }
// Note: We're not using StringMap here because we don't need to own the // Note: We're not using StringMap here because we don't need to own the
@@ -1468,7 +1481,7 @@ void Serializer::writeASTBlockEntity(const SILLayout *layout) {
typeRef |= 0x80000000U; typeRef |= 0x80000000U;
data.push_back(typeRef); data.push_back(typeRef);
} }
unsigned abbrCode unsigned abbrCode
= DeclTypeAbbrCodes[SILLayoutLayout::Code]; = DeclTypeAbbrCodes[SILLayoutLayout::Code];
@@ -1703,7 +1716,7 @@ static bool shouldSerializeMember(Decl *D) {
case DeclKind::OpaqueType: case DeclKind::OpaqueType:
return true; return true;
case DeclKind::EnumElement: case DeclKind::EnumElement:
case DeclKind::Protocol: case DeclKind::Protocol:
case DeclKind::Constructor: case DeclKind::Constructor:
@@ -1801,14 +1814,14 @@ void Serializer::writeCrossReference(const DeclContext *DC, uint32_t pathLen) {
if (auto opaque = dyn_cast<OpaqueTypeDecl>(generic)) { if (auto opaque = dyn_cast<OpaqueTypeDecl>(generic)) {
if (!opaque->hasName()) { if (!opaque->hasName()) {
abbrCode = DeclTypeAbbrCodes[XRefOpaqueReturnTypePathPieceLayout::Code]; abbrCode = DeclTypeAbbrCodes[XRefOpaqueReturnTypePathPieceLayout::Code];
XRefOpaqueReturnTypePathPieceLayout::emitRecord(Out, ScratchRecord, XRefOpaqueReturnTypePathPieceLayout::emitRecord(Out, ScratchRecord,
abbrCode, abbrCode,
addDeclBaseNameRef(opaque->getOpaqueReturnTypeIdentifier())); addDeclBaseNameRef(opaque->getOpaqueReturnTypeIdentifier()));
break; break;
} }
} }
assert(generic->hasName()); assert(generic->hasName());
abbrCode = DeclTypeAbbrCodes[XRefTypePathPieceLayout::Code]; abbrCode = DeclTypeAbbrCodes[XRefTypePathPieceLayout::Code];
@@ -1849,7 +1862,7 @@ void Serializer::writeCrossReference(const DeclContext *DC, uint32_t pathLen) {
case DeclContextKind::SubscriptDecl: { case DeclContextKind::SubscriptDecl: {
auto SD = cast<SubscriptDecl>(DC); auto SD = cast<SubscriptDecl>(DC);
writeCrossReference(DC->getParent(), pathLen + 1); writeCrossReference(DC->getParent(), pathLen + 1);
Type ty = SD->getInterfaceType()->getCanonicalType(); Type ty = SD->getInterfaceType()->getCanonicalType();
abbrCode = DeclTypeAbbrCodes[XRefValuePathPieceLayout::Code]; abbrCode = DeclTypeAbbrCodes[XRefValuePathPieceLayout::Code];
@@ -1860,7 +1873,7 @@ void Serializer::writeCrossReference(const DeclContext *DC, uint32_t pathLen) {
SD->isStatic()); SD->isStatic());
break; break;
} }
case DeclContextKind::AbstractFunctionDecl: { case DeclContextKind::AbstractFunctionDecl: {
if (auto fn = dyn_cast<AccessorDecl>(DC)) { if (auto fn = dyn_cast<AccessorDecl>(DC)) {
auto storage = fn->getStorage(); auto storage = fn->getStorage();
@@ -1972,7 +1985,7 @@ void Serializer::writeCrossReference(const Decl *D) {
addDeclBaseNameRef(opaque->getOpaqueReturnTypeIdentifier())); addDeclBaseNameRef(opaque->getOpaqueReturnTypeIdentifier()));
return; return;
} }
if (auto genericParam = dyn_cast<GenericTypeParamDecl>(D)) { if (auto genericParam = dyn_cast<GenericTypeParamDecl>(D)) {
assert(!D->getDeclContext()->isModuleScopeContext() && assert(!D->getDeclContext()->isModuleScopeContext() &&
"Cannot cross reference a generic type decl at module scope."); "Cannot cross reference a generic type decl at module scope.");
@@ -4673,7 +4686,7 @@ class ClangToSwiftBasicWriter :
Serializer &S; Serializer &S;
SmallVectorImpl<uint64_t> &Record; SmallVectorImpl<uint64_t> &Record;
using TypeWriter = using TypeWriter =
clang::serialization::AbstractTypeWriter<ClangToSwiftBasicWriter>; clang::serialization::AbstractTypeWriter<ClangToSwiftBasicWriter>;
TypeWriter Types; TypeWriter Types;
@@ -5477,7 +5490,7 @@ void Serializer::writeAST(ModuleOrSourceFile DC) {
/*isLocal=*/true); /*isLocal=*/true);
} }
} }
for (auto OTD : opaqueReturnTypeDecls) { for (auto OTD : opaqueReturnTypeDecls) {
// FIXME: We should delay parsing function bodies so these type decls // FIXME: We should delay parsing function bodies so these type decls
// don't even get added to the file. // don't even get added to the file.

View File

@@ -0,0 +1,51 @@
// RUN: %empty-directory(%t)
// RUN: %empty-directory(%t/Frameworks/has_alias.framework/Modules/has_alias.swiftmodule)
// RUN: %target-swift-frontend -emit-module -o %t/Frameworks/has_alias.framework/Modules/has_alias.swiftmodule/%target-swiftmodule-name %S/Inputs/alias.swift -module-name has_alias
// RUN: %empty-directory(%t/secret)
// RUN: %target-swift-frontend -emit-module -o %t/secret %S/Inputs/struct_with_operators.swift
// RUN: %target-swift-frontend -emit-module -o %t -I %t/secret -F %t/Frameworks -Fsystem %t/SystemFrameworks %S/Inputs/has_xref.swift
// RUN: %empty-directory(%t/workingdir)
// RUN: cd %t/workingdir && %target-swift-frontend -sdk %t/sdk %s -emit-module -o %t/prefixed.swiftmodule \
// RUN: -I %t -I %t/secret -F %t/Frameworks -Fsystem %t/SystemFrameworks \
// RUN: -Xcc -I -Xcc %t/include -Xcc -isystem -Xcc %t/system -Xcc -F -Xcc %t/fw \
// RUN: -Xcc -I%t/includejoined -Xcc -isystem%t/systemjoined -Xcc -F%t/fwjoined \
// RUN: -Xcc -D -Xcc donotprefixme -prefix-serialized-debugging-options \
// RUN: -debug-prefix-map %t/workingdir=WORKINGDIR -debug-prefix-map %t/sdk=SDKROOT -debug-prefix-map %t=SRC -debug-prefix-map donotprefixme=ERROR
// RUN: llvm-bcanalyzer -dump %t/prefixed.swiftmodule | %FileCheck %s
import has_xref
numeric(42)
// CHECK-LABEL: <OPTIONS_BLOCK
// CHECK: <SDK_PATH abbrevid={{[0-9]+}}/> blob data = 'SDKROOT'
// CHECK: <XCC abbrevid={{[0-9]+}}/> blob data = '-working-directory'
// CHECK: <XCC abbrevid={{[0-9]+}}/> blob data = 'WORKINGDIR'
// CHECK: <XCC abbrevid={{[0-9]+}}/> blob data = '-I'
// CHECK: <XCC abbrevid={{[0-9]+}}/> blob data = 'SRC/include'
// CHECK: <XCC abbrevid={{[0-9]+}}/> blob data = '-isystem'
// CHECK: <XCC abbrevid={{[0-9]+}}/> blob data = 'SRC/system'
// CHECK: <XCC abbrevid={{[0-9]+}}/> blob data = '-F'
// CHECK: <XCC abbrevid={{[0-9]+}}/> blob data = 'SRC/fw'
// CHECK: <XCC abbrevid={{[0-9]+}}/> blob data = '-ISRC/includejoined'
// CHECK: <XCC abbrevid={{[0-9]+}}/> blob data = '-isystemSRC/systemjoined'
// CHECK: <XCC abbrevid={{[0-9]+}}/> blob data = '-FSRC/fwjoined'
// CHECK: <XCC abbrevid={{[0-9]+}}/> blob data = '-D'
// CHECK: <XCC abbrevid={{[0-9]+}}/> blob data = 'donotprefixme'
// CHECK-NOT: <XCC abbrevid={{[0-9]+}}/> blob data = '-fdebug-prefix-map
// CHECK: </OPTIONS_BLOCK>
// CHECK-LABEL: <INPUT_BLOCK
// CHECK: <SEARCH_PATH abbrevid={{[0-9]+}} op0=1 op1=0/> blob data = 'SRC/Frameworks'
// CHECK: <SEARCH_PATH abbrevid={{[0-9]+}} op0=1 op1=1/> blob data = 'SRC/SystemFrameworks'
// CHECK: <SEARCH_PATH abbrevid={{[0-9]+}} op0=0 op1=0/> blob data = 'SRC'
// CHECK: <SEARCH_PATH abbrevid={{[0-9]+}} op0=0 op1=0/> blob data = 'SRC/secret'
// CHECK: </INPUT_BLOCK>
// RUN: cd %t/workingdir && %target-swift-frontend -sdk %t/sdk %s -emit-module -o %t/unprefixed.swiftmodule \
// RUN: -I %t -F %t/Frameworks \
// RUN: -Xcc -I -Xcc %t/include \
// RUN: -debug-prefix-map %t=TESTPREFIX
// RUN: llvm-bcanalyzer -dump %t/unprefixed.swiftmodule | %FileCheck --check-prefix=UNPREFIXED %s
// UNPREFIXED-NOT: TESTPREFIX