llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.78k stars 11.9k forks source link

request: clang-format to recurse over directories #62108

Open mcandre opened 1 year ago

mcandre commented 1 year ago

Please implement directory recursion for Clang-Format. This helps to quickly format large C/C++ project directories.

As a workaround, UNIX users can write a custom shell script. But that would force WSL for Windows C/C++ developers.

As a workaround, cmake users can use a file ... GLOB_RECURSE ... directive. But not all C/C++ users use cmake.

Really, I would like to see cpplint do this work automatically.

Start with the conventional filenames: .c, .cpp, .h, .hpp, and introduce other common file extensions such as .cc, .cxx, .hxx, case insensitivity, and so on.

llvmbot commented 1 year ago

@llvm/issue-subscribers-clang-format

mkurdej commented 1 year ago

Personally, I'm not really fond of adding this to clang-format. First of all, it's not its responsibility, let's focus on doing one thing but well. Secondly, there are other ways as you mentioned yourself. Have a look at https://stackoverflow.com/questions/28896909/how-to-call-clang-format-over-a-cpp-project-folder. I also wrote a python script to do what you ask for, it should work on all common platforms: https://github.com/mkurdej/run-clang-format. Just my 2 cents.

EugeneZelenko commented 1 year ago

@mkurdej: Will be good idea to include your scrip into LLVM repository, likerun-clang-tidy.

mkurdej commented 1 year ago

Sure, I will submit a patch.

mcandre commented 1 year ago

I don't want to depend on Python to run clang-format or clang-tidy.

Are you endorsing a particular solution mentioned on that Stack Overflow post? I've already seen that article. The answers consist of people explicitly complaining about the lack of recursion in clang-format, or else posting non-portable, broken shell commands. It's a lot of needless copy & paste for workarounds that vendor lock to specifically GNU/Linux, and are likely to break in BSD, let alone Windows environments.

EugeneZelenko commented 1 year ago

Clang-tidy is also not recursive, but run-clang-tidy is (using compile database).

mkurdej commented 1 year ago

Review: https://reviews.llvm.org/D149088.

cpp77 commented 6 months ago

What's the status on this? Last update was a year ago.

cbusquet-expressivee commented 1 month ago

what is the counter argument to have a -r or --recursive option ?

celestialorb commented 3 weeks ago

A formatter should really be able to format more than a single file at once and recurse into directories. I know that clang-format has a --files option, but that takes in a file with each filename one a single line, which still would require some sort of preprocessing to generate. Adding the ability to recurse through directories wouldn't modify any current behavior -- only extend it -- and is a common feature with modern formatters (see Black for comparison).

I also agree with @mcandre in that I don't want to depend on Python in order to format my C++ code.

It's not its responsibility, let's focus on doing one thing but well.

I don't see how adding the ability to recurse through a directory and format all found source files would prevent clang-format from "doing one thing but well".

mcandre commented 3 weeks ago

Here's a workaround:

https://github.com/mcandre/sail

The file pattern data is probably imperfect. Because I'm not a C/C++ expert. It's not my responsibility, but something that clang(-format) is positioned well to manage.

cpp77 commented 3 weeks ago

@mcandre Hi, in your first message you mentioned there's a CMake workaround.

I'm not great with CMake but I do use it. Would it be possible if you could include the example CMake lines in your original message to "use" Clang format?

mcandre commented 3 weeks ago

Here is my cmake configuration.

https://github.com/mcandre/snek/blob/master/CMakeLists.txt

Indeed, there is a cmake specific workaround. However, in practice many engineers do not use cmake for 100% of C/C++ projects. For example, many Linux centric components require autotools instead. Most Windows C/C++ projects use Visual Studio build systems directly without the cmake wrapper. Some Unix programmers use the compiler directly.