The ultimate goal of engineering is minimizing the amount of effort human beings have to put into routine tasks. However, many engineers tend to forget about that spending time manually enforcing formatting rules, trying to figure out whether allocated memory is released correctly and a whole set of other depressing things.

I would like people to stop doing that. Engineers should start using tools for automating routine and spend their time designing and implementing systems instead of doing monkey business. The problem is not only that people lose their time doing meaningless work, but also that it is extremely hard to do that right. Nobody really remembers every rule in his project’s Code Style and it’s not easy to ensure that resources are always released after usage in codebases with over a million source lines of code. Machines never forget and it is only a matter of time for them to process the whole project.

I’ll go through a list of Clang-based tools, highlight their features and outline the reasons why everybody should start using them. The reason I write about Clang-based tools is because I either am heavily involved into their development or use them very very often.

clang-format

clang-format is an extremely useful tool for code formatting. Each project has a certain Code Style, which defines formatting rules and recommendations to the contributors. However, it’s really hard to enforce these formatting rules manually. In fact, it is really easy to miss something.

clang-format can use different styles for formatting. It comes with a pre-defined set of coding styles from which you can choose: LLVM, Google, Chromium, Mozilla, WebKit. You can define your own style configuring the options clang-format provides to create a custom setting. However, you probably won’t be able to define a completely different style, because clang-format supports a limited set of options gaining extreme accuracy from that.

Let’s look at the example of how clang-format would work on poorly formatted code.

Isn’t it amazing?

You can also use clang-format in Vim or Emacs after a trivial setup.

clang-format documentation.

clang-tidy

clang-tidy is a Clang-based linter. It uses the whole power of static analysis to diagnose programming errors and help users to get rid of bugs in their projects.

It’s hard to write even a short summary of what clang-tidy is capable of! It can prevent potential memory leaks, enforce CppCoreGuidelines rules, modernize your code, improve readability, fix bugs, warn about suspicious pieces of code and outline possible vulnerabilities. See the complete list of checks.

clang-tidy is a libTooling-based tool, which means it uses a compilation database. It helps perform more in-depth analysis (e.g. clang-tidy is aware of which defines you use etc).

Let’s see what clang-tidy can detect!

#include <string>

int main() {
  // Example 1.
  std::string a("Foo"), b("Baz");
  for (int i = 0; i < 20000; ++i) {
    a = a + "Bar" + b;
  //    ^ Inefficient std::string concatenation. Warning here.
  }
  // Example 2.
  const char *Test[] = {"line 1",
                        "line 2" // Missing comma! Warning here.
                        "line 3",
                        "line 4", "line 5"};
  // Example 3.
  int x = 1;
  if ((x+1) | (x+1))    // (x+1) is redundant. Warning here.
    x++;
  if (x - x + 1 == 12)  // x - x is always zero. Warning here.
    x++;
  // Example 4.
  if (true);  // <-- Is this semicolon intended? Warning here.
    x++;
  return 0;
}

Are you still here? Go check what clang-tidy thinks about your project!

clang-tidy documentation.

clang-include-fixer

clang-include-fixer helps you with managing your #includes. Did you forget to include something that you are trying to use? Did you miss namespace qualifiers for some type or function? clang-include-fixer is here to help!

clang-include-fixer can be also easily integrated into Vim and Emacs, so that you can press a couple of buttons to get this magic done for you!

clang-include-fixer documentation.

asan, msan, tsan

asan (AddressSanitizer), msan (MemorySanitizer) and tsan (ThreadSanitizer) are dynamic tools for detecting memory and thread issues, such as uses after free, out of bounds access, double free, memory leaks, uninitialized reads and data races. They can prevent thousands of potential vulnerabilities and bugs.

These tools found hundreds of bugs in Chrome, Mozilla and GNU projects. See GNU bugs found by AddressSanitizer list for an example.

And here’s what AddressSanitizer found in xxHash: Issue #35.

There’s also KernelAddresSanitizer (kasan), which is a solution for finding use-after-free and out-of-bounds bugs in Linux kernel. See the list of bugs kasan found in the Linux kernel.

asan documentation.

msan documentation.

tsan documentation.

clang-rename

clang-rename is a simple tool for renaming variables, classes and structs, namespaces, functions and other kinds of entities in C and C++ code.

Yes, of course you can use sed for basic renamings, but it’s really easy to get something wrong. See demo for an example.

Unfortunately, clang-rename doesn’t support multiple translation units yet, which is a serious limitation, but I am working hard to fix that.

clang-rename documentation.

clang-refactor

We want to get more Clang-based refactoring tools. Because of that I am working on clang-refactor, which is meant to become an umbrella for various refactoring tools. These will include tools for inlining methods, extracting variables and methods, moving classes and functions across the project and namespaces, changing method’s signature and many others! I am very excited about this project and hope that everybody would benefit from it.

See clang-refactor proposal in cfe-dev mailing list to get more details.

The design doc is now published, too.

Not enough?

If you think there’s a tool we’re missing you can either make a request on LLVM Bugzilla or implement and submit it yourself! Check out Clang to get started.