[PATCH] D158923: [clang-c] Visit ranged for initializer

Kai Stierand via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 26 02:47:56 PDT 2023


kiloalphaindia created this revision.
kiloalphaindia added reviewers: HAPPY, aaron.ballman.
kiloalphaindia added a project: clang-c.
Herald added a subscriber: arphaman.
Herald added a project: All.
kiloalphaindia requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Visit the ranged for init statement when using C-Interface.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158923

Files:
  clang/tools/libclang/CIndex.cpp
  clang/unittests/libclang/LibclangTest.cpp


Index: clang/unittests/libclang/LibclangTest.cpp
===================================================================
--- clang/unittests/libclang/LibclangTest.cpp
+++ clang/unittests/libclang/LibclangTest.cpp
@@ -1138,6 +1138,33 @@
             "class ns1::Class1");
 }
 
+TEST_F(LibclangParseTest, VisitCXXForRangeStmt_InitStmt) {
+  const char testSource[] = R"cpp(
+void fun()
+{
+    char text[] = "test";
+    for (int init = 0; auto x:text) {}
+}
+)cpp";
+  std::string fileName = "main.cpp";
+  WriteFile(fileName, testSource);
+  const char *Args[] = {"-xc++", "-std=c++20"};
+  ClangTU = clang_parseTranslationUnit(Index, fileName.c_str(), Args,
+                                       std::size(Args), nullptr, 0, TUFlags);
+
+  bool found = false;
+  ;
+  Traverse([&](CXCursor cursor, CXCursor parent) -> CXChildVisitResult {
+    if (cursor.kind == CXCursor_VarDecl &&
+        fromCXString(clang_getCursorSpelling(cursor)) == "init") {
+      found = true;
+      return CXChildVisit_Break;
+    }
+    return CXChildVisit_Recurse;
+  });
+  ASSERT_TRUE(found);
+}
+
 TEST_F(LibclangParseTest, BinaryOperator) {
   std::string Main = "main.cpp";
   WriteFile(Main, "int foo() { return 5 + 9; }");
Index: clang/tools/libclang/CIndex.cpp
===================================================================
--- clang/tools/libclang/CIndex.cpp
+++ clang/tools/libclang/CIndex.cpp
@@ -2831,6 +2831,9 @@
   AddStmt(S->getBody());
   AddStmt(S->getRangeInit());
   AddDecl(S->getLoopVariable());
+  if (auto *init = S->getInit()) {
+    AddStmt(init);
+  }
 }
 
 void EnqueueVisitor::VisitDeclRefExpr(const DeclRefExpr *DR) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158923.553712.patch
Type: text/x-patch
Size: 1633 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230826/26890aa7/attachment-0001.bin>


More information about the cfe-commits mailing list