Skip to content

Commit 70c5513

Browse files
rmazbenlangmuir
authored andcommitted
[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. (cherry picked from commit cf4ae97)
1 parent 7acad9a commit 70c5513

File tree

6 files changed

+35
-4
lines changed

6 files changed

+35
-4
lines changed

clang/include/clang/Lex/ModuleMap.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,16 @@ class ModuleMap {
422422
}
423423

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

429429
/// Is this a compiler builtin header?
430430
bool isBuiltinHeader(const FileEntry *File);
431431

432+
bool shouldImportRelativeToBuiltinIncludeDir(StringRef FileName,
433+
Module *Module) const;
434+
432435
/// Add a module map callback.
433436
void addModuleMapCallbacks(std::unique_ptr<ModuleMapCallbacks> Callback) {
434437
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(const FileEntry *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
421428
ModuleMap::findKnownHeader(const FileEntry *File) {
422429
resolveHeaderDirectives(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"
@@ -976,7 +978,12 @@ OptionalFileEntryRef Preprocessor::LookupFile(
976978
// map file.
977979
if (!FileEnt) {
978980
if (FID == SourceMgr.getMainFileID() && MainFileDir) {
979-
Includers.push_back(std::make_pair(nullptr, *MainFileDir));
981+
auto IncludeDir =
982+
HeaderInfo.getModuleMap().shouldImportRelativeToBuiltinIncludeDir(
983+
Filename, getCurrentModule())
984+
? HeaderInfo.getModuleMap().getBuiltinDir()
985+
: MainFileDir;
986+
Includers.push_back(std::make_pair(nullptr, *IncludeDir));
980987
BuildSystemModule = getCurrentModule()->IsSystem;
981988
} else if ((FileEnt = SourceMgr.getFileEntryRefForID(
982989
SourceMgr.getMainFileID()))) {

clang/test/ClangScanDeps/modules-include-tree-prefix-map.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105

106106
// CHECK-LABEL: System module-includes
107107
// CHECK-NEXT: #import "sys.h"
108-
// CHECK-NEXT: #import "/^tc/{{.*}}/stdbool.h"
108+
// CHECK-NEXT: #import "stdbool.h"
109109

110110
// CHECK-NEXT: {
111111
// CHECK-NEXT "modules": [
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)