Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit afd7092

Browse files
kastiglioneadrian-prantl
authored andcommitted
[lldb] Add setting to override ClangImporter driver options
(cherry picked from commit 2d7060f)
1 parent 186dc2e commit afd7092

File tree

8 files changed

+113
-5
lines changed

8 files changed

+113
-5
lines changed

lldb/include/lldb/Target/Target.h

+2
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ class TargetProperties : public Properties {
174174

175175
llvm::StringRef GetSwiftExtraClangFlags() const;
176176

177+
llvm::StringRef GetSwiftClangOverrideOptions() const;
178+
177179
bool GetSwiftReadMetadataFromFileCache() const;
178180

179181
bool GetSwiftUseReflectionSymbols() const;

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

+72-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "Plugins/TypeSystem/Swift/StoringDiagnosticConsumer.h"
1616
#include "Plugins/ExpressionParser/Swift/SwiftPersistentExpressionState.h"
1717

18+
#include "lldb/Utility/Log.h"
1819
#include "swift/AST/ASTContext.h"
1920
#include "swift/AST/ASTDemangler.h"
2021
#include "swift/AST/ASTMangler.h"
@@ -58,6 +59,7 @@
5859

5960
#include "llvm/ADT/ArrayRef.h"
6061
#include "llvm/ADT/STLExtras.h"
62+
#include "llvm/ADT/SmallVector.h"
6163
#include "llvm/ADT/StringRef.h"
6264
#include "llvm/ADT/StringSet.h"
6365
#include "llvm/CodeGen/TargetSubtargetInfo.h"
@@ -1645,9 +1647,72 @@ void RemoveExplicitModules(std::vector<std::string> &args) {
16451647

16461648
} // namespace
16471649

1648-
void SwiftASTContext::AddExtraClangArgs(const std::vector<std::string> &ExtraArgs) {
1650+
/// LLDB wrapper for `clang::driver::applyOverrideOptions` (which implements
1651+
/// CCC_OVERRIDE_OPTIONS behavior).
1652+
static void applyOverrideOptions(std::vector<std::string> &args,
1653+
llvm::StringRef overrideOpts) {
1654+
if (overrideOpts.empty())
1655+
return;
1656+
1657+
// Convert input args to the type required by applyOverrideOptions.
1658+
llvm::SmallVector<const char *, 64> raw_args;
1659+
// Add placeholder clang executable, which applyOverrideOptions expects to be
1660+
// the first argument.
1661+
raw_args.push_back("clang");
1662+
for (const std::string &arg : args)
1663+
raw_args.push_back(arg.data());
1664+
1665+
// LLVM stream backed by a callback. This is used to redirect
1666+
// applyOverrideOptions logging to LLDB.
1667+
struct CallbackStream : public llvm::raw_ostream {
1668+
using callback_t = std::function<void(const char *, size_t)>;
1669+
callback_t m_callback;
1670+
uint64_t m_pos = 0;
1671+
1672+
CallbackStream(callback_t callback) : m_callback(callback) {}
1673+
~CallbackStream() override { flush(); }
1674+
1675+
void write_impl(const char *Ptr, size_t Size) override {
1676+
m_callback(Ptr, Size);
1677+
m_pos += Size;
1678+
}
1679+
1680+
uint64_t current_pos() const override { return m_pos; }
1681+
};
1682+
1683+
// Perform the override operations.
1684+
llvm::StringSet<> savedStrings;
1685+
auto *log = GetLog(LLDBLog::Expressions);
1686+
CallbackStream log_stream{[log](const char *Ptr, size_t Size) {
1687+
if (!log)
1688+
return;
1689+
if (Ptr[Size] == '\n')
1690+
// Skip the newline because LLDB logging writes a newline.
1691+
Size--;
1692+
log->PutString({Ptr, Size});
1693+
}};
1694+
1695+
clang::driver::applyOverrideOptions(raw_args, overrideOpts.data(),
1696+
savedStrings, &log_stream);
1697+
1698+
// Delete the placeholder "clang" executable argument.
1699+
raw_args.erase(raw_args.begin());
1700+
1701+
// Copy `raw_args` into a new args vector.
1702+
std::vector<std::string> new_args;
1703+
for (const char *arg : raw_args)
1704+
new_args.emplace_back(arg);
1705+
1706+
// Only now that `raw_args` has been copied into `new_args`, can `args` be
1707+
// overwritten. This is because `args` owns the data pointed to by `raw_args`.
1708+
args = new_args;
1709+
}
1710+
1711+
void SwiftASTContext::AddExtraClangArgs(
1712+
const std::vector<std::string> &ExtraArgs, StringRef overrideOpts) {
16491713
swift::ClangImporterOptions &importer_options = GetClangImporterOptions();
16501714
AddExtraClangArgs(ExtraArgs, importer_options.ExtraArgs);
1715+
applyOverrideOptions(importer_options.ExtraArgs, overrideOpts);
16511716
if (HasNonexistentExplicitModule(importer_options.ExtraArgs))
16521717
RemoveExplicitModules(importer_options.ExtraArgs);
16531718
}
@@ -2075,7 +2140,8 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
20752140
// Apply the working directory to all relative paths.
20762141
std::vector<std::string> DeserializedArgs = swift_ast_sp->GetClangArguments();
20772142
swift_ast_sp->GetClangImporterOptions().ExtraArgs.clear();
2078-
swift_ast_sp->AddExtraClangArgs(DeserializedArgs);
2143+
StringRef overrideOpts = target ? target->GetSwiftClangOverrideOptions() : "";
2144+
swift_ast_sp->AddExtraClangArgs(DeserializedArgs, overrideOpts);
20792145
if (target)
20802146
swift_ast_sp->AddUserClangArgs(*target);
20812147
else
@@ -2565,7 +2631,8 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
25652631
use_all_compiler_flags, module_filter, target, triple,
25662632
plugin_search_options, module_search_paths,
25672633
framework_search_paths, extra_clang_args);
2568-
swift_ast_sp->AddExtraClangArgs(extra_clang_args);
2634+
swift_ast_sp->AddExtraClangArgs(extra_clang_args,
2635+
target.GetSwiftClangOverrideOptions());
25692636
}
25702637

25712638
for (const FileSpec &path : target.GetSwiftModuleSearchPaths())
@@ -2879,7 +2946,8 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
28792946
use_all_compiler_flags, module_filter, target, triple,
28802947
plugin_search_options, module_search_paths,
28812948
framework_search_paths, extra_clang_args);
2882-
swift_ast_sp->AddExtraClangArgs(extra_clang_args);
2949+
swift_ast_sp->AddExtraClangArgs(extra_clang_args,
2950+
target.GetSwiftClangOverrideOptions());
28832951
}
28842952

28852953
// Now fold any extra options we were passed. This has to be done

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ class SwiftASTContext : public TypeSystemSwift {
275275

276276
/// Add a list of Clang arguments to the ClangImporter options and
277277
/// apply the working directory to any relative paths.
278-
void AddExtraClangArgs(const std::vector<std::string> &ExtraArgs);
278+
void AddExtraClangArgs(const std::vector<std::string> &ExtraArgs,
279+
llvm::StringRef overrideOpts = "");
279280
static void AddExtraClangArgs(const std::vector<std::string>& source,
280281
std::vector<std::string>& dest);
281282
static std::string GetPluginServer(llvm::StringRef plugin_library_path);

lldb/source/Target/Target.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -5154,6 +5154,11 @@ llvm::StringRef TargetProperties::GetSwiftExtraClangFlags() const {
51545154
return GetPropertyAtIndexAs<llvm::StringRef>(idx, "");
51555155
}
51565156

5157+
llvm::StringRef TargetProperties::GetSwiftClangOverrideOptions() const {
5158+
const uint32_t idx = ePropertySwiftClangOverrideOptions;
5159+
return GetPropertyAtIndexAs<llvm::StringRef>(idx, "");
5160+
}
5161+
51575162
FileSpecList TargetProperties::GetClangModuleSearchPaths() {
51585163
const uint32_t idx = ePropertyClangModuleSearchPaths;
51595164
return GetPropertyAtIndexAs<FileSpecList>(idx, {});

lldb/source/Target/TargetProperties.td

+3
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ let Definition = "target" in {
223223
def SwiftExtraClangFlags: Property<"swift-extra-clang-flags", "String">,
224224
DefaultStringValue<"">,
225225
Desc<"Additional -Xcc flags to be passed to the Swift ClangImporter.">;
226+
def SwiftClangOverrideOptions: Property<"swift-clang-override-options", "String">,
227+
DefaultStringValue<"">,
228+
Desc<"Specify CCC_OVERRIDE_OPTIONS for ClangImporter flags.">;
226229
def SwiftAutoImportFrameworks : Property<"swift-auto-import-frameworks", "Boolean">,
227230
DefaultFalse,
228231
Desc<"Automatically import all frameworks and dynamic libraries that are autolinked by Swift modules in the target.">;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SWIFT_SOURCES := main.swift
2+
SWIFTFLAGS_EXTRAS = -Xcc -DDELETEME=1
3+
4+
include Makefile.rules
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import lldb
2+
from lldbsuite.test.decorators import *
3+
import lldbsuite.test.lldbtest as lldbtest
4+
import lldbsuite.test.lldbutil as lldbutil
5+
6+
7+
class TestCase(lldbtest.TestBase):
8+
@swiftTest
9+
def test(self):
10+
"""Check that ClangImporter options can be overridden."""
11+
self.build()
12+
13+
log = self.getBuildArtifact("lldb.log")
14+
self.runCmd(f"log enable lldb expr -f '{log}'")
15+
self.runCmd("settings set target.swift-clang-override-options x-DDELETEME=1")
16+
17+
lldbutil.run_to_name_breakpoint(self, "main", bkpt_module="a.out")
18+
self.expect("expression 1")
19+
20+
self.filecheck(f"platform shell cat {log}", __file__)
21+
# CHECK: CCC_OVERRIDE_OPTIONS: x-DDELETEME=1
22+
# CHECK: Deleting argument -DDELETEME=1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Foundation
2+
3+
// empty main

0 commit comments

Comments
 (0)