Hi djasper,

The C preprocessor definition of a for-each macro was being
improperly formatted. A space was being inserted between the
macro name and the opening parenthesis. Fix the extra space
by excluding such cases from the ForEachMacros checks.

http://reviews.llvm.org/D10266

Files:
  lib/Format/Format.cpp
  test/Format/for-each-macros.cpp

Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -1122,9 +1122,12 @@
       Column = FormatTok->LastLineColumnWidth;
     }
 
-    if (std::find(ForEachMacros.begin(), ForEachMacros.end(),
-                  FormatTok->Tok.getIdentifierInfo()) != ForEachMacros.end())
-      FormatTok->Type = TT_ForEachMacro;
+    if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() &&
+          Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() ==
+            tok::pp_define))
+      if (std::find(ForEachMacros.begin(), ForEachMacros.end(),
+                    FormatTok->Tok.getIdentifierInfo()) != ForEachMacros.end())
+        FormatTok->Type = TT_ForEachMacro;
 
     return FormatTok;
   }
Index: test/Format/for-each-macros.cpp
===================================================================
--- /dev/null
+++ test/Format/for-each-macros.cpp
@@ -0,0 +1,13 @@
+// RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
+// RUN: clang-format -style=LLVM -i %t.cpp
+// RUN: FileCheck -strict-whitespace -input-file=%t.cpp %s
+
+// CHECK: {{foreach\(x\)}}
+#define foreach(x)
+
+#undef foreach
+// CHECK: {{foreach \(x\)}}
+#define foreach (x)
+
+// CHECK: {{foreach \(x\)}}
+foreach(x) {}

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -1122,9 +1122,12 @@
       Column = FormatTok->LastLineColumnWidth;
     }
 
-    if (std::find(ForEachMacros.begin(), ForEachMacros.end(),
-                  FormatTok->Tok.getIdentifierInfo()) != ForEachMacros.end())
-      FormatTok->Type = TT_ForEachMacro;
+    if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() &&
+          Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() ==
+            tok::pp_define))
+      if (std::find(ForEachMacros.begin(), ForEachMacros.end(),
+                    FormatTok->Tok.getIdentifierInfo()) != ForEachMacros.end())
+        FormatTok->Type = TT_ForEachMacro;
 
     return FormatTok;
   }
Index: test/Format/for-each-macros.cpp
===================================================================
--- /dev/null
+++ test/Format/for-each-macros.cpp
@@ -0,0 +1,13 @@
+// RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
+// RUN: clang-format -style=LLVM -i %t.cpp
+// RUN: FileCheck -strict-whitespace -input-file=%t.cpp %s
+
+// CHECK: {{foreach\(x\)}}
+#define foreach(x)
+
+#undef foreach
+// CHECK: {{foreach \(x\)}}
+#define foreach (x)
+
+// CHECK: {{foreach \(x\)}}
+foreach(x) {}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to