<div dir="ltr">I am trying to determine data types in C++ source code with help of libclang. My program is<div>=====</div><div>#include <stdio.h></div><div>#include <stdlib.h></div><div>#include <clang-c/Index.h></div><div><br></div><div>int main(int argc, char *argv[])</div><div>{</div><div>    CXIndex index;</div><div>    CXTranslationUnit tu;</div><div>    CXFile file;</div><div>    CXSourceLocation loc;</div><div>    CXCursor cursor, def;</div><div>    CXType type;</div><div>    CXString typesp;</div><div>    const char *types;</div><div><br></div><div>    char const *args[2] = {"-x", "c++"};</div><div>    index = clang_createIndex(0, 0);</div><div>    tu = clang_createTranslationUnitFromSourceFile(index, argv[1],</div><div>            2, args, 0, NULL);</div><div>    file = clang_getFile(tu, argv[1]);</div><div>    loc = clang_getLocation(tu, file, atoi(argv[2]), atoi(argv[3]));</div><div>    cursor = clang_getCursor(tu, loc);</div><div><br></div><div>    if (clang_isPreprocessing(cursor.kind))</div><div>        printf("Preprocessor\n");</div><div>    else {</div><div>        def = clang_getCursorDefinition(cursor);</div><div>        if (clang_Cursor_isNull(def))</div><div>            type = clang_getCursorType(cursor);</div><div>        else </div><div>            type = clang_getCursorType(def);</div><div>        typesp = clang_getTypeSpelling(type);</div><div>        types = clang_getCString(typesp);</div><div>        printf("%s\n", types);</div><div>        clang_disposeString(typesp);</div><div>    }</div><div>    clang_disposeTranslationUnit(tu);</div><div>    clang_disposeIndex(index);</div><div>}</div><div>=====</div><div><br></div><div>And test source code helloworld.cpp which I feed to my program</div><div>=====</div><div>#include <iostream></div><div><br></div><div>int main()</div><div>{</div><div>    long t;</div><div>    t = 0;</div><div>    std::cout << "Hello World!";</div><div>}</div><div>=====</div><div><br></div><div>When I pass test source code to program into command line arguments, like ./program helloworld.cpp 7 2, it can't determine type of variable t. I watched value of cursor variable with gdb, it is {kind = CXCursor_NoDeclFound, xdata = 0, data = {0x0, 0x0, 0x0}}. But everything works fine if I generate AST file for helloworld.cpp and use clang_createTranslationUnit() instead of clang_createTranslationUnitFromSourceFile().</div><div><br></div><div>I do something wrong or clang_createTranslationUnitFromSourceFile() doesn't support parsing of C++ code? My clang version is 8.0.0</div></div>