329 Commits

Author SHA1 Message Date
Jan Svoboda
44af53b22a
[clang][modules] Avoid calling expensive SourceManager::translateFile() (#86216)
The `ASTWriter` algorithm for computing affecting module maps uses
`SourceManager::translateFile()` to get a `FileID` from a `FileEntry`.
This is slow (O(n)) since the function performs a linear walk over
`SLocEntries` until it finds one with a matching `FileEntry`.

This patch removes this use of `SourceManager::translateFile()` by
tracking `FileID` instead of `FileEntry` in couple of places in
`ModuleMap`, giving `ASTWriter` the desired `FileID` directly. There are
no changes required for clients that still want a `FileEntry` from
`ModuleMap`: the existing APIs internally use `SourceManager` to perform
the reverse `FileID` to `FileEntry` conversion in O(1).
2024-03-28 13:02:48 -07:00
Ian Anderson
f50d3582b4
[clang][modules] giving the __stddef_ headers their own modules can cause redeclaration errors with -fbuiltin-headers-in-system-modules (#84127)
On Apple platforms, some of the stddef.h types are also declared in
system headers. In particular NULL has a conflicting declaration in
<sys/_types/_null.h>. When that's in a different module from
<__stddef_null.h>, redeclaration errors can occur.

Make the \_\_stddef_ headers be non-modular in
-fbuiltin-headers-in-system-modules and restore them back to not
respecting their header guards. Still define the header guards though.
__stddef_max_align_t.h was in _Builtin_stddef_max_align_t prior to the
addition of _Builtin_stddef, and it needs to stay in a module because
struct's can't be type merged. __stddef_wint_t.h didn't used to have a
module, but leave it in it current module since it doesn't really belong
to stddef.h.
2024-03-13 11:15:41 -07:00
Kazu Hirata
9b2c25c704 [clang] Use SmallString::operator std::string (NFC) 2024-01-20 18:57:30 -08:00
Juergen Ributzka
f4bc70e886
[clang][modules] Remove _Private suffix from framework auto-link hints. (#77120)
- [clang][modules] Remove no longer needed autolink test for TBD files.
- [clang][modules] Remove `_Private` suffix from framework auto-link
hints.
2024-01-08 11:04:22 -08:00
Kazu Hirata
f3dcc2351c
[clang] Use StringRef::{starts,ends}_with (NFC) (#75149)
This patch replaces uses of StringRef::{starts,ends}with with
StringRef::{starts,ends}_with for consistency with
std::{string,string_view}::{starts,ends}_with in C++20.

I'm planning to deprecate and eventually remove
StringRef::{starts,ends}with.
2023-12-13 08:54:13 -08:00
Kazu Hirata
cc4ecfd68b
[ADT] Rename SmallString::{starts,ends}with to {starts,ends}_with (#74916)
This patch renames {starts,ends}with to {starts,ends}_with for
consistency with std::{string,string_view}::{starts,ends}_with in
C++20.  Since there are only a handful of occurrences, this patch
skips the deprecation phase and simply renames them.
2023-12-09 14:28:45 -08:00
Jan Svoboda
0cb0a48cde
[clang] NFC: Remove OptionalFileEntryRefDegradesToFileEntryPtr (#74899) 2023-12-08 18:22:41 -08:00
Tulio Magno Quites Machado Filho
cf1bde3342
[clang] Fix sorting module headers (#73146)
Struct Module::Header is not a POD type. As such, qsort() and
llvm::array_pod_sort() must not be used to sort it. This became an issue
with the new implementation of qsort() in glibc 2.39 that is not
guaranteed to be a stable sort, causing Headers to be re-ordered and
corrupted.

Replace the usage of llvm::array_pod_sort() with std::stable_sort() in
order to fix this issue. The signature of compareModuleHeaders() has to
be modified.

Fixes #73145.
2023-11-24 09:17:21 -03:00
Chuanqi Xu
0f7aaeb324 [C++20] [Modules] Allow export from language linkage
Close https://github.com/llvm/llvm-project/issues/71347

Previously I misread the concept of module purview. I thought if a
declaration attached to a unnamed module, it can't be part of the module
purview. But after the issue report, I recognized that module purview is
more of a concept about locations instead of semantics.

Concretely, the things in the language linkage after module declarations
can be exported.

This patch refactors `Module::isModulePurview()` and introduces some
possible code cleanups.
2023-11-09 17:44:41 +08:00
Richard Howell
396b5621f9
[clang] use relative paths for builtin headers during module compilation (#68023)
When including builtin headers as part of a system module, ensure we use
relative paths to those headers. Otherwise the module will fail to compile 
when specifying relative resource directories without extra search paths.
2023-10-27 07:10:46 -07:00
Ian Anderson
09ec0004ee
[Modules] textual headers in submodules never resolve their uses (#69651)
When an include from a textual header is resolved, the textual header's
submodule is used as the requesting module. The submodule's uses are
resolved, but that doesn't work because only top level modules have
uses, and only the top level module uses are used for checking uses in
Module::directlyUses. ModuleMap::resolveUses to resolve the top level
module instead of the submodule.
2023-10-20 13:23:34 -07:00
Jan Svoboda
1afb313b26
[clang][modules] Use file name as requested (#68957)
This prevents redefinition errors due to having multiple paths for the
same module map. (rdar://24116019)

Originally implemented and tested downstream by @bcardosolopes, I just
made use of `FileEntryRef::getNameAsRequested()`.
2023-10-20 09:23:19 -07:00
David Stone
06a05f3346
[NFC] remove duplicate ModuleId alias (#67899)
[clang] Remove duplicate `ModuleId` alias
2023-10-03 22:43:51 +08:00
Jan Svoboda
12866a2b87 [clang][modules] Use FileEntryRef in ModuleMap (2/2) 2023-09-28 12:59:26 -07:00
Jan Svoboda
12cb98fe04 [clang][modules] Use FileEntryRef in ModuleMap (1/2) 2023-09-28 12:59:26 -07:00
Ian Anderson
0ea3d88bdb [Modules] Add a flag to control builtin headers being in system modules
Including select builtin headers in system modules is a workaround for module cycles, primarily in Apple's Darwin module that includes all of its C standard library headers. The workaround is problematic because it doesn't include all of the builtin headers (inttypes.h is notably absent), and it also doesn't include C++ headers. The straightforward for for this is to make top level modules for all of the C standard library headers and unwind.h in C++, clang, and the OS.

However, doing so in clang before the OS modules are ready re-introduces the module cycles. Add a -fbuiltin-headers-in-system-modules option to control if the special builtin headers belong to system modules or builtin modules. Pass the option by default for Apple.

Reviewed By: ChuanqiXu, Bigcheese, benlangmuir

Differential Revision: https://reviews.llvm.org/D159483
2023-09-28 11:30:09 -07:00
Jan Svoboda
1b07d43298 [clang] NFCI: Use FileEntryRef in ModuleMap::InferredModuleAllowedBy 2023-09-13 13:32:26 -07:00
Jan Svoboda
e644f5973b [clang] NFCI: Use FileEntryRef in ModuleMapCallbacks 2023-09-09 21:51:04 -07:00
Jan Svoboda
37b08892fc [clang] NFCI: Use FileEntryRef in ModuleMap 2023-09-09 09:12:35 -07:00
Jan Svoboda
c23d65b90f [clang] NFCI: Use FileEntryRef in ModuleMapParser 2023-09-09 09:12:35 -07:00
Jan Svoboda
bbdb0c7e44 [clang][modules] Respect "-fmodule-name=" when serializing included files into a PCH
Clang writes the set of textually included files into AST files, so that importers know to avoid including those files again and instead deserialize their contents from the AST on-demand.

Logic for determining the set of included files files only considers headers that are either non-modular or that are modular but with `HeaderFileInfo::isCompilingModuleHeader` set. Logic for computing that bit is different than the one that determines whether to include a header textually with the "-fmodule-name=Mod" option. That can lead to header from module "Mod" being included textually in a PCH, but be omitted in the serialized set of included files. This can then allow such header to be textually included from importer of the PCH, wreaking havoc.

This patch fixes that by aligning the logic for computing `HeaderFileInfo::isCompilingModuleHeader` with the logic for deciding whether to include modular header textually.

As far as I can tell, this bug has been in Clang for forever. It got accidentally "fixed" by D114095 (that changed the logic for determining the set of included files) and got broken again in D155131 (which is essentially a revert of the former).

rdar://113520515

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D157559
2023-08-10 10:11:43 -07:00
Sindhu Chittireddy
2ce662c5d5 [NFC] Remove needless nullchecks.
Differential Revision: https://reviews.llvm.org/D155774
2023-07-20 23:33:33 -07:00
Jan Svoboda
dba2b5c931 [clang][modules] Skip submodule & framework re-definitions in module maps
Before D150478, there were situations when Clang avoided parsing a module map because it was likely to re-define an already defined module (either by a PCM or by previously-found module map). Since Clang no longer performs that check and does parse the extra module map (due to the FW/FW_Private issue described in D150478), this patch re-implements the same semantics by skipping the duplicate definition of the framework module while parsing the module map.

Depends on D150478.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D150479
2023-07-17 13:50:25 -07:00
Jan Svoboda
abcf7ce457 [clang][modules] Serialize Module::DefinitionLoc
This is a prep patch for avoiding the quadratic number of calls to `HeaderSearch::lookupModule()` in `ASTReader` for each (transitively) loaded PCM file. (Specifically in the context of `clang-scan-deps`).

This patch explicitly serializes `Module::DefinitionLoc` so that we can stop relying on it being filled by the module map parser. This change also required change to the module map parser, where we used the absence of `DefinitionLoc` to determine whether a file came from a PCM file. We also need to make sure we consider the "containing" module map affecting when writing a PCM, so that it's not stripped during serialization, which ensures `DefinitionLoc` still ends up pointing to the correct offset. This is intended to be a NFC change.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D150292
2023-07-17 13:50:23 -07:00
Jan Svoboda
06611e3613 [clang] Implement PointerLikeTraits for {File,Directory}EntryRef
This patch implements `llvm::PointerLikeTraits<FileEntryRef>` and `llvm::PointerLikeTraits<DirectoryEntryRef>`, allowing some simplifications around umbrella header/directory code.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D154905
2023-07-11 11:17:24 -07:00
Jan Svoboda
d77588df45 [llvm][vfs] For virtual directories, use the virtual path as the real path
A follow-up to D135841. This patch returns the virtual path for directories from `RedirectingFileSystem`. This ensures the contents of `Path` are the same as the contents of `FS->getRealPath(Path)`. This also means we can drop the workaround in Clang's module map canonicalization, where we couldn't use the real path for a directory if it resolved to a different `DirectoryEntry`. In addition to that, we can also avoid introducing new workaround for a bug triggered by the newly introduced test case.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D135849
2023-07-10 10:41:15 -07:00
Jan Svoboda
1dee56aed7 [clang] NFC: Use DirectoryEntryRef in FileManager::getCanonicalName()
This patch removes the last use of deprecated `DirectoryEntry::getName()`.

Depends on D151855.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D151922
2023-06-15 11:22:31 +02:00
Jan Svoboda
7bca6f452f [clang] Use {File,Directory}EntryRef in modular header search (part 2/2)
This patch removes some deprecated uses of `{File,Directory}Entry::getName()`. No functional change intended.

Depends on D151854.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D151855
2023-06-15 11:22:31 +02:00
Jan Svoboda
b092f417db [clang] Use FileEntryRef in modular header search (part 1/2)
This patch removes some deprecated uses of `{File,Directory}Entry::getName()`. No functional change indended.

Depends on D151853.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D151854
2023-06-01 10:38:07 -07:00
Jan Svoboda
2e1ec4c737 [clang] NFCI: Split HeaderSearch::findAllModulesForHeader()
This mimics the `ModuleMap` API and enables D151854, where the `AllowCreation = true` function needs `FileEntryRef` but `AllowCreation = false` functions is happy with plain `FileEntry`. No functional change intended.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D151853
2023-06-01 10:11:26 -07:00
Jan Svoboda
f09729042d [clang] NFCI: Use FileEntryRef in ModuleMapCallbacks
This patch removes path hackery from `ModuleMapCallbacks` by adopting `FileEntryRef`. No functional change intended.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D151852
2023-06-01 10:10:00 -07:00
Jan Svoboda
e6830b6028 [clang][modules] NFCI: Extract optionality out of Module::{Header,DirectoryName}
Most users of `Module::Header` already assume its `Entry` is populated. Enforce this assumption in the type system and handle the only case where this is not the case by wrapping the whole struct in `std::optional`. Do the same for `Module::DirectoryName`.

Depends on D151584.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D151586
2023-05-30 21:06:51 -07:00
Jan Svoboda
32d6f3cfd9 [clang] NFCI: Use DirectoryEntryRef in Module::Directory
This patch changes the type of `Module::Directory` from `const DirectoryEntry *` to (essentially) `Optional<DirectoryEntryRef>` in order to remove uses of the deprecated `DirectoryEntry::getName()`.

Depends on D127651.

Reviewed By: bnbarham

Differential Revision: https://reviews.llvm.org/D127654
2023-05-30 13:54:06 -07:00
Jan Svoboda
d574e918db [clang][lex] NFCI: Use DirectoryEntryRef in ModuleMap::parseModuleMapFile()
This patch changes the argument type of `ModuleMap::parseModuleMapFile()` from `const DirectoryEntry *` to `DirectoryEntryRef` in order to remove the deprecated uses of `DirectoryEntry::getName()`.

Depends on D127648.

Reviewed By: bnbarham

Differential Revision: https://reviews.llvm.org/D127651
2023-05-30 13:54:06 -07:00
Jan Svoboda
95279d7670 [clang][lex] NFCI: Use DirectoryEntryRef in ModuleMap::inferFrameworkModule()
This patch changes the argument type of `ModuleMap::inferFrameworkModule()` from `const DirectoryEntry *` to `DirectoryEntryRef` in order to remove the deprecated uses of `DirectoryEntry::getName()`.

Depends on D127647.

Reviewed By: bnbarham

Differential Revision: https://reviews.llvm.org/D127648
2023-05-30 13:54:06 -07:00
Jan Svoboda
769d282d72 [clang][lex] NFCI: Use FileEntryRef in ModuleMap::{load,lookup}ModuleMap()
This patch changes the return/argument types of `ModuleMap::{load,lookup}ModuleMap()` from `const FileEntry *` to `FileEntryRef` in order to remove uses of the deprecated `DirectoryEntry::getName()`.

Reviewed By: bnbarham

Differential Revision: https://reviews.llvm.org/D127647
2023-05-30 13:54:06 -07:00
Jan Svoboda
d6e617c8ef [clang][modules] NFCI: Use DirectoryEntryRef for umbrella directory
This removes some deprecated uses of `DirectoryEntry::getName()`.

Depends on D151581.

Differential Revision: https://reviews.llvm.org/D151584
2023-05-26 15:30:28 -07:00
Jan Svoboda
924912956e [clang][modules] NFCI: Distinguish as-written and effective umbrella directories
For modules with umbrellas, we track how they were written in the module map. Unfortunately, the getter for the umbrella directory conflates the "as written" directory and the "effective" directory (either the written one or the parent of the written umbrella header).

This patch makes the distinction between "as written" and "effective" umbrella directories clearer. No functional change intended.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D151581
2023-05-26 15:14:16 -07:00
Ben Langmuir
5984ea216d [clang] Prevent creation of new submodules in ASTWriter
Avoid inferring new submodules for headers in ASTWriter's collection of
affecting modulemap files, since we don't want to pick up dependencies
that didn't actually exist during parsing.

rdar://109112624

Differential Revision: https://reviews.llvm.org/D150151
2023-05-09 13:02:36 -07:00
Iain Sandoe
6e4f870a21 re-land [C++20][Modules] Introduce an implementation module.
We need to be able to distinguish individual TUs from the same module in cases
where TU-local entities either need to be hidden (or, for some cases of ADL in
template instantiation, need to be detected as exposures).

This creates a module type for the implementation which implicitly imports its
primary module interface per C++20:
[module.unit/8] 'A module-declaration that contains neither an export-keyword
nor a module-partition implicitly imports the primary module interface unit of
the module as if by a module-import-declaration.

Implementation modules are never serialized (-emit-module-interface for an
implementation unit is diagnosed and rejected).

Differential Revision: https://reviews.llvm.org/D126959
2023-03-29 08:52:28 +05:30
Mitch Phillips
074f6fd61d Revert "[C++20][Modules] Introduce an implementation module."
This reverts commit c6e9823724ef6bdfee262289ee34d162db436af0.

Reason: Broke the ASan buildbots, see https://reviews.llvm.org/D126959
(the original phabricator review) for more info.
2023-03-27 05:01:53 -07:00
Mitch Phillips
5ca710ab14 Revert "Silence unused variable warning in NDEBUG builds"
This reverts commit 8c7c1f11ffaacf762e612c65440fd2cbb58ee426.

Reason: Dependent change https://reviews.llvm.org/D126959 broke the ASan
buildbots. See that phabricator review for more comments.
2023-03-27 05:01:53 -07:00
Benjamin Kramer
8c7c1f11ff Silence unused variable warning in NDEBUG builds
I usually would fold this into the assert, but the comment there
suggests side effects. NFC.

ModuleMap.cpp:938:9: error: unused variable 'MainFile' [-Werror,-Wunused-variable]
  auto *MainFile = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
2023-03-23 14:41:03 +01:00
Iain Sandoe
c6e9823724 [C++20][Modules] Introduce an implementation module.
We need to be able to distinguish individual TUs from the same module in cases
where TU-local entities either need to be hidden (or, for some cases of ADL in
template instantiation, need to be detected as exposures).

This creates a module type for the implementation which implicitly imports its
primary module interface per C++20:
[module.unit/8] 'A module-declaration that contains neither an export-keyword
nor a module-partition implicitly imports the primary module interface unit of
the module as if by a module-import-declaration.

Implementation modules are never serialized (-emit-module-interface for an
implementation unit is diagnosed and rejected).

Differential Revision: https://reviews.llvm.org/D126959
2023-03-23 12:47:44 +00:00
Juergen Ributzka
29e2a4eff8 [clang] Unconditionally add autolink hints for frameworks.
Clang infers framework autolink hints when parsing a modulemap. In order to do
so, it checks if the module is a framework and if there is a framework binary
or TBD file in the SDK. Only when Clang finds the filei, then the autolink hint
is added to the module metadata.

During a project build many clang processes perform this check, which causes
many stat calls - even for modules/frameworks that are not even used.

The linker is already resilient to non-existing framework links that come from
the autolink metadata, so there is no need for Clang to do this check.

Instead the autolink hints are now added unconditionally and the linker only
needs to do the check once. This reduces the overall number of stat calls.

This fixes rdar://106578342.

Differential Revision: https://reviews.llvm.org/D146255
2023-03-16 15:31:17 -07:00
Chuanqi Xu
bf52ead24c [C++20] [Modules] Support to export declarations in language linkage
Close https://github.com/llvm/llvm-project/issues/60405

See the discussion in the above link for the background.

What the patch does:
- Rename `Module::ModuleKind::GlobalModuleFragment` to
  `Module::ModuleKind::ExplicitGlobalModuleFragment`.
- Add another module kind `ImplicitGlobalModuleFragment` to
  `ModuleKind`.
- Create an implicit global module fragment for the language linkage
  declarations inside a module purview.
    - If the language linkage lives inside the scope of an export decl,
      the created modules is marked as exported to outer modules.
- In fact, Sema will only create at most 2 implicit global module
  fragments to avoid creating a lot of unnecessary modules in the edging
case.

Reviewed By: iains

Differential Revision: https://reviews.llvm.org/D144367
2023-03-03 10:31:48 +08:00
James Y Knight
ab0116e2f0 [Clang] Improve error message for violations of -fmodules-decluse.
Now it reports the name of the indirectly-used module which is
missing.

Reviewed By: ChuanqiXu

Differential Revision: https://reviews.llvm.org/D142925
2023-01-31 09:57:23 -05:00
Richard Howell
75fbb5d223 [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
2023-01-20 07:23:11 -08:00
Kazu Hirata
6ad0788c33 [clang] Use std::optional instead of llvm::Optional (NFC)
This patch replaces (llvm::|)Optional< with std::optional<.  I'll post
a separate patch to remove #include "llvm/ADT/Optional.h".

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-14 12:31:01 -08:00
Kazu Hirata
a1580d7b59 [clang] Add #include <optional> (NFC)
This patch adds #include <optional> to those files containing
llvm::Optional<...> or Optional<...>.

I'll post a separate patch to actually replace llvm::Optional with
std::optional.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-14 11:07:21 -08:00