r336591 - [libclang] NFC, simplify clang_Cursor_Evaluate
Alex Lorenz via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 9 12:56:45 PDT 2018
Author: arphaman
Date: Mon Jul 9 12:56:45 2018
New Revision: 336591
URL: http://llvm.org/viewvc/llvm-project?rev=336591&view=rev
Log:
[libclang] NFC, simplify clang_Cursor_Evaluate
Take advantage of early returns as suggested by Duncan in
https://reviews.llvm.org/D49051
Modified:
cfe/trunk/tools/libclang/CIndex.cpp
Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=336591&r1=336590&r2=336591&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Mon Jul 9 12:56:45 2018
@@ -3889,36 +3889,35 @@ static const ExprEvalResult* evaluateExp
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) {
More information about the cfe-commits
mailing list