This revision was automatically updated to reflect the committed changes. sammccall marked an inline comment as done. Closed by commit rL370191: [clangd] Fix SelectionTree to allow selection range expression in foreach loops. (authored by sammccall, committed by ). Herald added a project: LLVM. Herald added a subscriber: llvm-commits.
Changed prior to commit: https://reviews.llvm.org/D66869?vs=217598&id=217609#toc Repository: rL LLVM CHANGES SINCE LAST ACTION https://reviews.llvm.org/D66869/new/ https://reviews.llvm.org/D66869 Files: clang-tools-extra/trunk/clangd/Selection.cpp clang-tools-extra/trunk/clangd/unittests/SelectionTests.cpp Index: clang-tools-extra/trunk/clangd/Selection.cpp =================================================================== --- clang-tools-extra/trunk/clangd/Selection.cpp +++ clang-tools-extra/trunk/clangd/Selection.cpp @@ -228,6 +228,16 @@ 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>; Index: clang-tools-extra/trunk/clangd/unittests/SelectionTests.cpp =================================================================== --- clang-tools-extra/trunk/clangd/unittests/SelectionTests.cpp +++ clang-tools-extra/trunk/clangd/unittests/SelectionTests.cpp @@ -261,6 +261,22 @@ 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);
Index: clang-tools-extra/trunk/clangd/Selection.cpp =================================================================== --- clang-tools-extra/trunk/clangd/Selection.cpp +++ clang-tools-extra/trunk/clangd/Selection.cpp @@ -228,6 +228,16 @@ 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>; Index: clang-tools-extra/trunk/clangd/unittests/SelectionTests.cpp =================================================================== --- clang-tools-extra/trunk/clangd/unittests/SelectionTests.cpp +++ clang-tools-extra/trunk/clangd/unittests/SelectionTests.cpp @@ -261,6 +261,22 @@ 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);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits