11

clang-format currently moves all pragmas to the first column. An example before clang-format:

for (int i = 0; i < 4; ++i) {
  #pragma UNROLL
  // ...some code...
}

The same code after clang-format:

for (int i = 0; i < 4; ++i) {
#pragma UNROLL
  // ...some code...
}

Is there a way to have clang-format ignore pragma lines entirely without changing the source code (i.e. without cluttering the source with // clang-format off)? For example with a regular expression?

This is related to this question (I would like to avoid installing a third-party tool), and will hopefully be resolved by this bug report.


Additionally, while clang-format off is respected for the line with the pragma, the commented line itself will get indented to what the pragma would have been indented to (with clang-format 6.0.0):

for (int i = 0; i < 4; ++i) {
// clang-format off
  #pragma UNROLL
  // clang-format on
  // ...some code...
}
definelicht
  • 428
  • 2
  • 14

1 Answers1

0

If you have only few pragmas that you are using, you could try to disguise them as regular commands. According to compiler explorer, the following has the correct effect on the compiler, while the formatting is not destroyed by clang-format.

An additional benefit is, that you could use different definitions, depending on the compiler (as in this question)

#define UNROLL _Pragma("unroll")

void f()
{
    volatile int x = 0;
    UNROLL for (int i = 0; i < 100; ++i)
    {
        x = x + i;
    }
}
hofingerandi
  • 517
  • 4
  • 20