Search code examples
cclang

Where can I find the meaning of clang return codes?


I can't find documentation for exit codes in clang.

I haven't been able to find it in the official Clang Compiler User's Manual

Important note: this question is about the clang compiler. I'm NOT asking about MSVC which is a different compiler.


Solution

  • As far as I'm aware, the exit codes from clang are not documented.

    So, I tried to figure it out from the source. The main for clang is in a generated file called build/tools/clang/tools/driver/clang-driver.cpp, which is created from llvm/cmake/modules/llvm-driver-template.cpp.in. That calls clang_main in clang/tools/driver/driver.cpp.

    Within driver.cpp, the only manifest exit codes are 0 for success and 1 for error. So, for most purposes, that is the answer: 0 or 1.

    • Example of 0: Give clang normal input.

    • Example of 1: Give clang input with a syntax error, or command line error, etc.

    However, if a program executed by the clang driver exits non-zero, such as ld when linking, then clang will relay that code. Consequently, you can get any exit code at all from clang, depending on what the programs it runs do. If you're seeing diverse codes, this is probably why. Add -v to the clang command line to see the exact command lines it is running to track down the origin of mysterious exit codes.

    • Example of 42: Create your own ld that does exit 42 and stick it in the front of the PATH.

    If clang crashes or fails an assertion, then the consequences are system-specific; on Linux, this results in the process dying by a signal, which the shell will typically map to an exit code greater than 128. However, in most cases, the process that crashes is one invoked by the clang driver. The driver then detects a crashing sub-process, and reports that using exit code 1.

    • Example of crash: Feed clang input from a crash report, for example issue 65153.

    Finally, there's a somewhat weird case: if clang receives SIGPIPE, then it exits with code EX_IOERR from sysexits.h, which on my Linux system is 74. There is also a #define EX_IOERR 74 in llvm/Support/ExitCodes.h for systems (like Windows) that do not have sysexits.h.

    • Example of 74: Run clang -E (preprocess) on a large file, pipe the output into head, then echo $PIPESTATUS.

    It's possible there are other cases I missed, as the above was discovered mainly by manually tracing backward from clang_main, and there are a lot of backward paths. But I think this is at least close to comprehensive.