unit tests for Clang TDD

I am working on some new static analysis capabilities for a class project (new for C/C++, derived from an existing Java tool). While making a contribution to the Clang code base isn't required for the class, it is a goal.

Now that I have a copy of LLVM and Clang source code downloaded, compiled, and the regression test suite running, i am looking at how to set up my own tests. When the documentation used the term "regression" for the test suite, it was accurate. The test suite is a nice integration-level set of tests.

But what about unit-level tests? Did i miss something in the documentation or directory tree? Is there a preferred unit test framework? I don't want to start coding until i have a failing unit test in place.

Thank you for your time,
David Kristola

There is a top-level directory in the Clang tree “unittests”. It has several tests under it, using the Google Test framework. Currently, the static analyzer doesn’t get a lot of unit testing, but there are certainly many parts of it where that would make sense (things like abstract graph algorithms, etc.).

That said, there is more emphasis on somewhat mid-level integration testing in Clang. The tools are architected in a way to facilitate remarkably targeted “regression” tests that focus on a specific piece of functionality. Specifically with the analyzer, usually you can craft a minimal test case which should exhibit a very specific static analysis report. These test cases can be extremely minimal and focused in order to ensure reasonable debugging and targeting of any failures.

Unit tests are often reserved for parts of Clang which don’t lend themselves to this, due to abstract functional behavior, etc.

I remember watching a video about clang/llvm that showed how you could
just create an instance of a lexer when and where you needed it, and that the
development of clang/llvm was an "engineering exercise" - painting by

That's what sold me on clang/llvm.

I had to surf the web for a "clang tutorial", it's not in the source tree.

It's exactly what I wanted but doesn't work with the current clang, and
making it work would involve N individuals of varying skill levels on their own,
instead of being something that made clang/llvm stand out even more.

It would be great if this found it's way into an examples/unittests/tutorial
folder and was maintained.

I don't mind API breakage as long as I can see how to change it.
Examples/unittests/tutorials are how you do that.

Open source will always beat closed source because of this kind of

You come up with a better/faster/simpler way to do something,
and if it hasn't already been burned into an eprom, developers will use it,
if they are shown how, and the whole system benefits.


Hi David,

clang uses gtest, see the unittests/ folder. But the integration-level
test approach of the tests in test/ are preferred if possible.


Hello Chandler, Philip, and Nico,

Thank you all for the information! Integration-level tests are important, but so are unit tests IMO. The intent is to write both, although the integration-level tests might be written *after* code instead of *before* (when unit tests are written). Between a full time job, a 16 month old daughter, an understandably frazzled wife, visiting in-laws, the normal grind of homework, and this project, i'm not getting enough sleep. I need the safety net of unit tests. I started practicing Test-Driven Development a little while ago and i've found that it really helps me.

I've downloaded gtest but it will have to wait until tomorrow for a test drive because my eyes won't stay open.

Thank you for your help,
David Kristola
"Bow ties are cool!" -- The Doctor