C++ 如何通过 cpp 项目文件夹调用 clang-format?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28896909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 21:06:25  来源:igfitidea点击:

How to call clang-format over a cpp project folder?

c++clangclang-format

提问by user3639557

Is there a way to call something like clang-format --style=Webkitfor an entire cpp project folder, rather than running it separately for each file?

有没有办法clang-format --style=Webkit为整个 cpp 项目文件夹调用类似的东西,而不是为每个文件单独运行它?

I am using clang-format.pyand vimto do this, but I assume there is a way to apply this once.

我正在使用clang-format.pyvim执行此操作,但我认为有一种方法可以将其应用一次。

采纳答案by sbarzowski

What about:

关于什么:

clang-format -i -style=WebKit *.cpp *.h

in the project folder. The -i option makes it inplace (by default formatted output is written to stdout).

在项目文件夹中。-i 选项使其就地(默认情况下格式化输出写入标准输出)。

回答by Antimony

Unfortunately, there is no way to apply clang-format recursively. *.cppwill only match files in the current directory, not subdirectories. Even **/*doesn't work.

不幸的是,没有办法递归地应用 clang-format。*.cpp只会匹配当前目录中的文件,而不是子目录。甚至**/*不起作用。

Luckily, there is a solution: grab all the file names with the findcommand and pipe them in. For example, if you want to format all .hand .cppfiles in the directory foo/bar/recursively, you can do

幸运的是,有一个解决方案:使用find命令获取所有文件名并通过管道将它们导入。例如,如果您想递归地格式化目录中的所有.h.cpp文件foo/bar/,您可以这样做

find foo/bar/ -iname *.h -o -iname *.cpp | xargs clang-format -i

See herefor additional discussion.

有关其他讨论,请参见此处

回答by Alexander

First create a .clang-formatfile if it doesn't exist:

.clang-format如果文件不存在,首先创建一个文件:

clang-format -style=WebKit -dump-config > .clang-format

Choose whichever predefined style you like, or edit the resulting .clang-formatfile.

选择您喜欢的任何预定义样式,或编辑生成的.clang-format文件。

Then run:

然后运行:

find . -regex '.*\.\(cpp\|hpp\|cc\|cxx\)' -exec clang-format -style=file -i {} \;

Other file extensions than cpp, hpp, ccand cxxcan be used in the regular expression, just make sure to separate them with \|.

其他文件扩展名比cpphppcc并且cxx可以在正则表达式中使用,只要确保他们分开\|

回答by Julius Bullinger

I recently found a bash-script which does exactly what you need:

我最近发现了一个 bash 脚本,它完全符合您的需求:

https://github.com/eklitzke/clang-format-all

https://github.com/eklitzke/clang-format-all

This is a bash script that will run clang-format -ion your code.

Features:

  • Finds the right path to clang-formaton Ubuntu/Debian, which encode the LLVM version in the clang-formatfilename
  • Fixes files recursively
  • Detects the most common file extensions used by C/C++ projects

这是一个将clang-format -i在您的代码上运行的 bash 脚本。

特征:

  • clang-format在 Ubuntu/Debian 上找到正确的路径,在clang-format文件名中对 LLVM 版本进行编码
  • 递归修复文件
  • 检测 C/C++ 项目使用的最常见的文件扩展名

On Windows, I used it successfully in Git Bash and WSL.

在 Windows 上,我在 Git Bash 和 WSL 中成功使用了它。

回答by Tim Meyer

For the Windows users: If you have Powershell 3.0 support, you can do:

对于 Windows 用户:如果您有 Powershell 3.0 支持,您可以执行以下操作:

Get-ChildItem -Path . -Directory -Recurse |
    foreach {
        cd $_.FullName
        &clang-format -i -style=WebKit *.cpp
    }

Note1: Use pushd .and popdif you want to have the same current directory before and after the script

注1:使用pushd .popd,如果你想之前和脚本后,具有相同的当前目录

Note2: The script operates in the current working directory

注2:脚本在当前工作目录下运行

Note3: This can probably be written in a single line if that was really important to you

注3:如果这对您来说真的很重要,这可能可以写在一行中

回答by marcelosalloum

I'm using the following command to format all objective-C files under the current folder recursively:

我正在使用以下命令递归地格式化当前文件夹下的所有 Objective-C 文件:

$ find . -name "*.m" -o -name "*.h" | sed 's| |\ |g' | xargs clang-format -i

I've defined the following alias in my .bash_profileto make things easier:

我在 my 中定义了以下别名.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"

回答by Bim

Here is a solution that searches recursively and pipes all files to clang-format as a file list in one command. It also excludes the "build" directory (I use CMake), but you can just omit the "grep" step to remove that.

这是一个解决方案,它在一个命令中递归搜索并将所有文件作为文件列表通过管道传输到 clang-format。它还排除了“build”目录(我使用 CMake),但您可以省略“grep”步骤来删除它。

shopt -s globstar extglob failglob && ls **/*.@(h|hpp|hxx|c|cpp|cxx) | grep -v build | tr '\n' ' ' | xargs clang-format -i