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

Commit

Permalink
[libclang] NFC, simplify clang_Cursor_Evaluate
Browse files Browse the repository at this point in the history
Take advantage of early returns as suggested by Duncan in
https://reviews.llvm.org/D49051


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@336591 91177308-0d34-0410-b5e6-96231b3b80d8
(cherry picked from commit 7a9552d)
  • Loading branch information
hyp committed Jul 9, 2018
1 parent f9c61eb commit a0e59b0
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions tools/libclang/CIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3878,36 +3878,35 @@ static const ExprEvalResult* evaluateExpr(Expr *expr, CXCursor C) {
return nullptr;
}

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;
if (auto *Var = dyn_cast<VarDecl>(D)) {
expr = Var->getInit();
} else if (auto *Field = dyn_cast<FieldDecl>(D)) {
expr = Field->getInClassInitializer();
}
if (expr)
return const_cast<CXEvalResult>(reinterpret_cast<const void *>(
evaluateExpr(const_cast<Expr *>(expr), C)));
static const Expr *evaluateDeclExpr(const Decl *D) {
if (!D)
return nullptr;
if (auto *Var = dyn_cast<VarDecl>(D))
return Var->getInit();
else if (auto *Field = dyn_cast<FieldDecl>(D))
return Field->getInClassInitializer();
return nullptr;
}

static const Expr *evaluateCompoundStmtExpr(const CompoundStmt *CS) {
assert(CS && "invalid compound statement");
for (auto *bodyIterator : CS->body()) {
if (const auto *E = dyn_cast<Expr>(bodyIterator))
return E;
}
return nullptr;
}

CXEvalResult clang_Cursor_Evaluate(CXCursor C) {
if (const Expr *E =
clang_getCursorKind(C) == CXCursor_CompoundStmt
? evaluateCompoundStmtExpr(cast<CompoundStmt>(getCursorStmt(C)))
: evaluateDeclExpr(getCursorDecl(C)))
return const_cast<CXEvalResult>(
reinterpret_cast<const void *>(evaluateExpr(const_cast<Expr *>(E), C)));
return nullptr;
}

unsigned clang_Cursor_hasAttrs(CXCursor C) {
const Decl *D = getCursorDecl(C);
if (!D) {
Expand Down

0 comments on commit a0e59b0

Please sign in to comment.