https://github.com/ugliansky created
https://github.com/llvm/llvm-project/pull/243317
`clang_Cursor_Evaluate` evaluates const char `*s = "hello"`, but returns null
for `const char *s = ("hello")`.
Ignore parentheses around the written subexpression of an implicit cast before
checking for string literals.
This also fixes evaluation of parenthesized string macros used by Kotlin/Native
cinterop:
[https://youtrack.jetbrains.com/issue/KT-85610](https://youtrack.jetbrains.com/issue/KT-85610)
and this was the original motivation for this small fix.
>From 4f26fdac4831275a97357b5124e6632d8506c6df Mon Sep 17 00:00:00 2001
From: Ivan Ugliansky <[email protected]>
Date: Mon, 16 Nov 2026 19:45:27 +0700
Subject: [PATCH] [libclang] Fix evaluation of parenthesized string
initializers
clang_Cursor_Evaluate evaluates const char *s = "hello", but returns
null for const char *s = ("hello").
Ignore parentheses around the written subexpression of an implicit
cast before checking for string literals.
This also fixes evaluation of parenthesized string macros used by
Kotlin/Native cinterop:
https://youtrack.jetbrains.com/issue/KT-85610
---
clang/test/Index/evaluate-parenthesized-strings.c | 10 ++++++++++
clang/tools/libclang/CIndex.cpp | 4 ++--
2 files changed, 12 insertions(+), 2 deletions(-)
create mode 100644 clang/test/Index/evaluate-parenthesized-strings.c
diff --git a/clang/test/Index/evaluate-parenthesized-strings.c
b/clang/test/Index/evaluate-parenthesized-strings.c
new file mode 100644
index 00000000000000..9329f77be7b7bf
--- /dev/null
+++ b/clang/test/Index/evaluate-parenthesized-strings.c
@@ -0,0 +1,10 @@
+const char *plain = "hello";
+const char *paren = ("hello");
+const char *nested = ((("hello")));
+
+// RUN: c-index-test -evaluate-cursor-at=%s:1:13 \
+// RUN: -evaluate-cursor-at=%s:2:13 \
+// RUN: -evaluate-cursor-at=%s:3:13 %s | FileCheck %s
+// CHECK: VarDecl=plain:{{.*}}Kind: CString , Value: hello
+// CHECK: VarDecl=paren:{{.*}}Kind: CString , Value: hello
+// CHECK: VarDecl=nested:{{.*}}Kind: CString , Value: hello
diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp
index a80a61f6492767..3a35b46fb32635 100644
--- a/clang/tools/libclang/CIndex.cpp
+++ b/clang/tools/libclang/CIndex.cpp
@@ -4748,7 +4748,7 @@ static const ExprEvalResult *evaluateExpr(Expr *expr,
CXCursor C) {
if (expr->getStmtClass() == Stmt::ImplicitCastExprClass) {
const auto *I = cast<ImplicitCastExpr>(expr);
- auto *subExpr = I->getSubExprAsWritten();
+ auto *subExpr = I->getSubExprAsWritten()->IgnoreParens();
if (subExpr->getStmtClass() == Stmt::StringLiteralClass ||
subExpr->getStmtClass() == Stmt::ObjCStringLiteralClass) {
const StringLiteral *StrE = nullptr;
@@ -4759,7 +4759,7 @@ static const ExprEvalResult *evaluateExpr(Expr *expr,
CXCursor C) {
StrE = ObjCExpr->getString();
result->EvalType = CXEval_ObjCStrLiteral;
} else {
- StrE = cast<StringLiteral>(I->getSubExprAsWritten());
+ StrE = cast<StringLiteral>(subExpr);
result->EvalType = CXEval_StrLiteral;
}
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits