Skip to content

Commit

Permalink
[clang-format] Find main include after block ended with #pragma hdrstop
Browse files Browse the repository at this point in the history
Find main include in first include block not ended with #pragma hdrstop

Reviewed By: curdeius

Differential Revision: https://reviews.llvm.org/D94217
  • Loading branch information
rjelonek authored and mkurdej committed Jan 11, 2021
1 parent 7473940 commit 89878e8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
5 changes: 4 additions & 1 deletion clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2308,7 +2308,10 @@ tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code,
sortCppIncludes(Style, IncludesInBlock, Ranges, FileName, Code,
Replaces, Cursor);
IncludesInBlock.clear();
FirstIncludeBlock = false;
if (Trimmed.startswith("#pragma hdrstop")) // Precompiled headers.
FirstIncludeBlock = true;
else
FirstIncludeBlock = false;
}
}
if (Pos == StringRef::npos || Pos + 1 == Code.size())
Expand Down
39 changes: 39 additions & 0 deletions clang/unittests/Format/SortIncludesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,45 @@ TEST_F(SortIncludesTest, DoNotRegroupGroupsInGoogleObjCStyle) {
"#include \"a.h\""));
}

TEST_F(SortIncludesTest, DoNotTreatPrecompiledHeadersAsFirstBlock) {
Style.IncludeBlocks = Style.IBS_Merge;
std::string Code = "#include \"d.h\"\r\n"
"#include \"b.h\"\r\n"
"#pragma hdrstop\r\n"
"\r\n"
"#include \"c.h\"\r\n"
"#include \"a.h\"\r\n"
"#include \"e.h\"\r\n";

std::string Expected = "#include \"b.h\"\r\n"
"#include \"d.h\"\r\n"
"#pragma hdrstop\r\n"
"\r\n"
"#include \"e.h\"\r\n"
"#include \"a.h\"\r\n"
"#include \"c.h\"\r\n";

EXPECT_EQ(Expected, sort(Code, "e.cpp", 2));

Code = "#include \"d.h\"\n"
"#include \"b.h\"\n"
"#pragma hdrstop( \"c:\\projects\\include\\myinc.pch\" )\n"
"\n"
"#include \"c.h\"\n"
"#include \"a.h\"\n"
"#include \"e.h\"\n";

Expected = "#include \"b.h\"\n"
"#include \"d.h\"\n"
"#pragma hdrstop(\"c:\\projects\\include\\myinc.pch\")\n"
"\n"
"#include \"e.h\"\n"
"#include \"a.h\"\n"
"#include \"c.h\"\n";

EXPECT_EQ(Expected, sort(Code, "e.cpp", 2));
}

TEST_F(SortIncludesTest, skipUTF8ByteOrderMarkMerge) {
Style.IncludeBlocks = Style.IBS_Merge;
std::string Code = "\xEF\xBB\xBF#include \"d.h\"\r\n"
Expand Down

0 comments on commit 89878e8

Please sign in to comment.