[PATCH] D49051: [libclang] check that the cursor is declaration before trying to evaluate the cursor like a declaration

Alex Lorenz via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 9 12:46:32 PDT 2018


This revision was automatically updated to reflect the committed changes.
arphaman marked an inline comment as done.
Closed by commit rL336590: [libclang] evalute compound statement cursors before trying to evaluate (authored by arphaman, committed by ).
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D49051?vs=154479&id=154680#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D49051

Files:
  cfe/trunk/tools/libclang/CIndex.cpp
  cfe/trunk/unittests/libclang/LibclangTest.cpp


Index: cfe/trunk/unittests/libclang/LibclangTest.cpp
===================================================================
--- cfe/trunk/unittests/libclang/LibclangTest.cpp
+++ cfe/trunk/unittests/libclang/LibclangTest.cpp
@@ -461,6 +461,47 @@
   clang_disposeSourceRangeList(Ranges);
 }
 
+TEST_F(LibclangParseTest, EvaluateChildExpression) {
+  std::string Main = "main.m";
+  WriteFile(Main, "#define kFOO @\"foo\"\n"
+                  "void foobar(void) {\n"
+                  " {kFOO;}\n"
+                  "}\n");
+  ClangTU = clang_parseTranslationUnit(Index, Main.c_str(), nullptr, 0, nullptr,
+                                       0, TUFlags);
+
+  CXCursor C = clang_getTranslationUnitCursor(ClangTU);
+  clang_visitChildren(
+      C,
+      [](CXCursor cursor, CXCursor parent,
+         CXClientData client_data) -> CXChildVisitResult {
+        if (clang_getCursorKind(cursor) == CXCursor_FunctionDecl) {
+          int numberedStmt = 0;
+          clang_visitChildren(
+              cursor,
+              [](CXCursor cursor, CXCursor parent,
+                 CXClientData client_data) -> CXChildVisitResult {
+                int &numberedStmt = *((int *)client_data);
+                if (clang_getCursorKind(cursor) == CXCursor_CompoundStmt) {
+                  if (numberedStmt) {
+                    CXEvalResult RE = clang_Cursor_Evaluate(cursor);
+                    EXPECT_NE(RE, nullptr);
+                    EXPECT_EQ(clang_EvalResult_getKind(RE),
+                              CXEval_ObjCStrLiteral);
+                    return CXChildVisit_Break;
+                  }
+                  numberedStmt++;
+                }
+                return CXChildVisit_Recurse;
+              },
+              &numberedStmt);
+          EXPECT_EQ(numberedStmt, 1);
+        }
+        return CXChildVisit_Continue;
+      },
+      nullptr);
+}
+
 class LibclangReparseTest : public LibclangParseTest {
 public:
   void DisplayDiagnostics() {
Index: cfe/trunk/tools/libclang/CIndex.cpp
===================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp
+++ cfe/trunk/tools/libclang/CIndex.cpp
@@ -3890,6 +3890,19 @@
 }
 
 CXEvalResult clang_Cursor_Evaluate(CXCursor C) {
+  if (clang_getCursorKind(C) == CXCursor_CompoundStmt) {
+    const CompoundStmt *compoundStmt = cast<CompoundStmt>(getCursorStmt(C));
+    Expr *expr = nullptr;
+    for (auto *bodyIterator : compoundStmt->body()) {
+      if ((expr = dyn_cast<Expr>(bodyIterator))) {
+        break;
+      }
+    }
+    if (expr)
+      return const_cast<CXEvalResult>(
+          reinterpret_cast<const void *>(evaluateExpr(expr, C)));
+  }
+
   const Decl *D = getCursorDecl(C);
   if (D) {
     const Expr *expr = nullptr;
@@ -3903,19 +3916,6 @@
           evaluateExpr(const_cast<Expr *>(expr), C)));
     return nullptr;
   }
-
-  const CompoundStmt *compoundStmt = dyn_cast_or_null<CompoundStmt>(getCursorStmt(C));
-  if (compoundStmt) {
-    Expr *expr = nullptr;
-    for (auto *bodyIterator : compoundStmt->body()) {
-      if ((expr = dyn_cast<Expr>(bodyIterator))) {
-        break;
-      }
-    }
-    if (expr)
-      return const_cast<CXEvalResult>(
-          reinterpret_cast<const void *>(evaluateExpr(expr, C)));
-  }
   return nullptr;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D49051.154680.patch
Type: text/x-patch
Size: 3289 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180709/0c50974b/attachment.bin>


More information about the cfe-commits mailing list