[clang-format] clang-format off/on not respected when using C Style comments

Summary:
If the clang-format on/off is in a /* comment */ then the sorting of headers is not ignored

PR40901 - https://bugs.llvm.org/show_bug.cgi?id=40901

Reviewers: djasper, klimek, JonasToth, krasimir, alexfh

Reviewed By: alexfh

Subscribers: alexfh, cfe-commits, llvm-commits

Tags: #clang, #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D58819

llvm-svn: 355266
This commit is contained in:
Paul Hoad 2019-03-02 09:08:51 +00:00
parent 23b1dfe675
commit 9b468c0b1e
2 changed files with 40 additions and 2 deletions

View File

@ -1786,9 +1786,10 @@ tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code,
Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev);
StringRef Trimmed = Line.trim();
if (Trimmed == "// clang-format off")
if (Trimmed == "// clang-format off" || Trimmed == "/* clang-format off */")
FormattingOff = true;
else if (Trimmed == "// clang-format on")
else if (Trimmed == "// clang-format on" ||
Trimmed == "/* clang-format on */")
FormattingOff = false;
const bool EmptyLineSkipped =

View File

@ -117,6 +117,43 @@ TEST_F(SortIncludesTest, SupportClangFormatOff) {
"// clang-format on\n"));
}
TEST_F(SortIncludesTest, SupportClangFormatOffCStyle) {
EXPECT_EQ("#include <a>\n"
"#include <b>\n"
"#include <c>\n"
"/* clang-format off */\n"
"#include <b>\n"
"#include <a>\n"
"#include <c>\n"
"/* clang-format on */\n",
sort("#include <b>\n"
"#include <a>\n"
"#include <c>\n"
"/* clang-format off */\n"
"#include <b>\n"
"#include <a>\n"
"#include <c>\n"
"/* clang-format on */\n"));
// Not really turning it off
EXPECT_EQ("#include <a>\n"
"#include <b>\n"
"#include <c>\n"
"/* clang-format offically */\n"
"#include <a>\n"
"#include <b>\n"
"#include <c>\n"
"/* clang-format onwards */\n",
sort("#include <b>\n"
"#include <a>\n"
"#include <c>\n"
"/* clang-format offically */\n"
"#include <b>\n"
"#include <a>\n"
"#include <c>\n"
"/* clang-format onwards */\n"));
}
TEST_F(SortIncludesTest, IncludeSortingCanBeDisabled) {
FmtStyle.SortIncludes = false;
EXPECT_EQ("#include \"a.h\"\n"