[clang] [clang] use absolute path for builtin headers during module compilation (PR #68023)

Richard Howell via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 2 14:25:39 PDT 2023


https://github.com/rmaz updated https://github.com/llvm/llvm-project/pull/68023

>From a44aa3a04dd6391cf4660cefe451ac1d53aa429d Mon Sep 17 00:00:00 2001
From: Richard Howell <rhow at fb.com>
Date: Mon, 2 Oct 2023 11:10:52 -0700
Subject: [PATCH] [clang] use absolute path for builtin headers during module
 compilation

When including builtin headers as part of a system module, ensure
we use absolute paths to those headers. Otherwise the module will
fail to compile when specifying relative resource directories.
---
 clang/lib/Lex/ModuleMap.cpp                               | 4 ++++
 .../test/Modules/Inputs/builtin-headers/module.modulemap  | 3 +++
 clang/test/Modules/relative-resource-dir.m                | 8 ++++++++
 3 files changed, 15 insertions(+)
 create mode 100644 clang/test/Modules/Inputs/builtin-headers/module.modulemap
 create mode 100644 clang/test/Modules/relative-resource-dir.m

diff --git a/clang/lib/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp
index e8437572ebf4bf6..80ffdee2e025bc4 100644
--- a/clang/lib/Lex/ModuleMap.cpp
+++ b/clang/lib/Lex/ModuleMap.cpp
@@ -348,6 +348,10 @@ bool ModuleMap::resolveAsBuiltinHeader(
   if (!File)
     return false;
 
+  // Ensure the path to the module directory is absolute, otherwise
+  // builtin headers will fail to resolve when using relative resource
+  // directory paths without a -I.
+  SourceMgr.getFileManager().makeAbsolutePath(Path);
   auto Role = headerKindToRole(Header.Kind);
   Module::Header H = {Header.FileName, std::string(Path.str()), *File};
   addHeader(Mod, H, Role);
diff --git a/clang/test/Modules/Inputs/builtin-headers/module.modulemap b/clang/test/Modules/Inputs/builtin-headers/module.modulemap
new file mode 100644
index 000000000000000..78a5b730dc6a925
--- /dev/null
+++ b/clang/test/Modules/Inputs/builtin-headers/module.modulemap
@@ -0,0 +1,3 @@
+module ModuleWithBuiltinHeader [system] {
+    header "float.h"
+}
\ No newline at end of file
diff --git a/clang/test/Modules/relative-resource-dir.m b/clang/test/Modules/relative-resource-dir.m
new file mode 100644
index 000000000000000..1d38fe922e71849
--- /dev/null
+++ b/clang/test/Modules/relative-resource-dir.m
@@ -0,0 +1,8 @@
+// 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 -resource-dir resource-dir \
+// RUN:   -emit-module %S/Inputs/builtin-headers/module.modulemap \
+// RUN:   -fmodule-name=ModuleWithBuiltinHeader -o %t.pcm



More information about the cfe-commits mailing list