[Lldb-commits] [lldb] 906d74b - [lldb] fix single-character token not underlined correctly in diagnostics (#197658)

via lldb-commits lldb-commits at lists.llvm.org
Fri May 15 06:24:18 PDT 2026


Author: Charles Zablit
Date: 2026-05-15T15:24:12+02:00
New Revision: 906d74b96421c86dd245566231b70394652ca72d

URL: https://github.com/llvm/llvm-project/commit/906d74b96421c86dd245566231b70394652ca72d
DIFF: https://github.com/llvm/llvm-project/commit/906d74b96421c86dd245566231b70394652ca72d.diff

LOG: [lldb] fix single-character token not underlined correctly in diagnostics (#197658)

Added: 
    

Modified: 
    lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
    lldb/unittests/Host/common/DiagnosticsRenderingTest.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
index ae02fda33fe66..861522623f68b 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
@@ -30,6 +30,7 @@
 #include "clang/Frontend/TextDiagnostic.h"
 #include "clang/Frontend/TextDiagnosticBuffer.h"
 #include "clang/Frontend/TextDiagnosticPrinter.h"
+#include "clang/Lex/Lexer.h"
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Parse/ParseAST.h"
 #include "clang/Rewrite/Core/Rewriter.h"
@@ -278,13 +279,19 @@ class ClangDiagnosticManagerAdapter : public clang::DiagnosticConsumer {
 
           // Find the range of the primary location.
           for (const auto &range : Info.getRanges()) {
-            if (range.getBegin() == sloc) {
-              // FIXME: This is probably not handling wide characters correctly.
-              unsigned end_col = sm.getSpellingColumnNumber(range.getEnd());
-              if (end_col > loc.column)
-                loc.length = end_col - loc.column;
+            if (range.getBegin() != sloc)
+              continue;
+            SourceLocation end = range.getEnd();
+            if (range.isTokenRange())
+              end = clang::Lexer::getLocForEndOfToken(end, 0, sm, m_lang_opts);
+            // FIXME: This is probably not handling wide characters correctly.
+            unsigned end_col = sm.getSpellingColumnNumber(end);
+            // Ignore ranges that span multiple lines.
+            if (end_col != sm.getSpellingLineNumber(sloc))
               break;
-            }
+            if (end_col > loc.column)
+              loc.length = end_col - loc.column;
+            break;
           }
           detail.source_location = loc;
         }
@@ -307,6 +314,7 @@ class ClangDiagnosticManagerAdapter : public clang::DiagnosticConsumer {
   }
 
   void BeginSourceFile(const LangOptions &LO, const Preprocessor *PP) override {
+    m_lang_opts = LO;
     m_passthrough->BeginSourceFile(LO, PP);
   }
 
@@ -315,6 +323,7 @@ class ClangDiagnosticManagerAdapter : public clang::DiagnosticConsumer {
 private:
   DiagnosticManager *m_manager = nullptr;
   DiagnosticOptions m_options;
+  LangOptions m_lang_opts;
   /// Output string filled by m_os.
   std::string m_output;
   /// Output stream of m_passthrough.

diff  --git a/lldb/unittests/Host/common/DiagnosticsRenderingTest.cpp b/lldb/unittests/Host/common/DiagnosticsRenderingTest.cpp
index 896ce1995fe1c..da2372837acb2 100644
--- a/lldb/unittests/Host/common/DiagnosticsRenderingTest.cpp
+++ b/lldb/unittests/Host/common/DiagnosticsRenderingTest.cpp
@@ -26,6 +26,18 @@ TEST_F(ErrorDisplayTest, RenderStatus) {
     ASSERT_TRUE(StringRef(result).contains("foo"));
   }
 
+  {
+    // Test that a single-character token (length=1) renders as just a caret
+    // with no underlines.
+    SourceLocation loc1 = {FileSpec{"a.c"}, 1, 6, 1, false, true};
+    std::string result =
+        Render({DiagnosticDetail{loc1, eSeverityError, "X", "X"}});
+    llvm::SmallVector<StringRef> lines;
+    StringRef(result).split(lines, '\n');
+    //                123456
+    ASSERT_EQ(lines[0], "     ^");
+    ASSERT_EQ(lines[1], "     error: X");
+  }
   {
     // Test that diagnostics on the same column can be handled and all
     // three errors are diagnosed.


        


More information about the lldb-commits mailing list