From: Evgeniy Stepanov Date: Tue, 10 Jul 2018 19:48:53 +0000 (+0000) Subject: Revert r336591 "[libclang] NFC, simplify clang_Cursor_Evaluate" X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d33a97ceed4c92d978cf4a760b79aa9f6745fb94;p=clang Revert r336591 "[libclang] NFC, simplify clang_Cursor_Evaluate" This change is blocking r336590 which is being reverted due to memory leaks. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@336715 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index ab606ec92f..a4698830d9 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -3889,32 +3889,33 @@ static const ExprEvalResult* evaluateExpr(Expr *expr, CXCursor C) { return nullptr; } -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; +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))); } - 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))); + 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))); + return nullptr; + } return nullptr; }