https://github.com/owenca created https://github.com/llvm/llvm-project/pull/120680
Fixes #109864. >From cb71ed39160ad46566db7ce5d4ab622ffcf842ee Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Thu, 19 Dec 2024 19:59:23 -0800 Subject: [PATCH] [clang-format] Skip line splices when sorting C++ includes Fixes #109864. --- clang/lib/Format/Format.cpp | 9 ++++++++- clang/unittests/Format/SortIncludesTest.cpp | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index dcaac4b0d42cc5..f79f9cb1ecdd22 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -3247,7 +3247,14 @@ tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code, std::string RawStringTermination = ")\""; for (;;) { - auto Pos = Code.find('\n', SearchFrom); + 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" _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits