[clang-tools-extra] r370191 - [clangd] Fix SelectionTree to allow selection range expression in foreach loops.

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 28 05:05:12 PDT 2019


Author: sammccall
Date: Wed Aug 28 05:05:12 2019
New Revision: 370191

URL: http://llvm.org/viewvc/llvm-project?rev=370191&view=rev
Log:
[clangd] Fix SelectionTree to allow selection range expression in foreach loops.

Reviewers: hokein

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits

Tags: #clang

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

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

Modified: clang-tools-extra/trunk/clangd/Selection.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/Selection.cpp?rev=370191&r1=370190&r2=370191&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/Selection.cpp (original)
+++ clang-tools-extra/trunk/clangd/Selection.cpp Wed Aug 28 05:05:12 2019
@@ -228,6 +228,16 @@ public:
   bool TraverseNestedNameSpecifier(NestedNameSpecifier *) { return true; }
   bool TraverseType(QualType) { return true; }
 
+  // The DeclStmt for the loop variable claims to cover the whole range
+  // inside the parens, this causes the range-init expression to not be hit.
+  // Traverse the loop VarDecl instead, which has the right source range.
+  bool TraverseCXXForRangeStmt(CXXForRangeStmt *S) {
+    return traverseNode(S, [&] {
+      return TraverseStmt(S->getInit()) && TraverseDecl(S->getLoopVariable()) &&
+             TraverseStmt(S->getRangeInit()) && TraverseStmt(S->getBody());
+    });
+  }
+
 private:
   using Base = RecursiveASTVisitor<SelectionVisitor>;
 

Modified: clang-tools-extra/trunk/clangd/unittests/SelectionTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SelectionTests.cpp?rev=370191&r1=370190&r2=370191&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/unittests/SelectionTests.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/SelectionTests.cpp Wed Aug 28 05:05:12 2019
@@ -261,6 +261,22 @@ TEST(SelectionTest, CommonAncestor) {
              struct Foo<U<int>*> {};
           )cpp",
           "TemplateTemplateParmDecl"},
+
+      // Foreach has a weird AST, ensure we can select parts of the range init.
+      // This used to fail, because the DeclStmt for C claimed the whole range.
+      {
+          R"cpp(
+            struct Str {
+              const char *begin();
+              const char *end();
+            };
+            Str makeStr(const char*);
+            void loop() {
+              for (const char* C : [[mak^eStr("foo"^)]])
+                ;
+            }
+          )cpp",
+          "CallExpr"},
   };
   for (const Case &C : Cases) {
     Annotations Test(C.Code);




More information about the cfe-commits mailing list