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

后端 未结 9 2133
眼角桃花
眼角桃花 2021-01-29 21:43

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 us

相关标签:
9条回答
  • 2021-01-29 21:48

    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.

    shopt -s globstar extglob failglob && ls **/*.@(h|hpp|hxx|c|cpp|cxx) | grep -v build | tr '\n' ' ' | xargs clang-format -i
    
    0 讨论(0)
  • 2021-01-29 21:51

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

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

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

    See here for additional discussion.

    0 讨论(0)
  • 2021-01-29 21:51

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

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

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

    Features:

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

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

    0 讨论(0)
  • 2021-01-29 21:51

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

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

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

    # 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"
    
    0 讨论(0)
  • 2021-01-29 22:00

    In modern bash you can recursively crawl the file tree

    for file_name in ./src/**/*.{cpp,h,hpp}; do
        if [ -f "$file_name" ]; then
            printf '%s\n' "$file_name"
            clang-format -i $file_name
        fi
    done
    

    Here the source is assumed to be located in ./src and the .clang-format contains the formatting information.

    0 讨论(0)
  • 2021-01-29 22:02

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

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

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

    Note2: The script operates in the current working directory

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

    0 讨论(0)
提交回复
热议问题