summaryrefslogtreecommitdiffstats
path: root/clang/tools/libclang/CIndex.cpp
diff options
context:
space:
mode:
authorAlex Lorenz <arphaman@gmail.com>2019-01-08 22:32:51 +0000
committerAlex Lorenz <arphaman@gmail.com>2019-01-08 22:32:51 +0000
commit65317e1ca0700fc124612cb6659679b6d8e73601 (patch)
tree2391646812587a49135048cf61bba19bf937199f /clang/tools/libclang/CIndex.cpp
parentb6d674f6feca095a52891ac692858e1a251cb5c3 (diff)
downloadbcm5719-llvm-65317e1ca0700fc124612cb6659679b6d8e73601.tar.gz
bcm5719-llvm-65317e1ca0700fc124612cb6659679b6d8e73601.zip
[libclang] Recommit r336590 with a fix for the memory leak in the test
The original commit had a memory leak in the test has a leak as it doesn't dispose of the evaluated cursor result. This also contains the follow-up NFC refactoring commit r336591. rdar://45893054 Original commit message: [libclang] evalute compound statement cursors before trying to evaluate the cursor like a declaration This change fixes a bug in libclang in which it tries to evaluate a statement cursor as a declaration cursor, because that statement still has a pointer to the declaration parent. rdar://38888477 Differential Revision: https://reviews.llvm.org/D49051 llvm-svn: 350666
Diffstat (limited to 'clang/tools/libclang/CIndex.cpp')
-rw-r--r--clang/tools/libclang/CIndex.cpp47
1 files changed, 23 insertions, 24 deletions
diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp
index bc791954fc7..15b2df84935 100644
--- a/clang/tools/libclang/CIndex.cpp
+++ b/clang/tools/libclang/CIndex.cpp
@@ -3902,36 +3902,35 @@ static const ExprEvalResult* evaluateExpr(Expr *expr, CXCursor C) {
return nullptr;
}
-CXEvalResult clang_Cursor_Evaluate(CXCursor 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;
+}
- const CompoundStmt *compoundStmt = dyn_cast_or_null<CompoundStmt>(getCursorStmt(C));
- if (compoundStmt) {
- 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)));
+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) {
OpenPOWER on IntegriCloud