I’ve assembled a list of random tips and tricks that I came across while working with Clang. Most of them serve to clear up some confusing behaviors or other complicated parts not well covered in the Clang documentation.
Introduction to Clang Plugins
As mentioned in Part I of this tutorial, a Clang Plugin is similar to the LibTooling environment, except that it cannot reason about multiple source files (among other differences). In other words, it cannot remember details of more than one source file at a time, so keeping track of information about several source files (with something like global variables) is impossible.
That being said, there are instances when a Clang Plugin is still useful, like a syntax checker or formatting helper. As such, here’s how to build one that does the exact same thing as our LibTooling example from Part II.
What is Clang?
I’ve spent the last few months working with Clang, a frontend for the LLVM compiler project. Clang can parse and analyze any source code in the C language family (C, C++, ObjectiveC, etc…) and has a wonderful modular design that makes it easy to use.
If you’re looking to do static analysis on some C code, I highly recommend Clang — it’s vastly superior to other static analysis tools (like CIL … yuck!) and has decent documentation. Also, the Clang mailing list is very active and helpful if you ever find yourself stuck on something.
I personally used Clang to perform static analysis on I/O device drivers in the Linux kernel, including camera drivers and DRM drivers for graphics cards. Kernel code (especially drivers) can be very complex and difficult to analyze, but Clang allows you to handle it with ease. Let’s take a look at what you can do with Clang.