Skip to content

Commit 7c0ac3b

Browse files
committed
Implement preliminary support for deserializing cc1 flags in Swift modules.
There is no functional change on all other code paths. Support for cc1 flags isn't complete. Since cc1 flags are not stable it's not feasible to keep a list of all multi-arg flags, for example. It also makes it difficult to correctly identify where working directories and path remappings should applied. For all these reasons, using cc1 flags for anything but a local build with explicit modules and precise compiler invocations isn't supported yet. rdar://133249595
1 parent a0bfc8c commit 7c0ac3b

File tree

9 files changed

+92
-6
lines changed

9 files changed

+92
-6
lines changed

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,7 +1573,21 @@ bool ShouldUnique(StringRef arg) {
15731573

15741574
// static
15751575
void SwiftASTContext::AddExtraClangArgs(const std::vector<std::string> &source,
1576-
std::vector<std::string> &dest) {
1576+
std::vector<std::string> &dest,
1577+
bool cc1) {
1578+
// FIXME: Support for cc1 flags isn't complete. The uniquing
1579+
// algortihm below does not work for cc1 flags. Since cc1 flags are
1580+
// not stable it's not feasible to keep a list of all multi-arg
1581+
// flags, for example. It also makes it difficult to correctly
1582+
// identify where workng directories and path remappings should
1583+
// applied. For all these reasons, using cc1 flags for anything but
1584+
// a local build with explicit modules and precise compiler
1585+
// invocations isn't supported yet.
1586+
if (cc1) {
1587+
dest.insert(dest.end(), source.begin(), source.end());
1588+
return;
1589+
}
1590+
15771591
llvm::StringSet<> unique_flags;
15781592
for (auto &arg : dest)
15791593
unique_flags.insert(arg);
@@ -1751,11 +1765,27 @@ static void applyOverrideOptions(std::vector<std::string> &args,
17511765
void SwiftASTContext::AddExtraClangArgs(
17521766
const std::vector<std::string> &ExtraArgs, StringRef overrideOpts) {
17531767
swift::ClangImporterOptions &importer_options = GetClangImporterOptions();
1754-
AddExtraClangArgs(ExtraArgs, importer_options.ExtraArgs);
1768+
1769+
// Detect cc1 flags. When DirectClangCC1ModuleBuild is on then the
1770+
// clang arguments in the serialized invocation are clang cc1 flags,
1771+
// which are very specific to one compiler version and cannot
1772+
// be merged with driver options.
1773+
bool fresh_invocation = importer_options.ExtraArgs.empty();
1774+
if (fresh_invocation && !ExtraArgs.empty() && ExtraArgs.front() == "-cc1")
1775+
importer_options.DirectClangCC1ModuleBuild = true;
1776+
if (!importer_options.DirectClangCC1ModuleBuild && !ExtraArgs.empty() &&
1777+
ExtraArgs.front() == "-cc1")
1778+
AddDiagnostic(
1779+
eSeverityWarning,
1780+
"Mixing and matching of driver and cc1 Clang options detected");
1781+
1782+
AddExtraClangArgs(ExtraArgs, importer_options.ExtraArgs,
1783+
importer_options.DirectClangCC1ModuleBuild);
17551784
applyOverrideOptions(importer_options.ExtraArgs, overrideOpts);
17561785
if (HasNonexistentExplicitModule(importer_options.ExtraArgs))
17571786
RemoveExplicitModules(importer_options.ExtraArgs);
17581787

1788+
// Detect explicitly-built modules.
17591789
m_has_explicit_modules =
17601790
llvm::any_of(importer_options.ExtraArgs, [](const std::string &arg) {
17611791
return StringRef(arg).starts_with("-fmodule-file=");
@@ -1864,6 +1894,11 @@ void SwiftASTContext::FilterClangImporterOptions(
18641894
arg_sr == "-fno-implicit-module-maps")
18651895
continue;
18661896

1897+
// This is not a cc1 option.
1898+
if (arg_sr.starts_with("--target=") && ctx &&
1899+
ctx->GetClangImporterOptions().DirectClangCC1ModuleBuild)
1900+
continue;
1901+
18671902
// The VFS options turn into fatal errors when the referenced file
18681903
// is not found. Since the Xcode build system tends to create a
18691904
// lot of VFS overlays by default, stat them and emit a warning if
@@ -1889,6 +1924,7 @@ void SwiftASTContext::FilterClangImporterOptions(
18891924
// Keep it.
18901925
extra_args.push_back(ivfs_arg);
18911926
}
1927+
18921928
extra_args.push_back(std::move(arg));
18931929
}
18941930
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class SwiftASTContext : public TypeSystemSwift {
279279
void AddExtraClangArgs(const std::vector<std::string> &ExtraArgs,
280280
llvm::StringRef overrideOpts = "");
281281
static void AddExtraClangArgs(const std::vector<std::string>& source,
282-
std::vector<std::string>& dest);
282+
std::vector<std::string>& dest, bool cc1);
283283
static std::string GetPluginServer(llvm::StringRef plugin_library_path);
284284
/// Removes nonexisting VFS overlay options.
285285
static void FilterClangImporterOptions(std::vector<std::string> &extra_args,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
SWIFT_SOURCES := main.swift
2+
SWIFT_ENABLE_EXPLICIT_MODULES := YES
3+
SWIFTFLAGS_EXTRAS = -I$(SRCDIR) -Xfrontend -experimental-clang-importer-direct-cc1-scan
4+
5+
include Makefile.rules
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import lldb
2+
from lldbsuite.test.lldbtest import *
3+
from lldbsuite.test.decorators import *
4+
import lldbsuite.test.lldbutil as lldbutil
5+
import unittest2
6+
7+
class TestSwiftClangImporterExplicitCC1(TestBase):
8+
9+
NO_DEBUG_INFO_TESTCASE = True
10+
11+
# Don't run ClangImporter tests if Clangimporter is disabled.
12+
@skipIf(setting=('symbols.use-swift-clangimporter', 'false'))
13+
@skipIf(setting=('symbols.swift-precise-compiler-invocation', 'false'))
14+
@skipIf(setting=('plugin.typesystem.clang.experimental-redecl-completion', 'true'), bugnumber='rdar://128094135')
15+
@skipUnlessDarwin
16+
@swiftTest
17+
def test(self):
18+
"""
19+
Test flipping on/off implicit modules.
20+
"""
21+
self.build()
22+
lldbutil.run_to_source_breakpoint(self, "break here",
23+
lldb.SBFileSpec('main.swift'))
24+
log = self.getBuildArtifact("types.log")
25+
self.expect('log enable lldb types -f "%s"' % log)
26+
self.expect("expression obj", DATA_TYPES_DISPLAYED_CORRECTLY,
27+
substrs=["b ="])
28+
self.filecheck('platform shell cat "%s"' % log, __file__)
29+
# CHECK: SwiftASTContextForExpressions(module: "a", cu: "main.swift")::LogConfiguration() -- -cc1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "b.h"
2+
3+
struct A {
4+
struct B b;
5+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
struct B {};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import ClangA
2+
let obj = A()
3+
print("break here \(obj)")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module ClangA {
2+
header "a.h"
3+
}
4+
5+
module ClangB {
6+
header "b.h"
7+
}

lldb/unittests/Symbol/TestSwiftASTContext.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,15 @@ const std::vector<std::string> uniqued_flags = {
234234
TEST_F(ClangArgs, UniquingCollisionWithExistingFlags) {
235235
const std::vector<std::string> source = duplicated_flags;
236236
std::vector<std::string> dest = uniqued_flags;
237-
SwiftASTContext::AddExtraClangArgs(source, dest);
237+
SwiftASTContext::AddExtraClangArgs(source, dest, false);
238238

239239
EXPECT_EQ(dest, uniqued_flags);
240240
}
241241

242242
TEST_F(ClangArgs, UniquingCollisionWithAddedFlags) {
243243
const std::vector<std::string> source = duplicated_flags;
244244
std::vector<std::string> dest;
245-
SwiftASTContext::AddExtraClangArgs(source, dest);
245+
SwiftASTContext::AddExtraClangArgs(source, dest, false);
246246

247247
EXPECT_EQ(dest, uniqued_flags);
248248
}
@@ -251,7 +251,7 @@ TEST_F(ClangArgs, DoubleDash) {
251251
// -v with all currently ignored arguments following.
252252
const std::vector<std::string> source{"-v", "--", "-Werror", ""};
253253
std::vector<std::string> dest;
254-
SwiftASTContext::AddExtraClangArgs(source, dest);
254+
SwiftASTContext::AddExtraClangArgs(source, dest, false);
255255

256256
// Check that all ignored arguments got removed.
257257
EXPECT_EQ(dest, std::vector<std::string>({"-v"}));

0 commit comments

Comments
 (0)