[PATCH] D136100: [clang-format] Do not parse certain characters in pragma directives

Joseph Huber via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 17 11:36:50 PDT 2022


jhuber6 created this revision.
jhuber6 added reviewers: jdoerfert, JonChesterfield, ronlieb, MyDeveloperDay, owenpan, HazardyKnusperkeks, curdeius, wanders.
Herald added a project: All.
jhuber6 requested review of this revision.
Herald added subscribers: cfe-commits, sstefan1.
Herald added a project: clang.

Currently, we parse lines inside of a compiler `#pragma` the same way we
parse any other line. This is fine for some cases, like separating
expressions and adding proper spacing, but in others it causes some poor
results from miscategorizing some tokens.

For example, the OpenMP offloading uses certain clauses that contain
special characters like `map(tofrom : A[0:N])`. This will be formatted
poorly as it will be split between lines on the first colon.
Additionally the subscript notation will lead to poor spacing. This can
be seen in the OpenMP tests as the automatic clang formatting with
inevitably ruin the formatting.

For example, the following contrived example will be formatted poorly.

  #pragma omp target teams distribute collapse(2) map(to: A[0 : M * K])  \
      map(to: B[0:K * N]) map(tofrom:C[0:M*N]) firstprivate(Alpha) \
      firstprivate(Beta) firstprivate(X) firstprivate(D) firstprivate(Y) \
      firstprivate(E) firstprivate(Z) firstprivate(F)

This results in this when formatted, which is far from ideal.

  #pragma omp target teams distribute collapse(2) map(to                         \
                                                      : A [0:M * K])             \
      map(to                                                                     \
          : B [0:K * N]) map(tofrom                                              \
                             : C [0:M * N]) firstprivate(Alpha)                  \
          firstprivate(Beta) firstprivate(X) firstprivate(D) firstprivate(Y)     \
              firstprivate(E) firstprivate(Z) firstprivate(F)

This patch seeks to improve this by adding extra logic where the parsing goes
awry. This is primarily caused by the colon being parsed as an inline-asm
directive and the brackes an objective-C expressions. Also the line gets
indented every single time the line is dropped.

This doesn't implement true parsing handling for OpenMP statements.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136100

Files:
  clang/lib/Format/ContinuationIndenter.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/lib/Format/TokenAnnotator.h
  clang/lib/Format/UnwrappedLineParser.cpp
  clang/lib/Format/UnwrappedLineParser.h
  clang/unittests/Format/FormatTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136100.468278.patch
Type: text/x-patch
Size: 6145 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221017/3a37724d/attachment.bin>


More information about the cfe-commits mailing list