From 0f7b42201f3746b909ac5a8482c7b3608c91af4c Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Mon, 9 Sep 2024 12:38:49 -0700 Subject: [PATCH 1/4] Clean up test decorators (NFC) (cherry picked from commit 6e0815dcb851f6fa2e96d2cbbb96115122c39c35) --- .../cpp_exceptions/TestSwiftCPPExceptionsInREPL.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lldb/test/API/repl/cpp_exceptions/TestSwiftCPPExceptionsInREPL.py b/lldb/test/API/repl/cpp_exceptions/TestSwiftCPPExceptionsInREPL.py index b479a1e4217d0..55e5fc6639448 100644 --- a/lldb/test/API/repl/cpp_exceptions/TestSwiftCPPExceptionsInREPL.py +++ b/lldb/test/API/repl/cpp_exceptions/TestSwiftCPPExceptionsInREPL.py @@ -13,7 +13,7 @@ import swift import lldbsuite.test.lldbutil as lldbutil from lldbsuite.test.lldbtest import * -from lldbsuite.test import decorators +from lldbsuite.test.decorators import * class TestSwiftREPLExceptions(TestBase): @@ -23,8 +23,8 @@ class TestSwiftREPLExceptions(TestBase): # each debug info format. NO_DEBUG_INFO_TESTCASE = True - @decorators.skipUnlessDarwin - @decorators.swiftTest + @skipUnlessDarwin + @swiftTest def test_set_repl_mode_exceptions(self): """ Test that SetREPLMode turns off trapping exceptions.""" return @@ -32,8 +32,8 @@ def test_set_repl_mode_exceptions(self): self.main_source_file = lldb.SBFileSpec("main.swift") self.do_repl_mode_test() - @decorators.skipUnlessDarwin - @decorators.swiftTest + @skipUnlessDarwin + @swiftTest def test_repl_exceptions(self): """ Test the lldb --repl turns off trapping exceptions.""" self.build() @@ -43,7 +43,7 @@ def setUp(self): # Call super's setUp(). TestBase.setUp(self) - @decorators.skipIfRemote + @skipIfRemote def do_repl_test(self): sdk_root = "" with open(self.getBuildArtifact("sdkroot.txt"), 'r') as f: From 38c9db930de94c46f53d18a7062a26abedd9855b Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Mon, 9 Sep 2024 12:40:27 -0700 Subject: [PATCH 2/4] Remove unnecessary function (NFC) (cherry picked from commit 377ba4728a7b74fbc80585360a3087b3823db616) --- .../API/repl/cpp_exceptions/TestSwiftCPPExceptionsInREPL.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lldb/test/API/repl/cpp_exceptions/TestSwiftCPPExceptionsInREPL.py b/lldb/test/API/repl/cpp_exceptions/TestSwiftCPPExceptionsInREPL.py index 55e5fc6639448..f1e6d7ae5e5df 100644 --- a/lldb/test/API/repl/cpp_exceptions/TestSwiftCPPExceptionsInREPL.py +++ b/lldb/test/API/repl/cpp_exceptions/TestSwiftCPPExceptionsInREPL.py @@ -39,10 +39,6 @@ def test_repl_exceptions(self): self.build() self.do_repl_test() - def setUp(self): - # Call super's setUp(). - TestBase.setUp(self) - @skipIfRemote def do_repl_test(self): sdk_root = "" From 0b67150055d617978ba668a30133596e99486333 Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Mon, 9 Sep 2024 13:47:02 -0700 Subject: [PATCH 3/4] More test cleanups (NFC) (cherry picked from commit a33c59e85c6b9e6e53237742480b20a3d1b74096) --- .../repl/cpp_exceptions/TestSwiftCPPExceptionsInREPL.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lldb/test/API/repl/cpp_exceptions/TestSwiftCPPExceptionsInREPL.py b/lldb/test/API/repl/cpp_exceptions/TestSwiftCPPExceptionsInREPL.py index f1e6d7ae5e5df..26bb55ab2da6d 100644 --- a/lldb/test/API/repl/cpp_exceptions/TestSwiftCPPExceptionsInREPL.py +++ b/lldb/test/API/repl/cpp_exceptions/TestSwiftCPPExceptionsInREPL.py @@ -25,9 +25,8 @@ class TestSwiftREPLExceptions(TestBase): @skipUnlessDarwin @swiftTest - def test_set_repl_mode_exceptions(self): + def DISABLED_test_set_repl_mode_exceptions(self): """ Test that SetREPLMode turns off trapping exceptions.""" - return self.build() self.main_source_file = lldb.SBFileSpec("main.swift") self.do_repl_mode_test() @@ -45,6 +44,8 @@ def do_repl_test(self): with open(self.getBuildArtifact("sdkroot.txt"), 'r') as f: sdk_root = f.readlines()[0] self.assertGreater(len(sdk_root), 0) + if sdk_root[-1] == '\n': + sdk_root = sdk_root[:-1] build_dir = self.getBuildDir() repl_args = [lldbtest_config.lldbExec, "-x", "--repl=-enable-objc-interop -sdk %s -L%s -I%s"%(sdk_root, build_dir, build_dir)] repl_proc = subprocess.Popen(repl_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd=build_dir) @@ -67,7 +68,7 @@ def do_repl_mode_test(self): lldb.SBFileSpec(os.path.join(wd, filename))) self.assertFalse(err.Fail(), 'Failed to copy ' + filename) (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self, - "Set a breakpoint here", self.main_source_file) + "Set a breakpoint here", self.main_source_file) frame = thread.GetFrameAtIndex(0) options = lldb.SBExpressionOptions() From 4e4ee3e69d9d07c82928475630a6820c5c1bebf3 Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Fri, 6 Sep 2024 18:16:14 -0700 Subject: [PATCH 4/4] [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 48cc29d1ca91f04241c0a3c59a6bb8c686c09c54) --- lldb/include/lldb/Symbol/TypeSystem.h | 3 +- .../TypeSystem/Swift/SwiftASTContext.cpp | 38 ++++++++++++------- .../TypeSystem/Swift/SwiftASTContext.h | 5 ++- .../Swift/TypeSystemSwiftTypeRef.cpp | 9 ++--- .../TypeSystem/Swift/TypeSystemSwiftTypeRef.h | 3 +- lldb/source/Symbol/TypeSystem.cpp | 3 +- lldb/source/Target/Thread.cpp | 25 ++++++++---- .../TestSwiftImportSearchPaths.py | 10 ++--- 8 files changed, 59 insertions(+), 37 deletions(-) diff --git a/lldb/include/lldb/Symbol/TypeSystem.h b/lldb/include/lldb/Symbol/TypeSystem.h index cb3e80b996cf8..00b299d1f6695 100644 --- a/lldb/include/lldb/Symbol/TypeSystem.h +++ b/lldb/include/lldb/Symbol/TypeSystem.h @@ -572,7 +572,8 @@ class TypeSystem : public PluginInterface, /// have a way to communicate errors. This method can be called by a /// process to tell the TypeSystem to send any diagnostics to the /// process so they can be surfaced to the user. - virtual void DiagnoseWarnings(Process &process, Module &module) const; + virtual void DiagnoseWarnings(Process &process, + const SymbolContext &sc) const; virtual std::optional ReportStatistics(); diff --git a/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp b/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp index 175f28a55ead6..b52b68e2088c4 100644 --- a/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp +++ b/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp @@ -1167,13 +1167,14 @@ static void printASTValidationError( LLDB_LOG(log, " -- {0}", ExtraOpt); } -void SwiftASTContext::DiagnoseWarnings(Process &process, Module &module) const { - if (!HasDiagnostics()) +void SwiftASTContext::DiagnoseWarnings(Process &process, + const SymbolContext &sc) const { + if (!sc.module_sp || !HasDiagnostics()) return; auto debugger_id = process.GetTarget().GetDebugger().GetID(); std::string msg; llvm::raw_string_ostream(msg) << "Cannot load Swift type information for " - << module.GetFileSpec().GetPath(); + << sc.module_sp->GetFileSpec().GetPath(); Debugger::ReportWarning(msg, debugger_id, &m_swift_import_warning); StreamAllDiagnostics(debugger_id); } @@ -2111,7 +2112,8 @@ ProcessModule(Module &module, std::string m_description, std::vector &plugin_search_options, std::vector &module_search_paths, std::vector> &framework_search_paths, - std::vector &extra_clang_args) { + std::vector &extra_clang_args, + std::string &error) { { llvm::raw_string_ostream ss(m_description); ss << "::ProcessModule(" << '"'; @@ -2215,8 +2217,7 @@ ProcessModule(Module &module, std::string m_description, return; bool found_swift_modules = false; bool got_serialized_options = false; - llvm::SmallString<0> error; - llvm::raw_svector_ostream errs(error); + llvm::raw_string_ostream errs(error); swift::CompilerInvocation invocation; auto ast_file_datas = module.GetASTData(eLanguageTypeSwift); std::string module_name = module.GetSpecificationDescription(); @@ -2365,7 +2366,7 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module, bool got_serialized_options = false; llvm::SmallString<0> error; llvm::raw_svector_ostream errs(error); - // Implicit search paths will be discovered by ValidateSecionModules(). + // Implicit search paths will be discovered by ValidateSectionModules(). bool discover_implicit_search_paths = false; auto ast_file_datas = module.GetASTData(eLanguageTypeSwift); std::string module_name = module.GetSpecificationDescription(); @@ -2436,6 +2437,7 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module, const bool use_all_compiler_flags = false; const bool is_target_module = true; + std::string error; StringRef module_filter; std::vector plugin_search_options; std::vector extra_clang_args = swift_ast_sp->GetClangArguments(); @@ -2444,7 +2446,10 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module, ProcessModule(module, m_description, discover_implicit_search_paths, use_all_compiler_flags, is_target_module, module_filter, triple, plugin_search_options, module_search_paths, - framework_search_paths, extra_clang_args); + framework_search_paths, extra_clang_args, error); + if (!error.empty()) + swift_ast_sp->AddDiagnostic(eSeverityError, error); + // Apply the working directory to all relative paths. StringRef overrideOpts = target ? target->GetSwiftClangOverrideOptions() : ""; swift_ast_sp->AddExtraClangArgs(extra_clang_args, overrideOpts); @@ -2800,6 +2805,7 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance( for (ModuleSP module_sp : target.GetImages().Modules()) if (module_sp) { + std::string error; StringRef module_filter; std::vector extra_clang_args; ProcessModule(*module_sp, m_description, discover_implicit_search_paths, @@ -2807,7 +2813,7 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance( target.GetExecutableModulePointer() == module_sp.get(), module_filter, triple, plugin_search_options, module_search_paths, framework_search_paths, - extra_clang_args); + extra_clang_args, error); swift_ast_sp->AddExtraClangArgs(extra_clang_args, target.GetSwiftClangOverrideOptions()); } @@ -3137,14 +3143,17 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance( /*is_system*/ false}); ModuleSP module_sp = sc.module_sp; if (module_sp) { + std::string error; StringRef module_filter = swift_module_name; std::vector extra_clang_args; ProcessModule(*module_sp, m_description, discover_implicit_search_paths, use_all_compiler_flags, target.GetExecutableModulePointer() == module_sp.get(), module_filter, triple, plugin_search_options, - module_search_paths, framework_search_paths, - extra_clang_args); + module_search_paths, framework_search_paths, extra_clang_args, + error); + if (!error.empty()) + swift_ast_sp->AddDiagnostic(eSeverityError, error); swift_ast_sp->AddExtraClangArgs(extra_clang_args, target.GetSwiftClangOverrideOptions()); } @@ -5604,13 +5613,16 @@ void SwiftASTContextForExpressions::ModulesDidLoad(ModuleList &module_list) { lldb::ModuleSP module_sp = module_list.GetModuleAtIndex(mi); if (!module_sp) continue; + std::string error; StringRef module_filter; ProcessModule(*module_sp, m_description, discover_implicit_search_paths, use_all_compiler_flags, target_sp->GetExecutableModulePointer() == module_sp.get(), module_filter, GetTriple(), plugin_search_options, - module_search_paths, framework_search_paths, - extra_clang_args); + module_search_paths, framework_search_paths, extra_clang_args, + error); + if (!error.empty()) + AddDiagnostic(eSeverityError, error); // If the use-all-compiler-flags setting is enabled, the // expression context is supposed to merge all search paths // from all dylibs. diff --git a/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h b/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h index 4f86f06c43dc8..3d41987913e8b 100644 --- a/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h +++ b/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h @@ -487,8 +487,9 @@ class SwiftASTContext : public TypeSystemSwift { /// Return only fatal errors. Status GetFatalErrors() const; /// Notify the Process about any Swift or ClangImporter errors. - void DiagnoseWarnings(Process &process, Module &module) const override; - + void DiagnoseWarnings(Process &process, + const SymbolContext &sc) const override; + bool SetColorizeDiagnostics(bool b); void PrintDiagnostics(DiagnosticManager &diagnostic_manager, diff --git a/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp b/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp index 2a06416838bdb..49c6950e7bab4 100644 --- a/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp +++ b/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp @@ -2059,11 +2059,10 @@ Status TypeSystemSwiftTypeRef::IsCompatible() { } void TypeSystemSwiftTypeRef::DiagnoseWarnings(Process &process, - Module &module) const { - // This gets called only from Thread::FrameSelectedCallback(StackFrame) - // and is of limited usefuleness. - if (auto *swift_ast_context = GetSwiftASTContextOrNull(nullptr)) - swift_ast_context->DiagnoseWarnings(process, module); + const SymbolContext &sc) const { + // This gets called only from Thread::FrameSelectedCallback(StackFrame). + if (auto *swift_ast_context = GetSwiftASTContextOrNull(&sc)) + swift_ast_context->DiagnoseWarnings(process, sc); } plugin::dwarf::DWARFASTParser *TypeSystemSwiftTypeRef::GetDWARFParser() { diff --git a/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h b/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h index 0f75946c14556..42693e93fe3ce 100644 --- a/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h +++ b/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h @@ -108,7 +108,8 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift { bool SupportsLanguage(lldb::LanguageType language) override; Status IsCompatible() override; - void DiagnoseWarnings(Process &process, Module &module) const override; + void DiagnoseWarnings(Process &process, + const SymbolContext &sc) const override; plugin::dwarf::DWARFASTParser *GetDWARFParser() override; // CompilerDecl functions ConstString DeclGetName(void *opaque_decl) override { diff --git a/lldb/source/Symbol/TypeSystem.cpp b/lldb/source/Symbol/TypeSystem.cpp index 7913b7d7aafb3..a5428d685976f 100644 --- a/lldb/source/Symbol/TypeSystem.cpp +++ b/lldb/source/Symbol/TypeSystem.cpp @@ -166,7 +166,8 @@ bool TypeSystem::IsMeaninglessWithoutDynamicResolution(void *type) { return false; } -void TypeSystem::DiagnoseWarnings(Process &process, Module &module) const {} +void TypeSystem::DiagnoseWarnings(Process &process, + const SymbolContext &sc) const {} Status TypeSystem::IsCompatible() { // Assume a language is compatible. Override this virtual function diff --git a/lldb/source/Target/Thread.cpp b/lldb/source/Target/Thread.cpp index db293a370c694..d2dc673fb4ffd 100644 --- a/lldb/source/Target/Thread.cpp +++ b/lldb/source/Target/Thread.cpp @@ -53,6 +53,10 @@ #include "lldb/Utility/StreamString.h" #include "lldb/lldb-enumerations.h" +#ifdef LLDB_ENABLE_SWIFT +#include "Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h" +#endif + #include #include @@ -344,13 +348,20 @@ void Thread::FrameSelectedCallback(StackFrame *frame) { GetProcess()->PrintWarningToolchainMismatch(sc); #endif } - SymbolContext msc = frame->GetSymbolContext(eSymbolContextModule); - if (msc.module_sp) - msc.module_sp->ForEachTypeSystem([&](lldb::TypeSystemSP ts) { - if (ts) - ts->DiagnoseWarnings(*GetProcess(), *msc.module_sp); - return true; - }); +#ifdef LLDB_ENABLE_SWIFT + { + SymbolContext msc = + frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextModule); + Status error; + ExecutionContext exe_ctx; + frame->CalculateExecutionContext(exe_ctx); + if (auto *exe_scope = exe_ctx.GetBestExecutionContextScope()) + if (auto target = frame->CalculateTarget()) + if (auto swift_ast_ctx = + target->GetSwiftScratchContext(error, *exe_scope, false)) + swift_ast_ctx->get()->DiagnoseWarnings(*GetProcess(), msc); + } +#endif } lldb::StopInfoSP Thread::GetStopInfo() { diff --git a/lldb/test/API/lang/swift/expression/import_search_paths/TestSwiftImportSearchPaths.py b/lldb/test/API/lang/swift/expression/import_search_paths/TestSwiftImportSearchPaths.py index 9cf21e28af417..560aa860816ef 100644 --- a/lldb/test/API/lang/swift/expression/import_search_paths/TestSwiftImportSearchPaths.py +++ b/lldb/test/API/lang/swift/expression/import_search_paths/TestSwiftImportSearchPaths.py @@ -36,12 +36,8 @@ def do_test(self, flag): prefix = 'POSITIVE' else: prefix = 'NEGATIVE' - self.filecheck('platform shell cat "%s"' % types_log, __file__, - '--check-prefix=CHECK_MOD_'+prefix) self.filecheck('platform shell cat "%s"' % types_log, __file__, '--check-prefix=CHECK_EXP_'+prefix) -# CHECK_MOD_POSITIVE: SwiftASTContextForModule("a.out")::LogConfiguration(){{.*hidden$}} -# CHECK_MOD_NEGATIVE: SwiftASTContextForModule("a.out")::LogConfiguration(){{.*hidden$}} -# CHECK_EXP_POSITIVE: SwiftASTContextForExpressions::LogConfiguration(){{.*hidden$}} -# CHECK_EXP_NEGATIVE-NOT: SwiftASTContextForExpressions::LogConfiguration(){{.*hidden$}} -# CHECK_EXP_NEGATIVE: SwiftASTContextForExpressions::LogConfiguration(){{.*}}Extra clang arguments +# CHECK_EXP_POSITIVE: SwiftASTContextForExpressions{{.*}}::LogConfiguration(){{.*hidden$}} +# CHECK_EXP_NEGATIVE-NOT: SwiftASTContextForExpressions{{.*}}::LogConfiguration(){{.*hidden$}} +# CHECK_EXP_NEGATIVE: SwiftASTContextForExpressions{{.*}}::LogConfiguration(){{.*}}Extra clang arguments