Skip to content

Commit cb71ed3

Browse files
committed
[clang-format] Skip line splices when sorting C++ includes
Fixes #109864.
1 parent fe26853 commit cb71ed3

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

clang/lib/Format/Format.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3247,7 +3247,14 @@ tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code,
32473247
std::string RawStringTermination = ")\"";
32483248

32493249
for (;;) {
3250-
auto Pos = Code.find('\n', SearchFrom);
3250+
size_t Pos = SearchFrom;
3251+
if (Code[SearchFrom] != '\n') {
3252+
do { // Search for the first newline while skipping line splices.
3253+
++Pos;
3254+
Pos = Code.find('\n', Pos);
3255+
} while (Pos != StringRef::npos && Code[Pos - 1] == '\\');
3256+
}
3257+
32513258
StringRef Line =
32523259
Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev);
32533260

clang/unittests/Format/SortIncludesTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,18 @@ TEST_F(SortIncludesTest, SortAndDeduplicateIncludes) {
984984
"#include <c>\n"
985985
"#include <b>"));
986986

987+
verifyFormat("/* COPYRIGHT *\\\n"
988+
"\\* (C) 2024 */\n"
989+
"\n"
990+
"#include <a>\n"
991+
"#include <b>",
992+
sort("/* COPYRIGHT *\\\n"
993+
"\\* (C) 2024 */\n"
994+
"\n"
995+
"#include <b>\n"
996+
"#include <a>\n"
997+
"#include <b>"));
998+
987999
Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge;
9881000
verifyFormat("#include <a>\n"
9891001
"#include <b>\n"

0 commit comments

Comments
 (0)