Typz created this revision.
Herald added a subscriber: klimek.

These macros are used in the body of function, and actually contain
the trailing semicolon: they should thus be automatically followed by
a new line, and not get merged with the next line.

  void foo(int a, int b) {
    Q_UNUSED(a)
    return b;
  }

This patch deals with this case, also handling the case where the
macro would be immediately followed by semicolon.


https://reviews.llvm.org/D33440

Files:
  lib/Format/FormatToken.h
  lib/Format/UnwrappedLineParser.cpp
  unittests/Format/FormatTest.cpp


Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -1976,6 +1976,16 @@
                    getLLVMStyleWithColumns(40)));
 
   verifyFormat("MACRO(>)");
+
+  // Some macros contain an implicit semicolon
+  verifyFormat("Q_UNUSED(a)\n"
+               "int b = 0;");
+  verifyFormat("Q_UNUSED(a);\n"
+               "int b = 0;");
+  verifyFormat("QT_REQUIRE_VERSION(argc, argv, \"4.0.2\")\n"
+               "int b = 0;");
+  verifyFormat("QT_REQUIRE_VERSION(argc, argv, \"4.0.2\");\n"
+               "int b = 0;");
 }
 
 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -926,6 +926,17 @@
         return;
       }
     }
+    if (Style.isCpp() && FormatTok->isOneOf(Keywords.kw_qunused,
+                                            Keywords.kw_qtrequireversion)) {
+      nextToken();
+      if (FormatTok->is(tok::l_paren)) {
+        parseParens();
+        if (FormatTok->is(tok::semi))
+          nextToken();
+        addUnwrappedLine();
+        return;
+      }
+    }
     // In all other cases, parse the declaration.
     break;
   default:
Index: lib/Format/FormatToken.h
===================================================================
--- lib/Format/FormatToken.h
+++ lib/Format/FormatToken.h
@@ -652,6 +652,8 @@
     kw_qsignals = &IdentTable.get("Q_SIGNALS");
     kw_slots = &IdentTable.get("slots");
     kw_qslots = &IdentTable.get("Q_SLOTS");
+    kw_qunused = &IdentTable.get("Q_UNUSED");
+    kw_qtrequireversion = &IdentTable.get("QT_REQUIRE_VERSION");
   }
 
   // Context sensitive keywords.
@@ -711,6 +713,8 @@
   IdentifierInfo *kw_qsignals;
   IdentifierInfo *kw_slots;
   IdentifierInfo *kw_qslots;
+  IdentifierInfo *kw_qunused;
+  IdentifierInfo *kw_qtrequireversion;
 };
 
 } // namespace format


Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -1976,6 +1976,16 @@
                    getLLVMStyleWithColumns(40)));
 
   verifyFormat("MACRO(>)");
+
+  // Some macros contain an implicit semicolon
+  verifyFormat("Q_UNUSED(a)\n"
+               "int b = 0;");
+  verifyFormat("Q_UNUSED(a);\n"
+               "int b = 0;");
+  verifyFormat("QT_REQUIRE_VERSION(argc, argv, \"4.0.2\")\n"
+               "int b = 0;");
+  verifyFormat("QT_REQUIRE_VERSION(argc, argv, \"4.0.2\");\n"
+               "int b = 0;");
 }
 
 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -926,6 +926,17 @@
         return;
       }
     }
+    if (Style.isCpp() && FormatTok->isOneOf(Keywords.kw_qunused,
+                                            Keywords.kw_qtrequireversion)) {
+      nextToken();
+      if (FormatTok->is(tok::l_paren)) {
+        parseParens();
+        if (FormatTok->is(tok::semi))
+          nextToken();
+        addUnwrappedLine();
+        return;
+      }
+    }
     // In all other cases, parse the declaration.
     break;
   default:
Index: lib/Format/FormatToken.h
===================================================================
--- lib/Format/FormatToken.h
+++ lib/Format/FormatToken.h
@@ -652,6 +652,8 @@
     kw_qsignals = &IdentTable.get("Q_SIGNALS");
     kw_slots = &IdentTable.get("slots");
     kw_qslots = &IdentTable.get("Q_SLOTS");
+    kw_qunused = &IdentTable.get("Q_UNUSED");
+    kw_qtrequireversion = &IdentTable.get("QT_REQUIRE_VERSION");
   }
 
   // Context sensitive keywords.
@@ -711,6 +713,8 @@
   IdentifierInfo *kw_qsignals;
   IdentifierInfo *kw_slots;
   IdentifierInfo *kw_qslots;
+  IdentifierInfo *kw_qunused;
+  IdentifierInfo *kw_qtrequireversion;
 };
 
 } // namespace format
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to