r181590 - Always format entire macro definitions.

Daniel Jasper djasper at google.com
Fri May 10 06:00:50 PDT 2013


Author: djasper
Date: Fri May 10 08:00:49 2013
New Revision: 181590

URL: http://llvm.org/viewvc/llvm-project?rev=181590&view=rev
Log:
Always format entire macro definitions.

Thereby, the macro is consistently formatted (including the trailing
escaped newlines) even if clang-format is invoked only on single lines
of the macro.

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=181590&r1=181589&r2=181590&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Fri May 10 08:00:49 2013
@@ -1118,12 +1118,21 @@ public:
     std::vector<int> IndentForLevel;
     bool PreviousLineWasTouched = false;
     const AnnotatedToken *PreviousLineLastToken = 0;
+    bool FormatPPDirective = false;
     for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(),
                                               E = AnnotatedLines.end();
          I != E; ++I) {
       const AnnotatedLine &TheLine = *I;
       const FormatToken &FirstTok = TheLine.First.FormatTok;
       int Offset = getIndentOffset(TheLine.First);
+
+      // Check whether this line is part of a formatted preprocessor directive.
+      if (FirstTok.HasUnescapedNewline)
+        FormatPPDirective = false;
+      if (!FormatPPDirective && TheLine.InPPDirective &&
+          (touchesLine(TheLine) || touchesPPDirective(I + 1, E)))
+        FormatPPDirective = true;
+
       while (IndentForLevel.size() <= TheLine.Level)
         IndentForLevel.push_back(-1);
       IndentForLevel.resize(TheLine.Level + 1);
@@ -1135,7 +1144,7 @@ public:
                                         /*WhitespaceStartColumn*/ 0);
         }
       } else if (TheLine.Type != LT_Invalid &&
-                 (WasMoved || touchesLine(TheLine))) {
+                 (WasMoved || FormatPPDirective || touchesLine(TheLine))) {
         unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level);
         unsigned Indent = LevelIndent;
         if (static_cast<int>(Indent) + Offset >= 0)
@@ -1412,6 +1421,17 @@ private:
     return touchesRanges(LineRange);
   }
 
+  bool touchesPPDirective(std::vector<AnnotatedLine>::iterator I,
+                          std::vector<AnnotatedLine>::iterator E) {
+    for (; I != E; ++I) {
+      if (I->First.FormatTok.HasUnescapedNewline)
+        return false;
+      if (touchesLine(*I))
+        return true;
+    }
+    return false;
+  }
+
   bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) {
     const FormatToken *First = &TheLine.First.FormatTok;
     CharSourceRange LineRange = CharSourceRange::getCharRange(

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=181590&r1=181589&r2=181590&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri May 10 08:00:49 2013
@@ -1289,9 +1289,29 @@ TEST_F(FormatTest, LayoutCodeInMacroDefi
 
 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
 
-TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
-  EXPECT_EQ("# define A\\\n  b;",
-            format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
+TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) {
+  EXPECT_EQ("int  i;\n"
+            "#define A \\\n"
+            "  int i;  \\\n"
+            "  int j\n"
+            "int  k;",
+            format("int  i;\n"
+                   "#define A  \\\n"
+                   " int   i    ;  \\\n"
+                   " int   j\n"
+                   "int  k;",
+                   8, 0, getGoogleStyle())); // 8: position of "#define".
+  EXPECT_EQ("int  i;\n"
+            "#define A \\\n"
+            "  int i;  \\\n"
+            "  int j\n"
+            "int  k;",
+            format("int  i;\n"
+                   "#define A  \\\n"
+                   " int   i    ;  \\\n"
+                   " int   j\n"
+                   "int  k;",
+                   45, 0, getGoogleStyle())); // 45: position of "j".
 }
 
 TEST_F(FormatTest, MacroDefinitionInsideStatement) {





More information about the cfe-commits mailing list