Skip to content

Commit c1b209c

Browse files
committed
[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.
1 parent ba9b150 commit c1b209c

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

clang/lib/Tooling/Inclusions/HeaderIncludes.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,12 @@ int IncludeCategoryManager::getSortIncludePriority(StringRef IncludeName,
233233
bool IncludeCategoryManager::isMainHeader(StringRef IncludeName) const {
234234
if (!IncludeName.startswith("\""))
235235
return false;
236-
StringRef HeaderStem = matchingStem(IncludeName.drop_front(1).drop_back(1));
236+
237+
// Not matchingStem: implementation files may have compound extensions but
238+
// headers may not.
239+
StringRef HeaderStem =
240+
llvm::sys::path::stem(IncludeName.drop_front(1).drop_back(
241+
1) /* remove the surrounding "" or <> */);
237242
if (FileStem.startswith(HeaderStem) ||
238243
FileStem.startswith_lower(HeaderStem)) {
239244
llvm::Regex MainIncludeRegex(HeaderStem.str() + Style.IncludeIsMainRegex,

clang/unittests/Format/SortIncludesTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ TEST_F(SortIncludesTest, NoReplacementsForValidIncludes) {
151151
EXPECT_TRUE(sortIncludes(FmtStyle, Code, GetCodeRange(Code), "a.cc").empty());
152152
}
153153

154+
TEST_F(SortIncludesTest, NoMainFileHeader) {
155+
std::string Code = "#include <string>\n"
156+
"\n"
157+
"#include \"a/extra_action.proto.h\"\n";
158+
FmtStyle = getGoogleStyle(FormatStyle::LK_Cpp);
159+
EXPECT_TRUE(
160+
sortIncludes(FmtStyle, Code, GetCodeRange(Code), "a/extra_action.cc")
161+
.empty());
162+
}
163+
154164
TEST_F(SortIncludesTest, SortedIncludesInMultipleBlocksAreMerged) {
155165
Style.IncludeBlocks = tooling::IncludeStyle::IBS_Merge;
156166
EXPECT_EQ("#include \"a.h\"\n"

0 commit comments

Comments
 (0)