Skip to content

Commit 1b4cd82

Browse files
authored
Merge pull request #58946 from DavidGoldman/release/5.7
[5.7] Support hermetic indexing information
2 parents f413935 + e1c312d commit 1b4cd82

File tree

14 files changed

+256
-38
lines changed

14 files changed

+256
-38
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,9 @@ WARNING(framework_search_path_includes_framework_extension,none,
338338
ERROR(error_optimization_remark_pattern, none, "%0 in '%1'",
339339
(StringRef, StringRef))
340340

341-
ERROR(error_invalid_debug_prefix_map, none,
342-
"values for '-debug-prefix-map' must be in the format 'original=remapped'"
343-
", but '%0' was provided", (StringRef))
344-
ERROR(error_invalid_coverage_prefix_map, none,
345-
"values for '-coverage-prefix-map' must be in the format "
346-
"'original=remapped', but '%0' was provided", (StringRef))
341+
ERROR(error_opt_invalid_mapping, none,
342+
"values for '%0' must be in the format 'original=remapped', but '%1' was "
343+
"provided", (StringRef, StringRef))
347344

348345
ERROR(invalid_vfs_overlay_file,none,
349346
"invalid virtual overlay file '%0'", (StringRef))

include/swift/AST/IRGenOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ class IRGenOptions {
254254
/// Path prefixes that should be rewritten in coverage info.
255255
PathRemapper CoveragePrefixMap;
256256

257+
/// Path prefixes that should be rewritten in info besides debug and coverage
258+
/// (use DebugPrefixMap and CoveragePrefixMap for those) - currently just
259+
/// indexing info.
260+
PathRemapper FilePrefixMap;
261+
257262
/// What level of debug info to generate.
258263
IRGenDebugInfoLevel DebugInfoLevel : 2;
259264

include/swift/Basic/PathRemapper.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#define SWIFT_BASIC_PATHREMAPPER_H
2626

2727
#include "swift/Basic/LLVM.h"
28+
#include "clang/Basic/PathRemapper.h"
2829
#include "llvm/ADT/SmallVector.h"
2930
#include "llvm/ADT/Twine.h"
3031

@@ -57,6 +58,15 @@ class PathRemapper {
5758
Path.substr(Mapping.first.size())).str();
5859
return Path.str();
5960
}
61+
62+
/// Returns the Clang PathRemapper equivalent, suitable for use with Clang
63+
/// APIs.
64+
clang::PathRemapper asClangPathRemapper() const {
65+
clang::PathRemapper Remapper;
66+
for (const auto &Mapping : PathMappings)
67+
Remapper.addMapping(Mapping.first, Mapping.second);
68+
return Remapper;
69+
}
6070
};
6171

6272
class PathObfuscator {

include/swift/Index/IndexRecord.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define SWIFT_INDEX_INDEXRECORD_H
1515

1616
#include "swift/Basic/LLVM.h"
17+
#include "swift/Basic/PathRemapper.h"
1718
#include "llvm/ADT/ArrayRef.h"
1819
#include "llvm/ADT/StringRef.h"
1920

@@ -44,11 +45,14 @@ namespace index {
4445
/// \param targetTriple The target for this compilation.
4546
///
4647
/// \param dependencyTracker The set of dependencies seen while building.
48+
///
49+
/// \param pathRemapper Remapper to use for paths in index data.
4750
bool indexAndRecord(SourceFile *primarySourceFile, StringRef indexUnitToken,
4851
StringRef indexStorePath, bool indexSystemModules,
4952
bool skipStdlib, bool isDebugCompilation,
5053
StringRef targetTriple,
51-
const DependencyTracker &dependencyTracker);
54+
const DependencyTracker &dependencyTracker,
55+
const PathRemapper &pathRemapper);
5256

5357
/// Index the given module and store the results to \p indexStorePath.
5458
///
@@ -76,11 +80,14 @@ bool indexAndRecord(SourceFile *primarySourceFile, StringRef indexUnitToken,
7680
/// \param targetTriple The target for this compilation.
7781
///
7882
/// \param dependencyTracker The set of dependencies seen while building.
83+
///
84+
/// \param pathRemapper Remapper to use for paths in index data.
7985
bool indexAndRecord(ModuleDecl *module, ArrayRef<std::string> indexUnitTokens,
8086
StringRef moduleUnitToken, StringRef indexStorePath,
8187
bool indexSystemModules, bool skipStdlib,
8288
bool isDebugCompilation, StringRef targetTriple,
83-
const DependencyTracker &dependencyTracker);
89+
const DependencyTracker &dependencyTracker,
90+
const PathRemapper &pathRemapper);
8491
// FIXME: indexUnitTokens could be StringRef, but that creates an impedance
8592
// mismatch in the caller.
8693

include/swift/Option/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,9 @@ def debug_prefix_map : Separate<["-"], "debug-prefix-map">,
857857
def coverage_prefix_map : Separate<["-"], "coverage-prefix-map">,
858858
Flags<[FrontendOption]>,
859859
HelpText<"Remap source paths in coverage info">, MetaVarName<"<prefix=replacement>">;
860+
def file_prefix_map : Separate<["-"], "file-prefix-map">,
861+
Flags<[FrontendOption]>,
862+
HelpText<"Remap source paths in debug, coverage, and index info">, MetaVarName<"<prefix=replacement>">;
860863

861864
def file_compilation_dir : Separate<["-"], "file-compilation-dir">,
862865
Flags<[FrontendOption]>, MetaVarName<"<path>">,

lib/Driver/Driver.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,16 @@ static void validateDebugInfoArgs(DiagnosticEngine &diags,
223223
}
224224
}
225225

226-
// Check for any -debug-prefix-map options that aren't of the form
226+
// Check for any -*-prefix-map options that aren't of the form
227227
// 'original=remapped' (either side can be empty, however).
228-
for (auto A : args.getAllArgValues(options::OPT_debug_prefix_map))
229-
if (A.find('=') == StringRef::npos)
230-
diags.diagnose(SourceLoc(), diag::error_invalid_debug_prefix_map, A);
231-
232-
// Check for any -coverage-prefix-map options that aren't of the form
233-
// 'original=remapped' (either side can be empty, however).
234-
for (auto A : args.getAllArgValues(options::OPT_coverage_prefix_map))
235-
if (A.find('=') == StringRef::npos)
236-
diags.diagnose(SourceLoc(), diag::error_invalid_coverage_prefix_map, A);
228+
for (const Arg *A : args.filtered(options::OPT_debug_prefix_map,
229+
options::OPT_coverage_prefix_map,
230+
options::OPT_file_prefix_map)) {
231+
StringRef val = A->getValue();
232+
if (val.find('=') == StringRef::npos)
233+
diags.diagnose(SourceLoc(), diag::error_opt_invalid_mapping,
234+
A->getOption().getPrefixedName(), val);
235+
}
237236
}
238237

239238
static void validateVerifyIncrementalDependencyArgs(DiagnosticEngine &diags,

lib/Driver/ToolChains.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,9 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
305305
inputArgs.AddAllArgs(arguments, options::OPT_D);
306306

307307
// Pass on file paths that should be remapped in debug info.
308-
inputArgs.AddAllArgs(arguments, options::OPT_debug_prefix_map);
309-
inputArgs.AddAllArgs(arguments, options::OPT_coverage_prefix_map);
308+
inputArgs.AddAllArgs(arguments, options::OPT_debug_prefix_map,
309+
options::OPT_coverage_prefix_map,
310+
options::OPT_file_prefix_map);
310311

311312
std::string globalRemapping = getGlobalDebugPathRemapping();
312313
if (!globalRemapping.empty()) {

lib/Frontend/CompilerInvocation.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,14 +1202,23 @@ static bool ParseClangImporterArgs(ClangImporterOptions &Opts,
12021202
Opts.ExtraArgs.push_back(A->getValue());
12031203
}
12041204

1205-
for (auto A : Args.getAllArgValues(OPT_debug_prefix_map)) {
1205+
for (const Arg *A : Args.filtered(OPT_file_prefix_map,
1206+
OPT_debug_prefix_map)) {
1207+
std::string Val(A->getValue());
12061208
// Forward -debug-prefix-map arguments from Swift to Clang as
1207-
// -fdebug-prefix-map. This is required to ensure DIFiles created there,
1208-
// like "<swift-imported-modules>", have their paths remapped properly.
1209+
// -fdebug-prefix-map= and -file-prefix-map as -ffile-prefix-map=.
1210+
//
1211+
// This is required to ensure DIFiles created there, like
1212+
/// "<swift-imported-modules>", as well as index data, have their paths
1213+
// remapped properly.
1214+
//
12091215
// (Note, however, that Clang's usage of std::map means that the remapping
12101216
// may not be applied in the same order, which can matter if one mapping is
12111217
// a prefix of another.)
1212-
Opts.ExtraArgs.push_back("-fdebug-prefix-map=" + A);
1218+
if (A->getOption().matches(OPT_file_prefix_map))
1219+
Opts.ExtraArgs.push_back("-ffile-prefix-map=" + Val);
1220+
else
1221+
Opts.ExtraArgs.push_back("-fdebug-prefix-map=" + Val);
12131222
}
12141223

12151224
if (!workingDirectory.empty()) {
@@ -1990,6 +1999,13 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
19901999
: "-gdwarf_types");
19912000
}
19922001

2002+
for (auto A : Args.getAllArgValues(options::OPT_file_prefix_map)) {
2003+
auto SplitMap = StringRef(A).split('=');
2004+
Opts.FilePrefixMap.addMapping(SplitMap.first, SplitMap.second);
2005+
Opts.DebugPrefixMap.addMapping(SplitMap.first, SplitMap.second);
2006+
Opts.CoveragePrefixMap.addMapping(SplitMap.first, SplitMap.second);
2007+
}
2008+
19932009
for (auto A : Args.getAllArgValues(options::OPT_debug_prefix_map)) {
19942010
auto SplitMap = StringRef(A).split('=');
19952011
Opts.DebugPrefixMap.addMapping(SplitMap.first, SplitMap.second);

lib/FrontendTool/FrontendTool.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,8 @@ static void emitIndexDataForSourceFile(SourceFile *PrimarySourceFile,
17611761
opts.IndexStorePath, opts.IndexSystemModules,
17621762
opts.IndexIgnoreStdlib, isDebugCompilation,
17631763
Invocation.getTargetTriple(),
1764-
*Instance.getDependencyTracker());
1764+
*Instance.getDependencyTracker(),
1765+
Invocation.getIRGenOptions().FilePrefixMap);
17651766
} else {
17661767
std::string moduleToken =
17671768
Invocation.getModuleOutputPathForAtMostOnePrimary();
@@ -1776,7 +1777,8 @@ static void emitIndexDataForSourceFile(SourceFile *PrimarySourceFile,
17761777
opts.IndexIgnoreStdlib,
17771778
isDebugCompilation,
17781779
Invocation.getTargetTriple(),
1779-
*Instance.getDependencyTracker());
1780+
*Instance.getDependencyTracker(),
1781+
Invocation.getIRGenOptions().FilePrefixMap);
17801782
}
17811783
}
17821784

lib/Index/IndexRecord.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/AST/SourceFile.h"
2323
#include "swift/AST/Stmt.h"
2424
#include "swift/AST/Types.h"
25+
#include "swift/Basic/PathRemapper.h"
2526
#include "swift/ClangImporter/ClangModule.h"
2627
#include "swift/Index/Index.h"
2728
#include "clang/Basic/FileManager.h"
@@ -381,6 +382,7 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
381382
const clang::CompilerInstance &clangCI,
382383
DiagnosticEngine &diags,
383384
IndexUnitWriter &parentUnitWriter,
385+
const PathRemapper &pathRemapper,
384386
SourceFile *initialFile);
385387

386388
static void addModuleDependencies(ArrayRef<ImportedModule> imports,
@@ -392,6 +394,7 @@ static void addModuleDependencies(ArrayRef<ImportedModule> imports,
392394
DiagnosticEngine &diags,
393395
IndexUnitWriter &unitWriter,
394396
StringScratchSpace &moduleNameScratch,
397+
const PathRemapper &pathRemapper,
395398
SourceFile *initialFile = nullptr) {
396399
auto &fileMgr = clangCI.getFileManager();
397400

@@ -442,7 +445,9 @@ static void addModuleDependencies(ArrayRef<ImportedModule> imports,
442445
emitDataForSwiftSerializedModule(mod, indexStorePath,
443446
indexSystemModules, skipStdlib,
444447
targetTriple, clangCI, diags,
445-
unitWriter, initialFile);
448+
unitWriter,
449+
pathRemapper,
450+
initialFile);
446451
withoutUnitName = false;
447452
}
448453

@@ -473,6 +478,7 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
473478
const clang::CompilerInstance &clangCI,
474479
DiagnosticEngine &diags,
475480
IndexUnitWriter &parentUnitWriter,
481+
const PathRemapper &pathRemapper,
476482
SourceFile *initialFile) {
477483
StringRef filename = module->getModuleFilename();
478484
std::string moduleName = module->getNameStr().str();
@@ -567,11 +573,13 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
567573
// For indexing serialized modules 'debug compilation' is irrelevant, so
568574
// set it to true by default.
569575
bool isDebugCompilation = true;
576+
auto clangRemapper = pathRemapper.asClangPathRemapper();
570577

571578
IndexUnitWriter unitWriter(fileMgr, indexStorePath,
572579
"swift", swiftVersion, indexUnitToken, moduleName,
573580
/*MainFile=*/nullptr, isSystem, /*IsModuleUnit=*/true,
574-
isDebugCompilation, targetTriple, sysrootPath, getModuleInfoFromOpaqueModule);
581+
isDebugCompilation, targetTriple, sysrootPath,
582+
clangRemapper, getModuleInfoFromOpaqueModule);
575583

576584
auto FE = fileMgr.getFile(filename);
577585
bool isSystemModule = module->isSystemModule();
@@ -590,7 +598,7 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
590598
StringScratchSpace moduleNameScratch;
591599
addModuleDependencies(imports, indexStorePath, indexSystemModules, skipStdlib,
592600
targetTriple, clangCI, diags, unitWriter,
593-
moduleNameScratch, initialFile);
601+
moduleNameScratch, pathRemapper, initialFile);
594602

595603
if (unitWriter.write(error)) {
596604
diags.diagnose(SourceLoc(), diag::error_write_index_unit, error);
@@ -607,19 +615,21 @@ recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
607615
StringRef targetTriple,
608616
ArrayRef<const clang::FileEntry *> fileDependencies,
609617
const clang::CompilerInstance &clangCI,
618+
const PathRemapper &pathRemapper,
610619
DiagnosticEngine &diags) {
611620
auto &fileMgr = clangCI.getFileManager();
612621
auto *module = primarySourceFile->getParentModule();
613622
bool isSystem = module->isSystemModule();
614623
auto mainFile = fileMgr.getFile(primarySourceFile->getFilename());
624+
auto clangRemapper = pathRemapper.asClangPathRemapper();
615625
// FIXME: Get real values for the following.
616626
StringRef swiftVersion;
617627
StringRef sysrootPath = clangCI.getHeaderSearchOpts().Sysroot;
618628
IndexUnitWriter unitWriter(
619629
fileMgr, indexStorePath, "swift", swiftVersion, indexUnitToken,
620630
module->getNameStr(), mainFile ? *mainFile : nullptr, isSystem,
621631
/*isModuleUnit=*/false, isDebugCompilation, targetTriple, sysrootPath,
622-
getModuleInfoFromOpaqueModule);
632+
clangRemapper, getModuleInfoFromOpaqueModule);
623633

624634
// Module dependencies.
625635
SmallVector<ImportedModule, 8> imports;
@@ -630,7 +640,7 @@ recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
630640
StringScratchSpace moduleNameScratch;
631641
addModuleDependencies(imports, indexStorePath, indexSystemModules, skipStdlib,
632642
targetTriple, clangCI, diags, unitWriter,
633-
moduleNameScratch, primarySourceFile);
643+
moduleNameScratch, pathRemapper, primarySourceFile);
634644

635645
// File dependencies.
636646
for (auto *F : fileDependencies)
@@ -684,7 +694,8 @@ bool index::indexAndRecord(SourceFile *primarySourceFile,
684694
bool skipStdlib,
685695
bool isDebugCompilation,
686696
StringRef targetTriple,
687-
const DependencyTracker &dependencyTracker) {
697+
const DependencyTracker &dependencyTracker,
698+
const PathRemapper &pathRemapper) {
688699
auto &astContext = primarySourceFile->getASTContext();
689700
auto &clangCI = astContext.getClangModuleLoader()->getClangInstance();
690701
auto &diags = astContext.Diags;
@@ -712,7 +723,7 @@ bool index::indexAndRecord(SourceFile *primarySourceFile,
712723
indexStorePath, indexSystemModules, skipStdlib,
713724
isDebugCompilation, targetTriple,
714725
fileDependencies.getArrayRef(),
715-
clangCI, diags);
726+
clangCI, pathRemapper, diags);
716727
}
717728

718729
bool index::indexAndRecord(ModuleDecl *module,
@@ -723,7 +734,8 @@ bool index::indexAndRecord(ModuleDecl *module,
723734
bool skipStdlib,
724735
bool isDebugCompilation,
725736
StringRef targetTriple,
726-
const DependencyTracker &dependencyTracker) {
737+
const DependencyTracker &dependencyTracker,
738+
const PathRemapper &pathRemapper) {
727739
auto &astContext = module->getASTContext();
728740
auto &clangCI = astContext.getClangModuleLoader()->getClangInstance();
729741
auto &diags = astContext.Diags;
@@ -759,7 +771,7 @@ bool index::indexAndRecord(ModuleDecl *module,
759771
indexStorePath, indexSystemModules, skipStdlib,
760772
isDebugCompilation, targetTriple,
761773
fileDependencies.getArrayRef(),
762-
clangCI, diags))
774+
clangCI, pathRemapper, diags))
763775
return true;
764776
unitIndex += 1;
765777
}

lib/Serialization/Serialization.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,9 +1088,12 @@ void Serializer::writeHeader(const SerializationOptions &options) {
10881088
++Arg;
10891089
continue;
10901090
}
1091-
} else if (arg.startswith("-fdebug-prefix-map=")) {
1092-
// We don't serialize the debug prefix map flags as these
1093-
// contain absoute paths that are not usable on different
1091+
} else if (arg.startswith("-fdebug-prefix-map=") ||
1092+
arg.startswith("-ffile-prefix-map=") ||
1093+
arg.startswith("-fcoverage-prefix-map=") ||
1094+
arg.startswith("-fmacro-prefix-map=")) {
1095+
// We don't serialize any of the prefix map flags as these flags
1096+
// contain absolute paths that are not usable on different
10941097
// machines. These flags are not necessary to compile the
10951098
// clang modules again so are safe to remove.
10961099
continue;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===--- Building source files separately with a module merge at the end
2+
3+
// RUN: %empty-directory(%t)
4+
// RUN: touch %t/s1.swift %t/s2.swift
5+
// RUN: %target-swift-frontend -index-store-path %t/idx -file-prefix-map %t=REMAPPED_OUT_DIR -primary-file %t/s1.swift %t/s2.swift -o %t/s1.o -c -module-name main -emit-module -emit-module-path %t/s1.swiftmodule
6+
// RUN: %target-swift-frontend -index-store-path %t/idx -file-prefix-map %t=REMAPPED_OUT_DIR %t/s1.swift -primary-file %t/s2.swift -o %t/s2.o -c -module-name main -emit-module -emit-module-path %t/s2.swiftmodule
7+
// RUN: %target-swift-frontend -index-store-path %t/idx -file-prefix-map %t=REMAPPED_OUT_DIR %t/s1.swiftmodule %t/s2.swiftmodule -emit-module -o %t/main.swiftmodule -module-name main
8+
// RUN: c-index-test core -print-unit %t/idx | %FileCheck %s
9+
10+
//===--- Building source files together (e.g. WMO)
11+
12+
// RUN: %empty-directory(%t)
13+
// RUN: touch %t/s1.swift %t/s2.swift
14+
// RUN: %target-swift-frontend -index-store-path %t/idx -file-prefix-map %t=REMAPPED_OUT_DIR %t/s1.swift %t/s2.swift -o %t/s1.o -o %t/s2.o -c -module-name main -emit-module -emit-module-path %t/main.swiftmodule
15+
// RUN: c-index-test core -print-unit %t/idx | %FileCheck %s
16+
17+
//===--- Building separately but with relative paths for the source file inputs
18+
19+
// RUN: %empty-directory(%t)
20+
// RUN: cd %t
21+
// RUN: touch %t/s1.swift %t/s2.swift
22+
// RUN: %target-swift-frontend -index-store-path idx -file-prefix-map %t=REMAPPED_OUT_DIR -primary-file s1.swift s2.swift -o s1.o -c -module-name main -emit-module -emit-module-path s1.swiftmodule
23+
// RUN: %target-swift-frontend -index-store-path idx -file-prefix-map %t=REMAPPED_OUT_DIR s1.swift -primary-file s2.swift -o s2.o -c -module-name main -emit-module -emit-module-path s2.swiftmodule
24+
// RUN: %target-swift-frontend -index-store-path idx -file-prefix-map %t=REMAPPED_OUT_DIR s1.swiftmodule s2.swiftmodule -emit-module -o main.swiftmodule -module-name main
25+
// RUN: c-index-test core -print-unit idx | %FileCheck %s
26+
// CHECK-NOT: main.swiftmodule-{{[A-Z0-9]*}}
27+
28+
// CHECK: s1.o-{{2LQAU7D8TZHZ8|2RHC8ZJFDYDW4}}
29+
// CHECK: --------
30+
// CHECK: out-file: REMAPPED_OUT_DIR{{/|\\}}s1.o
31+
// CHECK: DEPEND START
32+
// CHECK: Unit | system | {{.*}}Swift.swiftmodule
33+
// CHECK: DEPEND END
34+
35+
// CHECK: s2.o-{{2OIL2LG8UULK6|15MCL6ZLKZKNL}}
36+
// CHECK: --------
37+
// CHECK: out-file: REMAPPED_OUT_DIR{{/|\\}}s2.o
38+
// CHECK: DEPEND START
39+
// CHECK: Unit | system | {{.*}}Swift.swiftmodule
40+
// CHECK: DEPEND END

0 commit comments

Comments
 (0)