[clang] 7213142 - [libclang] Extend clang_Cursor_Evaluate().
Florian Hahn via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 24 04:03:48 PDT 2020
Author: Christian Kandeler
Date: 2020-06-24T11:58:39+01:00
New Revision: 72131423cc952ccbd6d8e021ff7c04fa22297fe3
URL: https://github.com/llvm/llvm-project/commit/72131423cc952ccbd6d8e021ff7c04fa22297fe3
DIFF: https://github.com/llvm/llvm-project/commit/72131423cc952ccbd6d8e021ff7c04fa22297fe3.diff
LOG: [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 at qt.io>
Reviewers: nik, akyrtzi, arphaman, jkorous
Reviewed By: jkorous
Differential Revision: https://reviews.llvm.org/D80279
Added:
Modified:
clang/include/clang-c/Index.h
clang/test/Index/evaluate-cursor.cpp
clang/tools/libclang/CIndex.cpp
Removed:
################################################################################
diff --git a/clang/include/clang-c/Index.h b/clang/include/clang-c/Index.h
index 19471af4a675..cfb8e58bfb7e 100644
--- a/clang/include/clang-c/Index.h
+++ b/clang/include/clang-c/Index.h
@@ -5934,6 +5934,7 @@ typedef void *CXEvalResult;
* 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
index af396318aab7..2bb687a1eb88 100644
--- a/clang/test/Index/evaluate-cursor.cpp
+++ b/clang/test/Index/evaluate-cursor.cpp
@@ -26,6 +26,9 @@ template <typename d> class e {
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 @@ template <typename d> class e {
// 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
index 4a65624268d8..8d33deb468e2 100644
--- a/clang/tools/libclang/CIndex.cpp
+++ b/clang/tools/libclang/CIndex.cpp
@@ -4056,10 +4056,14 @@ static const Expr *evaluateCompoundStmtExpr(const CompoundStmt *CS) {
}
CXEvalResult clang_Cursor_Evaluate(CXCursor C) {
- if (const Expr *E =
- clang_getCursorKind(C) == CXCursor_CompoundStmt
- ? evaluateCompoundStmtExpr(cast<CompoundStmt>(getCursorStmt(C)))
- : evaluateDeclExpr(getCursorDecl(C)))
+ const Expr *E = nullptr;
+ if (clang_getCursorKind(C) == CXCursor_CompoundStmt)
+ E = evaluateCompoundStmtExpr(cast<CompoundStmt>(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<CXEvalResult>(
reinterpret_cast<const void *>(evaluateExpr(const_cast<Expr *>(E), C)));
return nullptr;
More information about the cfe-commits
mailing list