Skip to content

Commit a7d7da6

Browse files
authored
[clang-format] Add SkipMacroDefinitionBody option (llvm#78682)
Closes llvm#67991. See also: llvm#70338 Co-authored-by: @tomekpaszek
1 parent 296fbee commit a7d7da6

File tree

7 files changed

+155
-0
lines changed

7 files changed

+155
-0
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4999,6 +4999,11 @@ the configuration (without a prefix: ``Auto``).
49994999
int bar; int bar;
50005000
} // namespace b } // namespace b
50015001

5002+
.. _SkipMacroDefinitionBody:
5003+
5004+
**SkipMacroDefinitionBody** (``Boolean``) :versionbadge:`clang-format 18` :ref:`<SkipMacroDefinitionBody>`
5005+
Do not format macro definition body.
5006+
50025007
.. _SortIncludes:
50035008

50045009
**SortIncludes** (``SortIncludesOptions``) :versionbadge:`clang-format 3.8` :ref:`<SortIncludes>`

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,7 @@ clang-format
12371237
- Add ``PenaltyBreakScopeResolution`` option.
12381238
- Add ``.clang-format-ignore`` files.
12391239
- Add ``AlignFunctionPointers`` sub-option for ``AlignConsecutiveDeclarations``.
1240+
- Add ``SkipMacroDefinitionBody`` option.
12401241

12411242
libclang
12421243
--------

clang/include/clang/Format/Format.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3932,6 +3932,10 @@ struct FormatStyle {
39323932
/// \version 13
39333933
unsigned ShortNamespaceLines;
39343934

3935+
/// Do not format macro definition body.
3936+
/// \version 18
3937+
bool SkipMacroDefinitionBody;
3938+
39353939
/// Include sorting options.
39363940
enum SortIncludesOptions : int8_t {
39373941
/// Includes are never sorted.
@@ -4895,6 +4899,7 @@ struct FormatStyle {
48954899
RequiresExpressionIndentation == R.RequiresExpressionIndentation &&
48964900
SeparateDefinitionBlocks == R.SeparateDefinitionBlocks &&
48974901
ShortNamespaceLines == R.ShortNamespaceLines &&
4902+
SkipMacroDefinitionBody == R.SkipMacroDefinitionBody &&
48984903
SortIncludes == R.SortIncludes &&
48994904
SortJavaStaticImport == R.SortJavaStaticImport &&
49004905
SpaceAfterCStyleCast == R.SpaceAfterCStyleCast &&

clang/lib/Format/Format.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,7 @@ template <> struct MappingTraits<FormatStyle> {
10851085
Style.RequiresExpressionIndentation);
10861086
IO.mapOptional("SeparateDefinitionBlocks", Style.SeparateDefinitionBlocks);
10871087
IO.mapOptional("ShortNamespaceLines", Style.ShortNamespaceLines);
1088+
IO.mapOptional("SkipMacroDefinitionBody", Style.SkipMacroDefinitionBody);
10881089
IO.mapOptional("SortIncludes", Style.SortIncludes);
10891090
IO.mapOptional("SortJavaStaticImport", Style.SortJavaStaticImport);
10901091
IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations);
@@ -1556,6 +1557,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
15561557
LLVMStyle.RequiresExpressionIndentation = FormatStyle::REI_OuterScope;
15571558
LLVMStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Leave;
15581559
LLVMStyle.ShortNamespaceLines = 1;
1560+
LLVMStyle.SkipMacroDefinitionBody = false;
15591561
LLVMStyle.SortIncludes = FormatStyle::SI_CaseSensitive;
15601562
LLVMStyle.SortJavaStaticImport = FormatStyle::SJSIO_Before;
15611563
LLVMStyle.SortUsingDeclarations = FormatStyle::SUD_LexicographicNumeric;

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,15 @@ void UnwrappedLineParser::parsePPDefine() {
11701170
assert((int)Line->PPLevel >= 0);
11711171
Line->InMacroBody = true;
11721172

1173+
if (Style.SkipMacroDefinitionBody) {
1174+
do {
1175+
FormatTok->Finalized = true;
1176+
nextToken();
1177+
} while (!eof());
1178+
addUnwrappedLine();
1179+
return;
1180+
}
1181+
11731182
if (FormatTok->is(tok::identifier) &&
11741183
Tokens->peekNextToken()->is(tok::colon)) {
11751184
nextToken();

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
184184
CHECK_PARSE_BOOL(ReflowComments);
185185
CHECK_PARSE_BOOL(RemoveBracesLLVM);
186186
CHECK_PARSE_BOOL(RemoveSemicolon);
187+
CHECK_PARSE_BOOL(SkipMacroDefinitionBody);
187188
CHECK_PARSE_BOOL(SpacesInSquareBrackets);
188189
CHECK_PARSE_BOOL(SpaceInEmptyBlock);
189190
CHECK_PARSE_BOOL(SpacesInContainerLiterals);

clang/unittests/Format/FormatTest.cpp

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24387,6 +24387,138 @@ TEST_F(FormatTest, WhitespaceSensitiveMacros) {
2438724387
verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style);
2438824388
}
2438924389

24390+
TEST_F(FormatTest, SkipMacroDefinitionBody) {
24391+
auto Style = getLLVMStyle();
24392+
Style.SkipMacroDefinitionBody = true;
24393+
24394+
verifyFormat("#define A", "#define A", Style);
24395+
verifyFormat("#define A a aa", "#define A a aa", Style);
24396+
verifyNoChange("#define A b", Style);
24397+
verifyNoChange("#define A ( args )", Style);
24398+
verifyNoChange("#define A ( args ) = func ( args )", Style);
24399+
verifyNoChange("#define A ( args ) { int a = 1 ; }", Style);
24400+
verifyNoChange("#define A ( args ) \\\n"
24401+
" {\\\n"
24402+
" int a = 1 ;\\\n"
24403+
"}",
24404+
Style);
24405+
24406+
verifyNoChange("#define A x:", Style);
24407+
verifyNoChange("#define A a. b", Style);
24408+
24409+
// Surrounded with formatted code.
24410+
verifyFormat("int a;\n"
24411+
"#define A a\n"
24412+
"int a;",
24413+
"int a ;\n"
24414+
"#define A a\n"
24415+
"int a ;",
24416+
Style);
24417+
24418+
// Columns are not broken when a limit is set.
24419+
Style.ColumnLimit = 10;
24420+
verifyFormat("#define A a a a a", " # define A a a a a ", Style);
24421+
verifyNoChange("#define A a a a a", Style);
24422+
24423+
Style.ColumnLimit = 15;
24424+
verifyFormat("#define A // a\n"
24425+
" // very\n"
24426+
" // long\n"
24427+
" // comment",
24428+
"#define A //a very long comment", Style);
24429+
Style.ColumnLimit = 0;
24430+
24431+
// Multiline definition.
24432+
verifyNoChange("#define A \\\n"
24433+
"Line one with spaces . \\\n"
24434+
" Line two.",
24435+
Style);
24436+
verifyNoChange("#define A \\\n"
24437+
"a a \\\n"
24438+
"a \\\n"
24439+
"a",
24440+
Style);
24441+
Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
24442+
verifyNoChange("#define A \\\n"
24443+
"a a \\\n"
24444+
"a \\\n"
24445+
"a",
24446+
Style);
24447+
Style.AlignEscapedNewlines = FormatStyle::ENAS_Right;
24448+
verifyNoChange("#define A \\\n"
24449+
"a a \\\n"
24450+
"a \\\n"
24451+
"a",
24452+
Style);
24453+
24454+
// Adjust indendations but don't change the definition.
24455+
Style.IndentPPDirectives = FormatStyle::PPDIS_None;
24456+
verifyNoChange("#if A\n"
24457+
"#define A a\n"
24458+
"#endif",
24459+
Style);
24460+
verifyFormat("#if A\n"
24461+
"#define A a\n"
24462+
"#endif",
24463+
"#if A\n"
24464+
" #define A a\n"
24465+
"#endif",
24466+
Style);
24467+
Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
24468+
verifyNoChange("#if A\n"
24469+
"# define A a\n"
24470+
"#endif",
24471+
Style);
24472+
verifyFormat("#if A\n"
24473+
"# define A a\n"
24474+
"#endif",
24475+
"#if A\n"
24476+
" #define A a\n"
24477+
"#endif",
24478+
Style);
24479+
Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
24480+
verifyNoChange("#if A\n"
24481+
" #define A a\n"
24482+
"#endif",
24483+
Style);
24484+
verifyFormat("#if A\n"
24485+
" #define A a\n"
24486+
"#endif",
24487+
"#if A\n"
24488+
" # define A a\n"
24489+
"#endif",
24490+
Style);
24491+
24492+
Style.IndentPPDirectives = FormatStyle::PPDIS_None;
24493+
// SkipMacroDefinitionBody should not affect other PP directives
24494+
verifyFormat("#if !defined(A)\n"
24495+
"#define A a\n"
24496+
"#endif",
24497+
"#if ! defined ( A )\n"
24498+
" #define A a\n"
24499+
"#endif",
24500+
Style);
24501+
24502+
// With comments.
24503+
verifyFormat("/* */ #define A a // a a", "/* */ # define A a // a a",
24504+
Style);
24505+
verifyNoChange("/* */ #define A a // a a", Style);
24506+
24507+
verifyFormat("int a; // a\n"
24508+
"#define A // a\n"
24509+
"int aaa; // a",
24510+
"int a; // a\n"
24511+
"#define A // a\n"
24512+
"int aaa; // a",
24513+
Style);
24514+
24515+
// multiline macro definitions
24516+
verifyNoChange("#define A a\\\n"
24517+
" A a \\\n "
24518+
" A a",
24519+
Style);
24520+
}
24521+
2439024522
TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
2439124523
// These tests are not in NamespaceEndCommentsFixerTest because that doesn't
2439224524
// test its interaction with line wrapping

0 commit comments

Comments
 (0)