Skip to content

Instantly share code, notes, and snippets.

@sandeep-datta
Created May 19, 2012 14:22
Show Gist options
  • Select an option

  • Save sandeep-datta/2730990 to your computer and use it in GitHub Desktop.

Select an option

Save sandeep-datta/2730990 to your computer and use it in GitHub Desktop.
A D program to analyse a C/C++ file supplied through the command line using the D bindings for libclang
import std.stdio;
import std.string;
import deimos.clang.index;
//Purpose: analyse a C/C++ file supplied through the command line using libclang
//How to compile:-
//dmd -c -gc testclang.d
//g++ -g -I../llvm/tools/clang/include -L../llvm/build/Debug+Asserts/lib -o testclang testclang.o -lclang -lphobos2 -lrt
//Note: libclang.so must be in the search path for this to work.
void main(string[] args)
{
CXIndex index = clang_createIndex(0, 0);
CXTranslationUnit tu = clang_parseTranslationUnit(index
, cast(char*)std.string.toStringz(args[1])
, null
, 0
, null
, 0
, CXTranslationUnit_Flags.CXTranslationUnit_None);
writefln("tu=%s", tu);
for(uint i = 0, n = clang_getNumDiagnostics(tu); i != n; ++i)
{
CXDiagnostic diag = clang_getDiagnostic(tu, i);
writefln("diag=%s", diag);
uint options = clang_defaultDiagnosticDisplayOptions();
//BUG: Wrong calling convention used by the D compiler?
CXString str = clang_formatDiagnostic(diag, options);
stderr.writeln(clang_getCString(str));
}
clang_disposeTranslationUnit(tu);
clang_disposeIndex(index);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment