有没有办法clang-format --style=Webkit为整个cpp项目文件夹调用类似的方法,而不是为每个文件单独运行它?
我正在使用clang-format.py并vim执行此操作,但是我认为有一种方法可以将其应用一次。
Answers:
关于什么:
clang-format -i -style=WebKit *.cpp *.h
在项目文件夹中。-i选项使它就位(缺省情况下,格式化的输出将写入stdout)。
**。
clang-tidy呢?
*.cpp生成文件列表应该可以工作。该clang-tidy命令允许传递多个文件。`用法:clang-tidy [选项] <source0> [... <sourceN>]`不过,实际上这并不是最好的方法。Clang-tidy进行了更深入的分析,因此它需要每个文件的编译器标记等。当我使用clang-tidy时,我通常有一个“编译数据库” – JSON文件,其中包含每个文件的命令以及一些脚本超过它。几年前,也许现在有了更好的方法。
不幸的是,没有办法递归地应用clang格式。*.cpp仅匹配当前目录中的文件,不匹配子目录。甚至**/*不起作用。
幸运的是,有一个解决方案:使用find命令获取所有文件名并将其通过管道输入。例如,如果要递归格式化所有文件.h和.cpp目录中的文件,则foo/bar/可以执行
find foo/bar/ -iname *.h -o -iname *.cpp | xargs clang-format -i
请参阅此处进行其他讨论。
**/*.cpp似乎在(合理的现代)bash中工作。您可能需要shopt -s globstar先。
find,并xargs应使用find ... -print0,并xargs -0 ...确保所有类型的文件名的正确处理。
find foo/bar/ -iname '*.h' -o -iname '*.cpp' | xargs clang-format -i将解决此问题。
.clang-format如果文件不存在,请首先创建一个文件:
clang-format -style=WebKit -dump-config > .clang-format
选择您喜欢的任何预定义样式,或编辑结果.clang-format文件。
clang格式的配置程序很有帮助。
然后运行:
find . -regex '.*\.\(cpp\|hpp\|cc\|cxx\)' -exec clang-format -style=file -i {} \;
其他文件扩展名比cpp,hpp,cc并且cxx可以在正则表达式中使用,只要确保他们分开\|。
-style=file有指定自定义文件路径的方法吗?我试过了-style=~/.clang-format,不起作用。
cp ~/.clang-format .想到的贫民窟解决方案是要创建一个函数,该函数首先运行,然后在您的答案中使用find命令。
我最近发现了一个bash脚本,它完全可以满足您的需求:
https://github.com/eklitzke/clang-format-all
这是一个bash脚本,将
clang-format -i在您的代码上运行。特征:
clang-format在Ubuntu / Debian上找到正确的路径,该路径以clang-format文件名编码LLVM版本- 递归修复文件
- 检测C / C ++项目使用的最常见文件扩展名
在Windows上,我在Git Bash和WSL中成功使用了它。
我正在使用以下命令以递归方式格式化当前文件夹下的所有Objective-C文件:
$ find . -name "*.m" -o -name "*.h" | sed 's| |\\ |g' | xargs clang-format -i
.bash_profile为了方便起见,我在其中定义了以下别名:
# Format objC files (*.h and *.m) under the current folder, recursively
alias clang-format-all="find . -name \"*.m\" -o -name \"*.h\" | sed 's| |\\ |g' | xargs clang-format -i"
clang-format-3.6 -i -style=file *.cpp *.h *.hpp