<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/127948>127948</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
libclang's `clang_Cursor_Evaluate()` does not properly evaluate string literals
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
YSakhno
</td>
</tr>
</table>
<pre>
The function `clang_Cursor_Evaluate()` of libclang's API (from `clang-c/Index.h`) does not seem to evaluate cursors pointing to actual string literals in the source code (even though it successfully evaluates cursors pointing to integer and floating point literals). See the test example below.
```cpp
// Test is line- and column-sensitive. Run lines are below.
const char* stringTest = "Hello \"World\"";
int printf(const char* fmt, ...);
void hello() {
printf("%s\n", stringTest);
}
// RUN: c-index-test -evaluate-cursor-at=%s:3:13 \
// RUN: -evaluate-cursor-at=%s:3:26 \
// RUN: -evaluate-cursor-at=%s:8:12 \
// RUN: -evaluate-cursor-at=%s:8:20 %s | FileCheck %s
// CHECK: 3:13 {{.*}}Kind: CString , Value: Hello "World"
// CHECK: 3:26 StringLiteral {{.*}}Kind: CString , Value: Hello "World"
// CHECK: 8:12 StringLiteral {{.*}}Kind: CString , Value: %s
```
## Actual behavior
The above test fails.
```text
// CHECK: 3:26 StringLiteral {{.*}}Kind: CString , Value: Hello "World"
^
<stdin>:1:134: note: scanning from here
3:13 VarDecl=stringTest:3:13 (Definition) Extent=[3:1 - 3:43] Spelling=stringTest ([3:13 - 3:23])Kind: CString , Value: Hello "World"
^
<stdin>:2:1: note: possible intended match here
3:26 StringLiteral="Hello \"World\"" Extent=[3:26 - 3:43] Spelling="Hello \"World\"" ([3:26 - 3:43])Not Evaluatable
^
Input file: <stdin>
Check file: /home/YSakhno/llvm-project/clang/test/Index/evaluate-cursor-strings.cpp
-dump-input=help explains the following input dump.
Input was:
<<<<<<
1: 3:13 VarDecl=stringTest:3:13 (Definition) Extent=[3:1 - 3:43] Spelling=stringTest ([3:13 - 3:23])Kind: CString , Value: Hello "World"
check:16'0 X error: no match found
2: 3:26 StringLiteral="Hello \"World\"" Extent=[3:26 - 3:43] Spelling="Hello \"World\"" ([3:26 - 3:43])Not Evaluatable
check:16'0 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
check:16'1 ? possible intended match
3: 8:12 StringLiteral="%s\n" Extent=[8:12 - 8:18] Spelling="%s\n" ([8:12 - 8:18])Not Evaluatable
check:16'0 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4: 8:20 DeclRefExpr=stringTest:3:13 Extent=[8:20 - 8:30] Spelling=stringTest ([8:20 - 8:30])Not Evaluatable
check:16'0 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
## Expected behavior
The individual string literals should be evaluatable, and evaluation should return the same result as is returned from evaluation of variables that are initialized with such literals (see initialization of variable `stringTest` from the example above).
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzUV02P2zYT_jX0ZWCBHlmyffDBa6-RIC-CF9k0bU8BLY0sNjQpkJTX6aG_vSAlf-5uFv1A0y50WJOcZzjPfHGEc3KrieYsu2PZaiBaXxs7__lBfKm1GWxM-XX-sSaoWl14aTSwnBdK6O3nZWudsZ_v90K1whPDKcMZyzmYCpTcxEMMJw4W_38LDKeVNbuT9LBguH6rSzokNcs5wxmUhhxo48ER7cAboB4ZiqjJQWOk9lJvw6YofCsUOG_DgpKerFAOpAZfEzjT2oKgMCUF1bSnsG7abQ3Sg2uLgpyrWqW-nrS4Z9VI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