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

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 27 12:22:00 PST 2022


This revision was automatically updated to reflect the committed changes.
Closed by commit rG4cb1686bfe8e: [clangd] Fix a selection tree crash for unmatched-bracket code. (authored by hokein).

Changed prior to commit:
  https://reviews.llvm.org/D118322?vs=403516&id=403753#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118322/new/

https://reviews.llvm.org/D118322

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


Index: clang-tools-extra/clangd/unittests/SelectionTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/SelectionTests.cpp
+++ clang-tools-extra/clangd/unittests/SelectionTests.cpp
@@ -523,6 +523,10 @@
           auto l = [^[[foo = bar]]] { };
         })cpp",
        "VarDecl"},
+      {R"cpp(
+        /*error-ok*/
+        void func() [[{^]])cpp",
+       "CompoundStmt"},
   };
 
   for (const Case &C : Cases) {
Index: clang-tools-extra/clangd/Selection.cpp
===================================================================
--- clang-tools-extra/clangd/Selection.cpp
+++ clang-tools-extra/clangd/Selection.cpp
@@ -306,6 +306,14 @@
       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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118322.403753.patch
Type: text/x-patch
Size: 1317 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220127/c2e0ff2a/attachment.bin>


More information about the cfe-commits mailing list