redirect clang warning/error ouput


How can I redirect clang (version 3.8.1) warning/error message to a buffer ?

Thanks in advance for helps,


Hi Ogzu,

Clang prints both errors and warnings to stderr. You could redirect stdout to a file and redirect stderr to stdout: clang foo.c > file 2>&1


To a “buffer” (along the lines of std::stringstream) is also possible if you write your own “driver” for the compiler, but clang as an exectuable file has no concept of “buffer” as output destination.

I’m not going to look up exactly how to do write the necessary code to capture the output from the compiler components, but I know it’s possible, and there have been posts about it in the past.

Hi Ogzu,

It is customary, when replying to a mailing list reply, to “reply all” (or at the very least include the mailing list as well as the person that replied to you), so that other people a) can see any further discussion (it helps others) and b) perhaps reply if they feel like (either to help out or to correct something that went wrong).

Basically you need to use the clang::Diagnostic and related functionality. Depending on what you actually want to have your string output do, you may want to derive a class from clang::DiagnosticConsumer, and implement (at least) the HandleDiagnostic function in that derived class - there are some “already prepared” classes, such as TextDiagnosticBuffer that may well do what you want (I haven’t looked closely into how this works).

There’s a bit of discussion on “diagnostics” here:

You probably want to read up about raw_ostream and raw_string_ostream too - as they are useful tools for capturing output from clang, and I’m sure I’ve seem such things in previous posts, but my search for that isn’t going well.

Many thanks.