From: Evgeniy Stepanov Date: Tue, 10 Jul 2018 19:49:07 +0000 (+0000) Subject: Revert r336590 "[libclang] evalute compound statement cursors before trying to evaluate" X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8430288c2717cbee07d3d5f8398ac127d3227b73;p=clang Revert r336590 "[libclang] evalute compound statement cursors before trying to evaluate" New memory leaks in LibclangParseTest_EvaluateChildExpression_Test::TestBody() git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@336716 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index a4698830d9..b61dff3238 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -3890,19 +3890,6 @@ static const ExprEvalResult* evaluateExpr(Expr *expr, CXCursor C) { } CXEvalResult clang_Cursor_Evaluate(CXCursor C) { - if (clang_getCursorKind(C) == CXCursor_CompoundStmt) { - const CompoundStmt *compoundStmt = cast(getCursorStmt(C)); - Expr *expr = nullptr; - for (auto *bodyIterator : compoundStmt->body()) { - if ((expr = dyn_cast(bodyIterator))) { - break; - } - } - if (expr) - return const_cast( - reinterpret_cast(evaluateExpr(expr, C))); - } - const Decl *D = getCursorDecl(C); if (D) { const Expr *expr = nullptr; @@ -3916,6 +3903,19 @@ CXEvalResult clang_Cursor_Evaluate(CXCursor C) { evaluateExpr(const_cast(expr), C))); return nullptr; } + + const CompoundStmt *compoundStmt = dyn_cast_or_null(getCursorStmt(C)); + if (compoundStmt) { + Expr *expr = nullptr; + for (auto *bodyIterator : compoundStmt->body()) { + if ((expr = dyn_cast(bodyIterator))) { + break; + } + } + if (expr) + return const_cast( + reinterpret_cast(evaluateExpr(expr, C))); + } return nullptr; } diff --git a/unittests/libclang/LibclangTest.cpp b/unittests/libclang/LibclangTest.cpp index 9629b62475..6fddcb2cbf 100644 --- a/unittests/libclang/LibclangTest.cpp +++ b/unittests/libclang/LibclangTest.cpp @@ -461,47 +461,6 @@ TEST_F(LibclangParseTest, AllSkippedRanges) { 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() {