Skip to content
This repository has been archived by the owner on Mar 28, 2020. It is now read-only.

Commit

Permalink
[libclang] evalute compound statement cursors before trying to evaluate
Browse files Browse the repository at this point in the history
the cursor like a declaration

This change fixes a bug in libclang in which it tries to evaluate a statement
cursor as a declaration cursor, because that statement still has a pointer to
the declaration parent.

rdar://38888477

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


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@336590 91177308-0d34-0410-b5e6-96231b3b80d8
(cherry picked from commit 0b6fe14)
  • Loading branch information
hyp committed Jul 9, 2018
1 parent 7728262 commit f9c61eb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
26 changes: 13 additions & 13 deletions tools/libclang/CIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3879,6 +3879,19 @@ static const ExprEvalResult* evaluateExpr(Expr *expr, CXCursor C) {
}

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;
Expand All @@ -3892,19 +3905,6 @@ CXEvalResult clang_Cursor_Evaluate(CXCursor C) {
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;
}

Expand Down
41 changes: 41 additions & 0 deletions unittests/libclang/LibclangTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,47 @@ 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() {
Expand Down

0 comments on commit f9c61eb

Please sign in to comment.