[cfe-commits] r172423 - in /cfe/trunk: lib/Format/Format.cpp unittests/Format/FormatTest.cpp

Daniel Jasper djasper at google.com
Mon Jan 14 07:40:57 PST 2013


Author: djasper
Date: Mon Jan 14 09:40:57 2013
New Revision: 172423

URL: http://llvm.org/viewvc/llvm-project?rev=172423&view=rev
Log:
Put simple preprocessor directives on a single line.

Before: #define A  \
          A
After:  #define A A

Modified:
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=172423&r1=172422&r2=172423&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Jan 14 09:40:57 2013
@@ -185,6 +185,7 @@
 static bool fitsIntoLimit(const AnnotatedToken &RootToken, unsigned Limit,
                           unsigned *Length = 0) {
   unsigned Columns = RootToken.FormatTok.TokenLength;
+  if (Columns > Limit) return false;
   const AnnotatedToken *Tok = &RootToken;
   while (!Tok->Children.empty()) {
     Tok = &Tok->Children[0];
@@ -1415,10 +1416,25 @@
       tryMergeSimpleBlock(I, E, Limit);
     } else if (I->First.is(tok::kw_if)) {
       tryMergeSimpleIf(I, E, Limit);
+    } else if (I->InPPDirective && (I->First.FormatTok.HasUnescapedNewline ||
+                                    I->First.FormatTok.IsFirst)) {
+      tryMergeSimplePPDirective(I, E, Limit);
     }
     return true;
   }
 
+  void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I,
+                                 std::vector<AnnotatedLine>::iterator E,
+                                 unsigned Limit) {
+    AnnotatedLine &Line = *I;
+    if (!(I + 1)->InPPDirective) return;
+    if (I + 2 != E && (I + 2)->InPPDirective &&
+        !(I + 2)->First.FormatTok.HasUnescapedNewline)
+      return;
+    if (!fitsIntoLimit((I + 1)->First, Limit)) return;
+    join(Line, *(++I));
+  }
+
   void tryMergeSimpleIf(std::vector<AnnotatedLine>::iterator &I,
                         std::vector<AnnotatedLine>::iterator E,
                         unsigned Limit) {

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=172423&r1=172422&r2=172423&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Jan 14 09:40:57 2013
@@ -50,8 +50,8 @@
         if (JustReplacedNewline)
           MessedUp[i - 1] = '\n';
         InComment = true;
-      } else if (MessedUp[i] == '#' && JustReplacedNewline) {
-        MessedUp[i - 1] = '\n';
+      } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) {
+        if (i != 0) MessedUp[i - 1] = '\n';
         InPreprocessorDirective = true;
       } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
         MessedUp[i] = ' ';
@@ -469,7 +469,7 @@
 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
   EXPECT_EQ("#line 42 \"test\"\n",
             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
-  EXPECT_EQ("#define A  \\\n  B\n",
+  EXPECT_EQ("#define A B\n",
             format("#  \\\n define  \\\n    A  \\\n       B\n",
                    getLLVMStyleWithColumns(12)));
 }
@@ -477,9 +477,8 @@
 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
   EXPECT_EQ("#line 42 \"test\"",
             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
-  EXPECT_EQ("#define A  \\\n  B",
-            format("#  \\\n define  \\\n    A  \\\n       B",
-                   getLLVMStyleWithColumns(12)));
+  EXPECT_EQ("#define A B",
+            format("#  \\\n define  \\\n    A  \\\n       B"));
 }
 
 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
@@ -491,6 +490,13 @@
   verifyFormat("#define A( \\\n    B)", getLLVMStyleWithColumns(12));
   verifyFormat("#define AA(\\\n    B)", getLLVMStyleWithColumns(12));
   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
+
+  verifyFormat("#define A A\n#define A A");
+  verifyFormat("#define A(X) A\n#define A A");
+
+  verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
+  verifyFormat("#define Something     \\\n"
+               "  Other", getLLVMStyleWithColumns(23));
 }
 
 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
@@ -548,8 +554,7 @@
   verifyFormat("#define A(a, b, c)   \\\n"
                "  void a##b##c()", getLLVMStyleWithColumns(22));
 
-  verifyFormat("#define A            \\\n"
-               "  void # ## #", getLLVMStyleWithColumns(22));
+  verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
 }
 
 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
@@ -605,8 +610,7 @@
       "functionCallTo(someOtherFunction(\n"
       "    withSomeParameters, whichInSequence,\n"
       "    areLongerThanALine(andAnotherCall,\n"
-      "#define A                                                           \\\n"
-      "  B\n"
+      "#define A B\n"
       "                       withMoreParamters,\n"
       "                       whichStronglyInfluenceTheLayout),\n"
       "    andMoreParameters),\n"





More information about the cfe-commits mailing list