Skip to content

Commit c6a5292

Browse files
Merge remote-tracking branch 'apple/release/5.3' into katei-merge-5.3
2 parents 0535399 + 89edca6 commit c6a5292

31 files changed

+347
-48
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ ERROR(error_optimization_remark_pattern, none, "%0 in '%1'",
301301
ERROR(error_invalid_debug_prefix_map, none,
302302
"invalid argument '%0' to -debug-prefix-map; it must be of the form "
303303
"'original=remapped'", (StringRef))
304+
ERROR(error_invalid_coverage_prefix_map, none,
305+
"invalid argument '%0' to -coverage-prefix-map; it must be of the form "
306+
"'original=remapped'", (StringRef))
304307

305308

306309
ERROR(error_unable_to_write_swift_ranges_file, none,

include/swift/AST/IRGenOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ class IRGenOptions {
169169
/// Path prefixes that should be rewritten in debug info.
170170
PathRemapper DebugPrefixMap;
171171

172+
/// Path prefixes that should be rewritten in coverage info.
173+
PathRemapper CoveragePrefixMap;
174+
172175
/// What level of debug info to generate.
173176
IRGenDebugInfoLevel DebugInfoLevel : 2;
174177

include/swift/Option/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,9 @@ def gdwarf_types : Flag<["-"], "gdwarf-types">,
708708
def debug_prefix_map : Separate<["-"], "debug-prefix-map">,
709709
Flags<[FrontendOption]>,
710710
HelpText<"Remap source paths in debug info">;
711+
def coverage_prefix_map : Separate<["-"], "coverage-prefix-map">,
712+
Flags<[FrontendOption]>,
713+
HelpText<"Remap source paths in coverage info">;
711714

712715
def debug_info_format : Joined<["-"], "debug-info-format=">,
713716
Flags<[FrontendOption]>,

lib/AST/Type.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3059,8 +3059,8 @@ operator()(SubstitutableType *maybeOpaqueType) const {
30593059
}))
30603060
return maybeOpaqueType;
30613061

3062-
// If the type still contains opaque types, recur.
3063-
if (substTy->hasOpaqueArchetype()) {
3062+
// If the type changed, but still contains opaque types, recur.
3063+
if (!substTy->isEqual(maybeOpaqueType) && substTy->hasOpaqueArchetype()) {
30643064
return ::substOpaqueTypesWithUnderlyingTypes(
30653065
substTy, inContext, contextExpansion, isContextWholeModule);
30663066
}

lib/Driver/Driver.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ static void validateDebugInfoArgs(DiagnosticEngine &diags,
179179
for (auto A : args.getAllArgValues(options::OPT_debug_prefix_map))
180180
if (A.find('=') == StringRef::npos)
181181
diags.diagnose(SourceLoc(), diag::error_invalid_debug_prefix_map, A);
182+
183+
// Check for any -coverage-prefix-map options that aren't of the form
184+
// 'original=remapped' (either side can be empty, however).
185+
for (auto A : args.getAllArgValues(options::OPT_coverage_prefix_map))
186+
if (A.find('=') == StringRef::npos)
187+
diags.diagnose(SourceLoc(), diag::error_invalid_coverage_prefix_map, A);
182188
}
183189

184190
static void validateVerifyIncrementalDependencyArgs(DiagnosticEngine &diags,

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
265265

266266
// Pass on file paths that should be remapped in debug info.
267267
inputArgs.AddAllArgs(arguments, options::OPT_debug_prefix_map);
268+
inputArgs.AddAllArgs(arguments, options::OPT_coverage_prefix_map);
268269

269270
// Pass through the values passed to -Xfrontend.
270271
inputArgs.AddAllArgValues(arguments, options::OPT_Xfrontend);

lib/Frontend/CompilerInvocation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,11 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
12671267
Opts.DebugPrefixMap.addMapping(SplitMap.first, SplitMap.second);
12681268
}
12691269

1270+
for (auto A : Args.getAllArgValues(options::OPT_coverage_prefix_map)) {
1271+
auto SplitMap = StringRef(A).split('=');
1272+
Opts.CoveragePrefixMap.addMapping(SplitMap.first, SplitMap.second);
1273+
}
1274+
12701275
for (const Arg *A : Args.filtered(OPT_Xcc)) {
12711276
StringRef Opt = A->getValue();
12721277
if (Opt.startswith("-D") || Opt.startswith("-U"))

lib/IRGen/GenClangDecl.cpp

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,43 @@ class ClangDeclRefFinder
3434
return true;
3535
}
3636
};
37+
38+
// If any (re)declaration of `decl` contains executable code, returns that
39+
// redeclaration; otherwise, returns nullptr.
40+
// In the case of a function, executable code is contained in the function
41+
// definition. In the case of a variable, executable code can be contained in
42+
// the initializer of the variable.
43+
clang::Decl *getDeclWithExecutableCode(clang::Decl *decl) {
44+
if (auto fd = dyn_cast<clang::FunctionDecl>(decl)) {
45+
const clang::FunctionDecl *definition;
46+
if (fd->hasBody(definition)) {
47+
return const_cast<clang::FunctionDecl *>(definition);
48+
}
49+
} else if (auto vd = dyn_cast<clang::VarDecl>(decl)) {
50+
clang::VarDecl *initializingDecl = vd->getInitializingDeclaration();
51+
if (initializingDecl) {
52+
return initializingDecl;
53+
}
54+
}
55+
56+
return nullptr;
57+
}
58+
3759
} // end anonymous namespace
3860

3961
void IRGenModule::emitClangDecl(const clang::Decl *decl) {
40-
auto valueDecl = dyn_cast<clang::ValueDecl>(decl);
41-
if (!valueDecl || valueDecl->isExternallyVisible()) {
62+
// Ignore this decl if we've seen it before.
63+
if (!GlobalClangDecls.insert(decl->getCanonicalDecl()).second)
64+
return;
65+
66+
// Fast path for the case where `decl` doesn't contain executable code, so it
67+
// can't reference any other declarations that we would need to emit.
68+
if (getDeclWithExecutableCode(const_cast<clang::Decl *>(decl)) == nullptr) {
4269
ClangCodeGen->HandleTopLevelDecl(
4370
clang::DeclGroupRef(const_cast<clang::Decl*>(decl)));
4471
return;
4572
}
4673

47-
if (!GlobalClangDecls.insert(decl->getCanonicalDecl()).second)
48-
return;
4974
SmallVector<const clang::Decl *, 8> stack;
5075
stack.push_back(decl);
5176

@@ -69,13 +94,15 @@ void IRGenModule::emitClangDecl(const clang::Decl *decl) {
6994

7095
while (!stack.empty()) {
7196
auto *next = const_cast<clang::Decl *>(stack.pop_back_val());
72-
if (auto fn = dyn_cast<clang::FunctionDecl>(next)) {
73-
const clang::FunctionDecl *definition;
74-
if (fn->hasBody(definition)) {
75-
refFinder.TraverseDecl(const_cast<clang::FunctionDecl *>(definition));
76-
next = const_cast<clang::FunctionDecl *>(definition);
77-
}
97+
if (clang::Decl *executableDecl = getDeclWithExecutableCode(next)) {
98+
refFinder.TraverseDecl(executableDecl);
99+
next = executableDecl;
78100
}
101+
102+
if (auto var = dyn_cast<clang::VarDecl>(next))
103+
if (!var->isFileVarDecl())
104+
continue;
105+
79106
ClangCodeGen->HandleTopLevelDecl(clang::DeclGroupRef(next));
80107
}
81108
}

lib/IRGen/GenCoverage.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "IRGenModule.h"
1919
#include "SwiftTargetInfo.h"
2020

21+
#include "swift/AST/IRGenOptions.h"
2122
#include "swift/SIL/SILModule.h"
2223
#include "llvm/IR/Constants.h"
2324
#include "llvm/IR/Module.h"
@@ -60,6 +61,7 @@ void IRGenModule::emitCoverageMapping() {
6061
if (std::find(Files.begin(), Files.end(), M->getFile()) == Files.end())
6162
Files.push_back(M->getFile());
6263

64+
auto remapper = getOptions().CoveragePrefixMap;
6365
// Awkwardly munge absolute filenames into a vector of StringRefs.
6466
// TODO: This is heinous - the same thing is happening in clang, but the API
6567
// really needs to be cleaned up for both.
@@ -68,7 +70,7 @@ void IRGenModule::emitCoverageMapping() {
6870
for (StringRef Name : Files) {
6971
llvm::SmallString<256> Path(Name);
7072
llvm::sys::fs::make_absolute(Path);
71-
FilenameStrs.push_back(std::string(Path.begin(), Path.end()));
73+
FilenameStrs.push_back(remapper.remapPath(Path));
7274
FilenameRefs.push_back(FilenameStrs.back());
7375
}
7476

lib/IRGen/GenDecl.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,8 +1104,11 @@ void IRGenerator::emitGlobalTopLevel(llvm::StringSet<> *linkerDirectives) {
11041104
IGM->emitSILDifferentiabilityWitness(&dw);
11051105
}
11061106

1107-
// Emit code coverage mapping data.
1108-
PrimaryIGM->emitCoverageMapping();
1107+
// Emit code coverage mapping data for all modules
1108+
for (auto Iter : *this) {
1109+
IRGenModule *IGM = Iter.second;
1110+
IGM->emitCoverageMapping();
1111+
}
11091112

11101113
for (auto Iter : *this) {
11111114
IRGenModule *IGM = Iter.second;

0 commit comments

Comments
 (0)