]> granicus.if.org Git - clang/commitdiff
Do not ever allow using the full line in preprocessor directives.
authorManuel Klimek <klimek@google.com>
Mon, 7 Jan 2013 09:24:17 +0000 (09:24 +0000)
committerManuel Klimek <klimek@google.com>
Mon, 7 Jan 2013 09:24:17 +0000 (09:24 +0000)
We would format:
  #define A \
    int f(a); int i;
as
  #define A \
    int f(a);\
    int i

The fix will break up macro definitions that could fit a line, but hit
the last column; fixing that is more involved, though, as it requires
looking at the following line.

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

lib/Format/Format.cpp
unittests/Format/FormatTest.cpp

index 35c1e403c6dcb0eab139ac79d385dc1cffca8287..7b3c575d0b179c8892c72986741f079643d88148 100644 (file)
@@ -137,7 +137,7 @@ public:
       // FIXME: We need to check whether we're in a preprocessor directive, even
       // if all tokens fit - the next line might be a preprocessor directive,
       // too, in which case we need to account for the possible escaped newline.
-      if (Columns > Style.ColumnLimit ||
+      if (Columns > Style.ColumnLimit - (Line.InPPDirective ? 1 : 0) ||
           (Annotations[i].MustBreakBefore &&
            Annotations[i].Type != TokenAnnotation::TT_CtorInitializerColon)) {
         FitsOnALine = false;
index 27ca53c1eab5f691b079a47b66d741e71b0bb90a..54999e1f634c5633d7d5f207fed471c778586cf9 100644 (file)
@@ -413,11 +413,13 @@ TEST_F(FormatTest, EndOfFileEndsPPDirective) {
 }
 
 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
-  // If the macro fits in one line, we have the full width.
-  verifyFormat("#define A(B)", getLLVMStyleWithColumns(12));
+  // If the macro fits in one line, we still do not get the full
+  // line, as only the next line decides whether we need an escaped newline and
+  // thus use the last column.
+  verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
 
-  verifyFormat("#define A(\\\n    B)", getLLVMStyleWithColumns(11));
-  verifyFormat("#define AA(\\\n    B)", getLLVMStyleWithColumns(11));
+  verifyFormat("#define A( \\\n    B)", getLLVMStyleWithColumns(12));
+  verifyFormat("#define AA(\\\n    B)", getLLVMStyleWithColumns(12));
   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
 }
 
@@ -490,6 +492,13 @@ TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
                    getLLVMStyleWithColumns(11)));
 }
 
+TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
+  verifyFormat("#define A \\\n"
+               "  int v(  \\\n"
+               "      a); \\\n"
+               "  int i;", getLLVMStyleWithColumns(11));
+}
+
 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
   EXPECT_EQ(
       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("