Skip to content

Commit 9d45b9c

Browse files
author
git apple-llvm automerger
committed
Merge commit '08bf953b8ed6' from swift/release/6.0 into stable/20230725
2 parents 8f5e47f + 08bf953 commit 9d45b9c

File tree

10 files changed

+139
-22
lines changed

10 files changed

+139
-22
lines changed

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

Lines changed: 77 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include "clang/Basic/TargetOptions.h"
5858
#include "clang/Driver/Driver.h"
5959
#include "clang/Frontend/CompilerInstance.h"
60+
#include "clang/Frontend/TextDiagnosticPrinter.h"
6061
#include "clang/Lex/Preprocessor.h"
6162

6263
#include "clang/Lex/PreprocessorOptions.h"
@@ -1572,21 +1573,7 @@ bool ShouldUnique(StringRef arg) {
15721573

15731574
// static
15741575
void SwiftASTContext::AddExtraClangArgs(const std::vector<std::string> &source,
1575-
std::vector<std::string> &dest,
1576-
bool cc1) {
1577-
// FIXME: Support for cc1 flags isn't complete. The uniquing
1578-
// algortihm below does not work for cc1 flags. Since cc1 flags are
1579-
// not stable it's not feasible to keep a list of all multi-arg
1580-
// flags, for example. It also makes it difficult to correctly
1581-
// identify where workng directories and path remappings should
1582-
// applied. For all these reasons, using cc1 flags for anything but
1583-
// a local build with explicit modules and precise compiler
1584-
// invocations isn't supported yet.
1585-
if (cc1) {
1586-
dest.insert(dest.end(), source.begin(), source.end());
1587-
return;
1588-
}
1589-
1576+
std::vector<std::string> &dest) {
15901577
llvm::StringSet<> unique_flags;
15911578
for (auto &arg : dest)
15921579
unique_flags.insert(arg);
@@ -1778,8 +1765,14 @@ void SwiftASTContext::AddExtraClangArgs(
17781765
eSeverityWarning,
17791766
"Mixing and matching of driver and cc1 Clang options detected");
17801767

1781-
AddExtraClangArgs(ExtraArgs, importer_options.ExtraArgs,
1782-
importer_options.DirectClangCC1ModuleBuild);
1768+
// If using direct cc1 flags, compute the arguments and return.
1769+
// Since this is cc1 flags, no driver overwrite can be applied.
1770+
if (importer_options.DirectClangCC1ModuleBuild) {
1771+
AddExtraClangCC1Args(ExtraArgs, importer_options.ExtraArgs);
1772+
return;
1773+
}
1774+
1775+
AddExtraClangArgs(ExtraArgs, importer_options.ExtraArgs);
17831776
applyOverrideOptions(importer_options.ExtraArgs, overrideOpts);
17841777
if (HasNonexistentExplicitModule(importer_options.ExtraArgs))
17851778
RemoveExplicitModules(importer_options.ExtraArgs);
@@ -1791,6 +1784,73 @@ void SwiftASTContext::AddExtraClangArgs(
17911784
});
17921785
}
17931786

1787+
void SwiftASTContext::AddExtraClangCC1Args(
1788+
const std::vector<std::string> &source, std::vector<std::string> &dest) {
1789+
clang::CompilerInvocation invocation;
1790+
llvm::SmallVector<const char *> clangArgs;
1791+
clangArgs.reserve(source.size());
1792+
llvm::for_each(source, [&](const std::string &Arg) {
1793+
// Workaround for the extra driver argument embedded in the swiftmodule by
1794+
// some swift compiler version. It always starts with `--target=` and it is
1795+
// not a valid cc1 option.
1796+
if (!StringRef(Arg).starts_with("--target="))
1797+
clangArgs.push_back(Arg.c_str());
1798+
});
1799+
1800+
std::string diags;
1801+
llvm::raw_string_ostream os(diags);
1802+
auto diagOpts = llvm::makeIntrusiveRefCnt<clang::DiagnosticOptions>();
1803+
clang::DiagnosticsEngine clangDiags(
1804+
new clang::DiagnosticIDs(), diagOpts,
1805+
new clang::TextDiagnosticPrinter(os, diagOpts.get()));
1806+
1807+
if (!clang::CompilerInvocation::CreateFromArgs(invocation, clangArgs,
1808+
clangDiags)) {
1809+
// If cc1 arguments failed to parse, report diagnostics and return
1810+
// immediately.
1811+
AddDiagnostic(eSeverityError, diags);
1812+
// Disable direct-cc1 build as fallback.
1813+
GetClangImporterOptions().DirectClangCC1ModuleBuild = false;
1814+
return;
1815+
}
1816+
1817+
// Clear module cache key and other CAS options to load modules from disk
1818+
// directly.
1819+
invocation.getFrontendOpts().ModuleCacheKeys.clear();
1820+
invocation.getCASOpts() = clang::CASOptions();
1821+
1822+
// Remove non-existing modules in a systematic way.
1823+
bool module_missing = false;
1824+
auto CheckFileExists = [&](const char *file) {
1825+
if (!llvm::sys::fs::exists(file)) {
1826+
std::string m_description;
1827+
HEALTH_LOG_PRINTF("Nonexistent explicit module file %s", file);
1828+
module_missing = true;
1829+
}
1830+
};
1831+
llvm::for_each(invocation.getHeaderSearchOpts().PrebuiltModuleFiles,
1832+
[&](const auto &mod) { CheckFileExists(mod.second.c_str()); });
1833+
llvm::for_each(invocation.getFrontendOpts().ModuleFiles,
1834+
[&](const auto &mod) { CheckFileExists(mod.c_str()); });
1835+
1836+
// If missing, clear all the prebuilt module options and use implicit module
1837+
// build.
1838+
if (module_missing) {
1839+
invocation.getHeaderSearchOpts().PrebuiltModuleFiles.clear();
1840+
invocation.getFrontendOpts().ModuleFiles.clear();
1841+
invocation.getLangOpts().ImplicitModules = true;
1842+
invocation.getHeaderSearchOpts().ImplicitModuleMaps = true;
1843+
}
1844+
1845+
invocation.generateCC1CommandLine(
1846+
[&](const llvm::Twine &arg) { dest.push_back(arg.str()); });
1847+
1848+
// If cc1 arguments are parsed and generated correctly, set explicitly-built
1849+
// module since only explicit module build can use direct cc1 mode.
1850+
m_has_explicit_modules = true;
1851+
return;
1852+
}
1853+
17941854
void SwiftASTContext::AddUserClangArgs(TargetProperties &props) {
17951855
Args args(props.GetSwiftExtraClangFlags());
17961856
std::vector<std::string> user_clang_flags;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,10 @@ class SwiftASTContext : public TypeSystemSwift {
278278
/// apply the working directory to any relative paths.
279279
void AddExtraClangArgs(const std::vector<std::string> &ExtraArgs,
280280
llvm::StringRef overrideOpts = "");
281+
void AddExtraClangCC1Args(const std::vector<std::string>& source,
282+
std::vector<std::string>& dest);
281283
static void AddExtraClangArgs(const std::vector<std::string>& source,
282-
std::vector<std::string>& dest, bool cc1);
284+
std::vector<std::string>& dest);
283285
static std::string GetPluginServer(llvm::StringRef plugin_library_path);
284286
/// Removes nonexisting VFS overlay options.
285287
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) -cache-compile-job -cas-path $(BUILDDIR)/cas
4+
5+
include Makefile.rules
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 TestSwiftClangImporterCaching(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+
### -cc1 should be round-tripped so there is no more `-cc1` in the extra args. Look for `-triple` which is a cc1 flag.
30+
# CHECK: SwiftASTContextForExpressions(module: "a", cu: "main.swift")::LogConfiguration() -- -triple
31+
# CHECK-NOT: -cc1
32+
# CHECK-NOT: -fmodule-file-cache-key
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/test/API/lang/swift/clangimporter/explicit_cc1/TestSwiftClangImporterExplicitCC1.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ def test(self):
2626
self.expect("expression obj", DATA_TYPES_DISPLAYED_CORRECTLY,
2727
substrs=["b ="])
2828
self.filecheck('platform shell cat "%s"' % log, __file__)
29-
# CHECK: SwiftASTContextForExpressions(module: "a", cu: "main.swift")::LogConfiguration() -- -cc1
29+
### -cc1 should be round-tripped so there is no more `-cc1` in the extra args. Look for `-triple` which is a cc1 flag.
30+
# CHECK: SwiftASTContextForExpressions(module: "a", cu: "main.swift")::LogConfiguration() -- -triple
31+
# CHECK-NOT: -cc1

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, false);
237+
SwiftASTContext::AddExtraClangArgs(source, dest);
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, false);
245+
SwiftASTContext::AddExtraClangArgs(source, dest);
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, false);
254+
SwiftASTContext::AddExtraClangArgs(source, dest);
255255

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

0 commit comments

Comments
 (0)