[libclang] Extend clang_Cursor_Evaluate().

Let this function (try to) evaluate expressions, in addition to
declarations and compound statements.

Patch by Christian Kandeler <christian.kandeler@qt.io>

Reviewers: nik, akyrtzi, arphaman, jkorous

Reviewed By: jkorous

Differential Revision: https://reviews.llvm.org/D80279
This commit is contained in:
Christian Kandeler 2020-06-24 11:56:45 +01:00 committed by Florian Hahn
parent 05e10ee0ae
commit 72131423cc
3 changed files with 21 additions and 4 deletions

View File

@ -5934,6 +5934,7 @@ typedef void *CXEvalResult;
* If cursor is a statement declaration tries to evaluate the * If cursor is a statement declaration tries to evaluate the
* statement and if its variable, tries to evaluate its initializer, * statement and if its variable, tries to evaluate its initializer,
* into its corresponding type. * into its corresponding type.
* If it's an expression, tries to evaluate the expression.
*/ */
CINDEX_LINKAGE CXEvalResult clang_Cursor_Evaluate(CXCursor C); CINDEX_LINKAGE CXEvalResult clang_Cursor_Evaluate(CXCursor C);

View File

@ -26,6 +26,9 @@ template <typename d> class e {
static const auto g = alignof(f); static const auto g = alignof(f);
}; };
constexpr static int calc_val() { return 1 + 2; }
const auto the_value = calc_val() + sizeof(char);
// RUN: c-index-test -evaluate-cursor-at=%s:4:7 \ // RUN: c-index-test -evaluate-cursor-at=%s:4:7 \
// RUN: -evaluate-cursor-at=%s:8:7 \ // RUN: -evaluate-cursor-at=%s:8:7 \
// RUN: -evaluate-cursor-at=%s:8:11 -std=c++11 %s | FileCheck %s // RUN: -evaluate-cursor-at=%s:8:11 -std=c++11 %s | FileCheck %s
@ -53,3 +56,12 @@ template <typename d> class e {
// RUN: -evaluate-cursor-at=%s:26:21 \ // RUN: -evaluate-cursor-at=%s:26:21 \
// RUN: -std=c++11 %s | FileCheck -check-prefix=CHECK-DOES-NOT-CRASH %s // RUN: -std=c++11 %s | FileCheck -check-prefix=CHECK-DOES-NOT-CRASH %s
// CHECK-DOES-NOT-CRASH: Not Evaluatable // CHECK-DOES-NOT-CRASH: Not Evaluatable
// RUN: c-index-test -evaluate-cursor-at=%s:30:1 \
// RUN: -evaluate-cursor-at=%s:30:32 \
// RUN: -evaluate-cursor-at=%s:30:35 \
// RUN: -evaluate-cursor-at=%s:30:37 -std=c++11 %s | FileCheck %s -check-prefix=CHECK-EXPR
// CHECK-EXPR: unsigned, Value: 4
// CHECK-EXPR: Value: 3
// CHECK-EXPR: unsigned, Value: 4
// CHECK-EXPR: unsigned, Value: 1

View File

@ -4056,10 +4056,14 @@ static const Expr *evaluateCompoundStmtExpr(const CompoundStmt *CS) {
} }
CXEvalResult clang_Cursor_Evaluate(CXCursor C) { CXEvalResult clang_Cursor_Evaluate(CXCursor C) {
if (const Expr *E = const Expr *E = nullptr;
clang_getCursorKind(C) == CXCursor_CompoundStmt if (clang_getCursorKind(C) == CXCursor_CompoundStmt)
? evaluateCompoundStmtExpr(cast<CompoundStmt>(getCursorStmt(C))) E = evaluateCompoundStmtExpr(cast<CompoundStmt>(getCursorStmt(C)));
: evaluateDeclExpr(getCursorDecl(C))) else if (clang_isDeclaration(C.kind))
E = evaluateDeclExpr(getCursorDecl(C));
else if (clang_isExpression(C.kind))
E = getCursorExpr(C);
if (E)
return const_cast<CXEvalResult>( return const_cast<CXEvalResult>(
reinterpret_cast<const void *>(evaluateExpr(const_cast<Expr *>(E), C))); reinterpret_cast<const void *>(evaluateExpr(const_cast<Expr *>(E), C)));
return nullptr; return nullptr;