clang-format: Don't add spaces in foreach macro definition.
authorDaniel Jasper <djasper@google.com>
Thu, 11 Jun 2015 08:38:19 +0000 (08:38 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 11 Jun 2015 08:38:19 +0000 (08:38 +0000)
Before clang-format would e.g. add a space into

   #define Q_FOREACH(x, y)

which turns this into a non-function-like macro.

Patch by Strager Neds, thank you!

llvm-svn: 239513

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

index c725b4b..a774f8c 100644 (file)
@@ -1122,7 +1122,10 @@ private:
       Column = FormatTok->LastLineColumnWidth;
     }
 
-    if (std::find(ForEachMacros.begin(), ForEachMacros.end(),
+    if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() &&
+          Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() ==
+              tok::pp_define) &&
+        std::find(ForEachMacros.begin(), ForEachMacros.end(),
                   FormatTok->Tok.getIdentifierInfo()) != ForEachMacros.end())
       FormatTok->Type = TT_ForEachMacro;
 
index c4aa712..5b3980b 100644 (file)
@@ -616,6 +616,18 @@ TEST_F(FormatTest, ForEachLoops) {
                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
                "}");
+
+  // As function-like macros.
+  verifyFormat("#define foreach(x, y)\n"
+               "#define Q_FOREACH(x, y)\n"
+               "#define BOOST_FOREACH(x, y)\n"
+               "#define UNKNOWN_FOREACH(x, y)\n");
+
+  // Not as function-like macros.
+  verifyFormat("#define foreach (x, y)\n"
+               "#define Q_FOREACH (x, y)\n"
+               "#define BOOST_FOREACH (x, y)\n"
+               "#define UNKNOWN_FOREACH (x, y)\n");
 }
 
 TEST_F(FormatTest, FormatsWhileLoop) {