forked from OSchip/llvm-project
clang-format: Make sorting includes respect // clang-format off
llvm-svn: 253772
This commit is contained in:
parent
9f3c12565f
commit
9b8c7c72f5
|
@ -1780,11 +1780,20 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
|
|||
for (const auto &Category : Style.IncludeCategories)
|
||||
CategoryRegexs.emplace_back(Category.Regex);
|
||||
|
||||
bool FormattingOff = false;
|
||||
|
||||
for (;;) {
|
||||
auto Pos = Code.find('\n', SearchFrom);
|
||||
StringRef Line =
|
||||
Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev);
|
||||
if (!Line.endswith("\\")) {
|
||||
|
||||
StringRef Trimmed = Line.trim();
|
||||
if (Trimmed == "// clang-format off")
|
||||
FormattingOff = true;
|
||||
else if (Trimmed == "// clang-format on")
|
||||
FormattingOff = false;
|
||||
|
||||
if (!FormattingOff && !Line.endswith("\\")) {
|
||||
if (IncludeRegex.match(Line, &Matches)) {
|
||||
StringRef IncludeName = Matches[2];
|
||||
unsigned Category;
|
||||
|
|
|
@ -40,6 +40,25 @@ TEST_F(SortIncludesTest, BasicSorting) {
|
|||
"#include \"b.h\"\n"));
|
||||
}
|
||||
|
||||
TEST_F(SortIncludesTest, SupportClangFormatOff) {
|
||||
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"));
|
||||
}
|
||||
|
||||
TEST_F(SortIncludesTest, IncludeSortingCanBeDisabled) {
|
||||
Style.SortIncludes = false;
|
||||
EXPECT_EQ("#include \"a.h\"\n"
|
||||
|
|
Loading…
Reference in New Issue