【问题标题】:Setting IndentWidth doesn't work in clang-format设置 IndentWidth 在 clang 格式中不起作用
【发布时间】:2014-11-04 16:45:10
【问题描述】:

我的主目录中有.clang-format,并将缩进宽度设置为 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?

【问题讨论】:

    标签: c++ clang-format


    【解决方案1】:

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

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

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

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

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


    -style='~/.clang-format'

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

    【讨论】:

    • 感谢-style=file的解释。我从没想过file 意味着文字file
    猜你喜欢
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 2022-07-12
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 2022-06-11
    相关资源
    最近更新 更多