clang-format Visual Studio plugin and cmake

2k views Asked by At

I've installed the latest clang-format plugin for Visual Studio from http://llvm.org/builds/ and I'm trying to use it with a VS solution file that's generated from CMake. I've got the following directory structure:

./
├──project/
│  ├──.clang-format
│  └──source.cpp  #all sourcefiles are here
└──build/  # cmake build files

When I open the cmake-generated .sln file and press CTRL+R, CTRL+F, no formatting happens. When I set Tools->Options->LLVM/Clang->ClangFormat->Fallback Style to anything (e.g. 'LLVM') then it formats (to the default style), but not if I set fallback to 'none' - which means my .clang-format file doesn't get loaded.

I tried copying the .clang-format file all over the project directories, i.e. putting it in the project dir, sub-directories, next to the source files, in the build directory, in the build/x64 directory, etc. - but it just doesn't get picked up.

Where do I have to put the file so that it gets picked up by the plugin?

1

There are 1 answers

0
Ela782 On BEST ANSWER

I solved this, and it had nothing to do with where you put the .clang-format file. It's fine to just put it in the root of the source directory, as I did it.

Why it didn't format at all was that a .clang-format requires either the line BasedOnStyle: LLVM (or any other default style) or to define all required parameters.

If you just have a .clang-format file with a few lines, like for example:

---
ColumnLimit: '120'
IndentWidth: '4'
Language: Cpp
Standard: Cpp11
UseTab: Never

...

then it will not do any formatting whatsoever. Probably it can't work without a full set of rules, and in this case, it doesn't know to which default settings whatsoever it should fall back to.