1

Is there any way to get clang output to insert carriage returns? When compiling with the verbose option, I just get these huge unreadable dumps of compiler flags and paths.

  • 1
    Hm? Those long lines seem to exactly be the ones that are fed into Clang. All flags, for example, must occur in a single command: ""/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.7.4 -emit-obj ..." (etc.; admittedly, for another 433 characters, but still *one command line*). There is no logical position to insert a hard return anywhere in it. – Jongware Nov 07 '15 at 00:47

1 Answers1

0

Use popen to start your clang session. Create a new command line as clang -v (including the space) and concatenate the arguments that you usually feed to clang itself. After the final argument, add 2>&1 to convert Clang's stderr output to regular stdout so popen can pick it up. Then loop over popen's input and parse each separate line, adding extra information where you see fit.

As an example, I grabbed the entire set of flags for my local Clang using

clang -cc1 --help

and incorporated this as a single long string in my C program. Then I looped over the return results from popen, scanning for flags starting with -, and when one was found, I scanned the long flags string for this. If it found something, I write it out on a separate line in green (using ANSI escape sequences). Then I test the flags string if an argument should follow – these usually have a leading <...> indicator. If so, I write it out in blue. Finally, I write out the entire flag explanation line until I encounter an end-of-line.

With my very rough program called colorclang – 123 lines of actual code – I get output like this:

colorclang output

As it is, it tests every input line for possible flags so there is some mis-coloring. More exact parsing is possible; I'd have to add separate routines for the single line starting with "/usr/bin/clang" (for the common Clang flags), the single line starting with "/usr/bin/ld" (and parse the loader flags), and possibly the lines after each #include .. statement.

Pieced together with the help of Complete list of clang flags?, Steve Kemp's answer to C: Run a System Command and Get Output?, and after deducing clang -v writes to stderr, larsman's answer to c popen won't catch stderr.

Community
  • 1
  • 1
Jongware
  • 21,058
  • 8
  • 43
  • 86