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

Commit 7a9552d

Browse files
committed
[libclang] NFC, simplify clang_Cursor_Evaluate
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
1 parent 0b6fe14 commit 7a9552d

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

tools/libclang/CIndex.cpp

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3889,36 +3889,35 @@ static const ExprEvalResult* evaluateExpr(Expr *expr, CXCursor C) {
38893889
return nullptr;
38903890
}
38913891

3892-
CXEvalResult clang_Cursor_Evaluate(CXCursor C) {
3893-
if (clang_getCursorKind(C) == CXCursor_CompoundStmt) {
3894-
const CompoundStmt *compoundStmt = cast<CompoundStmt>(getCursorStmt(C));
3895-
Expr *expr = nullptr;
3896-
for (auto *bodyIterator : compoundStmt->body()) {
3897-
if ((expr = dyn_cast<Expr>(bodyIterator))) {
3898-
break;
3899-
}
3900-
}
3901-
if (expr)
3902-
return const_cast<CXEvalResult>(
3903-
reinterpret_cast<const void *>(evaluateExpr(expr, C)));
3904-
}
3905-
3906-
const Decl *D = getCursorDecl(C);
3907-
if (D) {
3908-
const Expr *expr = nullptr;
3909-
if (auto *Var = dyn_cast<VarDecl>(D)) {
3910-
expr = Var->getInit();
3911-
} else if (auto *Field = dyn_cast<FieldDecl>(D)) {
3912-
expr = Field->getInClassInitializer();
3913-
}
3914-
if (expr)
3915-
return const_cast<CXEvalResult>(reinterpret_cast<const void *>(
3916-
evaluateExpr(const_cast<Expr *>(expr), C)));
3892+
static const Expr *evaluateDeclExpr(const Decl *D) {
3893+
if (!D)
39173894
return nullptr;
3895+
if (auto *Var = dyn_cast<VarDecl>(D))
3896+
return Var->getInit();
3897+
else if (auto *Field = dyn_cast<FieldDecl>(D))
3898+
return Field->getInClassInitializer();
3899+
return nullptr;
3900+
}
3901+
3902+
static const Expr *evaluateCompoundStmtExpr(const CompoundStmt *CS) {
3903+
assert(CS && "invalid compound statement");
3904+
for (auto *bodyIterator : CS->body()) {
3905+
if (const auto *E = dyn_cast<Expr>(bodyIterator))
3906+
return E;
39183907
}
39193908
return nullptr;
39203909
}
39213910

3911+
CXEvalResult clang_Cursor_Evaluate(CXCursor C) {
3912+
if (const Expr *E =
3913+
clang_getCursorKind(C) == CXCursor_CompoundStmt
3914+
? evaluateCompoundStmtExpr(cast<CompoundStmt>(getCursorStmt(C)))
3915+
: evaluateDeclExpr(getCursorDecl(C)))
3916+
return const_cast<CXEvalResult>(
3917+
reinterpret_cast<const void *>(evaluateExpr(const_cast<Expr *>(E), C)));
3918+
return nullptr;
3919+
}
3920+
39223921
unsigned clang_Cursor_hasAttrs(CXCursor C) {
39233922
const Decl *D = getCursorDecl(C);
39243923
if (!D) {

0 commit comments

Comments
 (0)