[clang] 3c1b423 - [clang] sort additional module maps when serializing

2022-09-12 Thread Richard Howell via cfe-commits

Author: Richard Howell
Date: 2022-09-12T12:00:43-07:00
New Revision: 3c1b42347b3a0666c93948ade2f420a20e060c1a

URL: 
https://github.com/llvm/llvm-project/commit/3c1b42347b3a0666c93948ade2f420a20e060c1a
DIFF: 
https://github.com/llvm/llvm-project/commit/3c1b42347b3a0666c93948ade2f420a20e060c1a.diff

LOG: [clang] sort additional module maps when serializing

Sort additional module maps when serializing pcm files. This ensures
the `MODULE_MAP_FILE` record is deterministic across repeated builds.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D133611

Added: 


Modified: 
clang/lib/Serialization/ASTWriter.cpp

Removed: 




diff  --git a/clang/lib/Serialization/ASTWriter.cpp 
b/clang/lib/Serialization/ASTWriter.cpp
index 1e835a4c68531..838c6e306cfb0 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -1282,7 +1282,12 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, 
ASTContext &Context,
 if (auto *AdditionalModMaps =
 Map.getAdditionalModuleMapFiles(WritingModule)) {
   Record.push_back(AdditionalModMaps->size());
-  for (const FileEntry *F : *AdditionalModMaps)
+  SmallVector ModMaps(AdditionalModMaps->begin(),
+AdditionalModMaps->end());
+  llvm::sort(ModMaps, [](const FileEntry *A, const FileEntry *B) {
+return A->getName() < B->getName();
+  });
+  for (const FileEntry *F : ModMaps)
 AddPath(F->getName(), Record);
 } else {
   Record.push_back(0);



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Module] Mark test unsupported since objc doesn't have xcoff/g… (PR #70661)

2023-10-30 Thread Richard Howell via cfe-commits

https://github.com/rmaz approved this pull request.


https://github.com/llvm/llvm-project/pull/70661
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 646e502 - [clang] add -fmodule-file-home-is-cwd

2022-05-12 Thread Richard Howell via cfe-commits

Author: Richard Howell
Date: 2022-05-12T07:27:47-07:00
New Revision: 646e502de0d854cb3ecaca90ab52bebfe59a40cd

URL: 
https://github.com/llvm/llvm-project/commit/646e502de0d854cb3ecaca90ab52bebfe59a40cd
DIFF: 
https://github.com/llvm/llvm-project/commit/646e502de0d854cb3ecaca90ab52bebfe59a40cd.diff

LOG: [clang] add -fmodule-file-home-is-cwd

This diff adds a new frontend flag `-fmodule-file-home-is-cwd`.
The behavior of this flag is similar to
`-fmodule-map-file-home-is-cwd` but does not require the module
map files to be modified to have inputs relative to the cwd.
Instead the output modules will have their `BaseDirectory` set
to the cwd and will try and resolve paths relative to that.

The motiviation for this change is to support relocatable pcm
files that are built on different machines with different paths
without having to alter module map files, which is sometimes not
possible as they are provided by 3rd parties.

Reviewed By: urnathan

Differential Revision: https://reviews.llvm.org/D124874

Added: 
clang/test/Modules/module-file-home-is-cwd.m

Modified: 
clang/include/clang/Driver/Options.td
clang/include/clang/Lex/HeaderSearchOptions.h
clang/lib/Serialization/ASTWriter.cpp

Removed: 




diff  --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 8e840ede926a9..6668ac4ca9470 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5619,6 +5619,10 @@ def fmodule_map_file_home_is_cwd : Flag<["-"], 
"fmodule-map-file-home-is-cwd">,
   HelpText<"Use the current working directory as the home directory of "
"module maps specified by -fmodule-map-file=">,
   MarshallingInfoFlag>;
+def fmodule_file_home_is_cwd : Flag<["-"], "fmodule-file-home-is-cwd">,
+  HelpText<"Use the current working directory as the base directory of "
+   "compiled module files.">,
+  MarshallingInfoFlag>;
 def fmodule_feature : Separate<["-"], "fmodule-feature">,
   MetaVarName<"">,
   HelpText<"Enable  in module map requires declarations">,

diff  --git a/clang/include/clang/Lex/HeaderSearchOptions.h 
b/clang/include/clang/Lex/HeaderSearchOptions.h
index 4efdfc26c3c67..6436a9b3bde20 100644
--- a/clang/include/clang/Lex/HeaderSearchOptions.h
+++ b/clang/include/clang/Lex/HeaderSearchOptions.h
@@ -143,6 +143,12 @@ class HeaderSearchOptions {
   /// file.
   unsigned ModuleMapFileHomeIsCwd : 1;
 
+  /// Set the base path of a built module file to be the current working
+  /// directory. This is useful for sharing module files across machines
+  /// that build with 
diff erent paths without having to rewrite all
+  /// modulemap files to have working directory relative paths.
+  unsigned ModuleFileHomeIsCwd : 1;
+
   /// Also search for prebuilt implicit modules in the prebuilt module cache
   /// path.
   unsigned EnablePrebuiltImplicitModules : 1;
@@ -222,9 +228,9 @@ class HeaderSearchOptions {
   HeaderSearchOptions(StringRef _Sysroot = "/")
   : Sysroot(_Sysroot), ModuleFormat("raw"), DisableModuleHash(false),
 ImplicitModuleMaps(false), ModuleMapFileHomeIsCwd(false),
-EnablePrebuiltImplicitModules(false), UseBuiltinIncludes(true),
-UseStandardSystemIncludes(true), UseStandardCXXIncludes(true),
-UseLibcxx(false), Verbose(false),
+ModuleFileHomeIsCwd(false), EnablePrebuiltImplicitModules(false),
+UseBuiltinIncludes(true), UseStandardSystemIncludes(true),
+UseStandardCXXIncludes(true), UseLibcxx(false), Verbose(false),
 ModulesValidateOncePerBuildSession(false),
 ModulesValidateSystemHeaders(false),
 ValidateASTInputFilesContent(false), UseDebugInfo(false),

diff  --git a/clang/lib/Serialization/ASTWriter.cpp 
b/clang/lib/Serialization/ASTWriter.cpp
index 228bd9aa08db4..e42f41f8fbc2d 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -1224,15 +1224,24 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, 
ASTContext &Context,
   }
 
   if (WritingModule && WritingModule->Directory) {
-SmallString<128> BaseDir(WritingModule->Directory->getName());
+SmallString<128> BaseDir;
+if (PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd) {
+  // Use the current working directory as the base path for all inputs.
+  auto *CWD =
+  Context.getSourceManager().getFileManager().getDirectory(".").get();
+  BaseDir.assign(CWD->getName());
+} else {
+  BaseDir.assign(WritingModule->Directory->getName());
+}
 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
 
 // If the home of the module is the current working directory, then we
 // want to pick up the cwd of the build process loading the module, not
 // our cwd, when we load this module.
-if (!PP.getHeaderSearchInfo()
- .getHeaderSearchOpts()
-   

[clang] f110569 - [clang] serialize SUBMODULE_TOPHEADER relative to BaseDirectory

2022-05-12 Thread Richard Howell via cfe-commits

Author: Richard Howell
Date: 2022-05-12T07:29:37-07:00
New Revision: f11056943e56a32d81bb36d11fb5ce8d2b2ce79b

URL: 
https://github.com/llvm/llvm-project/commit/f11056943e56a32d81bb36d11fb5ce8d2b2ce79b
DIFF: 
https://github.com/llvm/llvm-project/commit/f11056943e56a32d81bb36d11fb5ce8d2b2ce79b.diff

LOG: [clang] serialize SUBMODULE_TOPHEADER relative to BaseDirectory

This diff changes the serialization of the `SUBMODULE_TOPHEADER`
entry in module files to be serialized relative to the module's
`BaseDirectory`. This matches the behavior of the
`SUBMODULE_HEADER` entry and will allow for the module to be
relocatable across machines.

The path is restored relative to the module's `BaseDirectory` on
deserialization.

Reviewed By: urnathan

Differential Revision: https://reviews.llvm.org/D124938

Added: 
clang/test/Modules/relative-submodule-topheader.m

Modified: 
clang/lib/Serialization/ASTReader.cpp
clang/lib/Serialization/ASTWriter.cpp

Removed: 




diff  --git a/clang/lib/Serialization/ASTReader.cpp 
b/clang/lib/Serialization/ASTReader.cpp
index c9601f0a164c9..93ecf222a5b30 100644
--- a/clang/lib/Serialization/ASTReader.cpp
+++ b/clang/lib/Serialization/ASTReader.cpp
@@ -5637,9 +5637,12 @@ llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
   // them here.
   break;
 
-case SUBMODULE_TOPHEADER:
-  CurrentModule->addTopHeaderFilename(Blob);
+case SUBMODULE_TOPHEADER: {
+  std::string HeaderName(Blob);
+  ResolveImportedPath(F, HeaderName);
+  CurrentModule->addTopHeaderFilename(HeaderName);
   break;
+}
 
 case SUBMODULE_UMBRELLA_DIR: {
   // See comments in SUBMODULE_UMBRELLA_HEADER

diff  --git a/clang/lib/Serialization/ASTWriter.cpp 
b/clang/lib/Serialization/ASTWriter.cpp
index e42f41f8fbc2d..4eeedc07fb1d4 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -2857,8 +2857,11 @@ void ASTWriter::WriteSubmodules(Module *WritingModule) {
 {
   auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
   RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
-  for (auto *H : TopHeaders)
-Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
+  for (auto *H : TopHeaders) {
+SmallString<128> HeaderName(H->getName());
+PreparePathForOutput(HeaderName);
+Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, HeaderName);
+  }
 }
 
 // Emit the imports.

diff  --git a/clang/test/Modules/relative-submodule-topheader.m 
b/clang/test/Modules/relative-submodule-topheader.m
new file mode 100644
index 0..c7c2f13076866
--- /dev/null
+++ b/clang/test/Modules/relative-submodule-topheader.m
@@ -0,0 +1,10 @@
+// RUN: cd %S
+// RUN: %clang_cc1 -fmodules -fno-implicit-modules -x objective-c++ 
-fmodule-name=std -emit-module Inputs/submodules/module.map -o %t/mod.pcm
+// RUN: llvm-bcanalyzer --dump --disable-histogram %t/mod.pcm | FileCheck %s
+
+// CHECK:  blob data = 'vector.h'
+// CHECK:  blob data = 'vector.h'
+// CHECK:  blob data = 'type_traits.h'
+// CHECK:  blob data = 'type_traits.h'
+// CHECK:  blob data = 'hash_map.h'
+// CHECK:  blob data = 'hash_map.h'



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] ee51e97 - [clang] serialize ORIGINAL_PCH_DIR relative to BaseDirectory

2022-05-12 Thread Richard Howell via cfe-commits

Author: Richard Howell
Date: 2022-05-12T07:31:19-07:00
New Revision: ee51e9795a31e1280e30179215c27e09927230e2

URL: 
https://github.com/llvm/llvm-project/commit/ee51e9795a31e1280e30179215c27e09927230e2
DIFF: 
https://github.com/llvm/llvm-project/commit/ee51e9795a31e1280e30179215c27e09927230e2.diff

LOG: [clang] serialize ORIGINAL_PCH_DIR relative to BaseDirectory

This diff changes the serialization of the `ORIGINAL_PCH_DIR`
entry in module files to be serialized relative to the module's
`BaseDirectory`. This will allow for the module to be relocatable
across machines.

The path is restored relative to the module's BaseDirectory on
deserialization.

Reviewed By: urnathan

Differential Revision: https://reviews.llvm.org/D124946

Added: 
clang/test/Modules/relative-original-dir.m

Modified: 
clang/lib/Serialization/ASTReader.cpp
clang/lib/Serialization/ASTWriter.cpp

Removed: 




diff  --git a/clang/lib/Serialization/ASTReader.cpp 
b/clang/lib/Serialization/ASTReader.cpp
index 93ecf222a5b3..85e5b3ecfdd7 100644
--- a/clang/lib/Serialization/ASTReader.cpp
+++ b/clang/lib/Serialization/ASTReader.cpp
@@ -2898,6 +2898,7 @@ ASTReader::ReadControlBlock(ModuleFile &F,
 
 case ORIGINAL_PCH_DIR:
   F.OriginalDir = std::string(Blob);
+  ResolveImportedPath(F, F.OriginalDir);
   break;
 
 case MODULE_NAME:

diff  --git a/clang/lib/Serialization/ASTWriter.cpp 
b/clang/lib/Serialization/ASTWriter.cpp
index 4eeedc07fb1d..6d9273ed9a53 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -1470,8 +1470,7 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, 
ASTContext &Context,
 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
 
 SmallString<128> OutputPath(OutputFile);
-
-SM.getFileManager().makeAbsolutePath(OutputPath);
+PreparePathForOutput(OutputPath);
 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
 
 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};

diff  --git a/clang/test/Modules/relative-original-dir.m 
b/clang/test/Modules/relative-original-dir.m
new file mode 100644
index ..3ed49142bc30
--- /dev/null
+++ b/clang/test/Modules/relative-original-dir.m
@@ -0,0 +1,7 @@
+// RUN: rm -rf %t/normal-module-map
+// RUN: mkdir -p %t
+// RUN: cp -r %S/Inputs/normal-module-map %t
+// RUN: %clang_cc1 -fmodules -fno-implicit-modules -fmodule-name=libA 
-emit-module %t/normal-module-map/module.map -o 
%t/normal-module-map/outdir/mod.pcm
+// RUN: llvm-bcanalyzer --dump --disable-histogram 
%t/normal-module-map/outdir/mod.pcm | FileCheck %s
+
+// CHECK:  blob data = 'outdir'



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-10-20 Thread Richard Howell via cfe-commits


@@ -180,8 +180,9 @@ static void appendSubframeworkPaths(Module *Mod,
 OptionalFileEntryRef ModuleMap::findHeader(
 Module *M, const Module::UnresolvedHeaderDirective &Header,
 SmallVectorImpl &RelativePathName, bool &NeedsFramework) {
-  // Search for the header file within the module's home directory.
-  auto Directory = M->Directory;
+  // Search for the header file within the module's home directory
+  // or the builtin include dir if this is a builtin header.
+  auto Directory = Header.HasBuiltinHeader ? BuiltinIncludeDir : M->Directory;

rmaz wrote:

Looks like we don't, no. The header ends up resolved against the resource dir 
as expected:
```


 blob data = 
'ModuleWithBuiltinHeader'
 blob data = 'float.h'
 blob data = 
'/home/llvmbuilddbg.noindex/tools/clang/test/Modules/Output/relative-resource-dir.m.tmp/resource-dir/include/float.h'
  
```

https://github.com/llvm/llvm-project/pull/68023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-10-20 Thread Richard Howell via cfe-commits

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

>From 5e3c6319c6a98a07dab6571f65ad1320815d76bf Mon Sep 17 00:00:00 2001
From: Richard Howell 
Date: Mon, 2 Oct 2023 11:10:52 -0700
Subject: [PATCH] [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.
---
 clang/include/clang/Lex/ModuleMap.h   |  5 -
 clang/lib/Lex/ModuleMap.cpp   |  9 -
 clang/lib/Lex/PPDirectives.cpp|  9 -
 .../Modules/Inputs/builtin-headers/module.modulemap   |  3 +++
 clang/test/Modules/relative-resource-dir.m| 11 +++
 5 files changed, 34 insertions(+), 3 deletions(-)
 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/include/clang/Lex/ModuleMap.h 
b/clang/include/clang/Lex/ModuleMap.h
index fc49742ad4af2c1..545d07c071c40e5 100644
--- a/clang/include/clang/Lex/ModuleMap.h
+++ b/clang/include/clang/Lex/ModuleMap.h
@@ -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 Callback) {
 Callbacks.push_back(std::move(Callback));
diff --git a/clang/lib/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp
index e8437572ebf4bf6..b7c1aee8c174dc7 100644
--- a/clang/lib/Lex/ModuleMap.cpp
+++ b/clang/lib/Lex/ModuleMap.cpp
@@ -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;
 }
@@ -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);
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 7899bfa1c4f5842..5d6aa4906b757d9 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -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"
@@ -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"
@@ -981,7 +983,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( {
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 000..78a5b730dc6a925
--- /dev/null
+++ b/clang/test/Modules/Inputs/builtin-headers/module.modulemap
@@ -0,0 +1,3 @@
+module ModuleWithBuiltinHeader [

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

2023-10-25 Thread Richard Howell via cfe-commits

rmaz wrote:

Thanks for the detailed reviews all, will get this rebased and landed.

https://github.com/llvm/llvm-project/pull/68023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-10-25 Thread Richard Howell via cfe-commits

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

>From 87bbfac1ea77b9ff528f05f9eab0e380a5d85cbb Mon Sep 17 00:00:00 2001
From: Richard Howell 
Date: Mon, 2 Oct 2023 11:10:52 -0700
Subject: [PATCH] [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.
---
 clang/include/clang/Lex/ModuleMap.h   |  5 -
 clang/lib/Lex/ModuleMap.cpp   |  9 -
 clang/lib/Lex/PPDirectives.cpp|  9 -
 .../Modules/Inputs/builtin-headers/module.modulemap   |  3 +++
 clang/test/Modules/relative-resource-dir.m| 11 +++
 5 files changed, 34 insertions(+), 3 deletions(-)
 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/include/clang/Lex/ModuleMap.h 
b/clang/include/clang/Lex/ModuleMap.h
index 054d4211b9c6d41..69ec8f34a324255 100644
--- a/clang/include/clang/Lex/ModuleMap.h
+++ b/clang/include/clang/Lex/ModuleMap.h
@@ -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 Callback) {
 Callbacks.push_back(std::move(Callback));
diff --git a/clang/lib/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp
index eb7cab54386c480..e4f3cdd3b163f50 100644
--- a/clang/lib/Lex/ModuleMap.cpp
+++ b/clang/lib/Lex/ModuleMap.cpp
@@ -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;
 }
@@ -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);
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 2892d4b777846ff..6fd515a5f0c756b 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -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"
@@ -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"
@@ -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( {
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 000..78a5b730dc6a925
--- /dev/null
+++ b/clang/test/Modules/Inputs/builtin-headers/module.modulemap
@@ -0,0 +1,3 @@
+module ModuleWithBuiltinHeader [

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

2023-10-26 Thread Richard Howell via cfe-commits

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

>From cf4ae971c3fd30f705fa79b8b918288649766d14 Mon Sep 17 00:00:00 2001
From: Richard Howell 
Date: Mon, 2 Oct 2023 11:10:52 -0700
Subject: [PATCH] [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.
---
 clang/include/clang/Lex/ModuleMap.h   |  5 -
 clang/lib/Lex/ModuleMap.cpp   |  9 -
 clang/lib/Lex/PPDirectives.cpp|  9 -
 .../Modules/Inputs/builtin-headers/module.modulemap   |  3 +++
 clang/test/Modules/relative-resource-dir.m| 11 +++
 5 files changed, 34 insertions(+), 3 deletions(-)
 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/include/clang/Lex/ModuleMap.h 
b/clang/include/clang/Lex/ModuleMap.h
index 054d4211b9c6d41..69ec8f34a324255 100644
--- a/clang/include/clang/Lex/ModuleMap.h
+++ b/clang/include/clang/Lex/ModuleMap.h
@@ -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 Callback) {
 Callbacks.push_back(std::move(Callback));
diff --git a/clang/lib/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp
index eb7cab54386c480..e4f3cdd3b163f50 100644
--- a/clang/lib/Lex/ModuleMap.cpp
+++ b/clang/lib/Lex/ModuleMap.cpp
@@ -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;
 }
@@ -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);
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 2892d4b777846ff..6fd515a5f0c756b 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -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"
@@ -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"
@@ -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( {
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 000..78a5b730dc6a925
--- /dev/null
+++ b/clang/test/Modules/Inputs/builtin-headers/module.modulemap
@@ -0,0 +1,3 @@
+module ModuleWithBuiltinHeader [

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

2023-10-27 Thread Richard Howell via cfe-commits

https://github.com/rmaz closed https://github.com/llvm/llvm-project/pull/68023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 1f451a8 - [clang] initialize type qualifiers for FunctionNoProtoType

2022-09-26 Thread Richard Howell via cfe-commits

Author: Richard Howell
Date: 2022-09-26T09:48:54-07:00
New Revision: 1f451a8bd6f32465b6ff26c30ba7fb6fc7e0e689

URL: 
https://github.com/llvm/llvm-project/commit/1f451a8bd6f32465b6ff26c30ba7fb6fc7e0e689
DIFF: 
https://github.com/llvm/llvm-project/commit/1f451a8bd6f32465b6ff26c30ba7fb6fc7e0e689.diff

LOG: [clang] initialize type qualifiers for FunctionNoProtoType

When initializing FunctionNoProtoType types, zero out the type
qualifiers. This will ensure the ODR hash remains stable as it
hashes the values for these qualifiers for all function types.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D133586

Added: 


Modified: 
clang/include/clang/AST/Type.h
clang/unittests/AST/DeclTest.cpp

Removed: 




diff  --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 347088780e4b8..507f72e62988e 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -3892,7 +3892,10 @@ class FunctionType : public Type {
   }
 
   Qualifiers getFastTypeQuals() const {
-return Qualifiers::fromFastMask(FunctionTypeBits.FastTypeQuals);
+if (isFunctionProtoType())
+  return Qualifiers::fromFastMask(FunctionTypeBits.FastTypeQuals);
+
+return Qualifiers();
   }
 
 public:

diff  --git a/clang/unittests/AST/DeclTest.cpp 
b/clang/unittests/AST/DeclTest.cpp
index 43a3798867f49..8d55f13e1f5c3 100644
--- a/clang/unittests/AST/DeclTest.cpp
+++ b/clang/unittests/AST/DeclTest.cpp
@@ -354,3 +354,25 @@ TEST(Decl, FriendFunctionWithinClassInHeaderUnit) {
 
   EXPECT_TRUE(getFooValue->isInlined());
 }
+
+TEST(Decl, NoProtoFunctionDeclAttributes) {
+  llvm::Annotations Code(R"(
+void f();
+)");
+
+  auto AST = tooling::buildASTFromCodeWithArgs(
+  Code.code(),
+  /*Args=*/{"-target", "i386-apple-darwin", "-x", "objective-c",
+"-std=c89"});
+  ASTContext &Ctx = AST->getASTContext();
+
+  auto *f = selectFirst(
+  "f", match(functionDecl(hasName("f")).bind("f"), Ctx));
+
+  const auto *FPT = f->getType()->getAs();
+
+  // Functions without prototypes always have 0 initialized qualifiers
+  EXPECT_FALSE(FPT->isConst());
+  EXPECT_FALSE(FPT->isVolatile());
+  EXPECT_FALSE(FPT->isRestrict());
+}



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 75fbb5d - [clang][nfc] refactor Module::Header to use OptionalFileEntryRef

2023-01-20 Thread Richard Howell via cfe-commits

Author: Richard Howell
Date: 2023-01-20T07:23:11-08:00
New Revision: 75fbb5d2238f1824f03d205b699061a115d5effc

URL: 
https://github.com/llvm/llvm-project/commit/75fbb5d2238f1824f03d205b699061a115d5effc
DIFF: 
https://github.com/llvm/llvm-project/commit/75fbb5d2238f1824f03d205b699061a115d5effc.diff

LOG: [clang][nfc] refactor Module::Header to use OptionalFileEntryRef

Refactor the `Module::Header` class to use an `OptionalFileEntryRef`
instead of a `FileEntry*`. This is preparation for refactoring the
`TopHeaderNames` to use `FileEntryRef` so that we preserve the
lookup path of the headers when serializing.

This is mostly based on https://reviews.llvm.org/D90497

Reviewed By: jansvoboda11

Differential Revision: https://reviews.llvm.org/D142113

Added: 


Modified: 
clang/include/clang/Basic/Module.h
clang/include/clang/Lex/ModuleMap.h
clang/lib/Frontend/FrontendAction.cpp
clang/lib/Lex/ModuleMap.cpp
clang/lib/Sema/SemaModule.cpp
clang/lib/Serialization/ASTReader.cpp

Removed: 




diff  --git a/clang/include/clang/Basic/Module.h 
b/clang/include/clang/Basic/Module.h
index 1b87e6628e31e..fc0ca163fe355 100644
--- a/clang/include/clang/Basic/Module.h
+++ b/clang/include/clang/Basic/Module.h
@@ -144,7 +144,8 @@ class alignas(8) Module {
   std::string PresumedModuleMapFile;
 
   /// The umbrella header or directory.
-  llvm::PointerUnion Umbrella;
+  llvm::PointerUnion
+  Umbrella;
 
   /// The module signature.
   ASTFileSignature Signature;
@@ -214,9 +215,9 @@ class alignas(8) Module {
   struct Header {
 std::string NameAsWritten;
 std::string PathRelativeToRootModuleDirectory;
-const FileEntry *Entry;
+OptionalFileEntryRefDegradesToFileEntryPtr Entry;
 
-explicit operator bool() { return Entry; }
+explicit operator bool() { return Entry.has_value(); }
   };
 
   /// Information about a directory name as found in the module map
@@ -622,9 +623,9 @@ class alignas(8) Module {
   /// Retrieve the header that serves as the umbrella header for this
   /// module.
   Header getUmbrellaHeader() const {
-if (auto *FE = Umbrella.dyn_cast())
+if (auto *ME = Umbrella.dyn_cast())
   return Header{UmbrellaAsWritten, UmbrellaRelativeToRootModuleDirectory,
-FE};
+FileEntryRef(*ME)};
 return Header{};
   }
 

diff  --git a/clang/include/clang/Lex/ModuleMap.h 
b/clang/include/clang/Lex/ModuleMap.h
index 77a1bdab0bdd1..f9eb0be538c00 100644
--- a/clang/include/clang/Lex/ModuleMap.h
+++ b/clang/include/clang/Lex/ModuleMap.h
@@ -676,7 +676,7 @@ class ModuleMap {
 
   /// Sets the umbrella header of the given module to the given
   /// header.
-  void setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader,
+  void setUmbrellaHeader(Module *Mod, FileEntryRef UmbrellaHeader,
  const Twine &NameAsWritten,
  const Twine &PathRelativeToRootModuleDirectory);
 

diff  --git a/clang/lib/Frontend/FrontendAction.cpp 
b/clang/lib/Frontend/FrontendAction.cpp
index 6ec6fa0499178..1e276642016d1 100644
--- a/clang/lib/Frontend/FrontendAction.cpp
+++ b/clang/lib/Frontend/FrontendAction.cpp
@@ -12,6 +12,7 @@
 #include "clang/AST/DeclGroup.h"
 #include "clang/Basic/Builtins.h"
 #include "clang/Basic/DiagnosticOptions.h"
+#include "clang/Basic/FileEntry.h"
 #include "clang/Basic/LangStandard.h"
 #include "clang/Basic/Sarif.h"
 #include "clang/Frontend/ASTUnit.h"
@@ -376,7 +377,9 @@ static std::error_code collectModuleHeaderIncludes(
 llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative);
 
 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
-SmallVector, 8> Headers;
+SmallVector<
+std::pair, 8>
+Headers;
 for (llvm::vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End;
  Dir != End && !EC; Dir.increment(EC)) {
   // Check whether this entry has an extension typically associated with
@@ -386,7 +389,7 @@ static std::error_code collectModuleHeaderIncludes(
.Default(false))
 continue;
 
-  auto Header = FileMgr.getFile(Dir->path());
+  auto Header = FileMgr.getOptionalFileRef(Dir->path());
   // FIXME: This shouldn't happen unless there is a file system race. Is
   // that worth diagnosing?
   if (!Header)

diff  --git a/clang/lib/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp
index 106e9c37ce227..ee2cca4e0814e 100644
--- a/clang/lib/Lex/ModuleMap.cpp
+++ b/clang/lib/Lex/ModuleMap.cpp
@@ -304,7 +304,7 @@ bool ModuleMap::resolveAsBuiltinHeader(
   // supplied by Clang. Find that builtin header.
   SmallString<128> Path;
   llvm::sys::path::append(Path, BuiltinIncludeDir->getName(), Header.FileName);
-  auto File = SourceMgr.getFileManager().getFile(Path);
+  auto File = SourceMgr.getFileManager().getOptionalFileRef(Path);
   if (!File)
 return false;
 
@@ -1024,7 +1024,7 @@ Modul

[clang] [clang][modules] Adopt `FileEntryRef` in the `HeaderFileInfo` block in PCM files (PR #67383)

2023-09-26 Thread Richard Howell via cfe-commits

rmaz wrote:

LGTM, possible to come up with some test case with multiple virtual entries to 
the same file that would now be serialized once?

https://github.com/llvm/llvm-project/pull/67383
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] set DebugCompilationDir in PCHContainer (PR #67744)

2023-09-28 Thread Richard Howell via cfe-commits

https://github.com/rmaz created https://github.com/llvm/llvm-project/pull/67744

This change sets the debug compilation directory when generating debug 
information for PCH object containers. This allows for overriding the 
compilation directory in debug information in precompiled pcm files.

>From 0f6bcb1b367ae74f92a15272e5a85664f901e1f4 Mon Sep 17 00:00:00 2001
From: Richard Howell 
Date: Thu, 28 Sep 2023 14:31:16 -0700
Subject: [PATCH] [clang] set DebugCompilationDir in PCHContainer

This change sets the debug compilation directory when generating
debug information for PCH object containers. This allows for
overriding the compilation directory in debug information in
precompiled pcm files.
---
 .../ObjectFilePCHContainerOperations.cpp  |  2 ++
 clang/test/Modules/module-debuginfo-compdir.m | 24 +++
 2 files changed, 26 insertions(+)
 create mode 100644 clang/test/Modules/module-debuginfo-compdir.m

diff --git a/clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp 
b/clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
index 9ffeef026bd1b76..ee543e40b46099d 100644
--- a/clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
+++ b/clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
@@ -161,6 +161,8 @@ class PCHContainerGenerator : public ASTConsumer {
 CodeGenOpts.setDebugInfo(llvm::codegenoptions::FullDebugInfo);
 CodeGenOpts.setDebuggerTuning(CI.getCodeGenOpts().getDebuggerTuning());
 CodeGenOpts.DwarfVersion = CI.getCodeGenOpts().DwarfVersion;
+CodeGenOpts.DebugCompilationDir =
+CI.getInvocation().getCodeGenOpts().DebugCompilationDir;
 CodeGenOpts.DebugPrefixMap =
 CI.getInvocation().getCodeGenOpts().DebugPrefixMap;
 CodeGenOpts.DebugStrictDwarf = CI.getCodeGenOpts().DebugStrictDwarf;
diff --git a/clang/test/Modules/module-debuginfo-compdir.m 
b/clang/test/Modules/module-debuginfo-compdir.m
new file mode 100644
index 000..fced242624f75b3
--- /dev/null
+++ b/clang/test/Modules/module-debuginfo-compdir.m
@@ -0,0 +1,24 @@
+// UNSUPPORTED: target={{.*}}-zos{{.*}}, target={{.*}}-aix{{.*}}
+// REQUIRES: asserts
+
+// Modules:
+// RUN: rm -rf %t
+// RUN: %clang_cc1 -x objective-c -fmodules -fmodule-format=obj \
+// RUN:   -fdebug-compilation-dir=/OVERRIDE \
+// RUN:   -fimplicit-module-maps -DMODULES -fmodules-cache-path=%t %s \
+// RUN:   -I %S/Inputs -I %t -emit-llvm -o %t.ll \
+// RUN:   -mllvm -debug-only=pchcontainer &>%t-mod.ll
+// RUN: cat %t-mod.ll | FileCheck %s
+
+// PCH:
+// RUN: %clang_cc1 -x objective-c -emit-pch -fmodule-format=obj -I %S/Inputs \
+// RUN:   -fdebug-compilation-dir=/OVERRIDE \
+// RUN:   -o %t.pch %S/Inputs/DebugObjC.h \
+// RUN:   -mllvm -debug-only=pchcontainer &>%t-pch.ll
+// RUN: cat %t-pch.ll | FileCheck %s
+
+#ifdef MODULES
+@import DebugObjC;
+#endif
+
+// CHECK: !DIFile(filename: "{{.*}}DebugObjC{{(\.h)?}}", directory: 
"/OVERRIDE")

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] set DebugCompilationDir in PCHContainer (PR #67744)

2023-09-29 Thread Richard Howell via cfe-commits

https://github.com/rmaz closed https://github.com/llvm/llvm-project/pull/67744
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-10-02 Thread Richard Howell via cfe-commits

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

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.

>From 6803c872fad2533673884f059912c0aeb645eadc Mon Sep 17 00:00:00 2001
From: Richard Howell 
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..75dd76a63063274 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.
+  llvm::sys::fs::make_absolute(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 000..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 000..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

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-10-02 Thread Richard Howell via cfe-commits

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 
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 000..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 000..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

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-10-03 Thread Richard Howell via cfe-commits

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

>From 3617539eecb4bfcb9f2a20a1ae1cff71b298 Mon Sep 17 00:00:00 2001
From: Richard Howell 
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 
 .../Modules/Inputs/builtin-headers/module.modulemap|  3 +++
 clang/test/Modules/relative-resource-dir.m | 10 ++
 3 files changed, 17 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 000..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 000..64b31f56be1d6a4
--- /dev/null
+++ b/clang/test/Modules/relative-resource-dir.m
@@ -0,0 +1,10 @@
+// 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 -resource-dir resource-dir \
+// RUN:   -emit-module %S/Inputs/builtin-headers/module.modulemap \
+// RUN:   -fmodule-name=ModuleWithBuiltinHeader -o %t.pcm

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-10-05 Thread Richard Howell via cfe-commits

rmaz wrote:

> This is now always storing an absolute path into 
> Header::PathRelativeToRootModuleDirectory for built-in headers

Wouldn't this always have been the case? I'll check shortly with a resource dir 
picked up via toolchain bundle layout.

> My bigger concern is that we now generate absolute paths into the 
>  buffer which gets serialized into the PCM file. I believe 
> that some folks using Clang modules rely on them being "relocatable", i.e. 
> having no absolute paths serialized. I believe this patch would break that 
> use-case.

This is the use case I am trying to support, via -fmodule-file-home-is-cwd and 
with the resource dir as a subfolder of the cwd. This works currently, but we 
require adding a -I. to module compilation or modules with builtin headers will 
not compile successfully. If the resource dir is outside of the cwd then you 
would have to have an absolute path here anyway, wouldn't you?

I guess the alternative would be to set PathRelativeToRootModuleDirectory 
relative to the resource dir module directory instead, and rely on the resource 
dir being added to the header search paths to make lookup succeed. This seems 
confusing too though, as you would expect the PathRelativeToRootModuleDirectory 
to be the module being compiled, not a different module.

https://github.com/llvm/llvm-project/pull/68023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-10-05 Thread Richard Howell via cfe-commits

rmaz wrote:

> Maybe prepending #pragma before #include of each builtin header?

Could you expand on this? Not sure I get the idea.

https://github.com/llvm/llvm-project/pull/68023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-10-05 Thread Richard Howell via cfe-commits

rmaz wrote:

> Wouldn't this always have been the case? I'll check shortly with a resource 
> dir picked up via toolchain bundle layout.

Looks like yes:
```
Process 77520 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x000109b568fd 
clang`clang::ModuleMap::resolveAsBuiltinHeader(this=0x00011b02ac70, 
Mod=0x00011b03d600, Header=0x7ff7bfefabd0) at ModuleMap.cpp:348:8
   345SmallString<128> Path;
   346llvm::sys::path::append(Path, BuiltinIncludeDir->getName(), 
Header.FileName);
   347auto File = SourceMgr.getFileManager().getOptionalFileRef(Path);
-> 348if (!File)
   349  return false;
   350  
   351// Ensure the path to the module directory is absolute, otherwise
Target 0: (clang) stopped.
(lldb) p Path.str()
(llvm::StringRef)  (Data = 
"/Users/rhow/local/github/llvmbuilddbg.noindex/lib/clang/18/include/float.h", 
Length = 74)
```

https://github.com/llvm/llvm-project/pull/68023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-10-05 Thread Richard Howell via cfe-commits

rmaz wrote:

> I thought you're trying to fix the situation where your resource dir is in 
> the CWD, so that's the context I'm assuming. Is that correct?

Yes.

> I was considering your test case, where we'd previously store 
> "resource-dir/include/float.h" into Header::PathRelativeToRootModuleDirectory

Oh I see, being a bit slow there.

> But -fmodule-file-home-is-cwd will not go as far as fixing up the contents of 
> SourceManager buffers. So while all the paths you serialize into the PCM will 
> get "relativized", the  buffer would still contain the 
> absolute path

Ah, didn't consider that, yes that is an issue you are right.

> I was going to suggest keeping Header::PathRelativeToRootModuleDirectory as 
> "float.h", and generating this into the  buffer

Wouldn't this work without the pragma? Isn't the resource directory always 
added to the header search paths, so the header should be found correctly if we 
set PathRelativeToRootModuleDirectory relative to the resource dir for builtin 
headers?


https://github.com/llvm/llvm-project/pull/68023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-10-06 Thread Richard Howell via cfe-commits

rmaz wrote:

I had a go at this approach this morning, it mostly works but fails for the 
case where we have a module with a textual header with the same name as a 
builtin header. When using relative paths for the builtin headers we now prefer 
the module's textual header instead of the builtin header. This fails the 
following test case:
https://github.com/llvm/llvm-project/blob/main/clang/test/Modules/cstd.m#L27-L29

I believe this is because:

- 
https://github.com/llvm/llvm-project/blob/main/clang/lib/Lex/PPDirectives.cpp#L984
 during file lookup we always add the modules directory at the front of the 
search paths
- 
https://github.com/llvm/llvm-project/blob/main/clang/lib/Lex/HeaderSearch.cpp#L883
 previously we would have hit this case for the builtin headers and directly 
included the file, but now we go through path lookup logic and instead pick up 
the location here from the module dir includers: 
https://github.com/llvm/llvm-project/blob/main/clang/lib/Lex/HeaderSearch.cpp#L932

What would you suggest as the best solution here? Do you think its reasonable 
to always add the builtin header directory to the front of the Includers ahead 
of the module build directory?


https://github.com/llvm/llvm-project/pull/68023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-10-06 Thread Richard Howell via cfe-commits

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

>From d824f3839d34b0d043469c95cff4c146124b3666 Mon Sep 17 00:00:00 2001
From: Richard Howell 
Date: Mon, 2 Oct 2023 11:10:52 -0700
Subject: [PATCH] [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.
---
 clang/include/clang/Lex/ModuleMap.h  |  5 -
 clang/lib/Lex/ModuleMap.cpp  | 16 +---
 clang/lib/Lex/PPDirectives.cpp   | 10 +-
 .../Inputs/builtin-headers/module.modulemap  |  3 +++
 clang/test/Modules/relative-resource-dir.m   | 11 +++
 5 files changed, 40 insertions(+), 5 deletions(-)
 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/include/clang/Lex/ModuleMap.h 
b/clang/include/clang/Lex/ModuleMap.h
index fc49742ad4af2c1..545d07c071c40e5 100644
--- a/clang/include/clang/Lex/ModuleMap.h
+++ b/clang/include/clang/Lex/ModuleMap.h
@@ -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 Callback) {
 Callbacks.push_back(std::move(Callback));
diff --git a/clang/lib/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp
index e8437572ebf4bf6..b12adbb9a7c7f32 100644
--- a/clang/lib/Lex/ModuleMap.cpp
+++ b/clang/lib/Lex/ModuleMap.cpp
@@ -180,8 +180,9 @@ static void appendSubframeworkPaths(Module *Mod,
 OptionalFileEntryRef ModuleMap::findHeader(
 Module *M, const Module::UnresolvedHeaderDirective &Header,
 SmallVectorImpl &RelativePathName, bool &NeedsFramework) {
-  // Search for the header file within the module's home directory.
-  auto Directory = M->Directory;
+  // Search for the header file within the module's home directory
+  // or the builtin include dir if this is a builtin header.
+  auto Directory = Header.HasBuiltinHeader ? BuiltinIncludeDir : M->Directory;
   SmallString<128> FullPathName(Directory->getName());
 
   auto GetFile = [&](StringRef Filename) -> OptionalFileEntryRef {
@@ -348,8 +349,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;
 }
@@ -417,6 +418,15 @@ bool ModuleMap::isBuiltinHeader(FileEntryRef File) {
  isBuiltinHeaderName(llvm::sys::path::filename(File.getName()));
 }
 
+bool ModuleMap::shouldImportRelativeToBuiltinIncludeDir(StringRef FileName,
+Module *Module) const {
+  // If this module imports builtin headers then check if this one is
+  // a builtin header.
+  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);
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 7899bfa1c4f5842..b11dd1d0294f636 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -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"
@@ -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"
@@ -981,7 +983,13 @@ OptionalFileEntryRef Preprocessor::LookupFile(
 // map file.
 if (!FileEnt) {
   if (FID == SourceMgr.getMainFileID() && MainFileDir) {
-Includers.push_back(std::make_pair(std::nullopt, *MainFileDir));
+if (HeaderInfo.getModuleMap().shouldImportRelat

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

2023-10-06 Thread Richard Howell via cfe-commits

https://github.com/rmaz edited https://github.com/llvm/llvm-project/pull/68023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-10-06 Thread Richard Howell via cfe-commits

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

>From 9d5ae4b53558614bdf9681bbefd02cf04aeb107b Mon Sep 17 00:00:00 2001
From: Richard Howell 
Date: Mon, 2 Oct 2023 11:10:52 -0700
Subject: [PATCH] [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.
---
 clang/include/clang/Lex/ModuleMap.h|  5 -
 clang/lib/Lex/ModuleMap.cpp| 14 +++---
 clang/lib/Lex/PPDirectives.cpp |  9 -
 .../Inputs/builtin-headers/module.modulemap|  3 +++
 clang/test/Modules/relative-resource-dir.m | 11 +++
 5 files changed, 37 insertions(+), 5 deletions(-)
 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/include/clang/Lex/ModuleMap.h 
b/clang/include/clang/Lex/ModuleMap.h
index fc49742ad4af2c1..545d07c071c40e5 100644
--- a/clang/include/clang/Lex/ModuleMap.h
+++ b/clang/include/clang/Lex/ModuleMap.h
@@ -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 Callback) {
 Callbacks.push_back(std::move(Callback));
diff --git a/clang/lib/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp
index e8437572ebf4bf6..d1c7b855b9bc919 100644
--- a/clang/lib/Lex/ModuleMap.cpp
+++ b/clang/lib/Lex/ModuleMap.cpp
@@ -180,8 +180,9 @@ static void appendSubframeworkPaths(Module *Mod,
 OptionalFileEntryRef ModuleMap::findHeader(
 Module *M, const Module::UnresolvedHeaderDirective &Header,
 SmallVectorImpl &RelativePathName, bool &NeedsFramework) {
-  // Search for the header file within the module's home directory.
-  auto Directory = M->Directory;
+  // Search for the header file within the module's home directory
+  // or the builtin include dir if this is a builtin header.
+  auto Directory = Header.HasBuiltinHeader ? BuiltinIncludeDir : M->Directory;
   SmallString<128> FullPathName(Directory->getName());
 
   auto GetFile = [&](StringRef Filename) -> OptionalFileEntryRef {
@@ -348,8 +349,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;
 }
@@ -417,6 +418,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);
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 7899bfa1c4f5842..5d6aa4906b757d9 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -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"
@@ -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"
@@ -981,7 +983,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, getCurrent

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

2023-10-06 Thread Richard Howell via cfe-commits

rmaz wrote:

How about the updated approach? This will use a different include dir based on 
if the header is a builtin header name or not, which also solves the issue of 
having to pass the extra include path to the frontend.

https://github.com/llvm/llvm-project/pull/68023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-10-16 Thread Richard Howell via cfe-commits

rmaz wrote:

@jansvoboda11 still good with this change, or is there a less invasive way to 
get this done?

https://github.com/llvm/llvm-project/pull/68023
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 8ee5029 - [clang] don't serialize MODULE_DIRECTORY with ModuleFileHomeIsCwd

2023-04-05 Thread Richard Howell via cfe-commits

Author: Richard Howell
Date: 2023-04-05T07:19:48-07:00
New Revision: 8ee5029b225ba1ff415e0a0a6a68dc4e3efee4d1

URL: 
https://github.com/llvm/llvm-project/commit/8ee5029b225ba1ff415e0a0a6a68dc4e3efee4d1
DIFF: 
https://github.com/llvm/llvm-project/commit/8ee5029b225ba1ff415e0a0a6a68dc4e3efee4d1.diff

LOG: [clang] don't serialize MODULE_DIRECTORY with ModuleFileHomeIsCwd

Fix a bug in the MODULE_DIRECTORY serialization logic
that would cause MODULE_DIRECTORY to be serialized when
`-fmodule-file-home-is-cwd` is specified.

This matches the original logic added in:
https://github.com/apple/llvm-project/commit/f7b41371d9ede1aecf0930e5bd4a463519264633

Reviewed By: keith

Differential Revision: https://reviews.llvm.org/D147561

Added: 


Modified: 
clang/lib/Serialization/ASTWriter.cpp
clang/test/Modules/module-file-home-is-cwd.m

Removed: 




diff  --git a/clang/lib/Serialization/ASTWriter.cpp 
b/clang/lib/Serialization/ASTWriter.cpp
index 177a3c3a34d73..245304254811a 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -1288,11 +1288,11 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, 
ASTContext &Context,
 // If the home of the module is the current working directory, then we
 // want to pick up the cwd of the build process loading the module, not
 // our cwd, when we load this module.
-if (!(PP.getHeaderSearchInfo()
+if (!PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd &&
+(!PP.getHeaderSearchInfo()
   .getHeaderSearchOpts()
   .ModuleMapFileHomeIsCwd ||
-  PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd) 
||
-WritingModule->Directory->getName() != StringRef(".")) {
+ WritingModule->Directory->getName() != StringRef("."))) {
   // Module directory.
   auto Abbrev = std::make_shared();
   Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));

diff  --git a/clang/test/Modules/module-file-home-is-cwd.m 
b/clang/test/Modules/module-file-home-is-cwd.m
index 706b815e6d0c2..a3875d578bb60 100644
--- a/clang/test/Modules/module-file-home-is-cwd.m
+++ b/clang/test/Modules/module-file-home-is-cwd.m
@@ -5,11 +5,12 @@
 // RUN: -fmodules-embed-all-files %S/Inputs/normal-module-map/module.map \
 // RUN: -o %t/mod.pcm
 // RUN: llvm-bcanalyzer --dump --disable-histogram %t/mod.pcm | FileCheck %s
+// RUN: llvm-bcanalyzer --dump --disable-histogram %t/mod.pcm | FileCheck %s 
--check-prefix=INPUT
 
-// CHECK:  blob data = 
'Inputs{{/|\\}}normal-module-map{{/|\\}}module.map'
-// CHECK:  blob data = 
'Inputs{{/|\\}}normal-module-map{{/|\\}}a2.h'
-// CHECK:  blob data = 
'Inputs{{/|\\}}normal-module-map{{/|\\}}a1.h'
 // CHECK-NOT: MODULE_DIRECTORY
+// INPUT:  blob data = 
'Inputs{{/|\\}}normal-module-map{{/|\\}}module.map'
+// INPUT:  blob data = 
'Inputs{{/|\\}}normal-module-map{{/|\\}}a2.h'
+// INPUT:  blob data = 
'Inputs{{/|\\}}normal-module-map{{/|\\}}a1.h'
 
 @import libA;
 



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Avoid repeated hash lookups (NFC) (PR #109375)

2024-09-20 Thread Richard Howell via cfe-commits

https://github.com/rmaz approved this pull request.


https://github.com/llvm/llvm-project/pull/109375
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits