[clang] [clang-format] Option to ignore macro definitions (PR #70338)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 28 07:21:38 PST 2023
https://github.com/tomekpaszek updated https://github.com/llvm/llvm-project/pull/70338
>From b5ba0b3fde2c6662e19dfdf96a787621ec767460 Mon Sep 17 00:00:00 2001
From: Tomek Paszek <tomek at unity3d.com>
Date: Sat, 11 Nov 2023 19:38:00 +0100
Subject: [PATCH 01/15] Added an option to ignore macro definitions.
---
clang/include/clang/Format/Format.h | 5 +++++
clang/lib/Format/Format.cpp | 2 ++
clang/lib/Format/UnwrappedLineFormatter.cpp | 2 ++
clang/unittests/Format/ConfigParseTest.cpp | 3 ++-
clang/unittests/Format/FormatTest.cpp | 16 ++++++++++++++++
5 files changed, 27 insertions(+), 1 deletion(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 3e9d1915badd87f..3af7241441c8b13 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -2442,6 +2442,10 @@ struct FormatStyle {
/// <https://github.com/capnproto/capnproto/blob/master/kjdoc/tour.md#maybes>`_
/// \version 13
std::vector<std::string> IfMacros;
+
+ /// Ignore formatting in preprocessor definitions.
+ /// \version 18
+ bool IgnorePPDefinitions;
/// Specify whether access modifiers should have their own indentation level.
///
@@ -4719,6 +4723,7 @@ struct FormatStyle {
R.IncludeStyle.IncludeIsMainRegex &&
IncludeStyle.IncludeIsMainSourceRegex ==
R.IncludeStyle.IncludeIsMainSourceRegex &&
+ IgnorePPDefinitions == R.IgnorePPDefinitions &&
IndentAccessModifiers == R.IndentAccessModifiers &&
IndentCaseBlocks == R.IndentCaseBlocks &&
IndentCaseLabels == R.IndentCaseLabels &&
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index edb33f4af4defef..6e5ec754dfdcdd9 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1000,6 +1000,7 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("FixNamespaceComments", Style.FixNamespaceComments);
IO.mapOptional("ForEachMacros", Style.ForEachMacros);
IO.mapOptional("IfMacros", Style.IfMacros);
+ IO.mapOptional("IgnorePPDefinitions", Style.IgnorePPDefinitions);
IO.mapOptional("IncludeBlocks", Style.IncludeStyle.IncludeBlocks);
IO.mapOptional("IncludeCategories", Style.IncludeStyle.IncludeCategories);
IO.mapOptional("IncludeIsMainRegex", Style.IncludeStyle.IncludeIsMainRegex);
@@ -1504,6 +1505,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.ForEachMacros.push_back("Q_FOREACH");
LLVMStyle.ForEachMacros.push_back("BOOST_FOREACH");
LLVMStyle.IfMacros.push_back("KJ_IF_MAYBE");
+ LLVMStyle.IgnorePPDefinitions = false;
LLVMStyle.IncludeStyle.IncludeCategories = {
{"^\"(llvm|llvm-c|clang|clang-c)/", 2, 0, false},
{"^(<|\"(gtest|gmock|isl|json)/)", 3, 0, false},
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 280485d9a90d1bf..bbf6383ff7673f6 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -1355,6 +1355,8 @@ unsigned UnwrappedLineFormatter::format(
bool FixIndentation = (FixBadIndentation || ContinueFormatting) &&
Indent != TheLine.First->OriginalColumn;
bool ShouldFormat = TheLine.Affected || FixIndentation;
+ if (Style.IgnorePPDefinitions && TheLine.Type == LT_PreprocessorDirective)
+ ShouldFormat = false;
// We cannot format this line; if the reason is that the line had a
// parsing error, remember that.
if (ShouldFormat && TheLine.Type == LT_Invalid && Status) {
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index f90ed178d99c286..110df624d44573c 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -166,6 +166,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(DerivePointerAlignment);
CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
CHECK_PARSE_BOOL(DisableFormat);
+ CHECK_PARSE_BOOL(IgnorePPDefinitions);
CHECK_PARSE_BOOL(IndentAccessModifiers);
CHECK_PARSE_BOOL(IndentCaseLabels);
CHECK_PARSE_BOOL(IndentCaseBlocks);
@@ -198,7 +199,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
CHECK_PARSE_BOOL(VerilogBreakBetweenInstancePorts);
-
+
CHECK_PARSE_NESTED_BOOL(AlignConsecutiveShortCaseStatements, Enabled);
CHECK_PARSE_NESTED_BOOL(AlignConsecutiveShortCaseStatements,
AcrossEmptyLines);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index b2d84f2ee389551..659132dcb9c3970 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24153,6 +24153,22 @@ TEST_F(FormatTest, WhitespaceSensitiveMacros) {
verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style);
}
+TEST_F(FormatTest, IgnorePPDefinitions) {
+ FormatStyle Style = getLLVMStyle();
+ Style.IgnorePPDefinitions = true;
+
+ verifyNoChange("#define A", Style);
+ verifyNoChange("#define A b", Style);
+ verifyNoChange("#define A ( args )", Style);
+ verifyNoChange("#define A ( args ) = func ( args )", Style);
+ verifyNoChange("#define TEXT Text . With periods.", Style);
+ verifyNoChange("#define TEXT \\\nLine number one . \\\nNumber two .",
+ Style);
+
+ // TODO
+ // verifyNoChange("/* comment */ #define A ( args )", Style);
+}
+
TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
// These tests are not in NamespaceEndCommentsFixerTest because that doesn't
// test its interaction with line wrapping
>From aa62209c0181232339d24c2cb767795dcfa2b2c1 Mon Sep 17 00:00:00 2001
From: Tomek Paszek <tomek at unity3d.com>
Date: Fri, 27 Oct 2023 13:57:50 +0200
Subject: [PATCH 02/15] Added edge cases to the test and code that fixes
handling them
---
clang/lib/Format/UnwrappedLineParser.cpp | 8 ++++++++
clang/unittests/Format/FormatTest.cpp | 3 ++-
2 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 488d8dc07b1eae3..07cf966a08ff5e5 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1134,6 +1134,14 @@ void UnwrappedLineParser::parsePPDefine() {
return;
}
+ if (Style.IgnorePPDefinitions) {
+ do {
+ nextToken();
+ } while (!eof());
+ addUnwrappedLine();
+ return;
+ }
+
if (IncludeGuard == IG_IfNdefed &&
IncludeGuardToken->TokenText == FormatTok->TokenText) {
IncludeGuard = IG_Defined;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 659132dcb9c3970..a1f8961c3e5edb6 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24161,9 +24161,10 @@ TEST_F(FormatTest, IgnorePPDefinitions) {
verifyNoChange("#define A b", Style);
verifyNoChange("#define A ( args )", Style);
verifyNoChange("#define A ( args ) = func ( args )", Style);
- verifyNoChange("#define TEXT Text . With periods.", Style);
verifyNoChange("#define TEXT \\\nLine number one . \\\nNumber two .",
Style);
+ verifyNoChange("#define A x:", Style);
+ verifyNoChange("#define A a. b", Style);
// TODO
// verifyNoChange("/* comment */ #define A ( args )", Style);
>From 9575b8f6c24435a0f6bed2593276ccbe10c8070b Mon Sep 17 00:00:00 2001
From: Tomek Paszek <tomek at unity3d.com>
Date: Sat, 11 Nov 2023 19:28:25 +0100
Subject: [PATCH 03/15] Added more tests
---
clang/unittests/Format/ConfigParseTest.cpp | 2 +-
clang/unittests/Format/FormatTest.cpp | 40 +++++++++++++++++++---
2 files changed, 36 insertions(+), 6 deletions(-)
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 110df624d44573c..7ce9ae26b36fbb9 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -199,7 +199,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
CHECK_PARSE_BOOL(VerilogBreakBetweenInstancePorts);
-
+
CHECK_PARSE_NESTED_BOOL(AlignConsecutiveShortCaseStatements, Enabled);
CHECK_PARSE_NESTED_BOOL(AlignConsecutiveShortCaseStatements,
AcrossEmptyLines);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index a1f8961c3e5edb6..19cf521107c604b 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24161,13 +24161,43 @@ TEST_F(FormatTest, IgnorePPDefinitions) {
verifyNoChange("#define A b", Style);
verifyNoChange("#define A ( args )", Style);
verifyNoChange("#define A ( args ) = func ( args )", Style);
- verifyNoChange("#define TEXT \\\nLine number one . \\\nNumber two .",
- Style);
+
verifyNoChange("#define A x:", Style);
verifyNoChange("#define A a. b", Style);
-
- // TODO
- // verifyNoChange("/* comment */ #define A ( args )", 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;
+ verifyNoChange("#define A a a a a", 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 \\\na",
+ Style);
+ Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
+ verifyNoChange("#define A \\\n"
+ "a a \\\n"
+ "a \\\na",
+ Style);
+ Style.AlignEscapedNewlines = FormatStyle::ENAS_Right;
+ verifyNoChange("#define A \\\n"
+ "a a \\\n"
+ "a \\\na",
+ Style);
}
TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
>From c5da2c40065bb5394a9d85fb0b82ad0a1b00757a Mon Sep 17 00:00:00 2001
From: Tomek Paszek <tomek at unity3d.com>
Date: Mon, 13 Nov 2023 10:58:12 +0100
Subject: [PATCH 04/15] Updated documentation files
---
clang/docs/ClangFormatStyleOptions.rst | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 21342e1b89ea866..80565620d8f24bf 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -3160,6 +3160,11 @@ the configuration (without a prefix: ``Auto``).
For example: `KJ_IF_MAYBE
<https://github.com/capnproto/capnproto/blob/master/kjdoc/tour.md#maybes>`_
+.. _IgnorePPDefinitions:
+
+**IgnorePPDefinitions** (``Boolean``) :versionbadge:`clang-format 18` :ref:`¶ <IgnorePPDefinitions>`
+ Ignore formatting in preprocessor definitions.
+
.. _IncludeBlocks:
**IncludeBlocks** (``IncludeBlocksStyle``) :versionbadge:`clang-format 6` :ref:`¶ <IncludeBlocks>`
>From 633448f1b636416e186d714c32c874bc8b4ac5e9 Mon Sep 17 00:00:00 2001
From: Tomek Paszek <tomek at unity3d.com>
Date: Mon, 13 Nov 2023 11:06:17 +0100
Subject: [PATCH 05/15] Reformatted changes
---
clang/include/clang/Format/Format.h | 2 +-
clang/unittests/Format/FormatTest.cpp | 10 +++++-----
2 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 3af7241441c8b13..2e261e0868313f8 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -2442,7 +2442,7 @@ struct FormatStyle {
/// <https://github.com/capnproto/capnproto/blob/master/kjdoc/tour.md#maybes>`_
/// \version 13
std::vector<std::string> IfMacros;
-
+
/// Ignore formatting in preprocessor definitions.
/// \version 18
bool IgnorePPDefinitions;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 19cf521107c604b..3d57726eb24d86a 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24164,7 +24164,7 @@ TEST_F(FormatTest, IgnorePPDefinitions) {
verifyNoChange("#define A x:", Style);
verifyNoChange("#define A a. b", Style);
-
+
// Surrounded with formatted code
verifyFormat("int a;\n"
"#define A a\n"
@@ -24173,17 +24173,17 @@ TEST_F(FormatTest, IgnorePPDefinitions) {
"#define A a\n"
"int a ;",
Style);
-
+
// Columns are not broken when a limit is set
Style.ColumnLimit = 10;
verifyNoChange("#define A a a a a", Style);
Style.ColumnLimit = 0;
-
+
// Multiline definition
verifyNoChange("#define A \\\n"
"Line one with spaces . \\\n"
" Line two.",
- Style);
+ Style);
verifyNoChange("#define A \\\n"
"a a \\\n"
"a \\\na",
@@ -24196,7 +24196,7 @@ TEST_F(FormatTest, IgnorePPDefinitions) {
Style.AlignEscapedNewlines = FormatStyle::ENAS_Right;
verifyNoChange("#define A \\\n"
"a a \\\n"
- "a \\\na",
+ "a \\\na",
Style);
}
>From cd834d26408f632d1f2f832d0b40abae646f4ef2 Mon Sep 17 00:00:00 2001
From: Tomek Paszek <tomek at unity3d.com>
Date: Mon, 13 Nov 2023 16:43:10 +0100
Subject: [PATCH 06/15] Fixed code order
---
clang/include/clang/Format/Format.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 2e261e0868313f8..af8c162ebd4b913 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -4717,13 +4717,13 @@ struct FormatStyle {
R.ExperimentalAutoDetectBinPacking &&
FixNamespaceComments == R.FixNamespaceComments &&
ForEachMacros == R.ForEachMacros &&
+ IgnorePPDefinitions == R.IgnorePPDefinitions &&
IncludeStyle.IncludeBlocks == R.IncludeStyle.IncludeBlocks &&
IncludeStyle.IncludeCategories == R.IncludeStyle.IncludeCategories &&
IncludeStyle.IncludeIsMainRegex ==
R.IncludeStyle.IncludeIsMainRegex &&
IncludeStyle.IncludeIsMainSourceRegex ==
R.IncludeStyle.IncludeIsMainSourceRegex &&
- IgnorePPDefinitions == R.IgnorePPDefinitions &&
IndentAccessModifiers == R.IndentAccessModifiers &&
IndentCaseBlocks == R.IndentCaseBlocks &&
IndentCaseLabels == R.IndentCaseLabels &&
>From a66a0c9597caf253e759b0a069e6e9d905287b35 Mon Sep 17 00:00:00 2001
From: Tomek Paszek <tomek at unity3d.com>
Date: Mon, 13 Nov 2023 16:44:50 +0100
Subject: [PATCH 07/15] Added more tests to cover PP indentation and fixed
readability in other tests
---
clang/unittests/Format/FormatTest.cpp | 44 +++++++++++++++++++++++----
1 file changed, 38 insertions(+), 6 deletions(-)
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 3d57726eb24d86a..a59aae7b5cb4dde 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24165,7 +24165,7 @@ TEST_F(FormatTest, IgnorePPDefinitions) {
verifyNoChange("#define A x:", Style);
verifyNoChange("#define A a. b", Style);
- // Surrounded with formatted code
+ // Surrounded with formatted code.
verifyFormat("int a;\n"
"#define A a\n"
"int a;",
@@ -24174,29 +24174,61 @@ TEST_F(FormatTest, IgnorePPDefinitions) {
"int a ;",
Style);
- // Columns are not broken when a limit is set
+ // Columns are not broken when a limit is set.
Style.ColumnLimit = 10;
verifyNoChange("#define A a a a a", Style);
Style.ColumnLimit = 0;
- // Multiline definition
+ // Multiline definition.
verifyNoChange("#define A \\\n"
"Line one with spaces . \\\n"
" Line two.",
Style);
verifyNoChange("#define A \\\n"
"a a \\\n"
- "a \\\na",
+ "a \\\n"
+ "a",
Style);
Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
verifyNoChange("#define A \\\n"
"a a \\\n"
- "a \\\na",
+ "a \\\n"
+ "a",
Style);
Style.AlignEscapedNewlines = FormatStyle::ENAS_Right;
verifyNoChange("#define A \\\n"
"a a \\\n"
- "a \\\na",
+ "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);
+ verifyNoChange("#if A\n"
+ "#define A a\\\n"
+ " 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);
+ Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
+ verifyNoChange("#if A\n"
+ " #define A a\n"
+ "#endif",
Style);
}
>From 13822926338483e59bf4cd5ecccaf8007526652c Mon Sep 17 00:00:00 2001
From: Tomek Paszek <tomek at unity3d.com>
Date: Tue, 14 Nov 2023 11:10:04 +0100
Subject: [PATCH 08/15] Fixed the option affecting other PP directives
---
clang/lib/Format/UnwrappedLineFormatter.cpp | 5 +++-
clang/unittests/Format/FormatTest.cpp | 28 +++++++++++++++------
2 files changed, 25 insertions(+), 8 deletions(-)
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index bbf6383ff7673f6..7bfb25089ac06ee 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -1355,8 +1355,11 @@ unsigned UnwrappedLineFormatter::format(
bool FixIndentation = (FixBadIndentation || ContinueFormatting) &&
Indent != TheLine.First->OriginalColumn;
bool ShouldFormat = TheLine.Affected || FixIndentation;
- if (Style.IgnorePPDefinitions && TheLine.Type == LT_PreprocessorDirective)
+ if (Style.IgnorePPDefinitions && TheLine.Type == LT_PreprocessorDirective &&
+ TheLine.getFirstNonComment()->Next->is(tok::pp_define)) {
ShouldFormat = false;
+ }
+
// We cannot format this line; if the reason is that the line had a
// parsing error, remember that.
if (ShouldFormat && TheLine.Type == LT_Invalid && Status) {
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index a59aae7b5cb4dde..9d895d69a6ed83f 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24206,10 +24206,11 @@ TEST_F(FormatTest, IgnorePPDefinitions) {
Style.IndentPPDirectives = FormatStyle::PPDIS_None;
verifyNoChange("#if A\n"
"#define A a\n"
- "#endif",
+ "#endif\n",
Style);
- verifyNoChange("#if A\n"
- "#define A a\\\n"
+ verifyNoChange("#define UNITY 1\n"
+ "#if A\n"
+ "# define A a\\\n"
" a a\n"
"#endif",
Style);
@@ -24221,15 +24222,28 @@ TEST_F(FormatTest, IgnorePPDefinitions) {
"#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);
+
+ Style.IndentPPDirectives = FormatStyle::PPDIS_None;
+ // IgnorePPDefinitions 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);
}
TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
>From 917034f151204d9ce138496096d871e6606ac8af Mon Sep 17 00:00:00 2001
From: Tomek Paszek <tomek at unity3d.com>
Date: Tue, 14 Nov 2023 12:52:54 +0100
Subject: [PATCH 09/15] Added test cases for trailing comments
---
clang/unittests/Format/FormatTest.cpp | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 9d895d69a6ed83f..2eac543aafb0e45 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24177,6 +24177,20 @@ TEST_F(FormatTest, IgnorePPDefinitions) {
// Columns are not broken when a limit is set.
Style.ColumnLimit = 10;
verifyNoChange("#define A a a a a", Style);
+
+ Style.ColumnLimit = 15;
+ verifyNoChange("#define A //a very long comment\n", Style);
+ // in the following examples, since second line will not be formtted, it won't
+ // take into considertaion the alignment from the first line. The third line
+ // will follow the second line's alignment.
+ verifyFormat("int aaaaaa; // a\n"
+ "#define A // a\n"
+ "int a; // a\n",
+ "int aaaaaa; // a\n"
+ "#define A // a\n"
+ "int a; // a\n",
+ Style);
+
Style.ColumnLimit = 0;
// Multiline definition.
>From dda55a5e4bae5fe07977528429d1cbe073d2a03c Mon Sep 17 00:00:00 2001
From: Tomek Paszek <tomek at unity3d.com>
Date: Sat, 18 Nov 2023 13:47:53 +0100
Subject: [PATCH 10/15] Refactored condition to ignore PP directives other than
#define
---
clang/lib/Format/UnwrappedLineFormatter.cpp | 12 +++++++++---
clang/unittests/Format/FormatTest.cpp | 18 ++++++++++++++----
2 files changed, 23 insertions(+), 7 deletions(-)
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 7bfb25089ac06ee..0a49e1cc282372e 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -1308,6 +1308,13 @@ class OptimizingLineFormatter : public LineFormatter {
} // anonymous namespace
+static bool lineContainsPPDefinition(const AnnotatedLine &Line) {
+ auto *Tok = Line.getFirstNonComment();
+ if (!Tok || !Tok->is(tok::hash) || !Tok->Next)
+ return false;
+ return Tok->Next->is(tok::pp_define);
+}
+
unsigned UnwrappedLineFormatter::format(
const SmallVectorImpl<AnnotatedLine *> &Lines, bool DryRun,
int AdditionalIndent, bool FixBadIndentation, unsigned FirstStartColumn,
@@ -1355,10 +1362,9 @@ unsigned UnwrappedLineFormatter::format(
bool FixIndentation = (FixBadIndentation || ContinueFormatting) &&
Indent != TheLine.First->OriginalColumn;
bool ShouldFormat = TheLine.Affected || FixIndentation;
- if (Style.IgnorePPDefinitions && TheLine.Type == LT_PreprocessorDirective &&
- TheLine.getFirstNonComment()->Next->is(tok::pp_define)) {
+
+ if (Style.IgnorePPDefinitions && lineContainsPPDefinition(TheLine))
ShouldFormat = false;
- }
// We cannot format this line; if the reason is that the line had a
// parsing error, remember that.
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 2eac543aafb0e45..33a8f620dcb52a7 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24179,16 +24179,16 @@ TEST_F(FormatTest, IgnorePPDefinitions) {
verifyNoChange("#define A a a a a", Style);
Style.ColumnLimit = 15;
- verifyNoChange("#define A //a very long comment\n", Style);
+ verifyNoChange("#define A //a very long comment", Style);
// in the following examples, since second line will not be formtted, it won't
// take into considertaion the alignment from the first line. The third line
// will follow the second line's alignment.
verifyFormat("int aaaaaa; // a\n"
"#define A // a\n"
- "int a; // a\n",
+ "int a; // a",
"int aaaaaa; // a\n"
"#define A // a\n"
- "int a; // a\n",
+ "int a; // a",
Style);
Style.ColumnLimit = 0;
@@ -24220,7 +24220,7 @@ TEST_F(FormatTest, IgnorePPDefinitions) {
Style.IndentPPDirectives = FormatStyle::PPDIS_None;
verifyNoChange("#if A\n"
"#define A a\n"
- "#endif\n",
+ "#endif",
Style);
verifyNoChange("#define UNITY 1\n"
"#if A\n"
@@ -24258,6 +24258,16 @@ TEST_F(FormatTest, IgnorePPDefinitions) {
" # define A a\n"
"#endif",
Style);
+
+ // With comments.
+ 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);
}
TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
>From 41088dbf81c834dd73ab516c2901c9e2346ea296 Mon Sep 17 00:00:00 2001
From: Tomek Paszek <tomek at unity3d.com>
Date: Wed, 22 Nov 2023 13:36:51 +0100
Subject: [PATCH 11/15] Fixed code style
---
clang/lib/Format/UnwrappedLineFormatter.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 0a49e1cc282372e..4ff483bde022648 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -1308,7 +1308,7 @@ class OptimizingLineFormatter : public LineFormatter {
} // anonymous namespace
-static bool lineContainsPPDefinition(const AnnotatedLine &Line) {
+static bool LineContainsPPDefinition(const AnnotatedLine &Line) {
auto *Tok = Line.getFirstNonComment();
if (!Tok || !Tok->is(tok::hash) || !Tok->Next)
return false;
@@ -1363,7 +1363,7 @@ unsigned UnwrappedLineFormatter::format(
Indent != TheLine.First->OriginalColumn;
bool ShouldFormat = TheLine.Affected || FixIndentation;
- if (Style.IgnorePPDefinitions && lineContainsPPDefinition(TheLine))
+ if (Style.IgnorePPDefinitions && LineContainsPPDefinition(TheLine))
ShouldFormat = false;
// We cannot format this line; if the reason is that the line had a
>From ef2fae703b48bd51eeaa6cbf41c7524dc6895def Mon Sep 17 00:00:00 2001
From: Tomek Paszek <tomek at unity3d.com>
Date: Thu, 23 Nov 2023 15:45:35 +0100
Subject: [PATCH 12/15] Renamed the option name to SkipMacroDefinition based on
feedback
---
clang/docs/ClangFormatStyleOptions.rst | 10 +++++-----
clang/docs/ReleaseNotes.rst | 1 +
clang/include/clang/Format/Format.h | 10 +++++-----
clang/lib/Format/Format.cpp | 4 ++--
clang/lib/Format/UnwrappedLineFormatter.cpp | 2 +-
clang/lib/Format/UnwrappedLineParser.cpp | 2 +-
clang/unittests/Format/ConfigParseTest.cpp | 2 +-
clang/unittests/Format/FormatTest.cpp | 8 ++++----
8 files changed, 20 insertions(+), 19 deletions(-)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 80565620d8f24bf..d03dee6ea73f149 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -3160,11 +3160,6 @@ the configuration (without a prefix: ``Auto``).
For example: `KJ_IF_MAYBE
<https://github.com/capnproto/capnproto/blob/master/kjdoc/tour.md#maybes>`_
-.. _IgnorePPDefinitions:
-
-**IgnorePPDefinitions** (``Boolean``) :versionbadge:`clang-format 18` :ref:`¶ <IgnorePPDefinitions>`
- Ignore formatting in preprocessor definitions.
-
.. _IncludeBlocks:
**IncludeBlocks** (``IncludeBlocksStyle``) :versionbadge:`clang-format 6` :ref:`¶ <IncludeBlocks>`
@@ -4833,6 +4828,11 @@ the configuration (without a prefix: ``Auto``).
int bar; int bar;
} // namespace b } // namespace b
+.. _SkipMacroDefinition:
+
+**SkipMacroDefinition** (``Boolean``) :versionbadge:`clang-format 18` :ref:`¶ <SkipMacroDefinition>`
+ Do not format macro definitions.
+
.. _SortIncludes:
**SortIncludes** (``SortIncludesOptions``) :versionbadge:`clang-format 3.8` :ref:`¶ <SortIncludes>`
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 074116d2edf9f99..00e4babdc65a585 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -790,6 +790,7 @@ clang-format
------------
- Add ``AllowBreakBeforeNoexceptSpecifier`` option.
- Add ``AllowShortCompoundRequirementOnASingleLine`` option.
+- Add ``SkipMacroDefinition`` option.
libclang
--------
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index af8c162ebd4b913..422806dfa186ea8 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -2443,10 +2443,6 @@ struct FormatStyle {
/// \version 13
std::vector<std::string> IfMacros;
- /// Ignore formatting in preprocessor definitions.
- /// \version 18
- bool IgnorePPDefinitions;
-
/// Specify whether access modifiers should have their own indentation level.
///
/// When ``false``, access modifiers are indented (or outdented) relative to
@@ -3818,6 +3814,10 @@ struct FormatStyle {
/// \version 13
unsigned ShortNamespaceLines;
+ /// Do not format macro definitions.
+ /// \version 18
+ bool SkipMacroDefinition;
+
/// Include sorting options.
enum SortIncludesOptions : int8_t {
/// Includes are never sorted.
@@ -4717,7 +4717,6 @@ struct FormatStyle {
R.ExperimentalAutoDetectBinPacking &&
FixNamespaceComments == R.FixNamespaceComments &&
ForEachMacros == R.ForEachMacros &&
- IgnorePPDefinitions == R.IgnorePPDefinitions &&
IncludeStyle.IncludeBlocks == R.IncludeStyle.IncludeBlocks &&
IncludeStyle.IncludeCategories == R.IncludeStyle.IncludeCategories &&
IncludeStyle.IncludeIsMainRegex ==
@@ -4779,6 +4778,7 @@ struct FormatStyle {
RequiresExpressionIndentation == R.RequiresExpressionIndentation &&
SeparateDefinitionBlocks == R.SeparateDefinitionBlocks &&
ShortNamespaceLines == R.ShortNamespaceLines &&
+ SkipMacroDefinition == R.SkipMacroDefinition &&
SortIncludes == R.SortIncludes &&
SortJavaStaticImport == R.SortJavaStaticImport &&
SpaceAfterCStyleCast == R.SpaceAfterCStyleCast &&
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 6e5ec754dfdcdd9..b6000ef8ba89e69 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1000,7 +1000,6 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("FixNamespaceComments", Style.FixNamespaceComments);
IO.mapOptional("ForEachMacros", Style.ForEachMacros);
IO.mapOptional("IfMacros", Style.IfMacros);
- IO.mapOptional("IgnorePPDefinitions", Style.IgnorePPDefinitions);
IO.mapOptional("IncludeBlocks", Style.IncludeStyle.IncludeBlocks);
IO.mapOptional("IncludeCategories", Style.IncludeStyle.IncludeCategories);
IO.mapOptional("IncludeIsMainRegex", Style.IncludeStyle.IncludeIsMainRegex);
@@ -1080,6 +1079,7 @@ template <> struct MappingTraits<FormatStyle> {
Style.RequiresExpressionIndentation);
IO.mapOptional("SeparateDefinitionBlocks", Style.SeparateDefinitionBlocks);
IO.mapOptional("ShortNamespaceLines", Style.ShortNamespaceLines);
+ IO.mapOptional("SkipMacroDefinition", Style.SkipMacroDefinition);
IO.mapOptional("SortIncludes", Style.SortIncludes);
IO.mapOptional("SortJavaStaticImport", Style.SortJavaStaticImport);
IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations);
@@ -1505,7 +1505,6 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.ForEachMacros.push_back("Q_FOREACH");
LLVMStyle.ForEachMacros.push_back("BOOST_FOREACH");
LLVMStyle.IfMacros.push_back("KJ_IF_MAYBE");
- LLVMStyle.IgnorePPDefinitions = false;
LLVMStyle.IncludeStyle.IncludeCategories = {
{"^\"(llvm|llvm-c|clang|clang-c)/", 2, 0, false},
{"^(<|\"(gtest|gmock|isl|json)/)", 3, 0, false},
@@ -1554,6 +1553,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.RequiresExpressionIndentation = FormatStyle::REI_OuterScope;
LLVMStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Leave;
LLVMStyle.ShortNamespaceLines = 1;
+ LLVMStyle.SkipMacroDefinition = false;
LLVMStyle.SortIncludes = FormatStyle::SI_CaseSensitive;
LLVMStyle.SortJavaStaticImport = FormatStyle::SJSIO_Before;
LLVMStyle.SortUsingDeclarations = FormatStyle::SUD_LexicographicNumeric;
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 4ff483bde022648..1fde54d407e100d 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -1363,7 +1363,7 @@ unsigned UnwrappedLineFormatter::format(
Indent != TheLine.First->OriginalColumn;
bool ShouldFormat = TheLine.Affected || FixIndentation;
- if (Style.IgnorePPDefinitions && LineContainsPPDefinition(TheLine))
+ if (Style.SkipMacroDefinition && LineContainsPPDefinition(TheLine))
ShouldFormat = false;
// We cannot format this line; if the reason is that the line had a
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 07cf966a08ff5e5..ff7e36044f2999b 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1134,7 +1134,7 @@ void UnwrappedLineParser::parsePPDefine() {
return;
}
- if (Style.IgnorePPDefinitions) {
+ if (Style.SkipMacroDefinition) {
do {
nextToken();
} while (!eof());
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 7ce9ae26b36fbb9..9eb42b8182a960d 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -166,7 +166,6 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(DerivePointerAlignment);
CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
CHECK_PARSE_BOOL(DisableFormat);
- CHECK_PARSE_BOOL(IgnorePPDefinitions);
CHECK_PARSE_BOOL(IndentAccessModifiers);
CHECK_PARSE_BOOL(IndentCaseLabels);
CHECK_PARSE_BOOL(IndentCaseBlocks);
@@ -184,6 +183,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(ReflowComments);
CHECK_PARSE_BOOL(RemoveBracesLLVM);
CHECK_PARSE_BOOL(RemoveSemicolon);
+ CHECK_PARSE_BOOL(SkipMacroDefinition);
CHECK_PARSE_BOOL(SpacesInSquareBrackets);
CHECK_PARSE_BOOL(SpaceInEmptyBlock);
CHECK_PARSE_BOOL(SpacesInContainerLiterals);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 33a8f620dcb52a7..444c6a9f3af3bbe 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24153,9 +24153,9 @@ TEST_F(FormatTest, WhitespaceSensitiveMacros) {
verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style);
}
-TEST_F(FormatTest, IgnorePPDefinitions) {
- FormatStyle Style = getLLVMStyle();
- Style.IgnorePPDefinitions = true;
+TEST_F(FormatTest, SkipMacroDefinition) {
+ auto Style = getLLVMStyle();
+ Style.SkipMacroDefinition = true;
verifyNoChange("#define A", Style);
verifyNoChange("#define A b", Style);
@@ -24250,7 +24250,7 @@ TEST_F(FormatTest, IgnorePPDefinitions) {
Style);
Style.IndentPPDirectives = FormatStyle::PPDIS_None;
- // IgnorePPDefinitions should not affect other PP directives
+ // SkipMacroDefinition should not affect other PP directives
verifyFormat("#if !defined(A)\n"
"# define A a\n"
"#endif",
>From 53a78f35fceab341f7a8726ca83c32981303bbeb Mon Sep 17 00:00:00 2001
From: Tomek Paszek <tomek at unity3d.com>
Date: Thu, 23 Nov 2023 16:14:11 +0100
Subject: [PATCH 13/15] Simplified condition
---
clang/lib/Format/UnwrappedLineFormatter.cpp | 9 +--------
1 file changed, 1 insertion(+), 8 deletions(-)
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 1fde54d407e100d..0db2b738c11002c 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -1308,13 +1308,6 @@ class OptimizingLineFormatter : public LineFormatter {
} // anonymous namespace
-static bool LineContainsPPDefinition(const AnnotatedLine &Line) {
- auto *Tok = Line.getFirstNonComment();
- if (!Tok || !Tok->is(tok::hash) || !Tok->Next)
- return false;
- return Tok->Next->is(tok::pp_define);
-}
-
unsigned UnwrappedLineFormatter::format(
const SmallVectorImpl<AnnotatedLine *> &Lines, bool DryRun,
int AdditionalIndent, bool FixBadIndentation, unsigned FirstStartColumn,
@@ -1363,7 +1356,7 @@ unsigned UnwrappedLineFormatter::format(
Indent != TheLine.First->OriginalColumn;
bool ShouldFormat = TheLine.Affected || FixIndentation;
- if (Style.SkipMacroDefinition && LineContainsPPDefinition(TheLine))
+ if (Style.SkipMacroDefinition && TheLine.startsWith(tok::hash, tok::pp_define))
ShouldFormat = false;
// We cannot format this line; if the reason is that the line had a
>From ce17f77a30dbdf4f1804c294029fc364dc40e440 Mon Sep 17 00:00:00 2001
From: Tomek Paszek <tomek at unity3d.com>
Date: Tue, 28 Nov 2023 16:07:25 +0100
Subject: [PATCH 14/15] changed the logic to only ignore the body of a PP
define
---
clang/lib/Format/ContinuationIndenter.cpp | 15 ++++-
clang/lib/Format/ContinuationIndenter.h | 8 ++-
clang/lib/Format/UnwrappedLineFormatter.cpp | 3 -
clang/lib/Format/UnwrappedLineParser.cpp | 18 +++---
clang/unittests/Format/FormatTest.cpp | 69 +++++++++++++--------
5 files changed, 71 insertions(+), 42 deletions(-)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 3b28f84fd8417d3..e46d132750703ce 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -739,8 +739,18 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
}
if (!DryRun) {
- Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, Spaces,
- State.Column + Spaces + PPColumnCorrection);
+ if (Style.SkipMacroDefinition && CurrentState.IsPPDefineBody &&
+ !Current.is(tok::comment)) {
+ Whitespaces.addUntouchableToken(Current, State.Line->InPPDirective);
+ } else {
+ Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, Spaces,
+ State.Column + Spaces + PPColumnCorrection);
+ }
+ }
+
+ if (Style.SkipMacroDefinition && Previous.is(tok::pp_define)) {
+ CurrentState.NoLineBreak = true;
+ CurrentState.IsPPDefineBody = true;
}
// If "BreakBeforeInheritanceComma" mode, don't break within the inheritance
@@ -1886,6 +1896,7 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
NewState.NestedBlockIndent = NestedBlockIndent;
NewState.BreakBeforeParameter = BreakBeforeParameter;
NewState.HasMultipleNestedBlocks = (Current.BlockParameterCount > 1);
+ NewState.IsPPDefineBody = CurrentState.IsPPDefineBody;
if (Style.BraceWrapping.BeforeLambdaBody && Current.Next &&
Current.is(tok::l_paren)) {
diff --git a/clang/lib/Format/ContinuationIndenter.h b/clang/lib/Format/ContinuationIndenter.h
index 057b85bd32d5051..d1a611bfa3aafe2 100644
--- a/clang/lib/Format/ContinuationIndenter.h
+++ b/clang/lib/Format/ContinuationIndenter.h
@@ -212,7 +212,8 @@ struct ParenState {
ObjCSelectorNameFound(false), HasMultipleNestedBlocks(false),
NestedBlockInlined(false), IsInsideObjCArrayLiteral(false),
IsCSharpGenericTypeConstraint(false), IsChainedConditional(false),
- IsWrappedConditional(false), UnindentOperator(false) {}
+ IsPPDefineBody(false), IsWrappedConditional(false),
+ UnindentOperator(false) {}
/// \brief The token opening this parenthesis level, or nullptr if this level
/// is opened by fake parenthesis.
@@ -349,6 +350,9 @@ struct ParenState {
/// a chained conditional expression (e.g. else-if)
bool IsChainedConditional : 1;
+ /// \brief true if in pre-processor define body
+ bool IsPPDefineBody : 1;
+
/// \brief true if there conditionnal was wrapped on the first operator (the
/// question mark)
bool IsWrappedConditional : 1;
@@ -402,6 +406,8 @@ struct ParenState {
return IsCSharpGenericTypeConstraint;
if (IsChainedConditional != Other.IsChainedConditional)
return IsChainedConditional;
+ if (IsPPDefineBody != Other.IsPPDefineBody)
+ return IsPPDefineBody;
if (IsWrappedConditional != Other.IsWrappedConditional)
return IsWrappedConditional;
if (UnindentOperator != Other.UnindentOperator)
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 0db2b738c11002c..2b66bb18edf9b38 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -1356,9 +1356,6 @@ unsigned UnwrappedLineFormatter::format(
Indent != TheLine.First->OriginalColumn;
bool ShouldFormat = TheLine.Affected || FixIndentation;
- if (Style.SkipMacroDefinition && TheLine.startsWith(tok::hash, tok::pp_define))
- ShouldFormat = false;
-
// We cannot format this line; if the reason is that the line had a
// parsing error, remember that.
if (ShouldFormat && TheLine.Type == LT_Invalid && Status) {
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index ff7e36044f2999b..cf276f639f8ac24 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1134,14 +1134,6 @@ void UnwrappedLineParser::parsePPDefine() {
return;
}
- if (Style.SkipMacroDefinition) {
- do {
- nextToken();
- } while (!eof());
- addUnwrappedLine();
- return;
- }
-
if (IncludeGuard == IG_IfNdefed &&
IncludeGuardToken->TokenText == FormatTok->TokenText) {
IncludeGuard = IG_Defined;
@@ -1161,7 +1153,15 @@ void UnwrappedLineParser::parsePPDefine() {
// guard processing above, and changes preprocessing nesting.
FormatTok->Tok.setKind(tok::identifier);
FormatTok->Tok.setIdentifierInfo(Keywords.kw_internal_ident_after_define);
- nextToken();
+
+ if (Style.SkipMacroDefinition) {
+ do {
+ nextToken();
+ } while (!eof());
+ } else {
+ nextToken();
+ }
+
if (FormatTok->Tok.getKind() == tok::l_paren &&
!FormatTok->hasWhitespaceBefore()) {
parseParens();
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 444c6a9f3af3bbe..df8a3141101ade6 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24157,17 +24157,24 @@ TEST_F(FormatTest, SkipMacroDefinition) {
auto Style = getLLVMStyle();
Style.SkipMacroDefinition = true;
- verifyNoChange("#define A", Style);
+ 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"
+ "#define A a\n"
"int a;",
"int a ;\n"
"#define A a\n"
@@ -24176,21 +24183,15 @@ TEST_F(FormatTest, SkipMacroDefinition) {
// 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;
- verifyNoChange("#define A //a very long comment", Style);
- // in the following examples, since second line will not be formtted, it won't
- // take into considertaion the alignment from the first line. The third line
- // will follow the second line's alignment.
- verifyFormat("int aaaaaa; // a\n"
- "#define A // a\n"
- "int a; // a",
- "int aaaaaa; // a\n"
- "#define A // a\n"
- "int a; // a",
- Style);
-
+ verifyFormat("#define A // a\n"
+ " // very\n"
+ " // long\n"
+ " // comment",
+ "#define A //a very long comment", Style);
Style.ColumnLimit = 0;
// Multiline definition.
@@ -24222,12 +24223,6 @@ TEST_F(FormatTest, SkipMacroDefinition) {
"#define A a\n"
"#endif",
Style);
- verifyNoChange("#define UNITY 1\n"
- "#if A\n"
- "# define A a\\\n"
- " a a\n"
- "#endif",
- Style);
verifyFormat("#if A\n"
"#define A a\n"
"#endif",
@@ -24236,11 +24231,15 @@ TEST_F(FormatTest, SkipMacroDefinition) {
"#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"
+ " #define A a\n"
"#endif",
Style);
Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
@@ -24248,26 +24247,42 @@ TEST_F(FormatTest, SkipMacroDefinition) {
" #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;
// SkipMacroDefinition should not affect other PP directives
verifyFormat("#if !defined(A)\n"
- "# define A a\n"
+ "#define A a\n"
"#endif",
"#if ! defined ( A )\n"
- " # define A a\n"
+ " #define A a\n"
"#endif",
Style);
// With comments.
- verifyNoChange("/* */ # define A a // a a", Style);
- verifyFormat("int a; // a\n"
- "#define A // a\n"
- "int aaa; // a",
+ 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) {
>From 72acbae5e3cbfafebb40df9cbe2210a7d8f5099d Mon Sep 17 00:00:00 2001
From: Tomek Paszek <tomek at unity3d.com>
Date: Tue, 28 Nov 2023 16:13:57 +0100
Subject: [PATCH 15/15] renamed the setting to SkipMacroDefinitionBody
---
clang/docs/ClangFormatStyleOptions.rst | 6 +++---
clang/docs/ReleaseNotes.rst | 2 +-
clang/include/clang/Format/Format.h | 6 +++---
clang/lib/Format/ContinuationIndenter.cpp | 4 ++--
clang/lib/Format/Format.cpp | 4 ++--
clang/lib/Format/UnwrappedLineFormatter.cpp | 1 -
clang/lib/Format/UnwrappedLineParser.cpp | 2 +-
clang/unittests/Format/ConfigParseTest.cpp | 2 +-
clang/unittests/Format/FormatTest.cpp | 6 +++---
9 files changed, 16 insertions(+), 17 deletions(-)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index d03dee6ea73f149..a2db74972980dd4 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -4828,10 +4828,10 @@ the configuration (without a prefix: ``Auto``).
int bar; int bar;
} // namespace b } // namespace b
-.. _SkipMacroDefinition:
+.. _SkipMacroDefinitionBody:
-**SkipMacroDefinition** (``Boolean``) :versionbadge:`clang-format 18` :ref:`¶ <SkipMacroDefinition>`
- Do not format macro definitions.
+**SkipMacroDefinitionBody** (``Boolean``) :versionbadge:`clang-format 18` :ref:`¶ <SkipMacroDefinitionBody>`
+ Do not format macro definition body.
.. _SortIncludes:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 00e4babdc65a585..9523d2b35f98250 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -790,7 +790,7 @@ clang-format
------------
- Add ``AllowBreakBeforeNoexceptSpecifier`` option.
- Add ``AllowShortCompoundRequirementOnASingleLine`` option.
-- Add ``SkipMacroDefinition`` option.
+- Add ``SkipMacroDefinitionBody`` option.
libclang
--------
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 422806dfa186ea8..5f9ceab6752ed3e 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -3814,9 +3814,9 @@ struct FormatStyle {
/// \version 13
unsigned ShortNamespaceLines;
- /// Do not format macro definitions.
+ /// Do not format macro definition body.
/// \version 18
- bool SkipMacroDefinition;
+ bool SkipMacroDefinitionBody;
/// Include sorting options.
enum SortIncludesOptions : int8_t {
@@ -4778,7 +4778,7 @@ struct FormatStyle {
RequiresExpressionIndentation == R.RequiresExpressionIndentation &&
SeparateDefinitionBlocks == R.SeparateDefinitionBlocks &&
ShortNamespaceLines == R.ShortNamespaceLines &&
- SkipMacroDefinition == R.SkipMacroDefinition &&
+ SkipMacroDefinitionBody == R.SkipMacroDefinitionBody &&
SortIncludes == R.SortIncludes &&
SortJavaStaticImport == R.SortJavaStaticImport &&
SpaceAfterCStyleCast == R.SpaceAfterCStyleCast &&
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index e46d132750703ce..750f9b55393af54 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -739,7 +739,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
}
if (!DryRun) {
- if (Style.SkipMacroDefinition && CurrentState.IsPPDefineBody &&
+ if (Style.SkipMacroDefinitionBody && CurrentState.IsPPDefineBody &&
!Current.is(tok::comment)) {
Whitespaces.addUntouchableToken(Current, State.Line->InPPDirective);
} else {
@@ -748,7 +748,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
}
}
- if (Style.SkipMacroDefinition && Previous.is(tok::pp_define)) {
+ if (Style.SkipMacroDefinitionBody && Previous.is(tok::pp_define)) {
CurrentState.NoLineBreak = true;
CurrentState.IsPPDefineBody = true;
}
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index b6000ef8ba89e69..948d60b2d889528 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1079,7 +1079,7 @@ template <> struct MappingTraits<FormatStyle> {
Style.RequiresExpressionIndentation);
IO.mapOptional("SeparateDefinitionBlocks", Style.SeparateDefinitionBlocks);
IO.mapOptional("ShortNamespaceLines", Style.ShortNamespaceLines);
- IO.mapOptional("SkipMacroDefinition", Style.SkipMacroDefinition);
+ IO.mapOptional("SkipMacroDefinitionBody", Style.SkipMacroDefinitionBody);
IO.mapOptional("SortIncludes", Style.SortIncludes);
IO.mapOptional("SortJavaStaticImport", Style.SortJavaStaticImport);
IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations);
@@ -1553,7 +1553,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.RequiresExpressionIndentation = FormatStyle::REI_OuterScope;
LLVMStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Leave;
LLVMStyle.ShortNamespaceLines = 1;
- LLVMStyle.SkipMacroDefinition = false;
+ LLVMStyle.SkipMacroDefinitionBody = false;
LLVMStyle.SortIncludes = FormatStyle::SI_CaseSensitive;
LLVMStyle.SortJavaStaticImport = FormatStyle::SJSIO_Before;
LLVMStyle.SortUsingDeclarations = FormatStyle::SUD_LexicographicNumeric;
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 2b66bb18edf9b38..280485d9a90d1bf 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -1355,7 +1355,6 @@ unsigned UnwrappedLineFormatter::format(
bool FixIndentation = (FixBadIndentation || ContinueFormatting) &&
Indent != TheLine.First->OriginalColumn;
bool ShouldFormat = TheLine.Affected || FixIndentation;
-
// We cannot format this line; if the reason is that the line had a
// parsing error, remember that.
if (ShouldFormat && TheLine.Type == LT_Invalid && Status) {
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index cf276f639f8ac24..b4f2e0f0a8ae06a 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1154,7 +1154,7 @@ void UnwrappedLineParser::parsePPDefine() {
FormatTok->Tok.setKind(tok::identifier);
FormatTok->Tok.setIdentifierInfo(Keywords.kw_internal_ident_after_define);
- if (Style.SkipMacroDefinition) {
+ if (Style.SkipMacroDefinitionBody) {
do {
nextToken();
} while (!eof());
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 9eb42b8182a960d..d43b9655e947476 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -183,7 +183,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(ReflowComments);
CHECK_PARSE_BOOL(RemoveBracesLLVM);
CHECK_PARSE_BOOL(RemoveSemicolon);
- CHECK_PARSE_BOOL(SkipMacroDefinition);
+ CHECK_PARSE_BOOL(SkipMacroDefinitionBody);
CHECK_PARSE_BOOL(SpacesInSquareBrackets);
CHECK_PARSE_BOOL(SpaceInEmptyBlock);
CHECK_PARSE_BOOL(SpacesInContainerLiterals);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index df8a3141101ade6..102a7628ab5299e 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24153,9 +24153,9 @@ TEST_F(FormatTest, WhitespaceSensitiveMacros) {
verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style);
}
-TEST_F(FormatTest, SkipMacroDefinition) {
+TEST_F(FormatTest, SkipMacroDefinitionBody) {
auto Style = getLLVMStyle();
- Style.SkipMacroDefinition = true;
+ Style.SkipMacroDefinitionBody = true;
verifyFormat("#define A", "#define A", Style);
verifyFormat("#define A a aa", "#define A a aa", Style);
@@ -24256,7 +24256,7 @@ TEST_F(FormatTest, SkipMacroDefinition) {
Style);
Style.IndentPPDirectives = FormatStyle::PPDIS_None;
- // SkipMacroDefinition should not affect other PP directives
+ // SkipMacroDefinitionBody should not affect other PP directives
verifyFormat("#if !defined(A)\n"
"#define A a\n"
"#endif",
More information about the cfe-commits
mailing list