10
votes

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...
}
1

1 Answers

-2
votes

This could be the answer you are looking for:

https://github.com/MedicineYeh/p-clang-format

The lines that should be able to help you are these:


    # Replace "#pragma omp" by "//#pragma omp"
    sed -i 's/#pragma omp/\/\/#pragma omp/g' ./main.c
    # Do format
    clang-format ./main.c
    # Replace "// *#pragma omp" by "#pragma omp"
    sed -i 's/\/\/ *#pragma omp/#pragma omp/g' ./main.c