<div dir="ltr"><div dir="ltr">I write program which determines type of instance under cursor in source code. I pass on ast-file and cursor location as cmd arguments and output instance location(line number and column) and its type.<div><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">#include <stdio.h><br>#include <stdlib.h><br>#include <clang-c/Index.h><br>int main(int argc, char *argv[])<br>{<br>    CXIndex index;<br>    CXTranslationUnit tu;<br>    CXFile file;<br>    CXSourceLocation loc;<br>    CXCursor cursor, def;<br>    CXType type;<br>    CXString typesp;<br>    const char *types;<br>    index = clang_createIndex(0, 0);<br>//    tu = clang_createTranslationUnitFromSourceFile(index, argv[1],<br>//            0, NULL, 0, NULL);<br>    tu = clang_createTranslationUnit(index, argv[1]);<br>    file = clang_getFile(tu, argv[1]);<br>    loc = clang_getLocation(tu, file, atoi(argv[2]), atoi(argv[3]));<br>    cursor = clang_getCursor(tu, loc);<br>/* Cursor location check */<br>CXSourceLocation testloc;<br>testloc = clang_getCursorLocation(cursor);<br>unsigned lnum, colnum;<br>clang_getFileLocation(testloc, NULL, &lnum, &colnum, NULL);<br>printf("%d %d\n", lnum, colnum);<br>    if (clang_isPreprocessing(cursor.kind))<br>        printf("Preprocessor\n");<br>    else {<br>        def = clang_getCursorDefinition(cursor);<br>        if (clang_Cursor_isNull(def))<br>            type = clang_getCursorType(cursor);<br>        else <br>            type = clang_getCursorType(def);<br>        typesp = clang_getTypeSpelling(type);<br>        types = clang_getCString(typesp);<br>        printf("%s\n", types);<br>        clang_disposeString(typesp);<br>    }<br>    clang_disposeTranslationUnit(tu);<br>    clang_disposeIndex(index);<br>}</blockquote><div>I don't want to generate ast file for source file before program starts. I can use clang_createTranslationUnitFromSourceFile() and clang_reparseTranslationUnit instead of clang_createTranslationUnit(). But problem is TranslationUnits which I will get with this functions is some kind of wrong. So I can't get appropriate Cursor for 7, 10 (line, column) position when force program process its own source code.</div><div><div>When I apply program to itself source code with 16 (line) and 3 (column) parameters which correspond</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">    index = clang_createIndex(0, 0);<br>//   ^</blockquote><div>it displays  6 (line) and 1 (column) which corresponds</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">int main(int argc, char *argv[])<br>{<br>^</blockquote><div>It's wrong. But when apply 29 3 which correspond</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">printf("%d %d\n", lnum, colnum);<br>//^</blockquote><div>It displays 28 1 and it's right. Why Cursor is made wrong in first case and how to make it right? Does some way exists to create right TranslationUnit for source code without using clang -emit-ast? </div></div></div></div></div>