c++ - 设置 IndentWidth 在 clang 格式中不起作用

标签 c++ clang-format

我的主目录中有 .clang-format 并设置 indentwidth 4 如下。

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

但是当我使用 clang-format -style='~/.clang-format' a.cpp 格式化我的代码时,缩进宽度变为 2。例如:

// 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();

clang-format --version 的输出是

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

如何让 clang-format 格式化我的代码(.h,.c,..)缩进宽度为 4?

最佳答案

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

-style 选项不采用文件路径。它使用字符串 file 来指示使用.clang 格式的文件,并在正在处理的文件的父目录中查找该文件,或者在转换时在工作目录及其父目录中查找该文件标准输入。

你也可以给它一个字符串,直接设置你想要的选项:

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

您还可以使用 -dump-config 选项来检查配置。


-style='~/.clang-format'

使用 ~ 来引用您的主目录通常依赖于 shell globbing。 shell 不会在这样的参数中为您做那件事。因此,即使 -style 确实选择了路径,也不会产生正确的路径。

关于c++ - 设置 IndentWidth 在 clang 格式中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26740500/

相关文章:

python - 用于通过具有析构函数的 C++ 类的值对象返回的函数的 ctypes 包装器

clang-format 可以破坏我的代码吗?

c++ - 设置回调函数,它是类的非静态成员函数

c++ - 将 Windows 命名管道与 ffmpeg 管道一起使用

coding-style - 如何在打开函数的大括号之前使 clang-format 添加新行?

c++ - 嵌套结构字段的 Clang 格式规则

clang-format - 如何告诉 clang-format 不要在一行上制作短的 lambda 表达式?

Xcode 8扩展执行NSTask

c++ - Class中的Const Static Function Pointer~如何初始化呢?

c++ - 如何将流读入 Concurrency::streams::streambuf<uint8_t> 缓冲区;在 C++ 中