3
votes

I'm on Ubuntu 18.04 with clang-format 9.

I've read the clang-format documentation where it says:

clang-format supports two ways to provide custom style options: directly specify style configuration in the -style= command line option or use -style=file and put style configuration in the .clang-format or _clang-format file in the project directory.

When using -style=file, clang-format for each input file will try to find the .clang-format file located in the closest parent directory of the input file. When the standard input is used, the search is started from the current directory.

No matter how I create the .clang_format file (I've tried with clang-format -style=google -dump-config > .clang_format) or where I put it, if I execute clang-format -style=file <file> it doesn't format anything.

Anyone that have the same issue?

For example, if I have a file hello.cpp:

#include <stdio>

std::string s=" VERY BAD"
"FORMATTING";

int   main()   {
  std::cout<< "Hello world!"<<'\n';
  return 0;


}

If I run:

$ clang-format -style=mozilla -dump-config > .clang_format

and even if I don't edit the .clang_format file, then

$ clang-format -style=file hello.cpp

I get the default LLVM formatting style instead of the Mozilla style:

#include <stdio>

std::string s = " VERY BAD"
                "FORMATTING";

int main() {
  std::cout << "Hello world!" << '\n';
  return 0;
}

but if I run $ clang-format -style=mozilla hello.cpp then I get

#include <stdio>

std::string s = " VERY BAD"
                "FORMATTING";

int
main()
{
  std::cout << "Hello world!" << '\n';
  return 0;
}

and the same happens if I move the previously generated .clang_format in the parent directory of the hello.cpp directory.

I've tried anything but it seems that I have to stick with preconfigured styles.

Anyone with the same problem?

Can I get some sort of logging from clang-format?

SOLUTION:

the name of the file must be .clang-format, not .clang_format!

2
I doubt anyone has memorized the subtle differences between Mozilla and LLVM style. What's supposed to be different, and did you actually verify the setting in your .clang-format?sweenish
I've edit the question with the code formatted accordingly to the Mozilla style. I used clang-format -dump-config so I assume that the .clang_format file should be correct. I swear I've tried any possible combination of .clang_format file / settingsceccoemi

2 Answers

2
votes

I was creating a configuration file with a wrong name.

It must be named .clang-format, not .clang_format.

1
votes

clang-format -i <file> is sufficient if you've placed your .clang-format file at the project root. The -i stands for in place. The commands you paste should have spit a formatted file out to standard output. The reason for this is that clang-format won't alter your file by default. Seems weird for a formatter at first, but it's a good safety precaution, in my opinion.