Manikishan created this revision.
Manikishan added reviewers: cfe-commits, mgorny, christos, MyDeveloperDay, 
rdwampler, lebedev.ri.
Manikishan added a project: clang.
Herald added a subscriber: krytarowski.

Patch: SpacesBeforeTrailingBlockComments

This patch is to support ```spacesBeforeTrailingComments``` to support spaces 
before  Trailing BlockComments. According to the Documentation, this was not 
implemented because block comments have different usage patterns and a various 
number of special cases. I am trying to cover as many cases as possible which 
can be useful.
This patch covers some cases such as Declarations, definitions, and Includes

This is also under the Project of Adding NetBSD-KNF support to clang-format.

Example for supported cases:

  Int a;        \*foo *\
  int b;        \*bar *\
  Int c;        \*baz *\
  
  #include ads.h                \*foo *\
  #include bar.h                \*bar *\

These following tests fail for this patch:

1. FormatTests/FormatTestComments.UnderstandsBlockComments
2. FormatTests/FormatTestJS.AddsLastLinePenaltyIfEndingIsBroken
3. FormatTests/FormatTestJS.TemplateStrings

I have to discuss whether to add support to those cases because I think the 
tests need to be modified while implementing this style.

I would like to discuss more one this specific style to know which cases I 
could work on, as it was chosen not to support. 
It will be good if I can get more inputs on the cases I could cover.


Repository:
  rC Clang

https://reviews.llvm.org/D65648

Files:
  lib/Format/TokenAnnotator.cpp
  unittests/Format/FormatTest.cpp
  unittests/Format/FormatTestComments.cpp


Index: unittests/Format/FormatTestComments.cpp
===================================================================
--- unittests/Format/FormatTestComments.cpp
+++ unittests/Format/FormatTestComments.cpp
@@ -2600,6 +2600,33 @@
                "  // b");
 }
 
+TEST_F(FormatTestComments, SpacesBeforeTrailingBlockComments){
+  FormatStyle Style = getGoogleStyle();
+  Style.SpacesBeforeTrailingComments = 4;
+  EXPECT_EQ("int a;    /*a*/\n"
+            "int b;    /*a*/\n"
+            "int c;    /*a*/\n"
+            "int d;    /*a*/\n"
+            "int e;    /*a*/\n"
+            "int f;    /*a*/\n"
+            "int g;    /*a*/\n"
+            "int h;    /*a*/",
+            format("int a; /*a*/\n"
+                   "int b; /*a*/\n"
+                   "int c; /*a*/\n"
+                   "int d; /*a*/\n"
+                   "int e; /*a*/\n"
+                   "int f; /*a*/\n"
+                   "int g; /*a*/\n"
+                   "int h; /*a*/", Style));
+  EXPECT_EQ("#define A           \\\n"
+            "  int i;      /*a*/ \\\n"
+            "  int jjj;    /*b*/",
+            format("#define A        \\\n"
+                   "  int i;   /*a*/ \\\n"
+                   "  int jjj; /*b*/", Style));
+
+}
 TEST_F(FormatTestComments, AlignTrailingComments) {
   EXPECT_EQ("#define MACRO(V)                       \\\n"
             "  V(Rt2) /* one more char */           \\\n"
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -3659,6 +3659,8 @@
 }
 
 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
+  FormatStyle Style = getGoogleStyle();
+  Style.SpacesBeforeTrailingComments = 0;
   EXPECT_EQ("#define MACRO()                     \\\n"
             "  Debug(aaa, /* force line break */ \\\n"
             "        {                           \\\n"
@@ -3667,7 +3669,7 @@
             "        })",
             format("#define   MACRO()   Debug(aaa,  /* force line break */ 
\\\n"
                    "          {  int   i;  int  j;   })",
-                   getGoogleStyle()));
+                   Style));
 
   EXPECT_EQ("#define A                                       \\\n"
             "  [] {                                          \\\n"
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -2159,6 +2159,14 @@
   while (Current) {
     if (isFunctionDeclarationName(*Current, Line))
       Current->Type = TT_FunctionDeclarationName;
+    if (Current->is(TT_BlockComment)){
+      std::cout << "TYPE"<<Line.Type<<"\n";
+      if ((Line.Type != LT_PreprocessorDirective)){
+        if (!Current->Previous->isOneOf(TT_TemplateCloser,tok::l_paren) && 
Current->isTrailingComment()){
+          Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
+        }
+      }
+    }
     if (Current->is(TT_LineComment)) {
       if (Current->Previous->BlockKind == BK_BracedInit &&
           Current->Previous->opensScope())


Index: unittests/Format/FormatTestComments.cpp
===================================================================
--- unittests/Format/FormatTestComments.cpp
+++ unittests/Format/FormatTestComments.cpp
@@ -2600,6 +2600,33 @@
                "  // b");
 }
 
+TEST_F(FormatTestComments, SpacesBeforeTrailingBlockComments){
+  FormatStyle Style = getGoogleStyle();
+  Style.SpacesBeforeTrailingComments = 4;
+  EXPECT_EQ("int a;    /*a*/\n"
+            "int b;    /*a*/\n"
+            "int c;    /*a*/\n"
+            "int d;    /*a*/\n"
+            "int e;    /*a*/\n"
+            "int f;    /*a*/\n"
+            "int g;    /*a*/\n"
+            "int h;    /*a*/",
+            format("int a; /*a*/\n"
+                   "int b; /*a*/\n"
+                   "int c; /*a*/\n"
+                   "int d; /*a*/\n"
+                   "int e; /*a*/\n"
+                   "int f; /*a*/\n"
+                   "int g; /*a*/\n"
+                   "int h; /*a*/", Style));
+  EXPECT_EQ("#define A           \\\n"
+            "  int i;      /*a*/ \\\n"
+            "  int jjj;    /*b*/",
+            format("#define A        \\\n"
+                   "  int i;   /*a*/ \\\n"
+                   "  int jjj; /*b*/", Style));
+
+}
 TEST_F(FormatTestComments, AlignTrailingComments) {
   EXPECT_EQ("#define MACRO(V)                       \\\n"
             "  V(Rt2) /* one more char */           \\\n"
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -3659,6 +3659,8 @@
 }
 
 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
+  FormatStyle Style = getGoogleStyle();
+  Style.SpacesBeforeTrailingComments = 0;
   EXPECT_EQ("#define MACRO()                     \\\n"
             "  Debug(aaa, /* force line break */ \\\n"
             "        {                           \\\n"
@@ -3667,7 +3669,7 @@
             "        })",
             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
                    "          {  int   i;  int  j;   })",
-                   getGoogleStyle()));
+                   Style));
 
   EXPECT_EQ("#define A                                       \\\n"
             "  [] {                                          \\\n"
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -2159,6 +2159,14 @@
   while (Current) {
     if (isFunctionDeclarationName(*Current, Line))
       Current->Type = TT_FunctionDeclarationName;
+    if (Current->is(TT_BlockComment)){
+      std::cout << "TYPE"<<Line.Type<<"\n";
+      if ((Line.Type != LT_PreprocessorDirective)){
+        if (!Current->Previous->isOneOf(TT_TemplateCloser,tok::l_paren) && Current->isTrailingComment()){
+          Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
+        }
+      }
+    }
     if (Current->is(TT_LineComment)) {
       if (Current->Previous->BlockKind == BK_BracedInit &&
           Current->Previous->opensScope())
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D65648: [cla... Manikishan Ghantasala via Phabricator via cfe-commits

Reply via email to