Skip to content

Commit cf4ae97

Browse files
committed
[clang] add module builtin headers relative to resource dir
When including builtin headers as part of a system module, serialize them relative to the builtin include dir. To handle later lookup add a method to provide the correct base directory. This makes it possible to compile modules including builtin headers with relative resource dirs.
1 parent 85f6b2f commit cf4ae97

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

clang/include/clang/Lex/ModuleMap.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,13 +410,16 @@ class ModuleMap {
410410
}
411411

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

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

420+
bool shouldImportRelativeToBuiltinIncludeDir(StringRef FileName,
421+
Module *Module) const;
422+
420423
/// Add a module map callback.
421424
void addModuleMapCallbacks(std::unique_ptr<ModuleMapCallbacks> Callback) {
422425
Callbacks.push_back(std::move(Callback));

clang/lib/Lex/ModuleMap.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,8 @@ bool ModuleMap::resolveAsBuiltinHeader(
348348
if (!File)
349349
return false;
350350

351+
Module::Header H = {Header.FileName, Header.FileName, *File};
351352
auto Role = headerKindToRole(Header.Kind);
352-
Module::Header H = {Header.FileName, std::string(Path.str()), *File};
353353
addHeader(Mod, H, Role);
354354
return true;
355355
}
@@ -417,6 +417,13 @@ bool ModuleMap::isBuiltinHeader(FileEntryRef File) {
417417
isBuiltinHeaderName(llvm::sys::path::filename(File.getName()));
418418
}
419419

420+
bool ModuleMap::shouldImportRelativeToBuiltinIncludeDir(StringRef FileName,
421+
Module *Module) const {
422+
return LangOpts.BuiltinHeadersInSystemModules && BuiltinIncludeDir &&
423+
Module->IsSystem && !Module->isPartOfFramework() &&
424+
isBuiltinHeaderName(FileName);
425+
}
426+
420427
ModuleMap::HeadersMap::iterator ModuleMap::findKnownHeader(FileEntryRef File) {
421428
resolveHeaderDirectives(File);
422429
HeadersMap::iterator Known = Headers.find(File);

clang/lib/Lex/PPDirectives.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "clang/Basic/CharInfo.h"
15+
#include "clang/Basic/DirectoryEntry.h"
1516
#include "clang/Basic/FileManager.h"
1617
#include "clang/Basic/IdentifierTable.h"
1718
#include "clang/Basic/LangOptions.h"
@@ -21,6 +22,7 @@
2122
#include "clang/Basic/TokenKinds.h"
2223
#include "clang/Lex/CodeCompletionHandler.h"
2324
#include "clang/Lex/HeaderSearch.h"
25+
#include "clang/Lex/HeaderSearchOptions.h"
2426
#include "clang/Lex/LexDiagnostic.h"
2527
#include "clang/Lex/LiteralSupport.h"
2628
#include "clang/Lex/MacroInfo.h"
@@ -982,7 +984,12 @@ OptionalFileEntryRef Preprocessor::LookupFile(
982984
// map file.
983985
if (!FileEnt) {
984986
if (FID == SourceMgr.getMainFileID() && MainFileDir) {
985-
Includers.push_back(std::make_pair(std::nullopt, *MainFileDir));
987+
auto IncludeDir =
988+
HeaderInfo.getModuleMap().shouldImportRelativeToBuiltinIncludeDir(
989+
Filename, getCurrentModule())
990+
? HeaderInfo.getModuleMap().getBuiltinDir()
991+
: MainFileDir;
992+
Includers.push_back(std::make_pair(std::nullopt, *IncludeDir));
986993
BuildSystemModule = getCurrentModule()->IsSystem;
987994
} else if ((FileEnt = SourceMgr.getFileEntryRefForID(
988995
SourceMgr.getMainFileID()))) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module ModuleWithBuiltinHeader [system] {
2+
header "float.h"
3+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// REQUIRES: shell
2+
3+
// RUN: EXPECTED_RESOURCE_DIR=`%clang -print-resource-dir` && \
4+
// RUN: mkdir -p %t && rm -rf %t/resource-dir && \
5+
// RUN: cp -R $EXPECTED_RESOURCE_DIR %t/resource-dir
6+
// RUN: cd %t && %clang -cc1 -x objective-c -fmodules -fmodule-format=obj \
7+
// RUN: -fimplicit-module-maps -fmodules-cache-path=%t.mcp \
8+
// RUN: -fbuiltin-headers-in-system-modules \
9+
// RUN: -resource-dir resource-dir \
10+
// RUN: -emit-module %S/Inputs/builtin-headers/module.modulemap \
11+
// RUN: -fmodule-name=ModuleWithBuiltinHeader -o %t.pcm

0 commit comments

Comments
 (0)