[cfe-users] clang_createTranslationUnitFromSourceFile() and C++ source code
Kirill Bugaev via cfe-users
cfe-users at lists.llvm.org
Fri May 31 01:18:21 PDT 2019
I am trying to determine data types in C++ source code with help of
libclang. My program is
=====
#include <stdio.h>
#include <stdlib.h>
#include <clang-c/Index.h>
int main(int argc, char *argv[])
{
CXIndex index;
CXTranslationUnit tu;
CXFile file;
CXSourceLocation loc;
CXCursor cursor, def;
CXType type;
CXString typesp;
const char *types;
char const *args[2] = {"-x", "c++"};
index = clang_createIndex(0, 0);
tu = clang_createTranslationUnitFromSourceFile(index, argv[1],
2, args, 0, NULL);
file = clang_getFile(tu, argv[1]);
loc = clang_getLocation(tu, file, atoi(argv[2]), atoi(argv[3]));
cursor = clang_getCursor(tu, loc);
if (clang_isPreprocessing(cursor.kind))
printf("Preprocessor\n");
else {
def = clang_getCursorDefinition(cursor);
if (clang_Cursor_isNull(def))
type = clang_getCursorType(cursor);
else
type = clang_getCursorType(def);
typesp = clang_getTypeSpelling(type);
types = clang_getCString(typesp);
printf("%s\n", types);
clang_disposeString(typesp);
}
clang_disposeTranslationUnit(tu);
clang_disposeIndex(index);
}
=====
And test source code helloworld.cpp which I feed to my program
=====
#include <iostream>
int main()
{
long t;
t = 0;
std::cout << "Hello World!";
}
=====
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().
I do something wrong or clang_createTranslationUnitFromSourceFile() doesn't
support parsing of C++ code? My clang version is 8.0.0
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20190531/68a99437/attachment.html>
More information about the cfe-users
mailing list