Skip to content

Commit 466b432

Browse files
committed
[clang-format] Only add pragma continuation indentation for 'omp' clauses
The patch in D136100 added custom handling for pragmas to assist in formatting OpenMP clauses correctly. One of these changes added extra indentation. This is desirable for OpenMP pragmas as they are several complete tokens that would otherwise we on the exact same line. However, this is not desired for the other pragmas. This solution is extremely hacky, I'm not overly familiar with the `clang-format` codebase. A better solution would probably require actually parsing these as tokens, but I just wanted to propose a solution. Fixes #59473 Reviewed By: HazardyKnusperkeks Differential Revision: https://reviews.llvm.org/D144884
1 parent 849529b commit 466b432

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,8 +1273,13 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
12731273
return ContinuationIndent;
12741274
}
12751275

1276-
if (State.Line->InPragmaDirective)
1277-
return CurrentState.Indent + Style.ContinuationIndentWidth;
1276+
// OpenMP clauses want to get additional indentation when they are pushed onto
1277+
// the next line.
1278+
if (State.Line->InPragmaDirective) {
1279+
FormatToken *PragmaType = State.Line->First->Next->Next;
1280+
if (PragmaType && PragmaType->TokenText.equals("omp"))
1281+
return CurrentState.Indent + Style.ContinuationIndentWidth;
1282+
}
12781283

12791284
// This ensure that we correctly format ObjC methods calls without inputs,
12801285
// i.e. where the last element isn't selector like: [callee method];

clang/unittests/Format/FormatTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20560,6 +20560,21 @@ TEST_F(FormatTest, UnderstandsPragmas) {
2056020560
"(including parentheses).",
2056120561
format("#pragma mark Any non-hyphenated or hyphenated string "
2056220562
"(including parentheses)."));
20563+
20564+
EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
20565+
"(including parentheses).",
20566+
format("#pragma mark Any non-hyphenated or hyphenated string "
20567+
"(including parentheses)."));
20568+
20569+
EXPECT_EQ(
20570+
"#pragma comment(linker, \\\n"
20571+
" \"argument\" \\\n"
20572+
" \"argument\"",
20573+
format("#pragma comment(linker, \\\n"
20574+
" \"argument\" \\\n"
20575+
" \"argument\"",
20576+
getStyleWithColumns(
20577+
getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp), 32)));
2056320578
}
2056420579

2056520580
TEST_F(FormatTest, UnderstandsPragmaOmpTarget) {

0 commit comments

Comments
 (0)