From 5596c4ee940e79b152eea89707a7ae096569543d Mon Sep 17 00:00:00 2001
From: Daniel Jasper <djasper@google.com>
Date: Thu, 11 Jun 2015 08:38:19 +0000
Subject: [PATCH] clang-format: Don't add spaces in foreach macro definition.

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!

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@239513 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/Format/Format.cpp           |  5 ++++-
 unittests/Format/FormatTest.cpp | 12 ++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index c725b4bf30a..a774f8cdd59 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -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;
 
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index c4aa712ba1d..5b3980bdb5f 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -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) {
-- 
GitLab