summaryrefslogtreecommitdiff
path: root/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
AgeCommit message (Collapse)Author
2025-11-21[clang][NFC] Inline Frontend/FrontendDiagnostic.h -> ↵Jordan Rupprecht
Basic/DiagnosticFrontend.h (#162883) d076608d58d1ec55016eb747a995511e3a3f72aa moved some deps around to avoid cycles and left clang/Frontend/FrontendDiagnostic.h as a shim that simply includes clang/Basic/DiagnosticFrontend.h. This PR inlines it so that nothing in tree still includes clang/Frontend/FrontendDiagnostic.h. Doing this will help prevent future layering issues. See #162865. Frontend already depends on Basic, so no new deps need to be added anywhere except for places that do strict dep checking.
2025-11-12[lldb][ClangExpressionParser] Clean up ownership of members in ↵Michael Buch
ClangDiagnosticManagerAdapter (#167731) This aligns `ClangDiagnosticManagerAdapter` with how we set up the diagnostics in `ClangModulesDeclVendor`. We fixed lifetime issues around the same kind of setup here: https://github.com/llvm/llvm-project/pull/167724 This class didn't suffer from the same lifetime issue because it used `shared_ptr`s. So the stream wasn't freed before `~TextDiagnosticPrinter` accessing it. But that begged the question of why these are `shared_ptr`s in the first place. This patch makes these `unique_ptr`s and fixes the destruction order that would now be an issue.
2025-11-07[lldb][ClangModulesDeclVendor] Revamp error handling when loading Clang ↵Michael Buch
modules (#166917) Instead of propagating the errors as a `bool`+`Stream` we change the `ClangModulesDeclVendor` module loading APIs to use `llvm::Error`. We also reword some of the diagnostics (notably removing the hardcoded `error:` prefix). A follow-up patch will further make the module loading errors less noisy. See the new tests for what the errors look like. rdar://164002569
2025-11-05[lldb] Mark single-argument SourceLanguage constructors explicit (#166527)Michael Buch
This avoids unintentional comparisons between `SourceLanguage` and `LanguageType`. Also marks `operator bool` explicit so we don't implicitly convert to bool.
2025-10-22[clang] Don't silently inherit the VFS from `FileManager` (#164323)Jan Svoboda
Since https://github.com/llvm/llvm-project/pull/158381 the `CompilerInstance` is aware of the VFS and co-owns it. To reduce scope of that PR, the VFS was being inherited from the `FileManager` during `setFileManager()` if it wasn't configured before. However, the implementation of that setter was buggy. This PR fixes the bug, and moves us closer to the long-term goal of `CompilerInstance` requiring the VFS to be configured explicitly and owned by the instance.
2025-10-10[lldb][Expression] Emit a 'Note' diagnostic that indicates the language used ↵Michael Buch
for expression evaluation (#161688) Depends on: * https://github.com/llvm/llvm-project/pull/162050 Since it's a 'Note' diagnostic it would only show up when expression evaluation actually failed. This helps with expression evaluation failure reports in mixed language environments where it's not quite clear what language the expression ran as. It may also reduce confusion around why the expression evaluator ran an expression in a language it wasn't asked to run (a softer alternative to what I attempted in https://github.com/llvm/llvm-project/pull/156648). Here are some example outputs: ``` # Without target (lldb) expr blah note: Falling back to default language. Ran expression as 'Objective C++'. # Stopped in target (lldb) expr blah note: Ran expression as 'C++14'. (lldb) expr -l objc -- blah note: Expression evaluation in pure Objective-C not supported. Ran expression as 'Objective C++'. (lldb) expr -l c -- blah note: Expression evaluation in pure C not supported. Ran expression as 'ISO C++'. (lldb) expr -l c++14 -- blah note: Ran expression as 'C++14' (lldb) expr -l c++20 -- blah note: Ran expression as 'C++20' (lldb) expr -l objective-c++ -- blah note: Ran expression as 'Objective C++' (lldb) expr -l D -- blah note: Expression evaluation in D not supported. Falling back to default language. Ran expression as 'Objective C++'. ``` I didn't put the diagnostic on the same line as the inline diagnostic for now because of implementation convenience, but if reviewers deem that a blocker I can take a stab at that again. Also, other language plugins (namely Swift), won't immediately benefit from this and will have to emit their own diagnistc. I played around with having a virtual API on `UserExpression` or `ExpressionParser` that will be called consistently, but by the time we're about to parse the expression we are already several frames deep into the plugin. Before (and at the beginning of) the generic `UserExpression::Parse` call we don't have enough information to notify which language we're going to parse in (at least for the C++ plugin). rdar://160297649 rdar://159669244
2025-10-02[clang] NFCI: Clean up `CompilerInstance::create{File,Source}Manager()` ↵Jan Svoboda
(#160748) The `CompilerInstance::createSourceManager()` function currently accepts the `FileManager` to be used. However, all clients call `CompilerInstance::createFileManager()` prior to creating the `SourceManager`, and it never makes sense to use a `FileManager` in the `SourceManager` that's different from the rest of the compiler. Passing the `FileManager` explicitly is redundant, error-prone, and deviates from the style of other `CompilerInstance` initialization APIs. This PR therefore removes the `FileManager` parameter from `createSourceManager()` and also stops returning the `FileManager` pointer from `createFileManager()`, since that was its primary use. Now, `createSourceManager()` internally calls `getFileManager()` instead.
2025-09-22[lldb][ClangExpressionParser] Reset DiagnosticManager before we create ↵Michael Buch
persistent variables (#160074) Here's an example crash that we've seen sporadically over the years: ``` 0 libsystem_kernel.dylib 0x19d392388 __pthread_kill + 8 1 libsystem_pthread.dylib 0x19d3cb88c pthread_kill + 296 2 libsystem_c.dylib 0x19d29cd04 raise + 32 3 LLDB 0x112cbbc94 SignalHandler(int, __siginfo*, void*) + 324 4 libsystem_platform.dylib 0x19d4056a4 _sigtramp + 56 5 LLDB 0x110dcbd38 clang::TextDiagnosticPrinter::HandleDiagnostic(clang::DiagnosticsEngine::Level, clang::Diagnostic const&) + 1216 6 LLDB 0x110dcbd38 clang::TextDiagnosticPrinter::HandleDiagnostic(clang::DiagnosticsEngine::Level, clang::Diagnostic const&) + 1216 7 LLDB 0x10de12128 ClangDiagnosticManagerAdapter::HandleDiagnostic(clang::DiagnosticsEngine::Level, clang::Diagnostic const&) + 332 8 LLDB 0x1121eb3dc clang::DiagnosticIDs::ProcessDiag(clang::DiagnosticsEngine&) const + 200 9 LLDB 0x1121e67a0 clang::DiagnosticsEngine::EmitCurrentDiagnostic(bool) + 100 10 LLDB 0x111d766cc IsStructurallyEquivalent(clang::StructuralEquivalenceContext&, clang::FieldDecl*, clang::FieldDecl*, clang::QualType) + 1568 11 LLDB 0x111d71b54 IsStructurallyEquivalent(clang::StructuralEquivalenceContext&, clang::RecordDecl*, clang::RecordDecl*) + 2076 12 LLDB 0x111d6e448 clang::StructuralEquivalenceContext::Finish() + 204 13 LLDB 0x111d6e1e0 clang::StructuralEquivalenceContext::IsEquivalent(clang::Decl*, clang::Decl*) + 32 14 LLDB 0x111d3b788 clang::ASTNodeImporter::IsStructuralMatch(clang::Decl*, clang::Decl*, bool, bool) + 168 15 LLDB 0x111d404e0 clang::ASTNodeImporter::VisitFunctionDecl(clang::FunctionDecl*) + 1300 16 LLDB 0x111d5cae0 clang::ASTImporter::ImportImpl(clang::Decl*) + 24 17 LLDB 0x10ddf30bc lldb_private::ClangASTImporter::ASTImporterDelegate::ImportImpl(clang::Decl*) + 308 18 LLDB 0x111d48140 clang::ASTImporter::Import(clang::Decl*) + 984 19 LLDB 0x10ddee9dc lldb_private::ClangASTImporter::CopyDecl(clang::ASTContext*, clang::Decl*) + 100 20 LLDB 0x10ddfab40 lldb_private::ClangASTSource::FindExternalLexicalDecls(clang::DeclContext const*, llvm::function_ref<bool (clang::Decl::Kind)>, llvm::SmallVectorImpl<clang::Decl*>&) + 1692 21 LLDB 0x111e1cb84 clang::DeclContext::LoadLexicalDeclsFromExternalStorage() const + 180 22 LLDB 0x111e1df50 clang::DeclContext::buildLookup() + 204 23 LLDB 0x111e1dcf4 clang::DeclContext::makeDeclVisibleInContextWithFlags(clang::NamedDecl*, bool, bool) + 504 24 LLDB 0x111d3b01c clang::ASTNodeImporter::ImportDeclContext(clang::DeclContext*, bool) + 724 25 LLDB 0x111d62d10 clang::ASTImporter::ImportDefinition(clang::Decl*) + 428 26 LLDB 0x10ddf1cb0 lldb_private::ClangASTImporter::ASTImporterDelegate::ImportDefinitionTo(clang::Decl*, clang::Decl*) + 524 27 LLDB 0x10ddef3c8 (anonymous namespace)::CompleteTagDeclsScope::~CompleteTagDeclsScope() + 616 28 LLDB 0x10ddef6dc lldb_private::ClangASTImporter::DeportDecl(clang::ASTContext*, clang::Decl*) + 436 29 LLDB 0x10ddec3dc lldb_private::ASTResultSynthesizer::CommitPersistentDecls() + 236 30 LLDB 0x10de1091c lldb_private::ClangExpressionParser::ParseInternal(lldb_private::DiagnosticManager&, clang::CodeCompleteConsumer*, unsigned int, unsigned int) + 2292 31 LLDB 0x10de21238 lldb_private::ClangUserExpression::TryParse(lldb_private::DiagnosticManager&, lldb_private::ExecutionContext&, lldb_private::ExecutionPolicy, bool, bool) + 328 ... ``` Here `ASTImporter::IsStructurallyEquivalent` is trying to emit a diagnostic using `ClangExpressionParser`'s `ClangDiagnosticManagerAdapter`. But `TextDiagnosticPrinter::TextDiag` seems to be `nullptr`. This can only happen when we call `HandleDiagnostic` on `ClangDiagnosticManagerAdapter` after we called `EndSourceFile`. Specifically, it looks like when moving a type from `Expression` AST to `Scratch` AST (in `CommitPersistentDecls`), something went wrong, so the ASTImporter tried to emit a diagnostic, but we already called `EndSourceFile` at that point. This patch moves the call to `ResetManager` to before `CommitPersistentDecls`, so if we called `HandleDiagnostic`, we would correctly short-circuit out of it. This seems to have been intended anyway based on this comment: https://github.com/llvm/llvm-project/blob/cecdff92838f3c049548e3445a15d8c9c7a49205/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp#L200-L204 But something must have broken that during a refactor. I'm not 100% sure how best to test this because we need a scenario where moving a type into the scratch AST fails, but the expression itself succeeded. rdar://159647906
2025-09-16[clang] Initialize the file system explicitly (#158381)Jan Svoboda
This PR is a part of the effort to make the VFS used in the compiler more explicit and consistent. Instead of creating the VFS deep within the compiler (in `CompilerInstance::createFileManager()`), clients are now required to explicitly call `CompilerInstance::createVirtualFileSystem()` and provide the base VFS from the outside. This PR also helps in breaking up the dependency cycle where creating a properly configured `DiagnosticsEngine` requires a properly configured VFS, but creating properly configuring a VFS requires the `DiagnosticsEngine`. Both `CompilerInstance::create{FileManager,Diagnostics}()` now just use the VFS already in `CompilerInstance` instead of taking one as a parameter, making the VFS consistent across the instance sub-object.
2025-09-03[lldb][ExpressionParser][NFC] Clean up expression language picking logic ↵Michael Buch
(#156642) This patch moves the `frame_lang` logic to just the logging (because that's what it was always used for anyway). The callsites decide whether to fall back on to the frame language or not when running the expression.
2025-08-04[lldb][darwin] force BuiltinHeadersInSystemModules to be always false (#151535)Charles Zablit
This is a re-landing of https://github.com/llvm/llvm-project/pull/144913, which was reverted because of tests failing on Darwin. The bots were failing because the x64 bots run the macOS 14.0 SDKs whereas we assumed all the bots were running the macOS 15.0+ SDKs. To address this, we deactivated the failing tests on SDKs that are older than 15.0 in https://github.com/llvm/llvm-project/pull/147768. Now that this is merged, we can re-land https://github.com/llvm/llvm-project/pull/144913.
2025-08-01NFC: Clean up of IntrusiveRefCntPtr construction from raw pointers. (#151782)James Y Knight
This commit handles the following types: - clang::ExternalASTSource - clang::TargetInfo - clang::ASTContext - clang::SourceManager - clang::FileManager Part of cleanup #151026
2025-07-31NFC: Clean up construction of IntrusiveRefCntPtr from raw pointers for ↵James Y Knight
llvm::vfs::FileSystem. (#151407) This switches to `makeIntrusiveRefCnt<FileSystem>` where creating a new object, and to passing/returning by `IntrusiveRefCntPtr<FileSystem>` instead of `FileSystem*` or `FileSystem&`, when dealing with existing objects. Part of cleanup #151026.
2025-07-09[lldb][Expression][NFC] Run clang-format on ClangExpressionParser.cpp and ↵Michael Buch
ClangModulesDeclVendor.cpp This wasn't addressed as part of https://github.com/llvm/llvm-project/pull/147727
2025-07-09[LLDB] Fix build after TargetInfo::adjust signature change (#147727)Yaxun (Sam) Liu
Fixes buildbot failure on lldb-x86_64-debian due to https://github.com/llvm/llvm-project/pull/146620 https://lab.llvm.org/buildbot/#/builders/162/builds/26414 Update LLDB calls to TargetInfo::adjust() to use the new 3-parameter signature introduced in beea2a941470368a87b1816e455b1db366c1bbb9. Pass nullptr for AuxTarget since LLDB doesn't use auxiliary targets in these contexts.
2025-06-26[lldb][Darwin] revert change to lang_opts.BuiltinHeadersInSystemModules ↵Charles Zablit
(#145864) Revert the changes made in the following PRs as they are causing bot failures: - https://github.com/llvm/llvm-project/pull/145744 - https://github.com/llvm/llvm-project/pull/144913
2025-06-25[lldb][darwin] force BuiltinHeadersInSystemModules to be always false (#144913)Charles Zablit
`SDKSupportsBuiltinModules` always returns true on newer versions of Darwin based OS. The only way for this call to return `false` would be to have a version mismatch between lldb and the SDK (recent lldb manually installed on macOS 14 for instance). This patch removes this check and hardcodes the value of `BuiltinHeadersInSystemModules` to `false`.
2025-05-29[LLDB] [NFC] - Remove duplicate #include headers from the files of lldb dir ↵Akash Agrawal
& few other files (#141478) A few files of lldb dir & few other files had duplicate headers included. This patch removes those redundancies. --------- Co-authored-by: Akash Agrawal <akashag@qti.qualcomm.com>
2025-05-22Reapply "[clang] Remove intrusive reference count from `DiagnosticOptions` ↵Jan Svoboda
(#139584)" This reverts commit e2a885537f11f8d9ced1c80c2c90069ab5adeb1d. Build failures were fixed right away and reverting the original commit without the fixes breaks the build again.
2025-05-22Revert "[clang] Remove intrusive reference count from `DiagnosticOptions` ↵Kazu Hirata
(#139584)" This reverts commit 9e306ad4600c4d3392c194a8be88919ee758425c. Multiple builtbot failures have been reported: https://github.com/llvm/llvm-project/pull/139584
2025-05-22[clang] Remove intrusive reference count from `DiagnosticOptions` (#139584)Jan Svoboda
The `DiagnosticOptions` class is currently intrusively reference-counted, which makes reasoning about its lifetime very difficult in some cases. For example, `CompilerInvocation` owns the `DiagnosticOptions` instance (wrapped in `llvm::IntrusiveRefCntPtr`) and only exposes an accessor returning `DiagnosticOptions &`. One would think this gives `CompilerInvocation` exclusive ownership of the object, but that's not the case: ```c++ void shareOwnership(CompilerInvocation &CI) { llvm::IntrusiveRefCntPtr<DiagnosticOptions> CoOwner = &CI.getDiagnosticOptions(); // ... } ``` This is a perfectly valid pattern that is being actually used in the codebase. I would like to ensure the ownership of `DiagnosticOptions` by `CompilerInvocation` is guaranteed to be exclusive. This can be leveraged for a copy-on-write optimization later on. This PR changes usages of `DiagnosticOptions` across `clang`, `clang-tools-extra` and `lldb` to not be intrusively reference-counted.
2025-04-28[clang] Hide the `TargetOptions` pointer from `CompilerInvocation` (#106271)Jan Svoboda
This PR hides the reference-counted pointer that holds `TargetOptions` from the public API of `CompilerInvocation`. This gives `CompilerInvocation` an exclusive control over the lifetime of this member, which will eventually be leveraged to implement a copy-on-write behavior. There are two clients that currently share ownership of that pointer: * `TargetInfo` - This was refactored to hold a non-owning reference to `TargetOptions`. The options object is typically owned by the `CompilerInvocation` or by the new `CompilerInstance::AuxTargetOpts` for the auxiliary target. This needed a bit of care in `ASTUnit::Parse()` to keep the `CompilerInvocation` alive. * `clangd::PreambleData` - This was refactored to exclusively own the `TargetOptions` that get moved out of the `CompilerInvocation`.
2025-04-17Reland [clang] Unify `SourceLocation` and `IdentifierInfo*` pair-like data ↵yronglin
structures to `IdentifierLoc` (#136077) This PR reland https://github.com/llvm/llvm-project/pull/135808, fixed some missed changes in LLDB. I found this issue when I working on https://github.com/llvm/llvm-project/pull/107168. Currently we have many similiar data structures like: - std::pair<IdentifierInfo *, SourceLocation>. - Element type of ModuleIdPath. - IdentifierLocPair. - IdentifierLoc. This PR unify these data structures to IdentifierLoc, moved IdentifierLoc definition to SourceLocation.h, and deleted other similer data structures. --------- Signed-off-by: yronglin <yronglin777@gmail.com>
2025-03-06[lldb][Expression] Allow specifying a preferred ModuleList for lookup during ↵Michael Buch
expression evaluation (#129733) The `TestMemoryHistory.py`/`TestReportData.py` are currently failing on the x86 macOS CI (started after we upgraded the Xcode SDK on that machien). The LLDB ASAN utility expression is failing to run with following error: ``` (lldb) image lookup -n __asan_get_alloc_stack 1 match found in /usr/lib/system/libsystem_sanitizers.dylib: Address: libsystem_sanitizers.dylib[0x00007ffd11e673f7] (libsystem_sanitizers.dylib.__TEXT.__text + 11287) Summary: libsystem_sanitizers.dylib`__asan_get_alloc_stack 1 match found in /Users/michaelbuch/Git/lldb-build-main-no-modules/lib/clang/21/lib/darwin/libclang_rt.asan_osx_dynamic.dylib: Address: libclang_rt.asan_osx_dynamic.dylib[0x0000000000009ec0] (libclang_rt.asan_osx_dynamic.dylib.__TEXT.__text + 34352) Summary: libclang_rt.asan_osx_dynamic.dylib`::__asan_get_alloc_stack(__sanitizer::uptr, __sanitizer::uptr *, __sanitizer::uptr, __sanitizer::u32 *) at asan_debugging.cpp:132 (lldb) memory history 'pointer' Assertion failed: ((uintptr_t)addr == report.access.address), function __asan_get_alloc_stack, file debugger_abi.cpp, line 62. warning: cannot evaluate AddressSanitizer expression: error: Expression execution was interrupted: signal SIGABRT. The process has been returned to the state before expression evaluation. ``` The reason for this is that the system sanitizer dylib and the locally built libclang_rt contain the same symbol `__asan_get_alloc_stack`, and depending on the order in which they're loaded, we may pick the one from the wrong dylib (this probably changed during the buildbot upgrade and is why it only now started failing). Based on discussion with @wrotki we always want to pick the one that's in the libclang_rt dylib if it was loaded, and libsystem_sanitizers otherwise. This patch addresses this by adding a "preferred lookup context list" to the expression evaluator. Currently this is only exposed in the `EvaluateExpressionOptions`. We make it a `SymbolContextList` in case we want the lookup contexts to be contexts other than modules (e.g., source files, etc.). In `IRExecutionUnit` we make it a `ModuleList` because it makes the symbol lookup implementation simpler and we only do module lookups here anyway. If we ever need it to be a `SymbolContext`, that transformation shouldn't be too difficult.
2024-11-21Reapply "[NFC] Explicitly pass a VFS when creating DiagnosticsEngine (#115852)"Kadir Cetinkaya
This reverts commit a1153cd6fedd4c906a9840987934ca4712e34cb2 with fixes to lldb breakages. Fixes https://github.com/llvm/llvm-project/issues/117145.
2024-11-08[lldb][LoongArch] Function calls support in lldb expressionswanglei
This patch adds desired feature flags in JIT compiler to enable hard-float instructions if target supports them and allows to use floats and doubles in lldb expressions. Fited tests: lldb-shell :: Expr/TestAnonNamespaceParamFunc.cpp lldb-shell :: Expr/TestIRMemoryMap.test lldb-shell :: Expr/TestStringLiteralExpr.test lldb-shell :: SymbolFile/DWARF/debug-types-expressions.test Similar as #99336 Depens on: https://github.com/llvm/llvm-project/pull/114741 Reviewed By: SixWeining Pull Request: https://github.com/llvm/llvm-project/pull/114742
2024-10-02[lldb][RISCV] function calls support in lldb expressions (#99336)dlav-sc
[lldb][RISCV] add jitted function calls to ABI Function calls support in LLDB expressions for RISCV: 1 of 4 Augments corresponding functionality to RISCV ABI, which allows to jit lldb expressions and thus make function calls inside them. Only function calls with integer and void function arguments and return value are supported. [lldb][RISCV] add JIT relocations resolver Function calls support in LLDB expressions for RISCV: 2 of 4 Adds required RISCV relocations resolving functionality in lldb ExecutionEngine. [lldb][RISCV] RISC-V large code model in lldb expressions Function calls support in LLDB expressions for RISCV: 3 of 4 This patch sets large code model in MCJIT settings for RISC-V 64-bit targets that allows to make assembly jumps at any 64bit address. This is needed, because resulted jitted code may contain more that +-2GB jumps, that are not available in RISC-V with medium code model. [lldb][RISCV] doubles support in lldb expressions Function calls support in LLDB expressions for RISCV: 4 of 4 This patch adds desired feature flags in MCJIT compiler to enable hard-float instructions if target supports them and allows to use floats and doubles in lldb expressions.
2024-09-27[lldb] Inline expression evaluator error visualization (#106470)Adrian Prantl
This patch is a reworking of Pete Lawrence's (@PortalPete) proposal for better expression evaluator error messages: https://github.com/llvm/llvm-project/pull/80938 Before: ``` $ lldb -o "expr a+b" (lldb) expr a+b error: <user expression 0>:1:1: use of undeclared identifier 'a' a+b ^ error: <user expression 0>:1:3: use of undeclared identifier 'b' a+b ^ ``` After: ``` (lldb) expr a+b ^ ^ │ ╰─ error: use of undeclared identifier 'b' ╰─ error: use of undeclared identifier 'a' ``` This eliminates the confusing `<user expression 0>:1:3` source location and avoids echoing the expression to the console again, which results in a cleaner presentation that makes it easier to grasp what's going on. You can't see it here, bug the word "error" is now also in color, if so desired. Depends on https://github.com/llvm/llvm-project/pull/106442.
2024-09-27Revert "[lldb] Inline expression evaluator error visualization (#106470)"Adrian Prantl
This reverts commit 49372d1cccf50f404d52d40ae4b663db5604eb2c.
2024-09-27[lldb] Inline expression evaluator error visualization (#106470)Adrian Prantl
This patch is a reworking of Pete Lawrence's (@PortalPete) proposal for better expression evaluator error messages: https://github.com/llvm/llvm-project/pull/80938 Before: ``` $ lldb -o "expr a+b" (lldb) expr a+b error: <user expression 0>:1:1: use of undeclared identifier 'a' a+b ^ error: <user expression 0>:1:3: use of undeclared identifier 'b' a+b ^ ``` After: ``` (lldb) expr a+b ^ ^ │ ╰─ error: use of undeclared identifier 'b' ╰─ error: use of undeclared identifier 'a' ``` This eliminates the confusing `<user expression 0>:1:3` source location and avoids echoing the expression to the console again, which results in a cleaner presentation that makes it easier to grasp what's going on. You can't see it here, bug the word "error" is now also in color, if so desired. Depends on https://github.com/llvm/llvm-project/pull/106442.
2024-09-27[lldb] Store expression evaluator diagnostics in an llvm::Error (NFC) (#106442)Adrian Prantl
…NFC] This patch is the first patch in a series reworking of Pete Lawrence's (@PortalPete) amazing proposal for better expression evaluator error messages (https://github.com/llvm/llvm-project/pull/80938) This patch is preparatory patch for improving the rendering of expression evaluator diagnostics. Currently diagnostics are rendered into a string and the command interpreter layer then textually parses words like "error:" to (sometimes) color the output accordingly. In order to enable user interfaces to do better with diagnostics, we need to store them in a machine-readable fromat. This patch does this by adding a new llvm::Error kind wrapping a DiagnosticDetail struct that is used when the error type is eErrorTypeExpression. Multiple diagnostics are modeled using llvm::ErrorList. Right now the extra information is not used by the CommandInterpreter, this will be added in a follow-up patch!
2024-09-26[lldb] Don't flush llvm::raw_string_ostream (NFC) (#110128)Youngsuk Kim
Don't call raw_string_ostream::flush(), which is essentially a no-op. As specified in the docs, raw_string_ostream is always unbuffered. ( 65b13610a5226b84889b923bae884ba395ad084d for further reference )
2024-09-16[lldb] Nits on uses of llvm::raw_string_ostream (NFC) (#108745)Youngsuk Kim
As specified in the docs, 1) raw_string_ostream is always unbuffered and 2) the underlying buffer may be used directly ( 65b13610a5226b84889b923bae884ba395ad084d for further reference ) * Don't call raw_string_ostream::flush(), which is essentially a no-op. * Avoid unneeded calls to raw_string_ostream::str(), to avoid excess indirection.
2024-08-28[lldb][ClangExpressionParser] Remove duplicate construction of ↵Michael Buch
ExternalASTSourceWrapper This is an oversight from https://github.com/llvm/llvm-project/pull/104817 where the intention was to hoist the ExternalASTSourceWrapper construction out of the conditional so it can be set on both the `SemaSourceWithPriorities` and be added as an external source to Sema. But the inner `ExternalASTSourceWrapper` allocation wasn't actually removed. This currently all works fine because all these AST sources are refcounted and point to the same underlying AST sources. But this patch cleans this up regardless.
2024-08-27[lldb] Turn lldb_private::Status into a value type. (#106163)Adrian Prantl
This patch removes all of the Set.* methods from Status. This cleanup is part of a series of patches that make it harder use the anti-pattern of keeping a long-lives Status object around and updating it while dropping any errors it contains on the floor. This patch is largely NFC, the more interesting next steps this enables is to: 1. remove Status.Clear() 2. assert that Status::operator=() never overwrites an error 3. remove Status::operator=() Note that step (2) will bring 90% of the benefits for users, and step (3) will dramatically clean up the error handling code in various places. In the end my goal is to convert all APIs that are of the form ` ResultTy DoFoo(Status& error) ` to ` llvm::Expected<ResultTy> DoFoo() ` How to read this patch? The interesting changes are in Status.h and Status.cpp, all other changes are mostly ` perl -pi -e 's/\.SetErrorString/ = Status::FromErrorString/g' $(git grep -l SetErrorString lldb/source) ` plus the occasional manual cleanup.
2024-08-20[lldb][ClangExpressionParser] Implement ↵Michael Buch
ExternalSemaSource::ReadUndefinedButUsed (#104817) While parsing an expression, Clang tries to diagnose usage of decls (with possibly non-external linkage) for which it hasn't been provided with a definition. This is the case, e.g., for functions with parameters that live in an anonymous namespace (those will have `UniqueExternal` linkage, this is computed [here in computeTypeLinkageInfo](https://github.com/llvm/llvm-project/blob/ea8bb4d633683f5cbfd82491620be3056f347a02/clang/lib/AST/Type.cpp#L4647-L4653)). Before diagnosing such situations, Clang calls `ExternalSemaSource::ReadUndefinedButUsed`. The intended use of this API is to extend the set of "used but not defined" decls with additional ones that the external source knows about. However, in LLDB's case, we never provide `FunctionDecl`s with a definition, and instead rely on the expression parser to resolve those symbols by linkage name. Thus, to avoid the Clang parser from erroring out in these situations, this patch implements `ReadUndefinedButUsed` which just removes the "undefined" non-external `FunctionDecl`s that Clang found. We also had to add an `ExternalSemaSource` to the `clang::Sema` instance LLDB creates. We previously didn't have any source on `Sema`. Because we add the `ExternalASTSourceWrapper` here, that means we'd also technically be adding the `ClangExpressionDeclMap` as an `ExternalASTSource` to `Sema`, which is fine because `Sema` will only be calling into the `ExternalSemaSource` APIs (though nothing currently strictly enforces this, which is a bit worrying). Note, the decision for whether to put a function into `UndefinedButUsed` is done in [Sema::MarkFunctionReferenced](https://github.com/llvm/llvm-project/blob/ea8bb4d633683f5cbfd82491620be3056f347a02/clang/lib/Sema/SemaExpr.cpp#L18083-L18087). The `UniqueExternal` linkage computation is done in [getLVForNamespaceScopeDecl](https://github.com/llvm/llvm-project/blob/ea8bb4d633683f5cbfd82491620be3056f347a02/clang/lib/AST/Decl.cpp#L821-L833). Fixes https://github.com/llvm/llvm-project/issues/104712
2024-08-20[lldb][ClangExpressionParser] Don't leak memory when multiplexing ↵Michael Buch
ExternalASTSources (#104799) When we use `SemaSourceWithPriorities` as the `ASTContext`s ExternalASTSource, we allocate a `ClangASTSourceProxy` (via `CreateProxy`) and two `ExternalASTSourceWrapper`. Then we push these sources into a vector in `SemaSourceWithPriorities`. The allocated `SemaSourceWithPriorities` itself will get properly deallocated because the `ASTContext` wraps it in an `IntrusiveRefCntPtr`. But the three sources we allocated earlier will never get released. This patch fixes this by mimicking what `MultiplexExternalSemaSource` does (which is what `SemaSourceWithPriorities` is based on anyway). I.e., when `SemaSourceWithPriorities` gets constructed, it increments the use count of its sources. And on destruction it decrements them. Similarly, to make sure we dealloacted the `ClangASTProxy` properly, the `ExternalASTSourceWrapper` now assumes shared ownership of the underlying source.
2024-08-09Reland "[lldb][ClangExpressionParser] Set BuiltinHeadersInSystemModules ↵Michael Buch
depending on SDK version (#102309)" (#102497) Depends on https://github.com/llvm/llvm-project/pull/102488 This reverts commit https://github.com/llvm/llvm-project/commit/cf56e265e4b74799aee72a04b634fcc261078084. The original change was reverted because it was causing linker failures in the unit-tests: ``` Undefined symbols for architecture arm64: "lldb_private::PlatformDarwin::GetSDKPathFromDebugInfo(lldb_private::Module&)", referenced from: lldb_private::ClangExpressionParser::ClangExpressionParser(lldb_private::ExecutionContextScope*, lldb_private::Expression&, bool, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>>, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>) in liblldbPluginExpressionParserClang.a[11](ClangExpressionParser.cpp.o) ld: symbol(s) not found for architecture arm64 c++: error: linker command failed with exit code 1 (use -v to see invocation) ``` The relanded version differs only in the fact that we now use the generic `Platform` abstraction to get to `GetSDKPathFromDebugInfo`.
2024-08-07Revert "[lldb][ClangExpressionParser] Set BuiltinHeadersInSystemModules ↵Jason Molenda
depending on SDK version (#102309)" This reverts commit 1cbcf74083e92021472ec9644b88418f377ce550. unittests do not build because liblldbPluginExpressionParserClang.a now depends on liblldbPluginPlatformMacOSX.a when built on macOS, reverting until we can straighten out the dependency.
2024-08-07[lldb][ClangExpressionParser] Set BuiltinHeadersInSystemModules depending on ↵Michael Buch
SDK version (#102309) This patch changes the way we initialize `BuiltinHeadersInSystemModules` which is one of the flags controlling Clang's behaviour when the Darwin module is split into more fine-grained modules. The ClangExpressionParser currently unconditionally sets `-fbuiltin-headers-in-system-modules` when evaluating expressions with the `target.import-std-module` setting. This flag should, however, be set depending on the SDK version (which is what the Clang Darwin toolchain does). Unfortunately, the compiler instance that we create with `ClangExpressionParser` never consults the Clang driver, and thus doesn't correctly infer `BuiltinHeadersInSystemModules`. Note, this isn't an issue with the `CompilerInstance` that the `ClangModulesDeclVendor` creates because it uses the `createInovcation` API, which calls into `Darwin::addClangTargetOptions`. This patch mimicks how `sdkSupportsBuiltinModules` is used in `Darwin::addClangTargetOptions`. This ensures that the `import-std-module` API tests run cleanly regardless of SDK version. The plan is to eventually make the `CompilerInstance` construction in `ClangExpressionParser` go through the driver, so we can avoid duplicating the logic in LLDB. But we aren't there yet. **Implementation** * We look for the `SDKSettings.json` in the sysroot directory that we found in DWARF (via `DW_AT_LLVM_sysroot`) * Then parse this file and extract the SDK version number out of it * Then mimick `sdkSupportsBuiltinModules` from `Toolchains/Darwin.cpp` and set `BuiltinHeadersInSystemModules` based on it rdar://116490281
2024-08-07[lldb][ClangExpressionParser][NFC] Remove unused local varsMichael Buch
These got removed in `d6cbcf93b227befaad00957a56acd63c837c26ff` but mistakenly added back when rebasing `12e3a06cb7615fbd91031420f3dec2a85d7877d6`
2024-08-02[lldb][ClangExpressionParser][NFC] Factor LangOptions logic out of ↵Michael Buch
ClangExpressionParser constructor (#101669) We plan to eventually use the Clang driver to initialize the `CompilerInstance`. This should make refactorings of this code more straightforward. **Changes**: * Introduced `SetupLangOpts` and `SetupImportStdModuleLangOpts` * Called them from `ClangExpressionParser::ClangExpressionParser`
2024-08-02[lldb][ClangExpressionParser] Log and assert on failure to create TargetInfo ↵Michael Buch
(#101697) `CreateTargetInfo` can return a `nullptr` in a couple cases. So we should log that and let the user know something is wrong (hence the `lldbassert`). I didn't actually run into this. Just stumbled upon it from reading the code.
2024-08-02[lldb][ClangExpressionParser][NFC] Factor out TargetOpts logic out of ↵Michael Buch
ClangExpressionParser (#101681) Same motivation as https://github.com/llvm/llvm-project/pull/101669. We plan to eventually use the Clang driver to initialize the `CompilerInstance`. This should make refactorings of this code more straightforward. **Changes**: * Introduced `SetupTargetOpts` * Called them from `ClangExpressionParser::ClangExpressionParser` * Made `GetClangTargetABI` a file-local function since it's not using any of the state in `ClangExpressionParser`, and isn't used anywhere outside the source file
2024-07-01Revert "[clang][AST] fix ast-print of extern <lang> with >=2 declarators"Aaron Ballman
This reverts commit 48f13d48a88c14acbaea7c3ee05018bb173fb360. It broke some external bots: https://green.lab.llvm.org/job/llvm.org/view/LLDB/job/as-lldb-cmake/6805/console https://logs.chromium.org/logs/fuchsia/buildbucket/cr-buildbucket/8743609724828014497/+/u/clang/build/stdout
2024-07-01[clang][AST] fix ast-print of extern <lang> with >=2 declaratorstemyurchenko
Fixes #93913
2024-06-22[lldb][ExpressionParser][NFCI] Add new DoPrepareForExecution interface to be ↵Michael Buch
implemented by language plugins (#96290) This patch adds a new `DoPrepareForExecution` API, which can be implemented by the Clang and Swift language plugins. This also moves `RunStaticInitializers` into `ExpressionParser::PrepareForExecution`, so we call it consistently between language plugins. This *should* be mostly NFC (the static initializers will still only run after we finished parsing). We've been living on this patch downstream for sometime now. rdar://130267058
2024-05-03[lldb] Create a single Severity enum in lldb-enumerations (#90917)Jonas Devlieghere
We have 3 different enums all expressing severity (info, warning, error). Remove all uses with a new Severity enum in lldb-enumerations.h.
2024-04-29Add a new SBExpressionOptions::SetLanguage() API (NFCI) (#89981)Adrian Prantl
that separates out language and version. To avoid reinventing the wheel and introducing subtle incompatibilities, this API uses the table of languages and versiond defined by the upcoming DWARF 6 standard (https://dwarfstd.org/languages-v6.html). While the DWARF 6 spec is not finialized, the list of languages is broadly considered stable. The primary motivation for this is to allow the Swift language plugin to switch between language dialects between, e.g., Swift 5.9 and 6.0 with out introducing a ton of new language codes. On the main branch this change is considered NFC. Depends on https://github.com/llvm/llvm-project/pull/89980
2024-04-11[lldb][ClangExpressionParser] Don't by default enable Objecitve-C support ↵Michael Buch
when evaluating C++ expressions (#87767) This patch attempts to decouple C++ expression evaluation from Objective-C support. We've previously enabled it by default (if a runtime existed), but that meant we're opting into extra work we only need to do for Objective-C, which complicates/slows down C++ expression evaluation. Of course there's a valid use-case for this, which is calling Objective-C APIs when stopped in C++ frames (which Objective-C++ developers might want to do). In those cases we should really prompt the user to add the `expr --language objc++` flag. To accomodate a likely frequent use-case where a user breaks in a system C++ library (without debug-symbols) but their application is actually an Objective-C app, we allow Objective-C support in C++ expressions if the current frame doesn't have debug-info. This fixes https://github.com/llvm/llvm-project/issues/75443 and allows us to add more `LangOpts.ObjC` guards around the expression evaluator in the future (e.g., we could avoid looking into the Objective-C runtime during C++ expression evaluation, which we currently do unconditionally). Depends on https://github.com/llvm/llvm-project/pull/87657