Skip to content

Commit

Permalink
[clang-format] Don't break lines after pragma region
Browse files Browse the repository at this point in the history
We have autogenerated pragma regions in our code
which where awkwardly broken up like this:

```
#pragma region foo(bar : hello)
```
becomes

```
#pragma region foo(bar \
                   : hello)
```

This fixes the problem by adding region as a keyword
and handling it the same way as pragma mark

Reviewed By: curdeius

Differential Revision: https://reviews.llvm.org/D125961
  • Loading branch information
tru committed May 20, 2022
1 parent 5450db5 commit 749fb33
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 2 additions & 0 deletions clang/lib/Format/FormatToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,7 @@ struct AdditionalKeywords {
kw___has_include_next = &IdentTable.get("__has_include_next");

kw_mark = &IdentTable.get("mark");
kw_region = &IdentTable.get("region");

kw_extend = &IdentTable.get("extend");
kw_option = &IdentTable.get("option");
Expand Down Expand Up @@ -1053,6 +1054,7 @@ struct AdditionalKeywords {

// Pragma keywords.
IdentifierInfo *kw_mark;
IdentifierInfo *kw_region;

// Proto keywords.
IdentifierInfo *kw_extend;
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1251,9 +1251,9 @@ class AnnotatingParser {
void parsePragma() {
next(); // Consume "pragma".
if (CurrentToken &&
CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option)) {
CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option, Keywords.kw_region)) {
bool IsMark = CurrentToken->is(Keywords.kw_mark);
next(); // Consume "mark".
next();
next(); // Consume first token (so we fix leading whitespace).
while (CurrentToken) {
if (IsMark || CurrentToken->Previous->is(TT_BinaryOperator))
Expand Down
7 changes: 7 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19597,6 +19597,13 @@ TEST_F(FormatTest, UnderstandPragmaOption) {
EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A"));
}

TEST_F(FormatTest, UnderstandPragmaRegion) {
auto Style = getLLVMStyleWithColumns(0);
verifyFormat("#pragma region TEST(FOO : BAR)", Style);

EXPECT_EQ("#pragma region TEST(FOO : BAR)", format("#pragma region TEST(FOO : BAR)", Style));
}

TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
FormatStyle Style = getLLVMStyleWithColumns(20);

Expand Down

0 comments on commit 749fb33

Please sign in to comment.