diff --git a/clang/lib/Tooling/Inclusions/HeaderIncludes.cpp b/clang/lib/Tooling/Inclusions/HeaderIncludes.cpp --- a/clang/lib/Tooling/Inclusions/HeaderIncludes.cpp +++ b/clang/lib/Tooling/Inclusions/HeaderIncludes.cpp @@ -233,7 +233,10 @@ bool IncludeCategoryManager::isMainHeader(StringRef IncludeName) const { if (!IncludeName.startswith("\"")) return false; - StringRef HeaderStem = matchingStem(IncludeName.drop_front(1).drop_back(1)); + + 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, diff --git a/clang/unittests/Format/SortIncludesTest.cpp b/clang/unittests/Format/SortIncludesTest.cpp --- a/clang/unittests/Format/SortIncludesTest.cpp +++ b/clang/unittests/Format/SortIncludesTest.cpp @@ -151,6 +151,16 @@ EXPECT_TRUE(sortIncludes(FmtStyle, Code, GetCodeRange(Code), "a.cc").empty()); } +TEST_F(SortIncludesTest, NoMainFileHeader) { + std::string Code = "#include \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"