Hi

The patch implements 3 types of pointer bindings:
type *var;
type* var;
type * var;

Feature requested in bug 19929.

Cheers,
Janusz
Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h	(revision 211013)
+++ include/clang/Format/Format.h	(working copy)
@@ -85,8 +85,18 @@
   /// \brief The penalty for breaking a function call after "call(".
   unsigned PenaltyBreakBeforeFirstCallParameter;
 
+  /// \brief The & and * binding style.
+  enum PointerBindingStyle {
+    /// Bind pointer to type.
+    PBS_Type,
+    /// Bind pointer to variable.
+    PBS_Variable,
+    /// Keep the pointer separated by space from type and variable.
+    PBS_None
+  };
+
   /// \brief Set whether & and * bind to the type as opposed to the variable.
-  bool PointerBindsToType;
+  PointerBindingStyle PointerBinding;
 
   /// \brief If \c true, analyze the formatted file for the most common binding
   /// and use \c PointerBindsToType only as fallback.
@@ -391,7 +401,7 @@
            PenaltyBreakString == R.PenaltyBreakString &&
            PenaltyExcessCharacter == R.PenaltyExcessCharacter &&
            PenaltyReturnTypeOnItsOwnLine == R.PenaltyReturnTypeOnItsOwnLine &&
-           PointerBindsToType == R.PointerBindsToType &&
+           PointerBinding == R.PointerBinding &&
            SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments &&
            Cpp11BracedListStyle == R.Cpp11BracedListStyle &&
            Standard == R.Standard && TabWidth == R.TabWidth &&
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp	(revision 211013)
+++ lib/Format/Format.cpp	(working copy)
@@ -97,6 +97,16 @@
 };
 
 template <>
+struct ScalarEnumerationTraits<FormatStyle::PointerBindingStyle> {
+  static void enumeration(IO &IO,
+                          FormatStyle::PointerBindingStyle &Value) {
+    IO.enumCase(Value, "None", FormatStyle::PBS_None);
+    IO.enumCase(Value, "Type", FormatStyle::PBS_Type);
+    IO.enumCase(Value, "Variable", FormatStyle::PBS_Variable);
+  }
+};
+
+template <>
 struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensOptions> {
   static void enumeration(IO &IO,
                           FormatStyle::SpaceBeforeParensOptions &Value) {
@@ -193,7 +203,7 @@
     IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
     IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
                    Style.PenaltyReturnTypeOnItsOwnLine);
-    IO.mapOptional("PointerBindsToType", Style.PointerBindsToType);
+    IO.mapOptional("PointerBinding", Style.PointerBinding);
     IO.mapOptional("SpacesBeforeTrailingComments",
                    Style.SpacesBeforeTrailingComments);
     IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle);
@@ -320,7 +330,7 @@
   LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
   LLVMStyle.ObjCSpaceAfterProperty = false;
   LLVMStyle.ObjCSpaceBeforeProtocolList = true;
-  LLVMStyle.PointerBindsToType = false;
+  LLVMStyle.PointerBinding = FormatStyle::PBS_Variable;
   LLVMStyle.SpacesBeforeTrailingComments = 1;
   LLVMStyle.Standard = FormatStyle::LS_Cpp11;
   LLVMStyle.UseTab = FormatStyle::UT_Never;
@@ -361,7 +371,7 @@
   GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
   GoogleStyle.ObjCSpaceAfterProperty = false;
   GoogleStyle.ObjCSpaceBeforeProtocolList = false;
-  GoogleStyle.PointerBindsToType = true;
+  GoogleStyle.PointerBinding = FormatStyle::PBS_Type;
   GoogleStyle.SpacesBeforeTrailingComments = 2;
   GoogleStyle.Standard = FormatStyle::LS_Auto;
 
@@ -402,7 +412,7 @@
   MozillaStyle.ObjCSpaceAfterProperty = true;
   MozillaStyle.ObjCSpaceBeforeProtocolList = false;
   MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
-  MozillaStyle.PointerBindsToType = true;
+  MozillaStyle.PointerBinding = FormatStyle::PBS_Type;
   MozillaStyle.Standard = FormatStyle::LS_Cpp03;
   return MozillaStyle;
 }
@@ -419,7 +429,7 @@
   Style.IndentWidth = 4;
   Style.NamespaceIndentation = FormatStyle::NI_Inner;
   Style.ObjCSpaceAfterProperty = true;
-  Style.PointerBindsToType = true;
+  Style.PointerBinding = FormatStyle::PBS_Type;
   Style.Standard = FormatStyle::LS_Cpp03;
   return Style;
 }
@@ -1908,9 +1918,9 @@
     }
     if (Style.DerivePointerBinding) {
       if (CountBoundToType > CountBoundToVariable)
-        Style.PointerBindsToType = true;
+        Style.PointerBinding = FormatStyle::PBS_Type;
       else if (CountBoundToType < CountBoundToVariable)
-        Style.PointerBindsToType = false;
+        Style.PointerBinding = FormatStyle::PBS_Variable;
     }
     if (Style.Standard == FormatStyle::LS_Auto) {
       Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp	(revision 211013)
+++ lib/Format/TokenAnnotator.cpp	(working copy)
@@ -1460,14 +1460,14 @@
   if (Right.Type == TT_PointerOrReference)
     return Left.Tok.isLiteral() ||
            ((Left.Type != TT_PointerOrReference) && Left.isNot(tok::l_paren) &&
-            !Style.PointerBindsToType);
+            Style.PointerBinding != FormatStyle::PBS_Type);
   if (Right.Type == TT_FunctionTypeLParen && Left.isNot(tok::l_paren) &&
-      (Left.Type != TT_PointerOrReference || Style.PointerBindsToType))
+      (Left.Type != TT_PointerOrReference || Style.PointerBinding != FormatStyle::PBS_Variable))
     return true;
   if (Left.Type == TT_PointerOrReference)
     return Right.Tok.isLiteral() || Right.Type == TT_BlockComment ||
            ((Right.Type != TT_PointerOrReference) &&
-            Right.isNot(tok::l_paren) && Style.PointerBindsToType &&
+            Right.isNot(tok::l_paren) && Style.PointerBinding != FormatStyle::PBS_Variable &&
             Left.Previous &&
             !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
   if (Right.is(tok::star) && Left.is(tok::l_paren))
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp	(revision 211013)
+++ unittests/Format/FormatTest.cpp	(working copy)
@@ -4587,7 +4587,7 @@
                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
                "}");
   FormatStyle Style = getLLVMStyle();
-  Style.PointerBindsToType = true;
+  Style.PointerBinding = FormatStyle::PBS_Type;
   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
 }
 
@@ -4823,7 +4823,7 @@
   verifyGoogleFormat("T** t = new T*();");
 
   FormatStyle PointerLeft = getLLVMStyle();
-  PointerLeft.PointerBindsToType = true;
+  PointerLeft.PointerBinding = FormatStyle::PBS_Type;
   verifyFormat("delete *x;", PointerLeft);
   verifyFormat("STATIC_ASSERT((a & b) == 0);");
   verifyFormat("STATIC_ASSERT(0 == (a & b));");
@@ -4857,6 +4857,12 @@
   // FIXME: We cannot handle this case yet; we might be able to figure out that
   // foo<x> d > v; doesn't make sense.
   verifyFormat("foo<a < b && c> d > v;");
+
+  FormatStyle PointerNone = getLLVMStyle();
+  PointerNone.PointerBinding = FormatStyle::PBS_None;
+  verifyFormat("delete *x;", PointerNone);
+  verifyFormat("int * x;", PointerNone);
+  verifyFormat("template <int * y> f() {}", PointerNone);
 }
 
 TEST_F(FormatTest, UnderstandsAttributes) {
@@ -4871,7 +4877,7 @@
   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
 
   FormatStyle PointersLeft = getLLVMStyle();
-  PointersLeft.PointerBindsToType = true;
+  PointersLeft.PointerBinding = FormatStyle::PBS_Type;
   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
 }
 
@@ -8076,7 +8082,6 @@
   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
-  CHECK_PARSE_BOOL(PointerBindsToType);
   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
   CHECK_PARSE_BOOL(IndentFunctionDeclarationAfterType);
   CHECK_PARSE_BOOL(SpacesInParentheses);
@@ -8101,6 +8106,11 @@
   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
 
+  Style.PointerBinding = FormatStyle::PBS_None;
+  CHECK_PARSE("PointerBinding: Type", PointerBinding, FormatStyle::PBS_Type);
+  CHECK_PARSE("PointerBinding: Variable", PointerBinding, FormatStyle::PBS_Variable);
+  CHECK_PARSE("PointerBinding: None", PointerBinding, FormatStyle::PBS_None);
+
   Style.Standard = FormatStyle::LS_Auto;
   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to