]> granicus.if.org Git - clang/commitdiff
clang-format: Improve free-standing macro detection.
authorDaniel Jasper <djasper@google.com>
Wed, 5 Nov 2014 10:48:04 +0000 (10:48 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 5 Nov 2014 10:48:04 +0000 (10:48 +0000)
Before:
  SOME_WEIRD_LOG_MACRO
  << "Something long enough to cause a line break";

After:
  SOME_WEIRD_LOG_MACRO
      << "Something long enough to cause a line break";

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

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

index 0b37acbebf9ea95bf54afcf925ac2f47d9fef52c..10b4aeaca333c9021d5c55289e1c7e6fa08135ef 100644 (file)
@@ -812,17 +812,14 @@ void UnwrappedLineParser::parseStructuralElement() {
           parseLabel();
           return;
         }
-        // Recognize function-like macro usages without trailing semicolon.
-        if (FormatTok->Tok.is(tok::l_paren)) {
+        // Recognize function-like macro usages without trailing semicolon as
+        // well as free-standing macrose like Q_OBJECT.
+        bool FunctionLike = FormatTok->is(tok::l_paren);
+        if (FunctionLike)
           parseParens();
-          if (FormatTok->NewlinesBefore > 0 &&
-              tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
-            addUnwrappedLine();
-            return;
-          }
-        } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
-                   Text == Text.upper()) {
-          // Recognize free-standing macros like Q_OBJECT.
+        if (FormatTok->NewlinesBefore > 0 &&
+            (Text.size() >= 5 || FunctionLike) &&
+            tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
           addUnwrappedLine();
           return;
         }
index cd8326510668f8d44d8e80540a96639e13967248..9e63bd11bb3bc4645d0f837497c57d4d82f4420b 100644 (file)
@@ -2569,6 +2569,11 @@ TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
                    "     Q_Object\n"
                    "  A() {\n}\n"
                    "}  ;"));
+
+  // Only if the next line can actually start an unwrapped line.
+  EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
+            format("SOME_WEIRD_LOG_MACRO\n"
+                   "<< SomeThing;"));
 }
 
 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {