[llvm-branch-commits] [clang] 7473940 - [clang-format] turn on formatting after "clang-format on" while sorting includes
Marek Kurdej via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Mon Jan 11 00:45:54 PST 2021
Author: RafaĆ Jelonek
Date: 2021-01-11T09:41:15+01:00
New Revision: 7473940bae0f263832456d2c99a4bd606ed0d76e
URL: https://github.com/llvm/llvm-project/commit/7473940bae0f263832456d2c99a4bd606ed0d76e
DIFF: https://github.com/llvm/llvm-project/commit/7473940bae0f263832456d2c99a4bd606ed0d76e.diff
LOG: [clang-format] turn on formatting after "clang-format on" while sorting includes
Formatting is not active after "clang-format on" due to merging lines while formatting is off. Also, use trimmed line. Behaviour with LF is different than with CRLF.
Reviewed By: curdeius, MyDeveloperDay
Differential Revision: https://reviews.llvm.org/D94206
Added:
Modified:
clang/lib/Format/Format.cpp
clang/unittests/Format/SortIncludesTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 5adfed5f3d32..37b6c4c8a20e 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2291,7 +2291,8 @@ tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code,
Style.IncludeStyle.IncludeBlocks ==
tooling::IncludeStyle::IBS_Regroup);
- if (!FormattingOff && !Line.endswith("\\")) {
+ bool MergeWithNextLine = Trimmed.endswith("\\");
+ if (!FormattingOff && !MergeWithNextLine) {
if (IncludeRegex.match(Line, &Matches)) {
StringRef IncludeName = Matches[2];
int Category = Categories.getIncludePriority(
@@ -2309,10 +2310,12 @@ tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code,
IncludesInBlock.clear();
FirstIncludeBlock = false;
}
- Prev = Pos + 1;
}
if (Pos == StringRef::npos || Pos + 1 == Code.size())
break;
+
+ if (!MergeWithNextLine)
+ Prev = Pos + 1;
SearchFrom = Pos + 1;
}
if (!IncludesInBlock.empty()) {
diff --git a/clang/unittests/Format/SortIncludesTest.cpp b/clang/unittests/Format/SortIncludesTest.cpp
index 6dc9d9850c59..f2f0e9391ece 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -207,6 +207,27 @@ TEST_F(SortIncludesTest, SupportClangFormatOff) {
"#include <a>\n"
"#include <c>\n"
"// clang-format on\n"));
+
+ Style.IncludeBlocks = Style.IBS_Merge;
+ std::string Code = "// clang-format off\r\n"
+ "#include \"d.h\"\r\n"
+ "#include \"b.h\"\r\n"
+ "// clang-format on\r\n"
+ "\r\n"
+ "#include \"c.h\"\r\n"
+ "#include \"a.h\"\r\n"
+ "#include \"e.h\"\r\n";
+
+ std::string Expected = "// clang-format off\r\n"
+ "#include \"d.h\"\r\n"
+ "#include \"b.h\"\r\n"
+ "// clang-format on\r\n"
+ "\r\n"
+ "#include \"e.h\"\r\n"
+ "#include \"a.h\"\r\n"
+ "#include \"c.h\"\r\n";
+
+ EXPECT_EQ(Expected, sort(Code, "e.cpp", 1));
}
TEST_F(SortIncludesTest, SupportClangFormatOffCStyle) {
@@ -915,6 +936,22 @@ TEST_F(SortIncludesTest, skipUTF8ByteOrderMarkPreserve) {
EXPECT_EQ(Expected, sort(Code, "e.cpp", 2));
}
+
+TEST_F(SortIncludesTest, MergeLines) {
+ Style.IncludeBlocks = Style.IBS_Merge;
+ std::string Code = "#include \"c.h\"\r\n"
+ "#include \"b\\\r\n"
+ ".h\"\r\n"
+ "#include \"a.h\"\r\n";
+
+ std::string Expected = "#include \"a.h\"\r\n"
+ "#include \"b\\\r\n"
+ ".h\"\r\n"
+ "#include \"c.h\"\r\n";
+
+ EXPECT_EQ(Expected, sort(Code, "a.cpp", 1));
+}
+
} // end namespace
} // end namespace format
} // end namespace clang
More information about the llvm-branch-commits
mailing list