Skip to content

Commit b09cd64

Browse files
[lldb] Fix clang importer for swift caching debugging (attempt #2)
Further improve how lldb constructing clang importer to utilize explicit modules: * Add logics to initialize the swift ASTContext from direct cc1 arguments embedded in the swiftmodule * Make sure the direct cc1 arguments are not repeated constructed. It should only be constructed as a fresh invocation * Make sure the lldb doesn't overwrite the explicit module build settings when filter arguments. This ensures the already built explicit modules are used. * Use the newly added clang option that ignore CAS info when loading PCMs. This allows lldb to load CAS enabled PCM directly from disk. rdar://135611011 (cherry picked from commit d3b9f2c)
1 parent cd48b7d commit b09cd64

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,24 +1752,32 @@ static void applyOverrideOptions(std::vector<std::string> &args,
17521752

17531753
void SwiftASTContext::AddExtraClangArgs(
17541754
const std::vector<std::string> &ExtraArgs, StringRef overrideOpts) {
1755+
if (ExtraArgs.empty())
1756+
return;
1757+
17551758
swift::ClangImporterOptions &importer_options = GetClangImporterOptions();
17561759

17571760
// Detect cc1 flags. When DirectClangCC1ModuleBuild is on then the
17581761
// clang arguments in the serialized invocation are clang cc1 flags,
17591762
// which are very specific to one compiler version and cannot
17601763
// be merged with driver options.
17611764
bool fresh_invocation = importer_options.ExtraArgs.empty();
1762-
if (fresh_invocation && !ExtraArgs.empty() && ExtraArgs.front() == "-cc1")
1763-
importer_options.DirectClangCC1ModuleBuild = true;
1764-
if (!importer_options.DirectClangCC1ModuleBuild && !ExtraArgs.empty() &&
1765-
ExtraArgs.front() == "-cc1")
1765+
bool invocation_direct_cc1 = ExtraArgs.front() == "-cc1";
1766+
1767+
// If it is not a fresh invocation, make sure the cc1 option matches.
1768+
if (!fresh_invocation &&
1769+
(importer_options.DirectClangCC1ModuleBuild != invocation_direct_cc1))
17661770
AddDiagnostic(
17671771
eSeverityWarning,
17681772
"Mixing and matching of driver and cc1 Clang options detected");
17691773

1774+
importer_options.DirectClangCC1ModuleBuild = invocation_direct_cc1;
1775+
17701776
// If using direct cc1 flags, compute the arguments and return.
1771-
// Since this is cc1 flags, no driver overwrite can be applied.
1777+
// Since this is cc1 flags, override options are driver flags and don't apply.
17721778
if (importer_options.DirectClangCC1ModuleBuild) {
1779+
if (!fresh_invocation)
1780+
importer_options.ExtraArgs.clear();
17731781
AddExtraClangCC1Args(ExtraArgs, importer_options.ExtraArgs);
17741782
return;
17751783
}
@@ -1821,12 +1829,17 @@ void SwiftASTContext::AddExtraClangCC1Args(
18211829
invocation.getFrontendOpts().ModuleCacheKeys.clear();
18221830
invocation.getCASOpts() = clang::CASOptions();
18231831

1832+
// Ignore CAS info inside modules when loading.
1833+
invocation.getFrontendOpts().ModuleLoadIgnoreCAS = true;
1834+
18241835
// Remove non-existing modules in a systematic way.
18251836
bool module_missing = false;
18261837
auto CheckFileExists = [&](const char *file) {
18271838
if (!llvm::sys::fs::exists(file)) {
1828-
std::string m_description;
1829-
HEALTH_LOG_PRINTF("Nonexistent explicit module file %s", file);
1839+
std::string warn;
1840+
llvm::raw_string_ostream(warn)
1841+
<< "Nonexistent explicit module file " << file;
1842+
AddDiagnostic(eSeverityWarning, warn);
18301843
module_missing = true;
18311844
}
18321845
};
@@ -1835,8 +1848,8 @@ void SwiftASTContext::AddExtraClangCC1Args(
18351848
llvm::for_each(invocation.getFrontendOpts().ModuleFiles,
18361849
[&](const auto &mod) { CheckFileExists(mod.c_str()); });
18371850

1838-
// If missing, clear all the prebuilt module options and use implicit module
1839-
// build.
1851+
// If missing, clear all the prebuilt module options and switch to implicit
1852+
// modules build.
18401853
if (module_missing) {
18411854
invocation.getHeaderSearchOpts().PrebuiltModuleFiles.clear();
18421855
invocation.getFrontendOpts().ModuleFiles.clear();
@@ -1944,6 +1957,10 @@ void SwiftASTContext::RemapClangImporterOptions(
19441957

19451958
void SwiftASTContext::FilterClangImporterOptions(
19461959
std::vector<std::string> &extra_args, SwiftASTContext *ctx) {
1960+
// The direct cc1 mode do not need any extra audit.
1961+
if (ctx && ctx->GetClangImporterOptions().DirectClangCC1ModuleBuild)
1962+
return;
1963+
19471964
std::string ivfs_arg;
19481965
// Copy back a filtered version of ExtraArgs.
19491966
std::vector<std::string> orig_args(std::move(extra_args));
@@ -1954,11 +1971,6 @@ void SwiftASTContext::FilterClangImporterOptions(
19541971
arg_sr == "-fno-implicit-module-maps")
19551972
continue;
19561973

1957-
// This is not a cc1 option.
1958-
if (arg_sr.starts_with("--target=") && ctx &&
1959-
ctx->GetClangImporterOptions().DirectClangCC1ModuleBuild)
1960-
continue;
1961-
19621974
// The VFS options turn into fatal errors when the referenced file
19631975
// is not found. Since the Xcode build system tends to create a
19641976
// lot of VFS overlays by default, stat them and emit a warning if
@@ -2245,6 +2257,11 @@ ProcessModule(Module &module, std::string m_description,
22452257
for (auto path : opts.getFrameworkSearchPaths())
22462258
framework_search_paths.push_back({path.Path, path.IsSystem});
22472259
auto &clang_opts = invocation.getClangImporterOptions().ExtraArgs;
2260+
// If the args embedded are cc1 args, they are not compatible with existing
2261+
// setting. Clear the previous args.
2262+
if (!clang_opts.empty() && clang_opts.front() == "-cc1")
2263+
extra_clang_args.clear();
2264+
22482265
for (const std::string &arg : clang_opts) {
22492266
extra_clang_args.push_back(arg);
22502267
LOG_VERBOSE_PRINTF(GetLog(LLDBLog::Types), "adding Clang argument \"%s\".",

lldb/test/API/lang/swift/clangimporter/caching/TestSwiftClangImporterCaching.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,7 @@ def test(self):
2727
self.filecheck('platform shell cat "%s"' % log, __file__)
2828
### -cc1 should be round-tripped so there is no more `-cc1` in the extra args. Look for `-triple` which is a cc1 flag.
2929
# CHECK: SwiftASTContextForExpressions(module: "a", cu: "main.swift")::LogConfiguration() -- -triple
30+
# CHECK: SwiftASTContextForExpressions(module: "a", cu: "main.swift") Module import remark: loaded module 'ClangA'
3031
# CHECK-NOT: -cc1
3132
# CHECK-NOT: -fmodule-file-cache-key
33+
# CHECK-NOT: Clang error:

lldb/test/Shell/Swift/caching.test

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# REQUIRES: swift
2+
# REQUIRES: system-darwin
3+
4+
# RUN: rm -rf %t && mkdir %t
5+
# RUN: split-file %s %t
6+
# RUN: %target-swiftc -g -Onone -save-temps \
7+
# RUN: -module-cache-path %t/cache %t/main.swift \
8+
# RUN: -cache-compile-job -cas-path %t/cas -explicit-module-build \
9+
# RUN: -module-name main -o %t/main
10+
11+
# RUN: %lldb %t/main -s %t/lldb.script 2>&1 | FileCheck %s
12+
# CHECK: LogConfiguration() -- Extra clang arguments
13+
# CHECK-COUNT-1: LogConfiguration() -- -triple
14+
# CHECK: (Int) ${{.*}} = 1
15+
16+
//--- main.swift
17+
func test() {
18+
print("break here")
19+
}
20+
test()
21+
22+
//--- lldb.script
23+
# Force loading from interface to simulate no binary module available.
24+
settings set symbols.swift-module-loading-mode prefer-interface
25+
log enable lldb types
26+
b test
27+
run
28+
# Create a SwiftASTContext
29+
expr 1
30+
quit

0 commit comments

Comments
 (0)