Having said that

clang currently has minimal documentation on its plugin interface; it's mostly doxygen annotations in the source. This is an attempt to be half map to the header files/half tutorial.

Building your plugin

Just copy the clang build system

I suggest you make a new dir in llvm/tools/clang/examples/ and copy the Makefile from PrintFunctionNames there. This way, you'll just leverage the existing clang build system. You can then build your plugin with

make -C llvm/tools/clang/examples/myplugin

See Using plugins on how to use your plugin while building chromium with clang.

Use the interface in tools/clang/plugins/ChromeClassTester.h

Here's a canned interface that filters code, only passing class definitions in non-blacklisted headers. The users of ChromeClassTester are good code to study to see what you can do.

PrintFunctionNames.cpp is a plugin in the clang distribution. It is the Hello World of plugins. As a most basic skeleton, it's a good starting point. Change all the identifiers that start with PrintFunction to your desired name. Take note of the final line:

This registers your PluginASTAction with a string plugin name that can be invoked on the command line. Note that everything else is in an anonymous namespace; all other symbols aren't exported.

Your PluginASTAction subclass exists just to build your ASTConsumer, which receives declarations, sort of like a SAX parser.

Your ASTConsumer

There is doxygen documentation on when each ASTConsumer::Handle method is called in llvm/tools/clang/include/clang/AST/ASTConsumer.h. For this tutorial, I‘ll assume you only want to look at type definitions (struct, class, enum definitions), so we’ll start with:

The interface on these classes is massive; We'll only cover some of the basics, but some basics about source location and errors.

Emitting Errors

Lots of location information is stored in the Decl tree. Most Decl subclasses have multiple methods that return a SourceLocation, but lets use TagDecl::getInnerLocStart() as an example. (SourceLocation is defined in llvm/tools/clang/include/clang/Basic/SourceLocation.h, for reference.)

Errors are emitted to the user through the CompilerInstance. You will probably want to pass the CompilerInstance object passed to ASTAction::CreateASTConsumer to your ASTConsumer subclass for reporting. You interact with the user through the Diagnostic object. You could report errors to the user like this:

(The above is the simplest error reporting. See llvm/tools/clang/include/clang/Basic/Diagnostic.h for all the things you can do, like FixItHints if you want to get fancy!)

Downcast early, Downcast often

The clang library will give you the most general types possible. For example TagDecl has comparably minimal interface. The library is designed so you will be downcasting all the time, and you won‘t use the standard dynamic_cast<>() builtin to do it. Instead, you’ll use llvm/clang's home built RTTI system:

virtualvoidHandleTagDeclDefinition(TagDecl* tag){if(CXXRecordDecl* record = dyn_cast<CXXRecordDecl>(tag)){// Do stuff with |record|.}}

A (not at all exhaustive) list of things you can do with (CXX)RecordDecl

Iterate across all constructors (CXXRecordDecl::ctor_begin(), CXXReocrdDecl::ctor_end())

CXXRecordDecl::isPOD(): is this a Plain Old Datatype (a type that has no construction or destruction semantics)?

Iterate across all of the base classes of a record type (CXXRecordDecl::bases_begin(), CXXRecordDecl::bases_end())

Get the simple string name NamedDecl::getNameAsString(). (This method is deprecated, but the replacement assert()s on error conditions). (If you had struct One {}, this method would return “One”.)

Modifying existing plugins

If you want to add additional checks to the existing plugins, be sure to add the new diagnostic behind a flag (there are several examples of this in the plugins already). The reason for this is that the plugin is bundled with clang, and the new check will get deployed with the next clang roll. If your check fires, then the next clang roll would now be blocked on cleaning up the whole codebase for your check – and even if the check doesn't fire at the moment, maybe that regresses until the next clang roll happens. If your new check is behind a flag, then the clang roll can happen first, and you can add the flag to enable your check after that, and then turn on the check everywhere once you know that the codebase is clean.