Manoj Rao bio photo

Manoj Rao

Your Average Common Man

Email Twitter Github

Below I will share a bare minimum config with clang-format and clang-tidy to get started in a new project.

clang-format

clang-format is easily one of the most useful tools for C++ developers. If you still haven’t used it or heard of it, just whip up your favorite editor, type a hello_world.cc

Throw a couple of functions in with badly formatted code (of course, this is on purpose we never write bad looking code, right?). Now all you need is:

clang-format -i hello_world.cc

More generally, in a lager project:

find . -iname "*.cc" -o -iname "*.hpp" | xargs clang-format -i

And voila! your code gets automatically formatted to match the specifications set by default. I usually, have a small file with my favorite set of formatting specifications. I will share them here, but in general, I try to make my code look closest to the LLVM coding style with a few major exceptions, I keep all the function names, variable names everything else in lower case.

I have installed clang-format package in Emacs which I can then run on a buffer from within my Emacs. I can map it to a single key but I haven’t bothered doing it yet.

BasedOnStyle: LLVM

AccessModifierOffset: -4
AlwaysBreakTemplateDeclarations: true
ColumnLimit: 100
Cpp11BracedListStyle: true
IndentWidth: 4
MaxEmptyLinesToKeep: 2
PointerBindsToType: true
TabWidth: 4

clang-tidy

clang-tidy is one of the most versatile tools in the llvm tools package for C++ developers. For the most part, a whole lot of static analysis and other sanitizations can be run.

In most cases you use clang-format and clang-tidy together.

Checks: '-*,readability-identifier-naming'
CheckOptions:
  - { key: readability-identifier-naming.NamespaceCase,          value: lower_case }
  - { key: readability-identifier-naming.ClassCase,              value: lower_case }
  - { key: readability-identifier-naming.ClassSuffix,            value: _t         }
  - { key: readability-identifier-naming.TypeAliasSuffix,        value: _t         }
  - { key: readability-identifier-naming.PrivateMemberSuffix,    value: _          }
  - { key: readability-identifier-naming.StructCase,             value: lower_case }
  - { key: readability-identifier-naming.FunctionCase,           value: lower_case }
  - { key: readability-identifier-naming.VariableCase,           value: lower_case }
  - { key: readability-identifier-naming.GlobalConstantCase,  	 value: UPPER_CASE }
  - { key: readability-identifier-naming.TemplateParameterCase,  value: UPPER_CASE }

Bring Your Own Cause

If you think any info here has remotely helped you consider dropping a penny for this cause, just click me . You can visit https://www.bbc.com/news/world-asia-india-52672764 Unfortunately, there are plenty of sad things happening all over the world, if you have a different cause or charity you'd rather support please do. And if you did make a donation, please drop a note to me (annotated) or leave a comment here (anonymous is OK!) and I will use that as motivation to write more useful content here.

If you like topics such as this then please consider subscribing to my podcast. I talk to some of the stalwarts in tech and ask them what their favorite productivity hacks are:

Available on iTunes Podcast

Visit Void Star Podcast’s page on iTunes Podcast Portal. Please Click ‘Subscribe’, leave a comment.

Get it iTunes