[Format] Don't treat compound extension headers (foo.proto.h) as foo.cc main-file header.

We receive internal bugs about this false positives after D86597.

Differential Revision: https://reviews.llvm.org/D88640.
This commit is contained in:
Haojian Wu 2020-10-01 19:45:01 +02:00
parent ba9b15072c
commit c1b209cc61
2 changed files with 16 additions and 1 deletions

View File

@ -233,7 +233,12 @@ int IncludeCategoryManager::getSortIncludePriority(StringRef IncludeName,
bool IncludeCategoryManager::isMainHeader(StringRef IncludeName) const {
if (!IncludeName.startswith("\""))
return false;
StringRef HeaderStem = matchingStem(IncludeName.drop_front(1).drop_back(1));
// Not matchingStem: implementation files may have compound extensions but
// headers may not.
StringRef HeaderStem =
llvm::sys::path::stem(IncludeName.drop_front(1).drop_back(
1) /* remove the surrounding "" or <> */);
if (FileStem.startswith(HeaderStem) ||
FileStem.startswith_lower(HeaderStem)) {
llvm::Regex MainIncludeRegex(HeaderStem.str() + Style.IncludeIsMainRegex,

View File

@ -151,6 +151,16 @@ TEST_F(SortIncludesTest, NoReplacementsForValidIncludes) {
EXPECT_TRUE(sortIncludes(FmtStyle, Code, GetCodeRange(Code), "a.cc").empty());
}
TEST_F(SortIncludesTest, NoMainFileHeader) {
std::string Code = "#include <string>\n"
"\n"
"#include \"a/extra_action.proto.h\"\n";
FmtStyle = getGoogleStyle(FormatStyle::LK_Cpp);
EXPECT_TRUE(
sortIncludes(FmtStyle, Code, GetCodeRange(Code), "a/extra_action.cc")
.empty());
}
TEST_F(SortIncludesTest, SortedIncludesInMultipleBlocksAreMerged) {
Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge;
EXPECT_EQ("#include \"a.h\"\n"