[cfe-dev] Running clang programmatically

Douglas Gregor dgregor at apple.com
Tue Mar 16 23:18:33 PDT 2010



Sent from my iPhone

On Mar 16, 2010, at 9:10 PM, Stefan Seefeld <stefan.seefeld at gmail.com>  
wrote:

> Hello,
>
> I have some trouble invoking  
> clang_createTranslationUnitFromSourceFile.
> Here is the sequence of functions I use. (Note that I assembled that
> from various comments in the code, and may likely have missed some
> important bits):
>
>
>   // excludeDeclsFromPCH = 1, displayDiagnostics=1
>   CXIndex idx = clang_createIndex(1, 1);
>
>   char const *args[] = { "-Xclang"};
>   CXTranslationUnit TU = clang_createTranslationUnitFromSourceFile 
> (idx,
> input_file,
>                                    1, args, 0, 0);
>   clang_visitChildren(clang_getTranslationUnitCursor(TU), translate,  
> 0);
>   clang_disposeTranslationUnit(TU);
>   clang_disposeIndex(idx);
>
> This results in an error output, plus a subsequent segfault:
>
> error: unable to handle compilation, expected exactly one compiler job
> in ' "/usr/local/bin/clang" "-cc1" "-triple" "x86_64-unknown-linux- 
> gnu"
> "-S" "-disable-free" "-main-file-name" "test.cc" "-mrelocation-model"
> "static" "-mdisable-fp-elim" "-mconstructor-aliases" "-munwind-tables"
> "-target-cpu" "x86-64" "-resource-dir" "/lib/clang/1.1"
> "-fmessage-length" "183" "-fexceptions" "-fgnu-runtime"
> "-fdiagnostics-show-option" "-fcolor-diagnostics" "-fsyntax-only" "-o"
> "/tmp/cc-GyUGlM.s" "-x" "c++" "/home/stefan/projects/Synopsis/ 
> test.cc";
> "/usr/bin/gcc" "-Xclang" "-fsyntax-only" "-c" "-m64" "-o"
> "/tmp/cc-fWJsot.o" "-x" "assembler" "/tmp/cc-GyUGlM.s";  "/usr/bin/ 
> gcc"
> "-Xclang" "-fsyntax-only" "-m64" "-o" "a.out" "/tmp/cc-fWJsot.o"; '
>
>
> My questions:
>
> 1) Is the function call sequence above (approximately) correct ? (If
> not, what is wrong ?)

Do not pass -Xclang. It is for internal use only, and misuse will  
confuse the driver. You don't need to pass any arguments.

> 2) Where is the current error emitted, and how may I intercept it
> programmatically ?

Driver errors can't easily be intercepted at the moment, so they go to  
standard error. In the near future, all diagnostics will go through  
the diagnostic interface of CIndex.

> 3) What is the error trying to tell me ?

That you really, really shouldn't pass -Xclang :)

>
> Thanks for helping me getting started with CLang !
>
>     Stefan
>
> -- 
>
>       ...ich hab' noch einen Koffer in Berlin...
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev



More information about the cfe-dev mailing list