diff --git a/clang/include/clang-c/Index.h b/clang/include/clang-c/Index.h --- a/clang/include/clang-c/Index.h +++ b/clang/include/clang-c/Index.h @@ -5934,6 +5934,7 @@ * If cursor is a statement declaration tries to evaluate the * statement and if its variable, tries to evaluate its initializer, * into its corresponding type. + * If it's an expression, tries to evaluate the expression. */ CINDEX_LINKAGE CXEvalResult clang_Cursor_Evaluate(CXCursor C); diff --git a/clang/test/Index/evaluate-cursor.cpp b/clang/test/Index/evaluate-cursor.cpp --- a/clang/test/Index/evaluate-cursor.cpp +++ b/clang/test/Index/evaluate-cursor.cpp @@ -26,6 +26,9 @@ 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: -evaluate-cursor-at=%s:8:7 \ // RUN: -evaluate-cursor-at=%s:8:11 -std=c++11 %s | FileCheck %s @@ -53,3 +56,12 @@ // RUN: -evaluate-cursor-at=%s:26:21 \ // RUN: -std=c++11 %s | FileCheck -check-prefix=CHECK-DOES-NOT-CRASH %s // 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 diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -4056,10 +4056,14 @@ } CXEvalResult clang_Cursor_Evaluate(CXCursor C) { - if (const Expr *E = - clang_getCursorKind(C) == CXCursor_CompoundStmt - ? evaluateCompoundStmtExpr(cast(getCursorStmt(C))) - : evaluateDeclExpr(getCursorDecl(C))) + const Expr *E = nullptr; + if (clang_getCursorKind(C) == CXCursor_CompoundStmt) + E = evaluateCompoundStmtExpr(cast(getCursorStmt(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( reinterpret_cast(evaluateExpr(const_cast(E), C))); return nullptr;