]> granicus.if.org Git - clang/commitdiff
Add style option for number of spaces before trailing comments.
authorDaniel Jasper <djasper@google.com>
Mon, 7 Jan 2013 11:09:06 +0000 (11:09 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 7 Jan 2013 11:09:06 +0000 (11:09 +0000)
In LLVM style, a single space should be enough. In Google style, two
spaces are required.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171725 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Format/Format.h
lib/Format/Format.cpp
unittests/Format/FormatTest.cpp

index 237c3e4c3b23e660f36de127cc3774c06531da39..f8ec26af2e59d9896e42929c88e6082735aefc0a 100644 (file)
@@ -52,6 +52,9 @@ struct FormatStyle {
   /// When false, use the same indentation level as for the switch statement.
   /// Switch statement body is always indented one level more than case labels.
   bool IndentCaseLabels;
+
+  /// \brief The number of spaces to before trailing line comments.
+  unsigned SpacesBeforeTrailingComments;
 };
 
 /// \brief Returns a format style complying with the LLVM coding standards:
index 3dbf95c5a40539d3b8d790baea8eefdce56eae9c..5c65c26ca67c2da5e5179ef8497b8fd60788cb27 100644 (file)
@@ -77,6 +77,7 @@ FormatStyle getLLVMStyle() {
   LLVMStyle.AccessModifierOffset = -2;
   LLVMStyle.SplitTemplateClosingGreater = true;
   LLVMStyle.IndentCaseLabels = false;
+  LLVMStyle.SpacesBeforeTrailingComments = 1;
   return LLVMStyle;
 }
 
@@ -88,6 +89,7 @@ FormatStyle getGoogleStyle() {
   GoogleStyle.AccessModifierOffset = -1;
   GoogleStyle.SplitTemplateClosingGreater = false;
   GoogleStyle.IndentCaseLabels = true;
+  GoogleStyle.SpacesBeforeTrailingComments = 2;
   return GoogleStyle;
 }
 
@@ -299,7 +301,7 @@ private:
 
       unsigned Spaces = Annotations[Index].SpaceRequiredBefore ? 1 : 0;
       if (Annotations[Index].Type == TT_LineComment)
-        Spaces = 2;
+        Spaces = Style.SpacesBeforeTrailingComments;
 
       if (!DryRun)
         replaceWhitespace(Current, 0, Spaces);
index db822e7b373e220500f62794a5ba11b3d55f8573..a743d1b2b59465ef68e2a33811f7114482c9e49c 100644 (file)
@@ -278,21 +278,23 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
                "  // Doesn't do anything\n"
                "}");
 
-  verifyFormat("int i  // This is a fancy variable\n"
+  verifyFormat("int i // This is a fancy variable\n"
                "    = 5;");
 
   verifyFormat("enum E {\n"
                "  // comment\n"
-               "  VAL_A,  // comment\n"
+               "  VAL_A, // comment\n"
                "  VAL_B\n"
                "};");
 
   verifyFormat(
       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
-      "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;  // Trailing comment");
+      "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
 
-  EXPECT_EQ("int i;  // single line trailing comment",
+  EXPECT_EQ("int i; // single line trailing comment",
             format("int i;\\\n// single line trailing comment"));
+
+  verifyGoogleFormat("int a;  // Trailing comment.");
 }
 
 TEST_F(FormatTest, UnderstandsMultiLineComments) {