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 }
My Podcast!
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.