llvm-project/clang/lib/Format
Joseph Huber 037669de8b [clang-format] Do not parse certain characters in pragma directives
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.

Reviewed By: HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D136100
2022-10-18 16:38:19 -05:00
..
.clang-format [clang-format][NFC] Reformat clang/lib/Format using 6257832bf9 2022-09-21 12:02:49 -07:00
AffectedRangeManager.cpp [clang-format][NFC] Insert/remove braces in clang/lib/Format/ 2022-05-24 19:06:04 -07:00
AffectedRangeManager.h
BreakableToken.cpp Use is_sorted (NFC) 2022-07-29 21:18:42 -07:00
BreakableToken.h [clang-format] Add option to control the spaces in a line comment 2021-02-01 22:48:50 +01:00
CMakeLists.txt [clang-format] Add MacroUnexpander. 2022-07-12 07:11:46 +00:00
ContinuationIndenter.cpp [clang-format] Do not parse certain characters in pragma directives 2022-10-18 16:38:19 -05:00
ContinuationIndenter.h [clang-format][NFC] Replace most of std::vector with SmallVector 2022-07-13 16:45:22 -07:00
DefinitionBlockSeparator.cpp [clang-format][NFC] Insert/remove braces in clang/lib/Format/ 2022-05-24 19:06:04 -07:00
DefinitionBlockSeparator.h [clang-format] Fix SeparateDefinitionBlocks issues 2022-01-24 14:23:20 +00:00
Encoding.h
Format.cpp [clang-format] Add support to remove unnecessary semicolons after function definition 2022-10-09 11:18:16 +01:00
FormatInternal.h
FormatToken.cpp [clang] adds unary type transformations as compiler built-ins 2022-08-22 03:03:32 +00:00
FormatToken.h [clang] adds unary type transformations as compiler built-ins 2022-08-22 03:03:32 +00:00
FormatTokenLexer.cpp [Format] Fix crash when hitting eof while lexing JS template string 2022-10-06 17:00:41 +02:00
FormatTokenLexer.h [clang-format] Handle Verilog numbers and operators 2022-07-29 00:38:29 +00:00
MacroCallReconstructor.cpp [clang-format][NFC] Reformat clang/lib/Format using 6257832bf9 2022-09-21 12:02:49 -07:00
MacroExpander.cpp [clang-format] Elide unnecessary braces. NFC. 2022-02-02 14:36:01 +01:00
Macros.h [clang-format][NFC] Replace most of std::vector with SmallVector 2022-07-13 16:45:22 -07:00
NamespaceEndCommentsFixer.cpp [clang-format] Fix namespace format when the name is followed by a macro 2022-03-10 15:00:32 -08:00
NamespaceEndCommentsFixer.h
QualifierAlignmentFixer.cpp Remove redundant initialization of Optional (NFC) 2022-08-20 21:18:28 -07:00
QualifierAlignmentFixer.h [clang-format] Fix a crash (assertion) in qualifier alignment when matching template closer is null 2022-01-06 19:40:39 +00:00
SortJavaScriptImports.cpp [clang-format][NFC] Insert/remove braces in clang/lib/Format/ 2022-05-24 19:06:04 -07:00
SortJavaScriptImports.h
TokenAnalyzer.cpp [clang-format] Skip token annotation in passes that don't need it 2022-09-18 11:19:40 -07:00
TokenAnalyzer.h [clang-format] Skip token annotation in passes that don't need it 2022-09-18 11:19:40 -07:00
TokenAnnotator.cpp [clang-format] Do not parse certain characters in pragma directives 2022-10-18 16:38:19 -05:00
TokenAnnotator.h [clang-format] Do not parse certain characters in pragma directives 2022-10-18 16:38:19 -05:00
UnwrappedLineFormatter.cpp [clang-format] Fix mis-attributing preprocessor directives to macros 2022-10-10 19:57:58 -07:00
UnwrappedLineFormatter.h [NFC][clang-format] Fix typo 2022-08-29 09:34:05 +08:00
UnwrappedLineParser.cpp [clang-format] Do not parse certain characters in pragma directives 2022-10-18 16:38:19 -05:00
UnwrappedLineParser.h [clang-format] Do not parse certain characters in pragma directives 2022-10-18 16:38:19 -05:00
UsingDeclarationsSorter.cpp [clang-format] Use ranged for loops. NFC. 2022-02-01 14:10:48 +01:00
UsingDeclarationsSorter.h
WhitespaceManager.cpp [clang-format] Fix alignment in #else preprocessor blocks 2022-09-27 15:41:09 -04:00
WhitespaceManager.h [clang-format] Minimize the damage caused by AlignArrayOfStructures when working on non square arrays 2022-03-12 17:22:31 +00:00