c++clangclang-format

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


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

I am using clang-format.py and vim to do this, but I assume there is a way to apply this once.


Solution

  • Most efficient solution:

    clang-format -i -- **.cpp **.h **.hpp
    

    in the project folder. This edited version works with subdirectories, too.

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

    As opposed to xargs-based solutions and "bash for loop" solutions, this one will format all files with a single clang-format invocation. It's much more efficient if you have many files.


    Alternatively, if you have thousands of files and they don't fit into your operating system-s maximum command length (think 100K-2M total characters), you can use a pipe like this:

    find . -iname '*.h' -o -iname '*.cpp' -o -iname '*.hpp' | clang-format --style=file -i --files=/dev/stdin
    

    This will also only use a single clang invocation, but it's more verbose. Solution 1 is recommended.