Open
Description
Libclang tutorial complete-example-code
Example Snippet from Libclang tutorial
#include <clang-c/Index.h>
#include <iostream>
int main(){
CXIndex index = clang_createIndex(0, 0); //Create index
CXTranslationUnit unit = clang_parseTranslationUnit(
index,
"file.cpp", nullptr, 0,
nullptr, 0,
CXTranslationUnit_None); //Parse "file.cpp"
if (unit == nullptr){
std::cerr << "Unable to parse translation unit. Quitting.\n";
return 0;
}
CXCursor cursor = clang_getTranslationUnitCursor(unit); //Obtain a cursor at the root of the translation unit
clang_visitChildren(
cursor,
[](CXCursor current_cursor, CXCursor parent, CXClientData client_data){
CXType cursor_type = clang_getCursorType(current_cursor);
CXString type_kind_spelling = clang_getTypeKindSpelling(cursor_type.kind);
std::cout << "TypeKind: " << clang_getCString(type_kind_spelling);
clang_disposeString(type_kind_spelling);
if(cursor_type.kind == CXType_Pointer || // If cursor_type is a pointer
cursor_type.kind == CXType_LValueReference || // or an LValue Reference (&)
cursor_type.kind == CXType_RValueReference){ // or an RValue Reference (&&),
CXType pointed_to_type = clang_getPointeeType(cursor_type);// retrieve the pointed-to type
CXString pointed_to_type_spelling = clang_getTypeSpelling(pointed_to_type); // Spell out the entire
std::cout << "pointing to type: " << clang_getCString(pointed_to_type_spelling);// pointed-to type
clang_disposeString(pointed_to_type_spelling);
}
else if(cursor_type.kind == CXType_Record){
CXString type_spelling = clang_getTypeSpelling(cursor_type);
std::cout << ", namely " << clang_getCString(type_spelling);
clang_disposeString(type_spelling);
}
std::cout << "\n";
return CXChildVisit_Recurse;
},
nullptr
);
clang_visitChildren(
cursor,
[](CXCursor current_cursor, CXCursor parent, CXClientData client_data){
CXType cursor_type = clang_getCursorType(current_cursor);
CXString cursor_spelling = clang_getCursorSpelling(current_cursor);
CXSourceRange cursor_range = clang_getCursorExtent(current_cursor);
std::cout << "Cursor " << clang_getCString(cursor_spelling);
CXFile file;
unsigned start_line, start_column, start_offset;
unsigned end_line, end_column, end_offset;
clang_getExpansionLocation(clang_getRangeStart(cursor_range), &file, &start_line, &start_column, &start_offset);
clang_getExpansionLocation(clang_getRangeEnd (cursor_range), &file, &end_line , &end_column , &end_offset);
std::cout << " spanning lines " << start_line << " to " << end_line;
clang_disposeString(cursor_spelling);
std::cout << "\n";
return CXChildVisit_Recurse;
},
nullptr
);
}
Error Message
cmake -G Ninja -B build -S .; cmake --build build
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: /home/illu/playgound/cxx/libclang-example/build
ninja: no work to do.
./build/main
TypeKind: Record, namely A
TypeKind: Int
TypeKind: Record, namely B
TypeKind: Int
TypeKind: Elaborated
TypeKind: Record, namely A
Cursor A spanning lines 1 to 3
Cursor value spanning lines 2 to 2
Cursor B spanning lines 4 to 7
Cursor value spanning lines 5 to 5
Cursor struct_value spanning lines 6 to 6
Cursor struct A spanning lines 6 to 6
=================================================================
==30291==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 80 byte(s) in 1 object(s) allocated from:
#0 0x79c1bbcfe548 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:95
#1 0x79c1b9e48bce (/usr/lib/llvm-18/lib/libclang-18.so.18+0x448bce) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#2 0x79c1b26eff76 in llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/usr/lib/llvm-18/lib/../lib/libLLVM.so.18.1+0xceff76) (BuildId: 495b188fed20e21b476ccb1eaea212f4304fad28)
#3 0x79c1b26f016c (/usr/lib/llvm-18/lib/../lib/libLLVM.so.18.1+0xcf016c) (BuildId: 495b188fed20e21b476ccb1eaea212f4304fad28)
#4 0x79c1b26f02b2 (/usr/lib/llvm-18/lib/../lib/libLLVM.so.18.1+0xcf02b2) (BuildId: 495b188fed20e21b476ccb1eaea212f4304fad28)
#5 0x79c1bbc5ea41 in asan_thread_start ../../../../src/libsanitizer/asan/asan_interceptors.cpp:234
#6 0x79c1b929caa3 in start_thread nptl/pthread_create.c:447
Indirect leak of 204 byte(s) in 1 object(s) allocated from:
#0 0x79c1bbcfd340 in calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x79c1b27495ba in llvm::StringMapImpl::LookupBucketFor(llvm::StringRef) (/usr/lib/llvm-18/lib/../lib/libLLVM.so.18.1+0xd495ba) (BuildId: 495b188fed20e21b476ccb1eaea212f4304fad28)
#2 0x79c1b9e41d6f (/usr/lib/llvm-18/lib/libclang-18.so.18+0x441d6f) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#3 0x79c1bb0bf131 (/usr/lib/llvm-18/lib/libclang-18.so.18+0x16bf131) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#4 0x79c1bb0bf0ad (/usr/lib/llvm-18/lib/libclang-18.so.18+0x16bf0ad) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#5 0x79c1b9e30172 (/usr/lib/llvm-18/lib/libclang-18.so.18+0x430172) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#6 0x5556a2a725c2 in main (/home/illu/playgound/cxx/libclang-example/build/main+0x35c2) (BuildId: 653bfd42ee4c63ab7063ac306faa3891d1d6c0d3)
#7 0x79c1b922a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
#8 0x79c1b922a28a in __libc_start_main_impl ../csu/libc-start.c:360
#9 0x5556a2a71404 in _start (/home/illu/playgound/cxx/libclang-example/build/main+0x2404) (BuildId: 653bfd42ee4c63ab7063ac306faa3891d1d6c0d3)
Indirect leak of 204 byte(s) in 1 object(s) allocated from:
#0 0x79c1bbcfd340 in calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x79c1b27495ba in llvm::StringMapImpl::LookupBucketFor(llvm::StringRef) (/usr/lib/llvm-18/lib/../lib/libLLVM.so.18.1+0xd495ba) (BuildId: 495b188fed20e21b476ccb1eaea212f4304fad28)
#2 0x79c1ba5d5daf (/usr/lib/llvm-18/lib/libclang-18.so.18+0xbd5daf) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#3 0x79c1bb0bf06f (/usr/lib/llvm-18/lib/libclang-18.so.18+0x16bf06f) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#4 0x79c1b9e30172 (/usr/lib/llvm-18/lib/libclang-18.so.18+0x430172) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#5 0x5556a2a725c2 in main (/home/illu/playgound/cxx/libclang-example/build/main+0x35c2) (BuildId: 653bfd42ee4c63ab7063ac306faa3891d1d6c0d3)
#6 0x79c1b922a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
#7 0x79c1b922a28a in __libc_start_main_impl ../csu/libc-start.c:360
#8 0x5556a2a71404 in _start (/home/illu/playgound/cxx/libclang-example/build/main+0x2404) (BuildId: 653bfd42ee4c63ab7063ac306faa3891d1d6c0d3)
Indirect leak of 152 byte(s) in 1 object(s) allocated from:
#0 0x79c1bbcfe548 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:95
#1 0x79c1b9e3013a (/usr/lib/llvm-18/lib/libclang-18.so.18+0x43013a) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#2 0x5556a2a725c2 in main (/home/illu/playgound/cxx/libclang-example/build/main+0x35c2) (BuildId: 653bfd42ee4c63ab7063ac306faa3891d1d6c0d3)
#3 0x79c1b922a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
#4 0x79c1b922a28a in __libc_start_main_impl ../csu/libc-start.c:360
#5 0x5556a2a71404 in _start (/home/illu/playgound/cxx/libclang-example/build/main+0x2404) (BuildId: 653bfd42ee4c63ab7063ac306faa3891d1d6c0d3)
Indirect leak of 128 byte(s) in 1 object(s) allocated from:
#0 0x79c1bbcfe548 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:95
#1 0x79c1b9e48ffe (/usr/lib/llvm-18/lib/libclang-18.so.18+0x448ffe) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#2 0x79c1b9e48c6f (/usr/lib/llvm-18/lib/libclang-18.so.18+0x448c6f) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#3 0x79c1b26eff76 in llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/usr/lib/llvm-18/lib/../lib/libLLVM.so.18.1+0xceff76) (BuildId: 495b188fed20e21b476ccb1eaea212f4304fad28)
#4 0x79c1b26f016c (/usr/lib/llvm-18/lib/../lib/libLLVM.so.18.1+0xcf016c) (BuildId: 495b188fed20e21b476ccb1eaea212f4304fad28)
#5 0x79c1b26f02b2 (/usr/lib/llvm-18/lib/../lib/libLLVM.so.18.1+0xcf02b2) (BuildId: 495b188fed20e21b476ccb1eaea212f4304fad28)
#6 0x79c1bbc5ea41 in asan_thread_start ../../../../src/libsanitizer/asan/asan_interceptors.cpp:234
#7 0x79c1b929caa3 in start_thread nptl/pthread_create.c:447
Indirect leak of 128 byte(s) in 1 object(s) allocated from:
#0 0x79c1bbcfe548 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:95
#1 0x79c1b9e30147 (/usr/lib/llvm-18/lib/libclang-18.so.18+0x430147) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#2 0x5556a2a725c2 in main (/home/illu/playgound/cxx/libclang-example/build/main+0x35c2) (BuildId: 653bfd42ee4c63ab7063ac306faa3891d1d6c0d3)
#3 0x79c1b922a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
#4 0x79c1b922a28a in __libc_start_main_impl ../csu/libc-start.c:360
#5 0x5556a2a71404 in _start (/home/illu/playgound/cxx/libclang-example/build/main+0x2404) (BuildId: 653bfd42ee4c63ab7063ac306faa3891d1d6c0d3)
Indirect leak of 48 byte(s) in 2 object(s) allocated from:
#0 0x79c1bbcfe548 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:95
#1 0x79c1b9e48cf7 (/usr/lib/llvm-18/lib/libclang-18.so.18+0x448cf7) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#2 0x79c1b26eff76 in llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/usr/lib/llvm-18/lib/../lib/libLLVM.so.18.1+0xceff76) (BuildId: 495b188fed20e21b476ccb1eaea212f4304fad28)
#3 0x79c1b26f016c (/usr/lib/llvm-18/lib/../lib/libLLVM.so.18.1+0xcf016c) (BuildId: 495b188fed20e21b476ccb1eaea212f4304fad28)
#4 0x79c1b26f02b2 (/usr/lib/llvm-18/lib/../lib/libLLVM.so.18.1+0xcf02b2) (BuildId: 495b188fed20e21b476ccb1eaea212f4304fad28)
#5 0x79c1bbc5ea41 in asan_thread_start ../../../../src/libsanitizer/asan/asan_interceptors.cpp:234
#6 0x79c1b929caa3 in start_thread nptl/pthread_create.c:447
Indirect leak of 48 byte(s) in 1 object(s) allocated from:
#0 0x79c1bbcfe548 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:95
#1 0x79c1b9e5b511 (/usr/lib/llvm-18/lib/libclang-18.so.18+0x45b511) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#2 0x79c1b9e48c17 (/usr/lib/llvm-18/lib/libclang-18.so.18+0x448c17) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#3 0x79c1b26eff76 in llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/usr/lib/llvm-18/lib/../lib/libLLVM.so.18.1+0xceff76) (BuildId: 495b188fed20e21b476ccb1eaea212f4304fad28)
#4 0x79c1b26f016c (/usr/lib/llvm-18/lib/../lib/libLLVM.so.18.1+0xcf016c) (BuildId: 495b188fed20e21b476ccb1eaea212f4304fad28)
#5 0x79c1b26f02b2 (/usr/lib/llvm-18/lib/../lib/libLLVM.so.18.1+0xcf02b2) (BuildId: 495b188fed20e21b476ccb1eaea212f4304fad28)
#6 0x79c1bbc5ea41 in asan_thread_start ../../../../src/libsanitizer/asan/asan_interceptors.cpp:234
#7 0x79c1b929caa3 in start_thread nptl/pthread_create.c:447
Indirect leak of 30 byte(s) in 1 object(s) allocated from:
#0 0x79c1bbcfe548 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:95
#1 0x79c1ba35c2bb (/usr/lib/llvm-18/lib/libclang-18.so.18+0x95c2bb) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#2 0x79c1b9e570dc (/usr/lib/llvm-18/lib/libclang-18.so.18+0x4570dc) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#3 0x79c1b9e48814 (/usr/lib/llvm-18/lib/libclang-18.so.18+0x448814) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#4 0x79c1b26eff76 in llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/usr/lib/llvm-18/lib/../lib/libLLVM.so.18.1+0xceff76) (BuildId: 495b188fed20e21b476ccb1eaea212f4304fad28)
#5 0x79c1b26f016c (/usr/lib/llvm-18/lib/../lib/libLLVM.so.18.1+0xcf016c) (BuildId: 495b188fed20e21b476ccb1eaea212f4304fad28)
#6 0x79c1b26f02b2 (/usr/lib/llvm-18/lib/../lib/libLLVM.so.18.1+0xcf02b2) (BuildId: 495b188fed20e21b476ccb1eaea212f4304fad28)
#7 0x79c1bbc5ea41 in asan_thread_start ../../../../src/libsanitizer/asan/asan_interceptors.cpp:234
#8 0x79c1b929caa3 in start_thread nptl/pthread_create.c:447
Indirect leak of 24 byte(s) in 1 object(s) allocated from:
#0 0x79c1bbcfe548 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:95
#1 0x79c1b9e48bf8 (/usr/lib/llvm-18/lib/libclang-18.so.18+0x448bf8) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#2 0x79c1b26eff76 in llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/usr/lib/llvm-18/lib/../lib/libLLVM.so.18.1+0xceff76) (BuildId: 495b188fed20e21b476ccb1eaea212f4304fad28)
#3 0x79c1b26f016c (/usr/lib/llvm-18/lib/../lib/libLLVM.so.18.1+0xcf016c) (BuildId: 495b188fed20e21b476ccb1eaea212f4304fad28)
#4 0x79c1b26f02b2 (/usr/lib/llvm-18/lib/../lib/libLLVM.so.18.1+0xcf02b2) (BuildId: 495b188fed20e21b476ccb1eaea212f4304fad28)
#5 0x79c1bbc5ea41 in asan_thread_start ../../../../src/libsanitizer/asan/asan_interceptors.cpp:234
#6 0x79c1b929caa3 in start_thread nptl/pthread_create.c:447
Indirect leak of 20 byte(s) in 1 object(s) allocated from:
#0 0x79c1bbcfeaf8 in operator new(unsigned long, std::align_val_t) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:107
#1 0x79c1ba5d5dfc (/usr/lib/llvm-18/lib/libclang-18.so.18+0xbd5dfc) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#2 0x79c1bb0bf06f (/usr/lib/llvm-18/lib/libclang-18.so.18+0x16bf06f) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#3 0x79c1b9e30172 (/usr/lib/llvm-18/lib/libclang-18.so.18+0x430172) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#4 0x5556a2a725c2 in main (/home/illu/playgound/cxx/libclang-example/build/main+0x35c2) (BuildId: 653bfd42ee4c63ab7063ac306faa3891d1d6c0d3)
#5 0x79c1b922a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
#6 0x79c1b922a28a in __libc_start_main_impl ../csu/libc-start.c:360
#7 0x5556a2a71404 in _start (/home/illu/playgound/cxx/libclang-example/build/main+0x2404) (BuildId: 653bfd42ee4c63ab7063ac306faa3891d1d6c0d3)
Indirect leak of 20 byte(s) in 1 object(s) allocated from:
#0 0x79c1bbcfeaf8 in operator new(unsigned long, std::align_val_t) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:107
#1 0x79c1b9e41dbc (/usr/lib/llvm-18/lib/libclang-18.so.18+0x441dbc) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#2 0x79c1bb0bf131 (/usr/lib/llvm-18/lib/libclang-18.so.18+0x16bf131) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#3 0x79c1bb0bf0ad (/usr/lib/llvm-18/lib/libclang-18.so.18+0x16bf0ad) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#4 0x79c1b9e30172 (/usr/lib/llvm-18/lib/libclang-18.so.18+0x430172) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#5 0x5556a2a725c2 in main (/home/illu/playgound/cxx/libclang-example/build/main+0x35c2) (BuildId: 653bfd42ee4c63ab7063ac306faa3891d1d6c0d3)
#6 0x79c1b922a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
#7 0x79c1b922a28a in __libc_start_main_impl ../csu/libc-start.c:360
#8 0x5556a2a71404 in _start (/home/illu/playgound/cxx/libclang-example/build/main+0x2404) (BuildId: 653bfd42ee4c63ab7063ac306faa3891d1d6c0d3)
Indirect leak of 8 byte(s) in 1 object(s) allocated from:
#0 0x79c1bbcfe548 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:95
#1 0x79c1bb0bf08f (/usr/lib/llvm-18/lib/libclang-18.so.18+0x16bf08f) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#2 0x79c1b9e30172 (/usr/lib/llvm-18/lib/libclang-18.so.18+0x430172) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#3 0x5556a2a725c2 in main (/home/illu/playgound/cxx/libclang-example/build/main+0x35c2) (BuildId: 653bfd42ee4c63ab7063ac306faa3891d1d6c0d3)
#4 0x79c1b922a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
#5 0x79c1b922a28a in __libc_start_main_impl ../csu/libc-start.c:360
#6 0x5556a2a71404 in _start (/home/illu/playgound/cxx/libclang-example/build/main+0x2404) (BuildId: 653bfd42ee4c63ab7063ac306faa3891d1d6c0d3)
Indirect leak of 8 byte(s) in 1 object(s) allocated from:
#0 0x79c1bbcfe548 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:95
#1 0x79c1bb0bf04a (/usr/lib/llvm-18/lib/libclang-18.so.18+0x16bf04a) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#2 0x79c1b9e30172 (/usr/lib/llvm-18/lib/libclang-18.so.18+0x430172) (BuildId: ea056e259864b6af08c46875208cc97be270ce0e)
#3 0x5556a2a725c2 in main (/home/illu/playgound/cxx/libclang-example/build/main+0x35c2) (BuildId: 653bfd42ee4c63ab7063ac306faa3891d1d6c0d3)
#4 0x79c1b922a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
#5 0x79c1b922a28a in __libc_start_main_impl ../csu/libc-start.c:360
#6 0x5556a2a71404 in _start (/home/illu/playgound/cxx/libclang-example/build/main+0x2404) (BuildId: 653bfd42ee4c63ab7063ac306faa3891d1d6c0d3)
SUMMARY: AddressSanitizer: 1102 byte(s) leaked in 15 allocation(s).
make: *** [Makefile:5: run_main] Error 1
Patch
#include <clang-c/Index.h>
#include <iostream>
int main(){
CXIndex index = clang_createIndex(0, 0); //Create index
CXTranslationUnit unit = clang_parseTranslationUnit(
index,
"file.cpp", nullptr, 0,
nullptr, 0,
CXTranslationUnit_None); //Parse "file.cpp"
if (unit == nullptr){
std::cerr << "Unable to parse translation unit. Quitting.\n";
return 0;
}
CXCursor cursor = clang_getTranslationUnitCursor(unit); //Obtain a cursor at the root of the translation unit
clang_visitChildren(
cursor,
[](CXCursor current_cursor, CXCursor parent, CXClientData client_data){
CXType cursor_type = clang_getCursorType(current_cursor);
CXString type_kind_spelling = clang_getTypeKindSpelling(cursor_type.kind);
std::cout << "TypeKind: " << clang_getCString(type_kind_spelling);
clang_disposeString(type_kind_spelling);
if(cursor_type.kind == CXType_Pointer || // If cursor_type is a pointer
cursor_type.kind == CXType_LValueReference || // or an LValue Reference (&)
cursor_type.kind == CXType_RValueReference){ // or an RValue Reference (&&),
CXType pointed_to_type = clang_getPointeeType(cursor_type);// retrieve the pointed-to type
CXString pointed_to_type_spelling = clang_getTypeSpelling(pointed_to_type); // Spell out the entire
std::cout << "pointing to type: " << clang_getCString(pointed_to_type_spelling);// pointed-to type
clang_disposeString(pointed_to_type_spelling);
}
else if(cursor_type.kind == CXType_Record){
CXString type_spelling = clang_getTypeSpelling(cursor_type);
std::cout << ", namely " << clang_getCString(type_spelling);
clang_disposeString(type_spelling);
}
std::cout << "\n";
return CXChildVisit_Recurse;
},
nullptr
);
clang_visitChildren(
cursor,
[](CXCursor current_cursor, CXCursor parent, CXClientData client_data){
CXType cursor_type = clang_getCursorType(current_cursor);
CXString cursor_spelling = clang_getCursorSpelling(current_cursor);
CXSourceRange cursor_range = clang_getCursorExtent(current_cursor);
std::cout << "Cursor " << clang_getCString(cursor_spelling);
CXFile file;
unsigned start_line, start_column, start_offset;
unsigned end_line, end_column, end_offset;
clang_getExpansionLocation(clang_getRangeStart(cursor_range), &file, &start_line, &start_column, &start_offset);
clang_getExpansionLocation(clang_getRangeEnd (cursor_range), &file, &end_line , &end_column , &end_offset);
std::cout << " spanning lines " << start_line << " to " << end_line;
clang_disposeString(cursor_spelling);
std::cout << "\n";
return CXChildVisit_Recurse;
},
nullptr
);
+ clang_disposeTranslationUnit(unit);
+ clang_disposeIndex(index);
}