Skip to content

Commit

Permalink
[clang-format] Add SkipMacroDefinitionBody option (#78682)
Browse files Browse the repository at this point in the history
Closes #67991.

See also: #70338

Co-authored-by: @tomekpaszek
  • Loading branch information
owenca committed Jan 20, 2024
1 parent 296fbee commit a7d7da6
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 0 deletions.
5 changes: 5 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4999,6 +4999,11 @@ the configuration (without a prefix: ``Auto``).
int bar; int bar;
} // namespace b } // namespace b

.. _SkipMacroDefinitionBody:

**SkipMacroDefinitionBody** (``Boolean``) :versionbadge:`clang-format 18` :ref:`<SkipMacroDefinitionBody>`
Do not format macro definition body.

.. _SortIncludes:

**SortIncludes** (``SortIncludesOptions``) :versionbadge:`clang-format 3.8` :ref:`<SortIncludes>`
Expand Down
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,7 @@ clang-format
- Add ``PenaltyBreakScopeResolution`` option.
- Add ``.clang-format-ignore`` files.
- Add ``AlignFunctionPointers`` sub-option for ``AlignConsecutiveDeclarations``.
- Add ``SkipMacroDefinitionBody`` option.

libclang
--------
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3932,6 +3932,10 @@ struct FormatStyle {
/// \version 13
unsigned ShortNamespaceLines;

/// Do not format macro definition body.
/// \version 18
bool SkipMacroDefinitionBody;

/// Include sorting options.
enum SortIncludesOptions : int8_t {
/// Includes are never sorted.
Expand Down Expand Up @@ -4895,6 +4899,7 @@ struct FormatStyle {
RequiresExpressionIndentation == R.RequiresExpressionIndentation &&
SeparateDefinitionBlocks == R.SeparateDefinitionBlocks &&
ShortNamespaceLines == R.ShortNamespaceLines &&
SkipMacroDefinitionBody == R.SkipMacroDefinitionBody &&
SortIncludes == R.SortIncludes &&
SortJavaStaticImport == R.SortJavaStaticImport &&
SpaceAfterCStyleCast == R.SpaceAfterCStyleCast &&
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,7 @@ template <> struct MappingTraits<FormatStyle> {
Style.RequiresExpressionIndentation);
IO.mapOptional("SeparateDefinitionBlocks", Style.SeparateDefinitionBlocks);
IO.mapOptional("ShortNamespaceLines", Style.ShortNamespaceLines);
IO.mapOptional("SkipMacroDefinitionBody", Style.SkipMacroDefinitionBody);
IO.mapOptional("SortIncludes", Style.SortIncludes);
IO.mapOptional("SortJavaStaticImport", Style.SortJavaStaticImport);
IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations);
Expand Down Expand Up @@ -1556,6 +1557,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.RequiresExpressionIndentation = FormatStyle::REI_OuterScope;
LLVMStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Leave;
LLVMStyle.ShortNamespaceLines = 1;
LLVMStyle.SkipMacroDefinitionBody = false;
LLVMStyle.SortIncludes = FormatStyle::SI_CaseSensitive;
LLVMStyle.SortJavaStaticImport = FormatStyle::SJSIO_Before;
LLVMStyle.SortUsingDeclarations = FormatStyle::SUD_LexicographicNumeric;
Expand Down
9 changes: 9 additions & 0 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,15 @@ void UnwrappedLineParser::parsePPDefine() {
assert((int)Line->PPLevel >= 0);
Line->InMacroBody = true;

if (Style.SkipMacroDefinitionBody) {
do {
FormatTok->Finalized = true;
nextToken();
} while (!eof());
addUnwrappedLine();
return;
}

if (FormatTok->is(tok::identifier) &&
Tokens->peekNextToken()->is(tok::colon)) {
nextToken();
Expand Down
1 change: 1 addition & 0 deletions clang/unittests/Format/ConfigParseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(ReflowComments);
CHECK_PARSE_BOOL(RemoveBracesLLVM);
CHECK_PARSE_BOOL(RemoveSemicolon);
CHECK_PARSE_BOOL(SkipMacroDefinitionBody);
CHECK_PARSE_BOOL(SpacesInSquareBrackets);
CHECK_PARSE_BOOL(SpaceInEmptyBlock);
CHECK_PARSE_BOOL(SpacesInContainerLiterals);
Expand Down
132 changes: 132 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24387,6 +24387,138 @@ TEST_F(FormatTest, WhitespaceSensitiveMacros) {
verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style);
}

TEST_F(FormatTest, SkipMacroDefinitionBody) {
auto Style = getLLVMStyle();
Style.SkipMacroDefinitionBody = true;

verifyFormat("#define A", "#define A", Style);
verifyFormat("#define A a aa", "#define A a aa", Style);
verifyNoChange("#define A b", Style);
verifyNoChange("#define A ( args )", Style);
verifyNoChange("#define A ( args ) = func ( args )", Style);
verifyNoChange("#define A ( args ) { int a = 1 ; }", Style);
verifyNoChange("#define A ( args ) \\\n"
" {\\\n"
" int a = 1 ;\\\n"
"}",
Style);

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

// Surrounded with formatted code.
verifyFormat("int a;\n"
"#define A a\n"
"int a;",
"int a ;\n"
"#define A a\n"
"int a ;",
Style);

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

Style.ColumnLimit = 15;
verifyFormat("#define A // a\n"
" // very\n"
" // long\n"
" // comment",
"#define A //a very long comment", Style);
Style.ColumnLimit = 0;

// Multiline definition.
verifyNoChange("#define A \\\n"
"Line one with spaces . \\\n"
" Line two.",
Style);
verifyNoChange("#define A \\\n"
"a a \\\n"
"a \\\n"
"a",
Style);
Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
verifyNoChange("#define A \\\n"
"a a \\\n"
"a \\\n"
"a",
Style);
Style.AlignEscapedNewlines = FormatStyle::ENAS_Right;
verifyNoChange("#define A \\\n"
"a a \\\n"
"a \\\n"
"a",
Style);

// Adjust indendations but don't change the definition.
Style.IndentPPDirectives = FormatStyle::PPDIS_None;
verifyNoChange("#if A\n"
"#define A a\n"
"#endif",
Style);
verifyFormat("#if A\n"
"#define A a\n"
"#endif",
"#if A\n"
" #define A a\n"
"#endif",
Style);
Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
verifyNoChange("#if A\n"
"# define A a\n"
"#endif",
Style);
verifyFormat("#if A\n"
"# define A a\n"
"#endif",
"#if A\n"
" #define A a\n"
"#endif",
Style);
Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
verifyNoChange("#if A\n"
" #define A a\n"
"#endif",
Style);
verifyFormat("#if A\n"
" #define A a\n"
"#endif",
"#if A\n"
" # define A a\n"
"#endif",
Style);

Style.IndentPPDirectives = FormatStyle::PPDIS_None;
// SkipMacroDefinitionBody should not affect other PP directives
verifyFormat("#if !defined(A)\n"
"#define A a\n"
"#endif",
"#if ! defined ( A )\n"
" #define A a\n"
"#endif",
Style);

// With comments.
verifyFormat("/* */ #define A a // a a", "/* */ # define A a // a a",
Style);
verifyNoChange("/* */ #define A a // a a", Style);

verifyFormat("int a; // a\n"
"#define A // a\n"
"int aaa; // a",
"int a; // a\n"
"#define A // a\n"
"int aaa; // a",
Style);

// multiline macro definitions
verifyNoChange("#define A a\\\n"
" A a \\\n "
" A a",
Style);
}

TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
// These tests are not in NamespaceEndCommentsFixerTest because that doesn't
// test its interaction with line wrapping
Expand Down

0 comments on commit a7d7da6

Please sign in to comment.