Skip to content

Commit

Permalink
[clang-format] Skip block commented out includes when sorting them (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
owenca authored Jul 5, 2024
1 parent 3485540 commit ceade83
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
15 changes: 11 additions & 4 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() &&
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions clang/unittests/Format/SortIncludesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ceade83

Please sign in to comment.