Author: Haojian Wu
Date: 2022-01-27T21:21:40+01:00
New Revision: 4cb1686bfe8ef847b583922bb5650c33453ad096

URL: 
https://github.com/llvm/llvm-project/commit/4cb1686bfe8ef847b583922bb5650c33453ad096
DIFF: 
https://github.com/llvm/llvm-project/commit/4cb1686bfe8ef847b583922bb5650c33453ad096.diff

LOG: [clangd] Fix a selection tree crash for unmatched-bracket code.

Fixes https://github.com/clangd/clangd/issues/999

Differential Revision: https://reviews.llvm.org/D118322

Added: 
    

Modified: 
    clang-tools-extra/clangd/Selection.cpp
    clang-tools-extra/clangd/unittests/SelectionTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/Selection.cpp 
b/clang-tools-extra/clangd/Selection.cpp
index 6dfaa8be95912..dbfe05c8e8b5c 100644
--- a/clang-tools-extra/clangd/Selection.cpp
+++ b/clang-tools-extra/clangd/Selection.cpp
@@ -306,6 +306,14 @@ class SelectionTester {
       return SelectionTree::Unselected;
     }
 
+    // The eof token is used as a sentinel.
+    // In general, source range from an AST node should not claim the eof 
token,
+    // but it could occur for unmatched-bracket cases.
+    // FIXME: fix it in TokenBuffer, expandedTokens(SourceRange) should not
+    // return the eof token.
+    if (ExpandedTokens.back().kind() == tok::eof)
+      ExpandedTokens = ExpandedTokens.drop_back();
+
     SelectionTree::Selection Result = NoTokens;
     while (!ExpandedTokens.empty()) {
       // Take consecutive tokens from the same context together for efficiency.

diff  --git a/clang-tools-extra/clangd/unittests/SelectionTests.cpp 
b/clang-tools-extra/clangd/unittests/SelectionTests.cpp
index 30e0ff0e41b48..73d6d0d238278 100644
--- a/clang-tools-extra/clangd/unittests/SelectionTests.cpp
+++ b/clang-tools-extra/clangd/unittests/SelectionTests.cpp
@@ -523,6 +523,10 @@ TEST(SelectionTest, CommonAncestor) {
           auto l = [^[[foo = bar]]] { };
         })cpp",
        "VarDecl"},
+      {R"cpp(
+        /*error-ok*/
+        void func() [[{^]])cpp",
+       "CompoundStmt"},
   };
 
   for (const Case &C : Cases) {


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to