[cfe-dev] Running clang programmatically

Daniel Dunbar daniel at zuster.org
Wed Mar 17 07:58:33 PDT 2010


On Tue, 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"};

Why are you passing -Xclang?

This amounts to trying to run
  clang -Xclang foo.c
which won't do what you want.

>   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 ?)

Yes.

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

We don't have full support for this yet.

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

That the driver ran, but the command line didn't end up as a single
job invoking clang. In this case, it ended up with no jobs:
--
ddunbar at giles:tmp$ clang -Xclang t.c
clang: error: no input files
--

 - Daniel

>
> 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