Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using clang-format with c++20 concepts

I have looked at the clang-format style options https://clang.llvm.org/docs/ClangFormatStyleOptions.html but don't see any reference to c++ concepts and requires clauses. Normally I can configure clang-format to do what I want but I can't figure out how to get it to handle my concepts and requires clauses nicely:

  1. Currently clang-format does this to my concepts:
template <typename F, typename P, typename T>
concept Accumulate_Fn = Parser<P>&& std::invocable<F, T, parser_t<P>>&&
    std::same_as<T, std::invoke_result_t<F, T, parser_t<P>>>;

But I would like to put each one constraint on its own line (as it does for function arguments that get too long) so that the result would look like:

template <typename F, typename P, typename T>
concept Accumulate_Fn = Parser<P> &&
                        std::invocable<F, T, parser_t<P>> &&
                        std::same_as<T, std::invoke_result_t<F, T, parser_t<P>>>;
  1. For a function with a requires clause, clang-format currently gives me:
template <Parser P1, Parser P2, typename T, Accumulate_Fn<P1, parser_t<P1>> F>
requires std::same_as<T, parser_t<P1>> constexpr Parser auto
separated_by(P1&& p1, P2&& p2, T&& init, F&& f)

But I would like something much closer to :

template <Parser P1, Parser P2, typename T, Accumulate_Fn<P1, parser_t<P1>> F>
requires std::same_as<T, parser_t<P1>> 
constexpr Parser auto separated_by(P1&& p1, P2&& p2, T&& init, F&& f)

Are there any magical options that will make that work? I'm currently on clang-format 10.0.

like image 891
ptb Avatar asked Jun 05 '20 15:06

ptb


People also ask

Does clang format work for C?

clang-format is located in clang/tools/clang-format and can be used to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# code.

How do I use clang format?

To automatically format a file according to Electron C++ code style, run clang-format -i path/to/electron/file.cc . It should work on macOS/Linux/Windows. The workflow to format your changed code: Make codes changes in Electron repository.

Where do I put the clang format?

clang-format file, we need to place it in the project folder or in any parent folder of the file you want to format. clang-format.exe searches for the config file automatically starting with the folder where the file you want to format is located, all the way to the topmost directory.

How do you customize your clang format?

clang-format supports two ways to provide custom style options: directly specify style configuration in the -style= command line option or use -style=file and put style configuration in the . clang-format or _clang-format file in the project directory.


1 Answers

As of Jul/20, Concepts are not properly supported by clang-format. There is an open issue in LLVM tracker.

like image 129
Eduardo Costa Avatar answered Oct 18 '22 15:10

Eduardo Costa