Skip to content

Commit

Permalink
[clang-format] Skip line splices when sorting C++ includes (#120680)
Browse files Browse the repository at this point in the history
Fixes #109864.
  • Loading branch information
owenca authored Dec 25, 2024
1 parent 6a7687c commit 141c544
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
11 changes: 9 additions & 2 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
12 changes: 12 additions & 0 deletions clang/unittests/Format/SortIncludesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 141c544

Please sign in to comment.