Skip to content

Commit 7213142

Browse files
ckandelerfhahn
authored andcommitted
[libclang] Extend clang_Cursor_Evaluate().
Let this function (try to) evaluate expressions, in addition to declarations and compound statements. Patch by Christian Kandeler <[email protected]> Reviewers: nik, akyrtzi, arphaman, jkorous Reviewed By: jkorous Differential Revision: https://reviews.llvm.org/D80279
1 parent 05e10ee commit 7213142

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

clang/include/clang-c/Index.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5934,6 +5934,7 @@ typedef void *CXEvalResult;
59345934
* If cursor is a statement declaration tries to evaluate the
59355935
* statement and if its variable, tries to evaluate its initializer,
59365936
* into its corresponding type.
5937+
* If it's an expression, tries to evaluate the expression.
59375938
*/
59385939
CINDEX_LINKAGE CXEvalResult clang_Cursor_Evaluate(CXCursor C);
59395940

clang/test/Index/evaluate-cursor.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ template <typename d> class e {
2626
static const auto g = alignof(f);
2727
};
2828

29+
constexpr static int calc_val() { return 1 + 2; }
30+
const auto the_value = calc_val() + sizeof(char);
31+
2932
// RUN: c-index-test -evaluate-cursor-at=%s:4:7 \
3033
// RUN: -evaluate-cursor-at=%s:8:7 \
3134
// RUN: -evaluate-cursor-at=%s:8:11 -std=c++11 %s | FileCheck %s
@@ -53,3 +56,12 @@ template <typename d> class e {
5356
// RUN: -evaluate-cursor-at=%s:26:21 \
5457
// RUN: -std=c++11 %s | FileCheck -check-prefix=CHECK-DOES-NOT-CRASH %s
5558
// CHECK-DOES-NOT-CRASH: Not Evaluatable
59+
60+
// RUN: c-index-test -evaluate-cursor-at=%s:30:1 \
61+
// RUN: -evaluate-cursor-at=%s:30:32 \
62+
// RUN: -evaluate-cursor-at=%s:30:35 \
63+
// RUN: -evaluate-cursor-at=%s:30:37 -std=c++11 %s | FileCheck %s -check-prefix=CHECK-EXPR
64+
// CHECK-EXPR: unsigned, Value: 4
65+
// CHECK-EXPR: Value: 3
66+
// CHECK-EXPR: unsigned, Value: 4
67+
// CHECK-EXPR: unsigned, Value: 1

clang/tools/libclang/CIndex.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4056,10 +4056,14 @@ static const Expr *evaluateCompoundStmtExpr(const CompoundStmt *CS) {
40564056
}
40574057

40584058
CXEvalResult clang_Cursor_Evaluate(CXCursor C) {
4059-
if (const Expr *E =
4060-
clang_getCursorKind(C) == CXCursor_CompoundStmt
4061-
? evaluateCompoundStmtExpr(cast<CompoundStmt>(getCursorStmt(C)))
4062-
: evaluateDeclExpr(getCursorDecl(C)))
4059+
const Expr *E = nullptr;
4060+
if (clang_getCursorKind(C) == CXCursor_CompoundStmt)
4061+
E = evaluateCompoundStmtExpr(cast<CompoundStmt>(getCursorStmt(C)));
4062+
else if (clang_isDeclaration(C.kind))
4063+
E = evaluateDeclExpr(getCursorDecl(C));
4064+
else if (clang_isExpression(C.kind))
4065+
E = getCursorExpr(C);
4066+
if (E)
40634067
return const_cast<CXEvalResult>(
40644068
reinterpret_cast<const void *>(evaluateExpr(const_cast<Expr *>(E), C)));
40654069
return nullptr;

0 commit comments

Comments
 (0)