Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the extra Size variable needed? Why can't it stay as for (;;) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's used in SearchFrom < Size. My previous version using for (;;) caused a crash in ToolingTests.

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
Loading