[Lldb-commits] [lldb] [lldb] Store expression evaluator diagnostics in an llvm::Error (NFC) (PR #106442)

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Thu Sep 19 07:33:13 PDT 2024


================
@@ -254,14 +255,46 @@ class ClangDiagnosticManagerAdapter : public clang::DiagnosticConsumer {
       std::string stripped_output =
           std::string(llvm::StringRef(m_output).trim());
 
-      auto new_diagnostic = std::make_unique<ClangDiagnostic>(
-          stripped_output, severity, Info.getID());
+      // Translate the source location.
+      if (Info.hasSourceManager()) {
+        DiagnosticDetail::SourceLocation loc;
+        clang::SourceManager &sm = Info.getSourceManager();
+        const clang::SourceLocation sloc = Info.getLocation();
+        if (sloc.isValid()) {
+          const clang::FullSourceLoc fsloc(sloc, sm);
+          clang::PresumedLoc PLoc = fsloc.getPresumedLoc(true);
+          StringRef filename =
+              PLoc.isValid() ? PLoc.getFilename() : StringRef{};
+          loc.file = FileSpec(filename);
+          loc.line = fsloc.getSpellingLineNumber();
+          loc.column = fsloc.getSpellingColumnNumber();
+          // A heuristic detecting the #line 1 "<user expression 1>".
+          loc.in_user_input = filename.starts_with("<user expression ");
----------------
labath wrote:

Here's an interesting test case:
```
(lldb) expr --top-level -- template<typename T> T FOO(T x) { return x/2; }
(lldb) expr -- FOO(4.0)
(double) $0 = 2
(lldb) expr -- FOO("")
error: <user expression 0>:1:43: invalid operands to binary expression ('const char *' and 'int')
    1 | template<typename T> T FOO(T x) { return x/2; }
      |                                          ~^~
<user expression 2>:1:1: in instantiation of function template specialization 'FOO<const char *>' requested here
    1 | FOO("")
      | ^
```
Which one of these diagnostics should have `in_user_input` set?

https://github.com/llvm/llvm-project/pull/106442


More information about the lldb-commits mailing list