how clang-tidy finds clang-format file


I have a question:
Do you know maybe how to make clang-tidy in version 4.0 use formatting style for fixes (-fix) from .clang-format file?

I tried in many different ways but I failed.

Documentation mentions ‘clang-format config file to be found’ but anywhere I put it, it is still not used by clang-tidy also if I put in in location pointed by ‘-p’ option.

-style=<string>              -
                                 Fallback style for reformatting after inserting fixes
                                 if there is no clang-format config file found.

I have no idea where I should put .clang-format file so it could be used by clang-tidy for applying clang-tidy fixes.

Thank you in advance.

Hi Adam,
it should look for “.clang-format” from the place where you run clang-tidy. I hope it helps.



Thank you for the answer.

It was my first guess and I was surprised that it did not work.

I have to use ‘-export-fixes=’ so it works. Then the fixes are written into json file and ‘clang-apply-replacements -format -style=file’ does the job.
But it does want to work without this exporting.

In version 5.0 I just use option ‘-format-style=file’ and it uses .clang-format file in the closest parent directory as described in the documentation:

But in version 4.0 there is only option “-style” which seems to not offer such option and does not work with .clang-format from the folder I run clang-tidy.

-style=<string>              -
                                 Fallback style for reformatting after inserting fixes
                                 if there is no clang-format config file found.

I am not sure why it it like that.

Best regards,

This probably not gonna solve the issue, but have you tried (extra/clang-tidy/tools/ It export the fixes and applies them automatically, and runs on multiple threads.



Thank you for the response.

I tried ‘’ and it started to work but I had to add one additional line to the script: ‘invocation.append(’-style=file’)’ so it takes .clang-format from the current directory otherwise it used some default formatting:

84 def apply_fixes(args, tmpdir):
85 “”“Calls clang-apply-fixes on a given directory. Deletes the dir when done.”“”
86 invocation = [args.clang_apply_replacements_binary]
87 if args.format:
88 invocation.append(‘-format’)
89 invocation.append(‘-style=file’)
90 invocation.append(tmpdir)
92 shutil.rmtree(tmpdir)

Thanks for that.

I do not know why it does not work just without using ‘clang-apply-replacements’ as an intermediate step and it does not apply .clang-format.

Can it be a bug in the version 4.0?

Do you have any other ideas how I could get it working? Or someone who could help?

I will try maybe to build it from source on a different platform.
In the last resort I will have to take a look at the source code and check.

Thank you in advance.

Best regards,

Hello again,

I took a look at the code and based on my research in version 4.0 there is no code to apply formatting for fixes.
It was added in the following commit:
[clang-tidy] Format code around applied fixes

Add -format option (disabled by default for now) to trigger formatting
of replacements.

In this commit: [clang-tidy] Make format style customizable:

There was added documentation which was misleading because format could not be applied - it was only found but not used:


  • -style= -
  • Fallback style for reformatting after inserting fixes
  • if there is no clang-format config file found.

When I added the fragment of the missing code in version 4.0 and recompiled from source it started to work as expected.

This was the missing part in ‘clang-tidy/ClangTidy.cpp in finish method of the ErrorReporter class’:

  • if (llvm::Expectedtooling::Replacements FormattedReplacements =
  • format::formatReplacements(Code, *Replacements, *Style)) {
  • Replacements = std::move(FormattedReplacements);
  • if (!Replacements)
  • llvm_unreachable(“!Replacements”);
  • } else {
  • llvm::errs() << llvm::toString(FormattedReplacements.takeError())
  • << “. Skipping formatting.\n”;
  • }

Please confirm my findings if you would be that kind.

Thank you in advance.

Best regards,