Skip to content

[cherry-pick swift/release/6.0][clang][modules] HeaderSearch::MarkFileModuleHeader sets textual headers' HeaderFileInfo non-external when it shouldn't #8905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion clang/include/clang/Lex/HeaderSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ struct HeaderFileInfo {
/// `ModuleMap::isModular()`).
unsigned isModuleHeader : 1;

/// Whether this header is a `textual header` in a module.
/// Whether this header is a `textual header` in a module. If a header is
/// textual in one module and normal in another module, this bit will not be
/// set, only `isModuleHeader`.
unsigned isTextualModuleHeader : 1;

/// Whether this header is part of the module that we are building, even if it
Expand Down
13 changes: 10 additions & 3 deletions clang/lib/Lex/HeaderSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1321,11 +1321,18 @@ OptionalFileEntryRef HeaderSearch::LookupSubframeworkHeader(
// File Info Management.
//===----------------------------------------------------------------------===//

static bool moduleMembershipNeedsMerge(const HeaderFileInfo *HFI,
ModuleMap::ModuleHeaderRole Role) {
if (ModuleMap::isModular(Role))
return !HFI->isModuleHeader || HFI->isTextualModuleHeader;
if (!HFI->isModuleHeader && (Role & ModuleMap::TextualHeader))
return !HFI->isTextualModuleHeader;
return false;
}

static void mergeHeaderFileInfoModuleBits(HeaderFileInfo &HFI,
bool isModuleHeader,
bool isTextualModuleHeader) {
assert((!isModuleHeader || !isTextualModuleHeader) &&
"A header can't build with a module and be textual at the same time");
HFI.isModuleHeader |= isModuleHeader;
if (HFI.isModuleHeader)
HFI.isTextualModuleHeader = false;
Expand Down Expand Up @@ -1440,7 +1447,7 @@ void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE,
if ((Role & ModuleMap::ExcludedHeader))
return;
auto *HFI = getExistingFileInfo(FE);
if (HFI && HFI->isModuleHeader)
if (HFI && !moduleMembershipNeedsMerge(HFI, Role))
return;
}

Expand Down
68 changes: 68 additions & 0 deletions clang/unittests/Lex/HeaderSearchTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,5 +292,73 @@ TEST_F(HeaderSearchTest, HeaderMapFrameworkLookup) {
EXPECT_EQ(Search.getIncludeNameForHeader(FE), "Foo/Foo.h");
}

TEST_F(HeaderSearchTest, HeaderFileInfoMerge) {
auto AddHeader = [&](std::string HeaderPath) -> FileEntryRef {
VFS->addFile(HeaderPath, 0,
llvm::MemoryBuffer::getMemBufferCopy("", HeaderPath),
/*User=*/std::nullopt, /*Group=*/std::nullopt,
llvm::sys::fs::file_type::regular_file);
return *FileMgr.getOptionalFileRef(HeaderPath);
};

class MockExternalHeaderFileInfoSource : public ExternalHeaderFileInfoSource {
HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE) {
HeaderFileInfo HFI;
auto FileName = FE->getName();
if (FileName == ModularPath)
HFI.mergeModuleMembership(ModuleMap::NormalHeader);
else if (FileName == TextualPath)
HFI.mergeModuleMembership(ModuleMap::TextualHeader);
HFI.External = true;
HFI.IsValid = true;
return HFI;
}

public:
std::string ModularPath = "/modular.h";
std::string TextualPath = "/textual.h";
};

auto ExternalSource = new MockExternalHeaderFileInfoSource();
Search.SetExternalSource(ExternalSource);

// Everything should start out external.
auto ModularFE = AddHeader(ExternalSource->ModularPath);
auto TextualFE = AddHeader(ExternalSource->TextualPath);
EXPECT_TRUE(Search.getExistingFileInfo(ModularFE)->External);
EXPECT_TRUE(Search.getExistingFileInfo(TextualFE)->External);

// Marking the same role should keep it external
Search.MarkFileModuleHeader(ModularFE, ModuleMap::NormalHeader,
/*isCompilingModuleHeader=*/false);
Search.MarkFileModuleHeader(TextualFE, ModuleMap::TextualHeader,
/*isCompilingModuleHeader=*/false);
EXPECT_TRUE(Search.getExistingFileInfo(ModularFE)->External);
EXPECT_TRUE(Search.getExistingFileInfo(TextualFE)->External);

// textual -> modular should update the HFI, but modular -> textual should be
// a no-op.
Search.MarkFileModuleHeader(ModularFE, ModuleMap::TextualHeader,
/*isCompilingModuleHeader=*/false);
Search.MarkFileModuleHeader(TextualFE, ModuleMap::NormalHeader,
/*isCompilingModuleHeader=*/false);
auto ModularFI = Search.getExistingFileInfo(ModularFE);
auto TextualFI = Search.getExistingFileInfo(TextualFE);
EXPECT_TRUE(ModularFI->External);
EXPECT_TRUE(ModularFI->isModuleHeader);
EXPECT_FALSE(ModularFI->isTextualModuleHeader);
EXPECT_FALSE(TextualFI->External);
EXPECT_TRUE(TextualFI->isModuleHeader);
EXPECT_FALSE(TextualFI->isTextualModuleHeader);

// Compiling the module should make the HFI local.
Search.MarkFileModuleHeader(ModularFE, ModuleMap::NormalHeader,
/*isCompilingModuleHeader=*/true);
Search.MarkFileModuleHeader(TextualFE, ModuleMap::NormalHeader,
/*isCompilingModuleHeader=*/true);
EXPECT_FALSE(Search.getExistingFileInfo(ModularFE)->External);
EXPECT_FALSE(Search.getExistingFileInfo(TextualFE)->External);
}

} // namespace
} // namespace clang