aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOwen Pan <owenpiano@gmail.com>2024-07-05 15:28:16 -0700
committerGitHub <noreply@github.com>2024-07-05 15:28:16 -0700
commitceade83ad5fc529f2b2beb896eec0dd0b29fdd44 (patch)
treea8eadd7f69dec4ae884bd54b910d4301a2fa42bd
parent34855405b0a7dd6719fa3278f9b888f7f11bc4d8 (diff)
downloadllvm-ceade83ad5fc529f2b2beb896eec0dd0b29fdd44.zip
llvm-ceade83ad5fc529f2b2beb896eec0dd0b29fdd44.tar.gz
llvm-ceade83ad5fc529f2b2beb896eec0dd0b29fdd44.tar.bz2
[clang-format] Skip block commented out includes when sorting them (#97787)
Fixes #97539.
-rw-r--r--clang/lib/Format/Format.cpp15
-rw-r--r--clang/unittests/Format/SortIncludesTest.cpp9
2 files changed, 20 insertions, 4 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 6a8883b..7fd42e4 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3222,10 +3222,16 @@ tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code,
if (Trimmed.contains(RawStringTermination))
FormattingOff = false;
- if (isClangFormatOff(Trimmed))
+ bool IsBlockComment = false;
+
+ if (isClangFormatOff(Trimmed)) {
FormattingOff = true;
- else if (isClangFormatOn(Trimmed))
+ } else if (isClangFormatOn(Trimmed)) {
FormattingOff = false;
+ } else if (Trimmed.starts_with("/*")) {
+ IsBlockComment = true;
+ Pos = Code.find("*/", SearchFrom + 2);
+ }
const bool EmptyLineSkipped =
Trimmed.empty() &&
@@ -3235,9 +3241,10 @@ tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code,
bool MergeWithNextLine = Trimmed.ends_with("\\");
if (!FormattingOff && !MergeWithNextLine) {
- if (tooling::HeaderIncludes::IncludeRegex.match(Line, &Matches)) {
+ if (!IsBlockComment &&
+ tooling::HeaderIncludes::IncludeRegex.match(Trimmed, &Matches)) {
StringRef IncludeName = Matches[2];
- if (Line.contains("/*") && !Line.contains("*/")) {
+ if (Trimmed.contains("/*") && !Trimmed.contains("*/")) {
// #include with a start of a block comment, but without the end.
// Need to keep all the lines until the end of the comment together.
// FIXME: This is somehow simplified check that probably does not work
diff --git a/clang/unittests/Format/SortIncludesTest.cpp b/clang/unittests/Format/SortIncludesTest.cpp
index 2eeb16b..3175382 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -1455,6 +1455,15 @@ TEST_F(SortIncludesTest, DisableRawStringLiteralSorting) {
#undef X
}
+TEST_F(SortIncludesTest, BlockCommentedOutIncludes) {
+ StringRef Code{"/* #include \"foo.h\"\n"
+ "#include \"bar.h\" */\n"
+ "#include <chrono>"};
+
+ FmtStyle = getGoogleStyle(FormatStyle::LK_Cpp);
+ verifyFormat(Code, sort(Code, "input.cpp", 0));
+}
+
} // end namespace
} // end namespace format
} // end namespace clang