Home > Net >  clang-format and special delimiters
clang-format and special delimiters

Time:10-18

I am using a special raw string delimiter in my code to format doc string, it looks something like

R"mydelimiter(
    some raw string
)mydelimiter"

Now, clang-format likes to produce the following

R "mydelimiter(
    some raw string
) mydelimiter "

which actually introduces a compilation error. I know that I can mark every block in my code with // clang-format on/off but I would like to find a more general solution.

Here is what I tried so far:

  • raw string format in my .clang-format
RawStringFormats:
  - Language: Cpp
    Delimiters: ['mydelimiter']
  • as well as several versions of regex macro blocks
MacroBlockBegin: 'R\"mydelimiter\('
MacroBlockEnd: '\)mydelimiter\",'

without any success. I am running clang-format version 14.0.6 and also I am running out of ideas :-/ help!

CodePudding user response:

As pointed out in the GitHub issue opened for this, the problem is in the GNU style option.

clang-format relies on the clang parser, but the clang parser may give different results for different versions of the C standard.

The GNU style option sets the C standard used for parsing to C 03, a time where raw string literals did not exist, therefore the parser does not correctly recognize them as such.

The standard set by GNU can be overridden in your config by adding Standard: Latest, or just specifying any standard version 11 or higher

  • Related