llvm / Clang hacking: Part 2

Part 2 in my N-part series on my exploration of hacking on llvm and Clang (c-language) tool chain.

Prerequisites

This post assumes you’ve successfully completed Part 1 of the series.

Debugging

By default, Clang presents a gcc-compatible command-line interface. In most circumstances, this allows Clang to be a drop-in replacement for gcc for rapid testing and easier adoption. When using the gcc interface, Clang spawns a new job to handle the compilation, which prevents debugging the various stages of the compilation process. You can see this by running the following command:

clang -### test.m -o test

With that in mind, you should invoke Clang with the -cc1 option as the first argument, which directly executes the Clang cc1 tool. TIP: running the following command outputs considerably useful command line documentation:

clang -cc1 --help

Also noteworthy is the following command

clang -cc1as --help

which documents many of the arguments available to the Clang Integrated Assembler.

If your using Xcode, you’ll find main() in Clang executables » clang » Source Files » driver.cpp. Notice in this folder you’ll also find the cc1_main.cpp, which is the entry point for the clang compiler.

Clang executable source files

To set these command-line options, select Product | Edit Scheme:

Edit Scheme

Note again that the first argument must be -cc1. Next is the input file to compile, which you will substitute for your own source file. Finally you must specific the include path for your current clang headers as this development build will not find them automatically.

With these set, you should be able to successfully run and debug the entire Clang tool chain.

Now that you have an environment and presumably can debug the compiler, I’d recommend you read the following articles for clarification on the design and internals of Clang:

Next Up

Next up I’ll walk through creating a language extension to Objective-C, supporting NSURL literals, following (in principal) new Objective-C Literals coming in the next release of Clang.

Twitter

Follow me on twitter, @stuartcarnie.