Skip to content

Commit 3958e98

Browse files
committed
[lldb] Ignore -triple flag for ClangImporter (#8025)
This change is in support of explicit swift modules (#8017). With explicit modules, the resulting clang flags include the `-triple` flag, which has not been supported by `SwiftASTContext::AddExtraClangArgs`. This change ignores any found triple, relying on lldb's existing triple/target discovery functionality. (cherry picked from commit 32cf95a)
1 parent f0939ce commit 3958e98

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,14 @@ static std::string GetPluginServerForSDK(llvm::StringRef sdk_path) {
11601160
return server_or_err->str();
11611161
}
11621162

1163+
namespace {
1164+
constexpr std::array<std::string_view, 4> g_known_eplicit_module_prefixes =
1165+
{"-fmodule-map-file=",
1166+
"-fmodule-file=",
1167+
"-fno-implicit-modules",
1168+
"-fno-implicit-module-maps"};
1169+
}
1170+
11631171
/// Retrieve the serialized AST data blobs and initialize the compiler
11641172
/// invocation with the concatenated search paths from the blobs.
11651173
/// \returns true if an error was encountered.
@@ -1483,8 +1491,8 @@ bool ConsumeIncludeOption(StringRef &arg, StringRef &prefix) {
14831491
}
14841492

14851493
std::array<StringRef, 2> macro_flags = { "-D", "-U" };
1486-
std::array<StringRef, 5> multi_arg_flags =
1487-
{ "-D", "-U", "-I", "-F", "-working-directory" };
1494+
std::array<StringRef, 6> multi_arg_flags = {
1495+
"-D", "-U", "-I", "-F", "-working-directory", "-triple"};
14881496
std::array<StringRef, 6> args_to_unique = {
14891497
"-D", "-U", "-I", "-F", "-fmodule-file=", "-fmodule-map-file="};
14901498

@@ -1519,7 +1527,25 @@ void SwiftASTContext::AddExtraClangArgs(const std::vector<std::string> &source,
15191527

15201528
llvm::SmallString<128> cur_working_dir;
15211529
llvm::SmallString<128> clang_argument;
1530+
1531+
auto match_explicit_build_option = [](StringRef arg) {
1532+
for (const auto &option : g_known_eplicit_module_prefixes)
1533+
if (arg.starts_with(option))
1534+
return true;
1535+
return false;
1536+
};
1537+
bool has_explicit_builds_enabled =
1538+
llvm::find(source, "-fno-implicit-modules") != source.end();
1539+
15221540
for (const std::string &arg : source) {
1541+
// Ignore the `-triple` flag. First, this is not a driver flag, and second,
1542+
// lldb has its own logic to determine the target. Ignore now, before
1543+
// appending the argument.
1544+
if (clang_argument == "-triple") {
1545+
clang_argument.clear();
1546+
continue;
1547+
}
1548+
15231549
// Join multi-arg options for uniquing.
15241550
clang_argument += arg;
15251551
if (IsMultiArgClangFlag(clang_argument))
@@ -1549,6 +1575,14 @@ void SwiftASTContext::AddExtraClangArgs(const std::vector<std::string> &source,
15491575
if (clang_argument.empty())
15501576
continue;
15511577

1578+
// In case of explicit modules, for now fallback to implicit
1579+
// module loading.
1580+
// TODO: Incorporate loading explicit module dependencies to
1581+
// speedup dependency resolution.
1582+
if (has_explicit_builds_enabled &&
1583+
match_explicit_build_option(clang_argument))
1584+
continue;
1585+
15521586
// Otherwise add the argument to the list.
15531587
if (!IsMacroDefinition(clang_argument))
15541588
ApplyWorkingDir(clang_argument, cur_working_dir);

0 commit comments

Comments
 (0)