Skip to content

Commit 4e4ee3e

Browse files
committed
[swift-lldb] Port TypeSystem::DiagnoseWarnings() to precise compiler invocations.
Due to the validation assertions triggering additional SwiftASTContexts to be created, this missing feature only breaks a shell test when running the tests with assertion disabled! (cherry picked from commit 48cc29d)
1 parent 0b67150 commit 4e4ee3e

File tree

8 files changed

+59
-37
lines changed

8 files changed

+59
-37
lines changed

lldb/include/lldb/Symbol/TypeSystem.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,8 @@ class TypeSystem : public PluginInterface,
572572
/// have a way to communicate errors. This method can be called by a
573573
/// process to tell the TypeSystem to send any diagnostics to the
574574
/// process so they can be surfaced to the user.
575-
virtual void DiagnoseWarnings(Process &process, Module &module) const;
575+
virtual void DiagnoseWarnings(Process &process,
576+
const SymbolContext &sc) const;
576577

577578
virtual std::optional<llvm::json::Value> ReportStatistics();
578579

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

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,13 +1167,14 @@ static void printASTValidationError(
11671167
LLDB_LOG(log, " -- {0}", ExtraOpt);
11681168
}
11691169

1170-
void SwiftASTContext::DiagnoseWarnings(Process &process, Module &module) const {
1171-
if (!HasDiagnostics())
1170+
void SwiftASTContext::DiagnoseWarnings(Process &process,
1171+
const SymbolContext &sc) const {
1172+
if (!sc.module_sp || !HasDiagnostics())
11721173
return;
11731174
auto debugger_id = process.GetTarget().GetDebugger().GetID();
11741175
std::string msg;
11751176
llvm::raw_string_ostream(msg) << "Cannot load Swift type information for "
1176-
<< module.GetFileSpec().GetPath();
1177+
<< sc.module_sp->GetFileSpec().GetPath();
11771178
Debugger::ReportWarning(msg, debugger_id, &m_swift_import_warning);
11781179
StreamAllDiagnostics(debugger_id);
11791180
}
@@ -2111,7 +2112,8 @@ ProcessModule(Module &module, std::string m_description,
21112112
std::vector<swift::PluginSearchOption> &plugin_search_options,
21122113
std::vector<std::string> &module_search_paths,
21132114
std::vector<std::pair<std::string, bool>> &framework_search_paths,
2114-
std::vector<std::string> &extra_clang_args) {
2115+
std::vector<std::string> &extra_clang_args,
2116+
std::string &error) {
21152117
{
21162118
llvm::raw_string_ostream ss(m_description);
21172119
ss << "::ProcessModule(" << '"';
@@ -2215,8 +2217,7 @@ ProcessModule(Module &module, std::string m_description,
22152217
return;
22162218
bool found_swift_modules = false;
22172219
bool got_serialized_options = false;
2218-
llvm::SmallString<0> error;
2219-
llvm::raw_svector_ostream errs(error);
2220+
llvm::raw_string_ostream errs(error);
22202221
swift::CompilerInvocation invocation;
22212222
auto ast_file_datas = module.GetASTData(eLanguageTypeSwift);
22222223
std::string module_name = module.GetSpecificationDescription();
@@ -2365,7 +2366,7 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
23652366
bool got_serialized_options = false;
23662367
llvm::SmallString<0> error;
23672368
llvm::raw_svector_ostream errs(error);
2368-
// Implicit search paths will be discovered by ValidateSecionModules().
2369+
// Implicit search paths will be discovered by ValidateSectionModules().
23692370
bool discover_implicit_search_paths = false;
23702371
auto ast_file_datas = module.GetASTData(eLanguageTypeSwift);
23712372
std::string module_name = module.GetSpecificationDescription();
@@ -2436,6 +2437,7 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
24362437
const bool use_all_compiler_flags = false;
24372438
const bool is_target_module = true;
24382439

2440+
std::string error;
24392441
StringRef module_filter;
24402442
std::vector<swift::PluginSearchOption> plugin_search_options;
24412443
std::vector<std::string> extra_clang_args = swift_ast_sp->GetClangArguments();
@@ -2444,7 +2446,10 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
24442446
ProcessModule(module, m_description, discover_implicit_search_paths,
24452447
use_all_compiler_flags, is_target_module, module_filter, triple,
24462448
plugin_search_options, module_search_paths,
2447-
framework_search_paths, extra_clang_args);
2449+
framework_search_paths, extra_clang_args, error);
2450+
if (!error.empty())
2451+
swift_ast_sp->AddDiagnostic(eSeverityError, error);
2452+
24482453
// Apply the working directory to all relative paths.
24492454
StringRef overrideOpts = target ? target->GetSwiftClangOverrideOptions() : "";
24502455
swift_ast_sp->AddExtraClangArgs(extra_clang_args, overrideOpts);
@@ -2800,14 +2805,15 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
28002805

28012806
for (ModuleSP module_sp : target.GetImages().Modules())
28022807
if (module_sp) {
2808+
std::string error;
28032809
StringRef module_filter;
28042810
std::vector<std::string> extra_clang_args;
28052811
ProcessModule(*module_sp, m_description, discover_implicit_search_paths,
28062812
use_all_compiler_flags,
28072813
target.GetExecutableModulePointer() == module_sp.get(),
28082814
module_filter, triple, plugin_search_options,
28092815
module_search_paths, framework_search_paths,
2810-
extra_clang_args);
2816+
extra_clang_args, error);
28112817
swift_ast_sp->AddExtraClangArgs(extra_clang_args,
28122818
target.GetSwiftClangOverrideOptions());
28132819
}
@@ -3137,14 +3143,17 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
31373143
/*is_system*/ false});
31383144
ModuleSP module_sp = sc.module_sp;
31393145
if (module_sp) {
3146+
std::string error;
31403147
StringRef module_filter = swift_module_name;
31413148
std::vector<std::string> extra_clang_args;
31423149
ProcessModule(*module_sp, m_description, discover_implicit_search_paths,
31433150
use_all_compiler_flags,
31443151
target.GetExecutableModulePointer() == module_sp.get(),
31453152
module_filter, triple, plugin_search_options,
3146-
module_search_paths, framework_search_paths,
3147-
extra_clang_args);
3153+
module_search_paths, framework_search_paths, extra_clang_args,
3154+
error);
3155+
if (!error.empty())
3156+
swift_ast_sp->AddDiagnostic(eSeverityError, error);
31483157
swift_ast_sp->AddExtraClangArgs(extra_clang_args,
31493158
target.GetSwiftClangOverrideOptions());
31503159
}
@@ -5604,13 +5613,16 @@ void SwiftASTContextForExpressions::ModulesDidLoad(ModuleList &module_list) {
56045613
lldb::ModuleSP module_sp = module_list.GetModuleAtIndex(mi);
56055614
if (!module_sp)
56065615
continue;
5616+
std::string error;
56075617
StringRef module_filter;
56085618
ProcessModule(*module_sp, m_description, discover_implicit_search_paths,
56095619
use_all_compiler_flags,
56105620
target_sp->GetExecutableModulePointer() == module_sp.get(),
56115621
module_filter, GetTriple(), plugin_search_options,
5612-
module_search_paths, framework_search_paths,
5613-
extra_clang_args);
5622+
module_search_paths, framework_search_paths, extra_clang_args,
5623+
error);
5624+
if (!error.empty())
5625+
AddDiagnostic(eSeverityError, error);
56145626
// If the use-all-compiler-flags setting is enabled, the
56155627
// expression context is supposed to merge all search paths
56165628
// from all dylibs.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,9 @@ class SwiftASTContext : public TypeSystemSwift {
487487
/// Return only fatal errors.
488488
Status GetFatalErrors() const;
489489
/// Notify the Process about any Swift or ClangImporter errors.
490-
void DiagnoseWarnings(Process &process, Module &module) const override;
491-
490+
void DiagnoseWarnings(Process &process,
491+
const SymbolContext &sc) const override;
492+
492493
bool SetColorizeDiagnostics(bool b);
493494

494495
void PrintDiagnostics(DiagnosticManager &diagnostic_manager,

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,11 +2059,10 @@ Status TypeSystemSwiftTypeRef::IsCompatible() {
20592059
}
20602060

20612061
void TypeSystemSwiftTypeRef::DiagnoseWarnings(Process &process,
2062-
Module &module) const {
2063-
// This gets called only from Thread::FrameSelectedCallback(StackFrame)
2064-
// and is of limited usefuleness.
2065-
if (auto *swift_ast_context = GetSwiftASTContextOrNull(nullptr))
2066-
swift_ast_context->DiagnoseWarnings(process, module);
2062+
const SymbolContext &sc) const {
2063+
// This gets called only from Thread::FrameSelectedCallback(StackFrame).
2064+
if (auto *swift_ast_context = GetSwiftASTContextOrNull(&sc))
2065+
swift_ast_context->DiagnoseWarnings(process, sc);
20672066
}
20682067

20692068
plugin::dwarf::DWARFASTParser *TypeSystemSwiftTypeRef::GetDWARFParser() {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
108108
bool SupportsLanguage(lldb::LanguageType language) override;
109109
Status IsCompatible() override;
110110

111-
void DiagnoseWarnings(Process &process, Module &module) const override;
111+
void DiagnoseWarnings(Process &process,
112+
const SymbolContext &sc) const override;
112113
plugin::dwarf::DWARFASTParser *GetDWARFParser() override;
113114
// CompilerDecl functions
114115
ConstString DeclGetName(void *opaque_decl) override {

lldb/source/Symbol/TypeSystem.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ bool TypeSystem::IsMeaninglessWithoutDynamicResolution(void *type) {
166166
return false;
167167
}
168168

169-
void TypeSystem::DiagnoseWarnings(Process &process, Module &module) const {}
169+
void TypeSystem::DiagnoseWarnings(Process &process,
170+
const SymbolContext &sc) const {}
170171

171172
Status TypeSystem::IsCompatible() {
172173
// Assume a language is compatible. Override this virtual function

lldb/source/Target/Thread.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353
#include "lldb/Utility/StreamString.h"
5454
#include "lldb/lldb-enumerations.h"
5555

56+
#ifdef LLDB_ENABLE_SWIFT
57+
#include "Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h"
58+
#endif
59+
5660
#include <memory>
5761
#include <optional>
5862

@@ -344,13 +348,20 @@ void Thread::FrameSelectedCallback(StackFrame *frame) {
344348
GetProcess()->PrintWarningToolchainMismatch(sc);
345349
#endif
346350
}
347-
SymbolContext msc = frame->GetSymbolContext(eSymbolContextModule);
348-
if (msc.module_sp)
349-
msc.module_sp->ForEachTypeSystem([&](lldb::TypeSystemSP ts) {
350-
if (ts)
351-
ts->DiagnoseWarnings(*GetProcess(), *msc.module_sp);
352-
return true;
353-
});
351+
#ifdef LLDB_ENABLE_SWIFT
352+
{
353+
SymbolContext msc =
354+
frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextModule);
355+
Status error;
356+
ExecutionContext exe_ctx;
357+
frame->CalculateExecutionContext(exe_ctx);
358+
if (auto *exe_scope = exe_ctx.GetBestExecutionContextScope())
359+
if (auto target = frame->CalculateTarget())
360+
if (auto swift_ast_ctx =
361+
target->GetSwiftScratchContext(error, *exe_scope, false))
362+
swift_ast_ctx->get()->DiagnoseWarnings(*GetProcess(), msc);
363+
}
364+
#endif
354365
}
355366

356367
lldb::StopInfoSP Thread::GetStopInfo() {

lldb/test/API/lang/swift/expression/import_search_paths/TestSwiftImportSearchPaths.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,8 @@ def do_test(self, flag):
3636
prefix = 'POSITIVE'
3737
else:
3838
prefix = 'NEGATIVE'
39-
self.filecheck('platform shell cat "%s"' % types_log, __file__,
40-
'--check-prefix=CHECK_MOD_'+prefix)
4139
self.filecheck('platform shell cat "%s"' % types_log, __file__,
4240
'--check-prefix=CHECK_EXP_'+prefix)
43-
# CHECK_MOD_POSITIVE: SwiftASTContextForModule("a.out")::LogConfiguration(){{.*hidden$}}
44-
# CHECK_MOD_NEGATIVE: SwiftASTContextForModule("a.out")::LogConfiguration(){{.*hidden$}}
45-
# CHECK_EXP_POSITIVE: SwiftASTContextForExpressions::LogConfiguration(){{.*hidden$}}
46-
# CHECK_EXP_NEGATIVE-NOT: SwiftASTContextForExpressions::LogConfiguration(){{.*hidden$}}
47-
# CHECK_EXP_NEGATIVE: SwiftASTContextForExpressions::LogConfiguration(){{.*}}Extra clang arguments
41+
# CHECK_EXP_POSITIVE: SwiftASTContextForExpressions{{.*}}::LogConfiguration(){{.*hidden$}}
42+
# CHECK_EXP_NEGATIVE-NOT: SwiftASTContextForExpressions{{.*}}::LogConfiguration(){{.*hidden$}}
43+
# CHECK_EXP_NEGATIVE: SwiftASTContextForExpressions{{.*}}::LogConfiguration(){{.*}}Extra clang arguments

0 commit comments

Comments
 (0)