[PATCH] D33440: clang-format: properly handle Q_UNUSED and QT_REQUIRE_VERSION

Francois Ferrand via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 23 07:27:53 PDT 2017


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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33440.99910.patch
Type: text/x-patch
Size: 2074 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170523/8284e36d/attachment.bin>


More information about the cfe-commits mailing list