Skip to content

Commit 89878e8

Browse files
rjelonekmkurdej
authored andcommitted
[clang-format] Find main include after block ended with #pragma hdrstop
Find main include in first include block not ended with #pragma hdrstop Reviewed By: curdeius Differential Revision: https://reviews.llvm.org/D94217
1 parent 7473940 commit 89878e8

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

clang/lib/Format/Format.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2308,7 +2308,10 @@ tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code,
23082308
sortCppIncludes(Style, IncludesInBlock, Ranges, FileName, Code,
23092309
Replaces, Cursor);
23102310
IncludesInBlock.clear();
2311-
FirstIncludeBlock = false;
2311+
if (Trimmed.startswith("#pragma hdrstop")) // Precompiled headers.
2312+
FirstIncludeBlock = true;
2313+
else
2314+
FirstIncludeBlock = false;
23122315
}
23132316
}
23142317
if (Pos == StringRef::npos || Pos + 1 == Code.size())

clang/unittests/Format/SortIncludesTest.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,45 @@ TEST_F(SortIncludesTest, DoNotRegroupGroupsInGoogleObjCStyle) {
900900
"#include \"a.h\""));
901901
}
902902

903+
TEST_F(SortIncludesTest, DoNotTreatPrecompiledHeadersAsFirstBlock) {
904+
Style.IncludeBlocks = Style.IBS_Merge;
905+
std::string Code = "#include \"d.h\"\r\n"
906+
"#include \"b.h\"\r\n"
907+
"#pragma hdrstop\r\n"
908+
"\r\n"
909+
"#include \"c.h\"\r\n"
910+
"#include \"a.h\"\r\n"
911+
"#include \"e.h\"\r\n";
912+
913+
std::string Expected = "#include \"b.h\"\r\n"
914+
"#include \"d.h\"\r\n"
915+
"#pragma hdrstop\r\n"
916+
"\r\n"
917+
"#include \"e.h\"\r\n"
918+
"#include \"a.h\"\r\n"
919+
"#include \"c.h\"\r\n";
920+
921+
EXPECT_EQ(Expected, sort(Code, "e.cpp", 2));
922+
923+
Code = "#include \"d.h\"\n"
924+
"#include \"b.h\"\n"
925+
"#pragma hdrstop( \"c:\\projects\\include\\myinc.pch\" )\n"
926+
"\n"
927+
"#include \"c.h\"\n"
928+
"#include \"a.h\"\n"
929+
"#include \"e.h\"\n";
930+
931+
Expected = "#include \"b.h\"\n"
932+
"#include \"d.h\"\n"
933+
"#pragma hdrstop(\"c:\\projects\\include\\myinc.pch\")\n"
934+
"\n"
935+
"#include \"e.h\"\n"
936+
"#include \"a.h\"\n"
937+
"#include \"c.h\"\n";
938+
939+
EXPECT_EQ(Expected, sort(Code, "e.cpp", 2));
940+
}
941+
903942
TEST_F(SortIncludesTest, skipUTF8ByteOrderMarkMerge) {
904943
Style.IncludeBlocks = Style.IBS_Merge;
905944
std::string Code = "\xEF\xBB\xBF#include \"d.h\"\r\n"

0 commit comments

Comments
 (0)