From: Roman Kashitsyn Date: Thu, 11 Sep 2014 14:47:20 +0000 (+0000) Subject: Fix bug 20892 - clang-format does not handle C-style comments X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ad7408609a740737cb6005948429a22ad50e3a48;p=clang Fix bug 20892 - clang-format does not handle C-style comments Summary: http://llvm.org/bugs/show_bug.cgi?id=20892 Add support of C-style formatting enabling/disabling directives. Now the following two styles are supported: // clang-format on /* clang-format on */ The flexibility in comments (support of extra spaces and/or slashes, etc.) is deliberately avoided to simplify search in large code bases. Reviewers: djasper Reviewed By: djasper Subscribers: cfe-commits, curdeius, klimek Differential Revision: http://reviews.llvm.org/D5309 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@217588 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 5b2f222bfc..36d0f62a85 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -1725,11 +1725,18 @@ private: Tok.Tok.setKind(tok::char_constant); } } - if (Tok.is(tok::comment) && Tok.TokenText == "// clang-format on") + + if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format on" || + Tok.TokenText == "/* clang-format on */")) { FormattingDisabled = false; + } + Tok.Finalized = FormattingDisabled; - if (Tok.is(tok::comment) && Tok.TokenText == "// clang-format off") + + if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format off" || + Tok.TokenText == "/* clang-format off */")) { FormattingDisabled = true; + } } }; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 8b8aaf1b4a..f8e318365b 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -9355,6 +9355,16 @@ TEST_F(FormatTest, DisableRegions) { " 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) {