[clang] [clang-format] modified goto bool to enum (PR #65140)

Devansh Varshney देवांश वार्ष्णेय via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 4 11:19:09 PDT 2023


https://github.com/varshneydevansh updated https://github.com/llvm/llvm-project/pull/65140:

>From be006b494c4e34463818a7f1fd30cde9282fcea1 Mon Sep 17 00:00:00 2001
From: varshneydevansh <varshney.devansh614 at gmail.com>
Date: Thu, 31 Aug 2023 17:27:58 +0530
Subject: [PATCH 1/3] [clang] modified goto bool to enum

---
 clang/docs/ClangFormatStyleOptions.rst   | 32 ++++++++++++++++--------
 clang/include/clang/Format/Format.h      | 29 +++++++++++++++------
 clang/lib/Format/Format.cpp              | 11 +++++++-
 clang/lib/Format/UnwrappedLineParser.cpp |  2 +-
 4 files changed, 53 insertions(+), 21 deletions(-)

diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 7847f6aa5fb1c9..05ca4a4ca78f8b 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -3311,22 +3311,32 @@ the configuration (without a prefix: ``Auto``).
 
 .. _IndentGotoLabels:
 
-**IndentGotoLabels** (``Boolean``) :versionbadge:`clang-format 10` :ref:`¶ <IndentGotoLabels>`
+**IndentGotoLabels** (``enum``) :versionbadge:`clang-format 10` :ref:`¶ <IndentGotoLabels>`
   Indent goto labels.
 
-  When ``false``, goto labels are flushed left.
+  When ``GLI_None``, goto labels are flushed left.
 
   .. code-block:: c++
 
-     true:                                  false:
-     int f() {                      vs.     int f() {
-       if (foo()) {                           if (foo()) {
-       label1:                              label1:
-         bar();                                 bar();
-       }                                      }
-     label2:                                label2:
-       return 1;                              return 1;
-     }                                      }
+    GLI_Indent:                            GLI_None:
+    int f() {                      vs.     int f() {
+      if (foo()) {                           if (foo()) {
+      label1:                              label1:
+        bar();                                 bar();
+      }                                      }
+    label2:                                label2:
+      return 1;                              return 1;
+    }                                      }
+
+    GLI_HalfIndent:
+    int f() {
+      if (foo()) {
+      label1:
+        bar();
+      }
+    label2:
+      return 1;
+    }                                    }
 
 .. _IndentPPDirectives:
 
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index b641b313e32d98..754e7d6a0aa9f8 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -2421,9 +2421,8 @@ struct FormatStyle {
 
   /// Indent goto labels.
   ///
-  /// When ``false``, goto labels are flushed left.
   /// \code
-  ///    true:                                  false:
+  ///    GLI_Indent:                            GLI_None:
   ///    int f() {                      vs.     int f() {
   ///      if (foo()) {                           if (foo()) {
   ///      label1:                              label1:
@@ -2432,9 +2431,25 @@ struct FormatStyle {
   ///    label2:                                label2:
   ///      return 1;                              return 1;
   ///    }                                      }
+  ///
+  ///    GLI_HalfIndent:
+  ///    int f() {
+  ///      if (foo()) {
+  ///      label1:
+  ///        bar();
+  ///      }
+  ///    label2:
+  ///      return 1;
+  ///    }
   /// \endcode
   /// \version 10
-  bool IndentGotoLabels;
+  enum GotoLabelIndentation {
+    GLI_None,   // Do not indent goto labels.
+    GLI_Indent, // Indent goto labels at the same level as the surrounding code.
+    GLI_HalfIndent, // Indent goto labels at a half indent level.
+  };
+
+  GotoLabelIndentation IndentGotoLabels;
 
   /// Indents extern blocks
   enum IndentExternBlockStyle : int8_t {
@@ -4335,17 +4350,15 @@ struct FormatStyle {
           InEmptyParentheses(false), Other(false) {}
 
     SpacesInParensCustom(bool InConditionalStatements, bool InCStyleCasts,
-        bool InEmptyParentheses, bool Other)
+                         bool InEmptyParentheses, bool Other)
         : InConditionalStatements(InConditionalStatements),
-          InCStyleCasts(InCStyleCasts),
-          InEmptyParentheses(InEmptyParentheses),
+          InCStyleCasts(InCStyleCasts), InEmptyParentheses(InEmptyParentheses),
           Other(Other) {}
 
     bool operator==(const SpacesInParensCustom &R) const {
       return InConditionalStatements == R.InConditionalStatements &&
              InCStyleCasts == R.InCStyleCasts &&
-             InEmptyParentheses == R.InEmptyParentheses &&
-             Other == R.Other;
+             InEmptyParentheses == R.InEmptyParentheses && Other == R.Other;
     }
     bool operator!=(const SpacesInParensCustom &R) const {
       return !(*this == R);
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 339af30e38a7a8..e0f264755cf8a7 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1199,6 +1199,15 @@ template <> struct DocumentListTraits<std::vector<FormatStyle>> {
     return Seq[Index];
   }
 };
+
+template <> struct ScalarEnumerationTraits<FormatStyle::GotoLabelIndentation> {
+  static void enumeration(IO &IO, FormatStyle::GotoLabelIndentation &Value) {
+    IO.enumCase(Value, "None", FormatStyle::GLI_None);
+    IO.enumCase(Value, "Indent", FormatStyle::GLI_Indent);
+    IO.enumCase(Value, "HalfIndent", FormatStyle::GLI_HalfIndent);
+  }
+};
+
 } // namespace yaml
 } // namespace llvm
 
@@ -1478,7 +1487,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
   LLVMStyle.IndentCaseLabels = false;
   LLVMStyle.IndentCaseBlocks = false;
   LLVMStyle.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
-  LLVMStyle.IndentGotoLabels = true;
+  LLVMStyle.IndentGotoLabels = FormatStyle::GLI_Indent;
   LLVMStyle.IndentPPDirectives = FormatStyle::PPDIS_None;
   LLVMStyle.IndentRequiresClause = true;
   LLVMStyle.IndentWidth = 2;
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 5a82b200055a87..4192bfbefd03bd 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1647,7 +1647,7 @@ void UnwrappedLineParser::parseStructuralElement(
       nextToken();
       Line->Tokens.begin()->Tok->MustBreakBefore = true;
       FormatTok->setFinalizedType(TT_GotoLabelColon);
-      parseLabel(!Style.IndentGotoLabels);
+      parseLabel(Style.IndentGotoLabels != FormatStyle::GLI_None);
       if (HasLabel)
         *HasLabel = true;
       return;

>From 22acfbe5a1906ffe4844ec59a9f81b2cbfd5595e Mon Sep 17 00:00:00 2001
From: varshneydevansh <varshney.devansh614 at gmail.com>
Date: Mon, 4 Sep 2023 21:57:22 +0530
Subject: [PATCH 2/3] corrected based on feedback ran git clang-format

---
 clang/docs/ClangFormatStyleOptions.rst     | 10 ------
 clang/include/clang/Format/Format.h        | 39 ++++++++++------------
 clang/lib/Format/Format.cpp                |  5 ++-
 clang/unittests/Format/ConfigParseTest.cpp | 13 +++++++-
 4 files changed, 31 insertions(+), 36 deletions(-)

diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 05ca4a4ca78f8b..d76af9f2fcb272 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -3328,16 +3328,6 @@ the configuration (without a prefix: ``Auto``).
       return 1;                              return 1;
     }                                      }
 
-    GLI_HalfIndent:
-    int f() {
-      if (foo()) {
-      label1:
-        bar();
-      }
-    label2:
-      return 1;
-    }                                    }
-
 .. _IndentPPDirectives:
 
 **IndentPPDirectives** (``PPDirectiveIndentStyle``) :versionbadge:`clang-format 6` :ref:`¶ <IndentPPDirectives>`
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 754e7d6a0aa9f8..b313f2d290ce30 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -2423,30 +2423,23 @@ struct FormatStyle {
   ///
   /// \code
   ///    GLI_Indent:                            GLI_None:
-  ///    int f() {                      vs.     int f() {
-  ///      if (foo()) {                           if (foo()) {
-  ///      label1:                              label1:
-  ///        bar();                                 bar();
-  ///      }                                      }
-  ///    label2:                                label2:
-  ///      return 1;                              return 1;
-  ///    }                                      }
-  ///
-  ///    GLI_HalfIndent:
-  ///    int f() {
-  ///      if (foo()) {
-  ///      label1:
-  ///        bar();
-  ///      }
-  ///    label2:
-  ///      return 1;
-  ///    }
+  /// namespace NS {                  vs.     namespace NS {
+  ///   class C {                              class C {
+  ///     void foo() {                           void foo() {
+  ///       while (x) {                            while (x) {
+  ///         if (y) {                               if (y) {
+  ///         label:                               label:
+  ///         }                                      }
+  ///       }                                       }
+  ///     }                                       } 
+  ///   }                                       } 
+  /// }                                       }
   /// \endcode
   /// \version 10
+
   enum GotoLabelIndentation {
     GLI_None,   // Do not indent goto labels.
     GLI_Indent, // Indent goto labels at the same level as the surrounding code.
-    GLI_HalfIndent, // Indent goto labels at a half indent level.
   };
 
   GotoLabelIndentation IndentGotoLabels;
@@ -4350,15 +4343,17 @@ struct FormatStyle {
           InEmptyParentheses(false), Other(false) {}
 
     SpacesInParensCustom(bool InConditionalStatements, bool InCStyleCasts,
-                         bool InEmptyParentheses, bool Other)
+        bool InEmptyParentheses, bool Other)
         : InConditionalStatements(InConditionalStatements),
-          InCStyleCasts(InCStyleCasts), InEmptyParentheses(InEmptyParentheses),
+          InCStyleCasts(InCStyleCasts), 
+          InEmptyParentheses(InEmptyParentheses),
           Other(Other) {}
 
     bool operator==(const SpacesInParensCustom &R) const {
       return InConditionalStatements == R.InConditionalStatements &&
              InCStyleCasts == R.InCStyleCasts &&
-             InEmptyParentheses == R.InEmptyParentheses && Other == R.Other;
+             InEmptyParentheses == R.InEmptyParentheses && 
+             Other == R.Other;
     }
     bool operator!=(const SpacesInParensCustom &R) const {
       return !(*this == R);
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index e0f264755cf8a7..99f7ce4bff0d3d 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1202,9 +1202,8 @@ template <> struct DocumentListTraits<std::vector<FormatStyle>> {
 
 template <> struct ScalarEnumerationTraits<FormatStyle::GotoLabelIndentation> {
   static void enumeration(IO &IO, FormatStyle::GotoLabelIndentation &Value) {
-    IO.enumCase(Value, "None", FormatStyle::GLI_None);
-    IO.enumCase(Value, "Indent", FormatStyle::GLI_Indent);
-    IO.enumCase(Value, "HalfIndent", FormatStyle::GLI_HalfIndent);
+    IO.enumCase(Value, "false", FormatStyle::GLI_None);
+    IO.enumCase(Value, "true", FormatStyle::GLI_Indent);
   }
 };
 
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index b3b6755cadee4b..a0f138506d3d2f 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -141,6 +141,16 @@ TEST(ConfigParseTest, GetsCorrectBasedOnStyle) {
   EXPECT_EQ(0, parseConfiguration(#STRUCT ":\n  " TEXT, &Style).value());      \
   EXPECT_EQ(VALUE, Style.STRUCT.FIELD) << "Unexpected value after parsing!"
 
+#define CHECK_PARSE_ENUM_FIELD(FIELD, CONFIG_NAME, ENUM_VAL)
+Style.FIELD = FormatStyle::GotoLabelIndentation::GLI_None;
+EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());
+EXPECT_EQ(ENUM_VAL, Style.FIELD);
+EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());
+EXPECT_EQ(FormatStyle::GotoLabelIndentation::GLI_None, Style.FIELD)
+
+#define CHECK_PARSE_ENUM(FIELD, ENUM_VAL)                                      \
+  CHECK_PARSE_ENUM_FIELD(FIELD, #FIELD, ENUM_VAL)
+
 TEST(ConfigParseTest, ParsesConfigurationBools) {
   FormatStyle Style = {};
   Style.Language = FormatStyle::LK_Cpp;
@@ -161,7 +171,8 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
   CHECK_PARSE_BOOL(IndentAccessModifiers);
   CHECK_PARSE_BOOL(IndentCaseLabels);
   CHECK_PARSE_BOOL(IndentCaseBlocks);
-  CHECK_PARSE_BOOL(IndentGotoLabels);
+  CHECK_PARSE_ENUM(IndentGotoLabels,
+                   FormatStyle::GotoLabelIndentation::GLI_Indent);
   CHECK_PARSE_BOOL_FIELD(IndentRequiresClause, "IndentRequires");
   CHECK_PARSE_BOOL(IndentRequiresClause);
   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);

>From 6ca74b455f8c633778c873caa8bc87832a82e268 Mon Sep 17 00:00:00 2001
From: varshneydevansh <varshney.devansh614 at gmail.com>
Date: Mon, 4 Sep 2023 23:48:22 +0530
Subject: [PATCH 3/3] removed trailing spaces for ci check-format

---
 clang/include/clang/Format/Format.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index b313f2d290ce30..4bc67d1908460c 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -2431,8 +2431,8 @@ struct FormatStyle {
   ///         label:                               label:
   ///         }                                      }
   ///       }                                       }
-  ///     }                                       } 
-  ///   }                                       } 
+  ///     }                                       }
+  ///   }                                       }
   /// }                                       }
   /// \endcode
   /// \version 10
@@ -4345,14 +4345,14 @@ struct FormatStyle {
     SpacesInParensCustom(bool InConditionalStatements, bool InCStyleCasts,
         bool InEmptyParentheses, bool Other)
         : InConditionalStatements(InConditionalStatements),
-          InCStyleCasts(InCStyleCasts), 
+          InCStyleCasts(InCStyleCasts),
           InEmptyParentheses(InEmptyParentheses),
           Other(Other) {}
 
     bool operator==(const SpacesInParensCustom &R) const {
       return InConditionalStatements == R.InConditionalStatements &&
              InCStyleCasts == R.InCStyleCasts &&
-             InEmptyParentheses == R.InEmptyParentheses && 
+             InEmptyParentheses == R.InEmptyParentheses &&
              Other == R.Other;
     }
     bool operator!=(const SpacesInParensCustom &R) const {



More information about the cfe-commits mailing list