From a0e59b03e6915292261adb93346ad241e6f5d939 Mon Sep 17 00:00:00 2001 From: Alex Lorenz Date: Mon, 9 Jul 2018 19:56:45 +0000 Subject: [PATCH] [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 (cherry picked from commit 7a9552d9f1a0e003326fa99215e050e75556127b) --- tools/libclang/CIndex.cpp | 49 +++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index 361475dbb9a..a11717690c9 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -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(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; - if (auto *Var = dyn_cast(D)) { - expr = Var->getInit(); - } else if (auto *Field = dyn_cast(D)) { - expr = Field->getInClassInitializer(); - } - if (expr) - return const_cast(reinterpret_cast( - evaluateExpr(const_cast(expr), C))); +static const Expr *evaluateDeclExpr(const Decl *D) { + if (!D) return nullptr; + if (auto *Var = dyn_cast(D)) + return Var->getInit(); + else if (auto *Field = dyn_cast(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(bodyIterator)) + return E; } return nullptr; } +CXEvalResult clang_Cursor_Evaluate(CXCursor C) { + if (const Expr *E = + clang_getCursorKind(C) == CXCursor_CompoundStmt + ? evaluateCompoundStmtExpr(cast(getCursorStmt(C))) + : evaluateDeclExpr(getCursorDecl(C))) + return const_cast( + reinterpret_cast(evaluateExpr(const_cast(E), C))); + return nullptr; +} + unsigned clang_Cursor_hasAttrs(CXCursor C) { const Decl *D = getCursorDecl(C); if (!D) {