Skip to content

Commit 7fd0352

Browse files
committed
[lldb][swift] Ignore -- flag in swift-extra-clang-flags
`--` indicates to Clang that all following arguments are file names (even when they match a Clang compiler flag). This leads to the confusion situation where LLDB users can run `settings set target.swift-extra-clang-flags -- -v` and completely break the embedded Clang instance (that now tries to open the file `-v`). Fixes rdar://58457122
1 parent 95a9b9a commit 7fd0352

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,15 @@ void SwiftASTContext::AddExtraClangArgs(const std::vector<std::string>& source,
14851485
if (clang_argument.startswith("-Werror"))
14861486
continue;
14871487

1488+
// Drop `--`. This might be coming from the user-provided setting
1489+
// swift-extra-clang-flags (where users sometimes think a -- is necessary
1490+
// to separate the flags from the settings name). `--` indicates to Clang
1491+
// that all following arguments are file names instead of flags, so this
1492+
// should never be passed to Clang (which would otherwise either crash or
1493+
// cause Clang to look for files with the name '-Wflag-name`).
1494+
if (clang_argument == "--")
1495+
continue;
1496+
14881497
if (clang_argument.empty())
14891498
continue;
14901499

lldb/test/API/lang/swift/clangimporter/extra_clang_flags/TestSwiftExtraClangFlags.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,22 @@ def test_extra_clang_flags(self):
5959
lldb.SBFileSpec('main.swift'))
6060
self.expect("frame var foo", "sanity check", substrs=['(Foo)'])
6161
self.expect("expr FromOverlay(i: 23)", substrs=['(FromOverlay)', '23'])
62+
63+
# Don't run ClangImporter tests if Clangimporter is disabled.
64+
@skipIf(setting=('symbols.use-swift-clangimporter', 'false'))
65+
@skipIf(oslist=['windows'])
66+
@swiftTest
67+
def test_invalid_extra_clang_flags(self):
68+
"""
69+
Test that LLDB ignores specific invalid arguments in
70+
swift-extra-clang-flags.
71+
"""
72+
self.build()
73+
self.addTearDownHook(
74+
lambda: self.runCmd("settings clear target.swift-extra-clang-flags"))
75+
76+
self.expect('settings set target.swift-extra-clang-flags -- -v')
77+
78+
lldbutil.run_to_source_breakpoint(self, "break here",
79+
lldb.SBFileSpec('main.swift'))
80+
self.expect("frame var foo", substrs=['(Foo)'])

lldb/unittests/Symbol/TestSwiftASTContext.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,13 @@ TEST(ClangArgs, UniquingCollisionWithAddedFlags) {
248248

249249
EXPECT_EQ(dest, uniqued_flags);
250250
}
251+
252+
TEST(ClangArgs, DoubleDash) {
253+
// -v with all currently ignored arguments following.
254+
const std::vector<std::string> source{"-v", "--", "-Werror", ""};
255+
std::vector<std::string> dest;
256+
SwiftASTContext::AddExtraClangArgs(source, dest);
257+
258+
// Check that all ignored arguments got removed.
259+
EXPECT_EQ(dest, std::vector<std::string>({"-v"}));
260+
}

0 commit comments

Comments
 (0)