Skip to content

[clang] use relative paths for builtin headers during module compilation #68023

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
merged 1 commit into from
Oct 27, 2023
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
5 changes: 4 additions & 1 deletion clang/include/clang/Lex/ModuleMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,13 +410,16 @@ class ModuleMap {
}

/// Get the directory that contains Clang-supplied include files.
const DirectoryEntry *getBuiltinDir() const {
OptionalDirectoryEntryRefDegradesToDirectoryEntryPtr getBuiltinDir() const {
return BuiltinIncludeDir;
}

/// Is this a compiler builtin header?
bool isBuiltinHeader(FileEntryRef File);

bool shouldImportRelativeToBuiltinIncludeDir(StringRef FileName,
Module *Module) const;

/// Add a module map callback.
void addModuleMapCallbacks(std::unique_ptr<ModuleMapCallbacks> Callback) {
Callbacks.push_back(std::move(Callback));
Expand Down
9 changes: 8 additions & 1 deletion clang/lib/Lex/ModuleMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ bool ModuleMap::resolveAsBuiltinHeader(
if (!File)
return false;

Module::Header H = {Header.FileName, Header.FileName, *File};
auto Role = headerKindToRole(Header.Kind);
Module::Header H = {Header.FileName, std::string(Path.str()), *File};
addHeader(Mod, H, Role);
return true;
}
Expand Down Expand Up @@ -417,6 +417,13 @@ bool ModuleMap::isBuiltinHeader(FileEntryRef File) {
isBuiltinHeaderName(llvm::sys::path::filename(File.getName()));
}

bool ModuleMap::shouldImportRelativeToBuiltinIncludeDir(StringRef FileName,
Module *Module) const {
return LangOpts.BuiltinHeadersInSystemModules && BuiltinIncludeDir &&
Module->IsSystem && !Module->isPartOfFramework() &&
isBuiltinHeaderName(FileName);
}

ModuleMap::HeadersMap::iterator ModuleMap::findKnownHeader(FileEntryRef File) {
resolveHeaderDirectives(File);
HeadersMap::iterator Known = Headers.find(File);
Expand Down
9 changes: 8 additions & 1 deletion clang/lib/Lex/PPDirectives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//

#include "clang/Basic/CharInfo.h"
#include "clang/Basic/DirectoryEntry.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/LangOptions.h"
Expand All @@ -21,6 +22,7 @@
#include "clang/Basic/TokenKinds.h"
#include "clang/Lex/CodeCompletionHandler.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/HeaderSearchOptions.h"
#include "clang/Lex/LexDiagnostic.h"
#include "clang/Lex/LiteralSupport.h"
#include "clang/Lex/MacroInfo.h"
Expand Down Expand Up @@ -982,7 +984,12 @@ OptionalFileEntryRef Preprocessor::LookupFile(
// map file.
if (!FileEnt) {
if (FID == SourceMgr.getMainFileID() && MainFileDir) {
Includers.push_back(std::make_pair(std::nullopt, *MainFileDir));
auto IncludeDir =
HeaderInfo.getModuleMap().shouldImportRelativeToBuiltinIncludeDir(
Filename, getCurrentModule())
? HeaderInfo.getModuleMap().getBuiltinDir()
: MainFileDir;
Includers.push_back(std::make_pair(std::nullopt, *IncludeDir));
BuildSystemModule = getCurrentModule()->IsSystem;
} else if ((FileEnt = SourceMgr.getFileEntryRefForID(
SourceMgr.getMainFileID()))) {
Expand Down
3 changes: 3 additions & 0 deletions clang/test/Modules/Inputs/builtin-headers/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module ModuleWithBuiltinHeader [system] {
header "float.h"
}
11 changes: 11 additions & 0 deletions clang/test/Modules/relative-resource-dir.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// REQUIRES: shell

// RUN: EXPECTED_RESOURCE_DIR=`%clang -print-resource-dir` && \
// RUN: mkdir -p %t && rm -rf %t/resource-dir && \
// RUN: cp -R $EXPECTED_RESOURCE_DIR %t/resource-dir
// RUN: cd %t && %clang -cc1 -x objective-c -fmodules -fmodule-format=obj \
// RUN: -fimplicit-module-maps -fmodules-cache-path=%t.mcp \
// RUN: -fbuiltin-headers-in-system-modules \
// RUN: -resource-dir resource-dir \
// RUN: -emit-module %S/Inputs/builtin-headers/module.modulemap \
// RUN: -fmodule-name=ModuleWithBuiltinHeader -o %t.pcm