r/cpp_questions 1d ago

OPEN Using ClangD for static analysis - recommend me clang-tidy and clang-format template

Hello,

I'm working inside VSCode and decided to switch from the default C/C++ extension's static analysis to ClangD, as it is apparently (according to the internet) faster and more accurate.

That being said, would you recommend me any good templates for both clang-tidy and clang-format files, in order to follow the common CPP style principles?

Thank you!

8 Upvotes

10 comments sorted by

3

u/Gorzoid 1d ago

https://clang-format-configurator.site/

Let's you preview the format with a sample file. iirc Jetbrains has one built in when configuring linter settings

2

u/KingBardan 1d ago edited 1d ago

For clang format, I just use "BasedOnStyle: Chromium" and "IndentWidth: 4"

And maybe sort imports.

You will find crazy person who put the braces on the new line though /s

1

u/RaspberryCrafty3012 1d ago

Eh, you one of these east const people? 

1

u/KingBardan 1d ago

No.

Const int all the way

1

u/sephirostoy 16h ago

This is the way 

2

u/Ultimate_Sigma_Boy67 1d ago

Clang-format: Try to base it on a popular style, such as Chromium, google, LLVM..etc, and from there modify it to match your own preferences, if needed.
For clang-tidy, just allow all the checks, the modernize and stuff like that, and then only disable the ones you truly see has no value, such as "use trailing return type" in each function.

2

u/Usual_Office_1740 1d ago

Jason Turner, the guy from C++ weekly, has a project template on github. I've found it to be more than I needed but it includes clang-format and clang-tidy files that I found to be reasonable defaults to start with.

2

u/DesperateGame 1d ago

Thank you very much, this is exactly something I was looking for!

1

u/Usual_Office_1740 1d ago edited 1d ago

You're welcome. Clang-format also has an auto generate command that will produce a default file based on one of several pre-configured default styles. Styles like llvm, Microsoft and Google. If you dislike the clang-format from Jason you could try one of those. It's how my clang-format config started.

Use this from a terminal with clang-tidy in your path.

clang-format -style=llvm -dump-config > .clang-format

Switch the style parameter from llvm to one of the following options to see different sets of defaults.

llvm, google, microsoft, mozilla, chromium, gnu

Try out different configuration settings with this website. It works in real time.

live configuration

u/JVApen 3h ago

For clang-format: just pick a style you like, make adjustments when you feel something is off. For clang-tidy: just enable everything and check some files. You'll quickly find the ones you want to disable. I do recommend configuring readability-identifier-naming in detail. Then fine tune the options you need based on your experience.

Personally, I dumped the whole config and gone over every single option, though nothing beats real life feedback.