Simplify formatting directives recognition logic

http://reviews.llvm.org/D5309

Files:
  lib/Format/Format.cpp
  unittests/Format/FormatTest.cpp
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -1725,12 +1725,22 @@
         Tok.Tok.setKind(tok::char_constant);
       }
     }
-    if (Tok.is(tok::comment) && Tok.TokenText == "// clang-format on")
+    if (isFormatOnDirective(Tok))
       FormattingDisabled = false;
     Tok.Finalized = FormattingDisabled;
-    if (Tok.is(tok::comment) && Tok.TokenText == "// clang-format off")
+    if (isFormatOffDirective(Tok))
       FormattingDisabled = true;
   }
+
+  static bool isFormatOffDirective(const FormatToken &Tok) {
+    return Tok.is(tok::comment) && (Tok.TokenText == "// clang-format off" ||
+                                    Tok.TokenText == "/* clang-format off */");
+  }
+
+  static bool isFormatOnDirective(const FormatToken &Tok) {
+    return Tok.is(tok::comment) && (Tok.TokenText == "// clang-format on" ||
+                                    Tok.TokenText == "/* clang-format on */");
+  }
 };
 
 static StringRef getLanguageName(FormatStyle::LanguageKind Language) {
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -9355,6 +9355,16 @@
                    "  int j;\n"
                    " // clang-format on\n"
                    "   int   k;"));
+  EXPECT_EQ("int i;\n"
+            "/* clang-format off */\n"
+            "  int j;\n"
+            "/* clang-format on */\n"
+            "int k;",
+            format(" int  i;\n"
+                   "   /* clang-format off */\n"
+                   "  int j;\n"
+                   " /* clang-format on */\n"
+                   "   int   k;"));
 }
 
 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to