[clang] [clang-format] Option to ignore macro definitions (PR #70338)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 23 06:48:15 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/12] 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/12] 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/12] 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/12] 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/12] 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/12] 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/12] 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/12] 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/12] 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/12] 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/12] 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/12] 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",



More information about the cfe-commits mailing list