Skip to content

libclang's clang_Cursor_Evaluate() does not properly evaluate string literals #127948

Open
@YSakhno

Description

@YSakhno

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.

// 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.

// 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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions