Skip to content

Commit edc929b

Browse files
committed
changed the logic to only ignore the body of a PP define
1 parent a0388ad commit edc929b

File tree

5 files changed

+71
-42
lines changed

5 files changed

+71
-42
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,18 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
739739
}
740740

741741
if (!DryRun) {
742-
Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, Spaces,
743-
State.Column + Spaces + PPColumnCorrection);
742+
if (Style.SkipMacroDefinition && CurrentState.IsPPDefineBody &&
743+
!Current.is(tok::comment)) {
744+
Whitespaces.addUntouchableToken(Current, State.Line->InPPDirective);
745+
} else {
746+
Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, Spaces,
747+
State.Column + Spaces + PPColumnCorrection);
748+
}
749+
}
750+
751+
if (Style.SkipMacroDefinition && Previous.is(tok::pp_define)) {
752+
CurrentState.NoLineBreak = true;
753+
CurrentState.IsPPDefineBody = true;
744754
}
745755

746756
// If "BreakBeforeInheritanceComma" mode, don't break within the inheritance
@@ -1888,6 +1898,7 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
18881898
NewState.NestedBlockIndent = NestedBlockIndent;
18891899
NewState.BreakBeforeParameter = BreakBeforeParameter;
18901900
NewState.HasMultipleNestedBlocks = (Current.BlockParameterCount > 1);
1901+
NewState.IsPPDefineBody = CurrentState.IsPPDefineBody;
18911902

18921903
if (Style.BraceWrapping.BeforeLambdaBody && Current.Next &&
18931904
Current.is(tok::l_paren)) {

clang/lib/Format/ContinuationIndenter.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ struct ParenState {
212212
ObjCSelectorNameFound(false), HasMultipleNestedBlocks(false),
213213
NestedBlockInlined(false), IsInsideObjCArrayLiteral(false),
214214
IsCSharpGenericTypeConstraint(false), IsChainedConditional(false),
215-
IsWrappedConditional(false), UnindentOperator(false) {}
215+
IsPPDefineBody(false), IsWrappedConditional(false),
216+
UnindentOperator(false) {}
216217

217218
/// \brief The token opening this parenthesis level, or nullptr if this level
218219
/// is opened by fake parenthesis.
@@ -349,6 +350,9 @@ struct ParenState {
349350
/// a chained conditional expression (e.g. else-if)
350351
bool IsChainedConditional : 1;
351352

353+
/// \brief true if in pre-processor define body
354+
bool IsPPDefineBody : 1;
355+
352356
/// \brief true if there conditionnal was wrapped on the first operator (the
353357
/// question mark)
354358
bool IsWrappedConditional : 1;
@@ -402,6 +406,8 @@ struct ParenState {
402406
return IsCSharpGenericTypeConstraint;
403407
if (IsChainedConditional != Other.IsChainedConditional)
404408
return IsChainedConditional;
409+
if (IsPPDefineBody != Other.IsPPDefineBody)
410+
return IsPPDefineBody;
405411
if (IsWrappedConditional != Other.IsWrappedConditional)
406412
return IsWrappedConditional;
407413
if (UnindentOperator != Other.UnindentOperator)

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,9 +1356,6 @@ unsigned UnwrappedLineFormatter::format(
13561356
Indent != TheLine.First->OriginalColumn;
13571357
bool ShouldFormat = TheLine.Affected || FixIndentation;
13581358

1359-
if (Style.SkipMacroDefinition && TheLine.startsWith(tok::hash, tok::pp_define))
1360-
ShouldFormat = false;
1361-
13621359
// We cannot format this line; if the reason is that the line had a
13631360
// parsing error, remember that.
13641361
if (ShouldFormat && TheLine.Type == LT_Invalid && Status) {

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,14 +1138,6 @@ void UnwrappedLineParser::parsePPDefine() {
11381138
return;
11391139
}
11401140

1141-
if (Style.SkipMacroDefinition) {
1142-
do {
1143-
nextToken();
1144-
} while (!eof());
1145-
addUnwrappedLine();
1146-
return;
1147-
}
1148-
11491141
if (IncludeGuard == IG_IfNdefed &&
11501142
IncludeGuardToken->TokenText == FormatTok->TokenText) {
11511143
IncludeGuard = IG_Defined;
@@ -1165,7 +1157,15 @@ void UnwrappedLineParser::parsePPDefine() {
11651157
// guard processing above, and changes preprocessing nesting.
11661158
FormatTok->Tok.setKind(tok::identifier);
11671159
FormatTok->Tok.setIdentifierInfo(Keywords.kw_internal_ident_after_define);
1168-
nextToken();
1160+
1161+
if (Style.SkipMacroDefinition) {
1162+
do {
1163+
nextToken();
1164+
} while (!eof());
1165+
} else {
1166+
nextToken();
1167+
}
1168+
11691169
if (FormatTok->Tok.getKind() == tok::l_paren &&
11701170
!FormatTok->hasWhitespaceBefore()) {
11711171
parseParens();

clang/unittests/Format/FormatTest.cpp

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24223,17 +24223,24 @@ TEST_F(FormatTest, SkipMacroDefinition) {
2422324223
auto Style = getLLVMStyle();
2422424224
Style.SkipMacroDefinition = true;
2422524225

24226-
verifyNoChange("#define A", Style);
24226+
verifyFormat("#define A", "#define A", Style);
24227+
verifyFormat("#define A a aa", "#define A a aa", Style);
2422724228
verifyNoChange("#define A b", Style);
2422824229
verifyNoChange("#define A ( args )", Style);
2422924230
verifyNoChange("#define A ( args ) = func ( args )", Style);
24231+
verifyNoChange("#define A ( args ) { int a = 1 ; }", Style);
24232+
verifyNoChange("#define A ( args ) \\\n"
24233+
" {\\\n"
24234+
" int a = 1 ;\\\n"
24235+
"}",
24236+
Style);
2423024237

2423124238
verifyNoChange("#define A x:", Style);
2423224239
verifyNoChange("#define A a. b", Style);
2423324240

2423424241
// Surrounded with formatted code.
2423524242
verifyFormat("int a;\n"
24236-
"#define A a\n"
24243+
"#define A a\n"
2423724244
"int a;",
2423824245
"int a ;\n"
2423924246
"#define A a\n"
@@ -24242,21 +24249,15 @@ TEST_F(FormatTest, SkipMacroDefinition) {
2424224249

2424324250
// Columns are not broken when a limit is set.
2424424251
Style.ColumnLimit = 10;
24252+
verifyFormat("#define A a a a a", " # define A a a a a ", Style);
2424524253
verifyNoChange("#define A a a a a", Style);
2424624254

2424724255
Style.ColumnLimit = 15;
24248-
verifyNoChange("#define A //a very long comment", Style);
24249-
// in the following examples, since second line will not be formtted, it won't
24250-
// take into considertaion the alignment from the first line. The third line
24251-
// will follow the second line's alignment.
24252-
verifyFormat("int aaaaaa; // a\n"
24253-
"#define A // a\n"
24254-
"int a; // a",
24255-
"int aaaaaa; // a\n"
24256-
"#define A // a\n"
24257-
"int a; // a",
24258-
Style);
24259-
24256+
verifyFormat("#define A // a\n"
24257+
" // very\n"
24258+
" // long\n"
24259+
" // comment",
24260+
"#define A //a very long comment", Style);
2426024261
Style.ColumnLimit = 0;
2426124262

2426224263
// Multiline definition.
@@ -24288,12 +24289,6 @@ TEST_F(FormatTest, SkipMacroDefinition) {
2428824289
"#define A a\n"
2428924290
"#endif",
2429024291
Style);
24291-
verifyNoChange("#define UNITY 1\n"
24292-
"#if A\n"
24293-
"# define A a\\\n"
24294-
" a a\n"
24295-
"#endif",
24296-
Style);
2429724292
verifyFormat("#if A\n"
2429824293
"#define A a\n"
2429924294
"#endif",
@@ -24302,38 +24297,58 @@ TEST_F(FormatTest, SkipMacroDefinition) {
2430224297
"#endif",
2430324298
Style);
2430424299
Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
24300+
verifyNoChange("#if A\n"
24301+
"# define A a\n"
24302+
"#endif",
24303+
Style);
2430524304
verifyFormat("#if A\n"
2430624305
"# define A a\n"
2430724306
"#endif",
2430824307
"#if A\n"
24309-
" # define A a\n"
24308+
" #define A a\n"
2431024309
"#endif",
2431124310
Style);
2431224311
Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
2431324312
verifyNoChange("#if A\n"
2431424313
" #define A a\n"
2431524314
"#endif",
2431624315
Style);
24316+
verifyFormat("#if A\n"
24317+
" #define A a\n"
24318+
"#endif",
24319+
"#if A\n"
24320+
" # define A a\n"
24321+
"#endif",
24322+
Style);
2431724323

2431824324
Style.IndentPPDirectives = FormatStyle::PPDIS_None;
2431924325
// SkipMacroDefinition should not affect other PP directives
2432024326
verifyFormat("#if !defined(A)\n"
24321-
"# define A a\n"
24327+
"#define A a\n"
2432224328
"#endif",
2432324329
"#if ! defined ( A )\n"
24324-
" # define A a\n"
24330+
" #define A a\n"
2432524331
"#endif",
2432624332
Style);
2432724333

2432824334
// With comments.
24329-
verifyNoChange("/* */ # define A a // a a", Style);
24330-
verifyFormat("int a; // a\n"
24331-
"#define A // a\n"
24332-
"int aaa; // a",
24335+
verifyFormat("/* */ #define A a // a a", "/* */ # define A a // a a",
24336+
Style);
24337+
verifyNoChange("/* */ #define A a // a a", Style);
24338+
24339+
verifyFormat("int a; // a\n"
24340+
"#define A // a\n"
24341+
"int aaa; // a",
2433324342
"int a; // a\n"
2433424343
"#define A // a\n"
2433524344
"int aaa; // a",
2433624345
Style);
24346+
24347+
// multiline macro definitions
24348+
verifyNoChange("#define A a\\\n"
24349+
" A a \\\n "
24350+
" A a",
24351+
Style);
2433724352
}
2433824353

2433924354
TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {

0 commit comments

Comments
 (0)