Home  >  Q&A  >  body text

clang-format 如何缩进 c++ 中的 pragma?

比如,

cpp#include <omp.h>
#include <cstdio>
int main()
{
#pragma omp parallel for
    for (int i = 0; i < 10; ++i)
    {
        puts("demo");
    }
    return 0;
}

希望用 clang-format 格式化成:

cpp#include <omp.h>
#include <cstdio>
int main()
{
    #pragma omp parallel for
    for (int i = 0; i < 10; ++i)
    {
        puts("demo");
    }
    return 0;
}
黄舟黄舟2715 days ago767

reply all(2)I'll reply

  • 迷茫

    迷茫2017-04-17 12:02:08

    Clang-format is not currently supported.

    For this somewhat special formatting requirement, clang-format provides a one-size-fits-all solution:

    cpp#include <omp.h>
    #include <cstdio>
    int main() {
    // clang-format off
      #pragma omp parallel for
      // clang-format on
      for (int i = 0; i < 10; ++i) {
        puts("demo");
      }
      return 0;
    }
    

    As shown above, between the // clang-format off and // clang-format on switches, you can keep whatever format you want, clang-format does not matter.

    reply
    0
  • 阿神

    阿神2017-04-17 12:02:08

    You won’t have to worry if you write this way

    #include <omp.h>
    #include <cstdio>
    int main()
    {
    #   pragma omp parallel for
        ...
    }
    

    reply
    0
  • Cancelreply