Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting IndentWidth doesn't work in clang-format

I have .clang-format in my home directory and set indentwidth 4 as following.

BasedOnStyle:  LLVM
Standard:        Cpp11
IndentWidth:     4   
TabWidth:        4   
UseTab:          Never 

But when I use clang-format -style='~/.clang-format' a.cpp to format my code, the indent width becomes 2. like:

// See this indent width 2. The original file has width 4, 
// but clang-format changes it to width 2.
int main(int argc, char const *argv[]) {
  A a;  
  a.bar();

The output of clang-format --version is

LLVM (http://llvm.org/):
  LLVM version 3.4.2
  Optimized build.
  Default target: x86_64-unknown-linux-gnu
  Host CPU: core-avx-i

How can I let clang-format format my code (.h,.c,..) with indent width 4?

like image 715
Peng Zhang Avatar asked Nov 04 '14 16:11

Peng Zhang


People also ask

How do I set up clang format?

You can install clang-format and git-clang-format via npm install -g clang-format . To automatically format a file according to Electron C++ code style, run clang-format -i path/to/electron/file.cc . It should work on macOS/Linux/Windows.

Does clang format work for C?

clang-format is located in clang/tools/clang-format and can be used to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# code.

Where is clang format config file?

Configuring Style with clang-format clang-format file located in the closest parent directory of the input file.


1 Answers

http://clang.llvm.org/docs/ClangFormat.html

The -style option does not take a file path. It takes the string file to indicate the use of a .clang-format file, and it looks for that file in the parent directories of the file being processed, or the working directory and its parent directories when transforming stdin.

You can also give it a string that directly sets the options you want:

clang-format -style="{IndentWidth: 4,TabWidth: 4}"

You can also use the -dump-config option to check the config.


-style='~/.clang-format'

Using ~ to refer to your home directory generally relies on shell globbing. The shell won't do that for you inside an argument like this. So even if -style did take a path, this wouldn't produce the right path.

like image 108
bames53 Avatar answered Oct 20 '22 03:10

bames53