[clang] 141c544 - [clang-format] Skip line splices when sorting C++ includes (#120680)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 24 21:47:57 PST 2024
Author: Owen Pan
Date: 2024-12-24T21:47:53-08:00
New Revision: 141c544c03702ac7c50522373ad781ede3685e0a
URL: https://github.com/llvm/llvm-project/commit/141c544c03702ac7c50522373ad781ede3685e0a
DIFF: https://github.com/llvm/llvm-project/commit/141c544c03702ac7c50522373ad781ede3685e0a.diff
LOG: [clang-format] Skip line splices when sorting C++ includes (#120680)
Fixes #109864.
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 dcaac4b0d42cc5..95129a8fe9240c 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3246,8 +3246,15 @@ tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code,
SmallVector<StringRef, 2> RawStringMatches;
std::string RawStringTermination = ")\"";
- for (;;) {
- auto Pos = Code.find('\n', SearchFrom);
+ for (const auto Size = Code.size(); SearchFrom < Size;) {
+ size_t Pos = SearchFrom;
+ if (Code[SearchFrom] != '\n') {
+ do { // Search for the first newline while skipping line splices.
+ ++Pos;
+ Pos = Code.find('\n', Pos);
+ } while (Pos != StringRef::npos && Code[Pos - 1] == '\\');
+ }
+
StringRef Line =
Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev);
diff --git a/clang/unittests/Format/SortIncludesTest.cpp b/clang/unittests/Format/SortIncludesTest.cpp
index 31753825646373..cb3f8c73a04871 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -984,6 +984,18 @@ TEST_F(SortIncludesTest, SortAndDeduplicateIncludes) {
"#include <c>\n"
"#include <b>"));
+ verifyFormat("/* COPYRIGHT *\\\n"
+ "\\* (C) 2024 */\n"
+ "\n"
+ "#include <a>\n"
+ "#include <b>",
+ sort("/* COPYRIGHT *\\\n"
+ "\\* (C) 2024 */\n"
+ "\n"
+ "#include <b>\n"
+ "#include <a>\n"
+ "#include <b>"));
+
Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge;
verifyFormat("#include <a>\n"
"#include <b>\n"
More information about the cfe-commits
mailing list