aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Lex/ModuleMap.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-05-28[C2y] Add stdcountof.h (#140890)Aaron Ballman1-0/+1
WG14 N3469 changed _Lengthof to _Countof but it also introduced the <stdcountof.h> header to expose a macro with a non-ugly identifier. GCC vends this header as part of the compiler implementation, so Clang should do the same. Suggested-by: Alejandro Colomar <alx@kernel.org>
2025-05-26[Lex] Remove unused includes (NFC) (#141523)Kazu Hirata1-2/+0
These are identified by misc-include-cleaner. I've filtered out those that break builds. Also, I'm staying away from llvm-config.h, config.h, and Compiler.h, which likely cause platform- or compiler-specific build failures.
2025-05-22Revert "[Modules] Don't fail when an unused textual header is missing. ↵Volodymyr Sapsai1-4/+2
(#138227)" This reverts commit 64bb60a471a5ddc9c9bec413c65fdab730a1e4b0. Revert to give more time affected parties to adjust to the change.
2025-05-17[clang] Use llvm::stable_sort (NFC) (#140413)Kazu Hirata1-1/+1
2025-05-08[Modules] Don't fail when an unused textual header is missing. (#138227)Volodymyr Sapsai1-2/+4
According to the documentation > A header declaration that does not contain `exclude` nor `textual` specifies a header that contributes to the enclosing module. Which means that `exclude` and `textual` header don't contribute to the enclosing module and their presence isn't required to build such a module. The keywords tell clang how a header should be treated in a context of the module but they don't add headers to the module. When a textual header *is* used, clang still emits "file not found" error pointing to the location where the missing file is included.
2025-05-06[clang][modules] Lazily load by name lookups in module maps (#132853)Michael Spencer1-18/+144
Instead of eagerly populating the `clang::ModuleMap` when looking up a module by name, this patch changes `HeaderSearch` to only load the modules that are actually used. This introduces `ModuleMap::findOrLoadModule` which will load modules from parsed but not loaded module maps. This cannot be used anywhere that the module loading code calls into as it can create infinite recursion. This currently just reparses module maps when looking up a module by header. This is fine as redeclarations are allowed from the same file, but future patches will also make looking up a module by header lazy. This patch changes the shadow.m test to use explicitly built modules and `#import`. This test and the shadow feature are very brittle and do not work in general. The test relied on pcm files being left behind by prior failing clang invocations that were then reused by the last invocation. If you clean the cache then the last invocation will always fail. This is because the input module map and the `-fmodule-map-file=` module map are parsed in the same module scope, and `-fmodule-map-file=` is forwarded to implicit module builds. That means you are guaranteed to hit a module redeclaration error if the TU actually imports the module it is trying to shadow. This patch changes when we load A2's module map to after the `A` module has been loaded, which sets the `IsFromModuleFile` bit on `A`. This means that A2's `A` is skipped entirely instead of creating a shadow module, and we get textual inclusion. It is possible to construct a case where this would happen before this patch too. An upcoming patch in this series will rework shadowing to work in the general case, but that's only possible once header -> module lookup is lazy too.
2025-05-01[Clang][NFC] Use std::move to avoid copy (#138073)Shafik Yaghmour1-1/+2
Static analysis flagged this code for using copy when we could use std::move. Worth noting that CD.Message is a StringRef but Conflict.Message is std::string. Otherwise I would have used a temporary in place and avoid a local variable.
2025-05-01[Clang][NFC] Use const auto & to avoid copy (#138069)Shafik Yaghmour1-1/+1
Static analysis flagged this code as causing a copy when we never modify ModName and therefore we can use const auto & and avoid copying.
2025-03-25[clang] Consistently use "load" to refer to populating clang::ModuleMap ↵Michael Spencer1-35/+36
(#132970) Now that we have ModuleMapFile.cpp which parses module maps, it's confusing what ModuleMap::parseModuleMapFile actually does. HeaderSearch already called this loading a module map, so consistently use that term in ModuleMap too. An upcoming patch will allow just parsing a module map without loading the modules from it.
2025-03-25[clang] Remove unused member variable from ModuleMapMichael Spencer1-1/+0
This became unused when module map parsing moved to ModuleMapFile.cpp.
2025-02-26[clang][modules] Separate parsing of modulemaps (#119740)Michael Spencer1-1261/+237
This separates out parsing of modulemaps from updating the `clang::ModuleMap` information. Currently this has no effect other than slightly changing diagnostics. Upcoming changes will use this to allow searching for modules without fully processing modulemaps. This creates a new `modulemap` namespace because there are too many things called ModuleMap* right now that mean different things. I'd like to clean this up, but I'm not sure yet what I want to call everything. This also drops the `SourceLocation` from `moduleMapFileRead`. This is never used in tree, and in future patches I plan to make the modulemap parser use a different `SourceManager` so that we can share modulemap parsing between `CompilerInstance`s. This will make the `SourceLocation` meaningless.
2025-02-10[Lex] Avoid repeated hash lookups (NFC) (#126462)Kazu Hirata1-3/+4
2024-11-16[Lex] Remove unused includes (NFC) (#116460)Kazu Hirata1-2/+0
Identified with misc-include-cleaner.
2024-10-28[clang][modules][lldb] Fix build after #113391Jan Svoboda1-10/+13
Instead of changing the return type of `ModuleMap::findOrCreateModule`, this patch adds a counterpart that only returns `Module *` and thus has the same signature as `createModule()`, which is important in `ASTReader`.
2024-10-28[clang][modules] Optimize construction and usage of the submodule index ↵Jan Svoboda1-12/+17
(#113391) This patch avoids eagerly populating the submodule index on `Module` construction. The `StringMap` allocation shows up in my profiles of `clang-scan-deps`, while the index is not necessary most of the time. We still construct it on-demand. Moreover, this patch avoids performing qualified submodule lookup in `ASTReader` whenever we're serializing a module graph whose top-level module is unknown. This is pointless, since that's guaranteed to never find any existing submodules anyway. This speeds up `clang-scan-deps` by ~0.5% on my workload.
2024-10-28[clang][modules] Preserve the module map that allowed inferring (#113389)Jan Svoboda1-7/+4
With inferred modules, the dependency scanner takes care to replace the fake "__inferred_module.map" path with the file that allowed the module to be inferred. However, this only worked when such a module was imported directly in the TU. Whenever such module got loaded transitively, the scanner would fail to perform the replacement. This is caused by the fact that PCM files are lossy and drop this information. This patch makes sure that PCMs include this file for each submodule (in the `SUBMODULE_DEFINITION` record), fixes one existing test with an incorrect assertion, and does a little drive-by refactoring of `ModuleMap`.
2024-10-25[clang][modules] Shrink the size of `Module::Headers` (#113395)Jan Svoboda1-10/+11
This patch shrinks the size of the `Module` class from 2112B to 1624B. I wasn't able to get a good data on the actual impact on memory usage, but given my `clang-scan-deps` workload at hand (with tens of thousands of instances), I think there should be some win here. This also speeds up my benchmark by under 0.1%.
2024-10-24[clang] Use {} instead of std::nullopt to initialize empty ArrayRef (#109399)Jay Foad1-2/+2
Follow up to #109133.
2024-10-22[clang] Allocate `Module` instances in `BumpPtrAllocator` (#112795)Jan Svoboda1-30/+42
In `clang-scan-deps`, we're creating lots of `Module` instances. Allocating them all in a bump-pointer allocator reduces the number of retired instructions by 1-1.5% on my workload.
2024-09-25[clang] Make deprecations of some `FileManager` APIs formal (#110014)Jan Svoboda1-1/+2
Some `FileManager` APIs still return `{File,Directory}Entry` instead of the preferred `{File,Directory}EntryRef`. These are documented to be deprecated, but don't have the attribute that warns on their usage. This PR marks them as such with `LLVM_DEPRECATED()` and replaces their usage with the recommended counterparts. NFCI.
2024-03-28[clang][modules] Avoid calling expensive `SourceManager::translateFile()` ↵Jan Svoboda1-30/+36
(#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-13[clang][modules] giving the __stddef_ headers their own modules can cause ↵Ian Anderson1-3/+6
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-01-20[clang] Use SmallString::operator std::string (NFC)Kazu Hirata1-1/+1
2024-01-08[clang][modules] Remove `_Private` suffix from framework auto-link hints. ↵Juergen Ributzka1-1/+3
(#77120) - [clang][modules] Remove no longer needed autolink test for TBD files. - [clang][modules] Remove `_Private` suffix from framework auto-link hints.
2023-12-13[clang] Use StringRef::{starts,ends}_with (NFC) (#75149)Kazu Hirata1-6/+6
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-09[ADT] Rename SmallString::{starts,ends}with to {starts,ends}_with (#74916)Kazu Hirata1-1/+1
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-08[clang] NFC: Remove `OptionalFileEntryRefDegradesToFileEntryPtr` (#74899)Jan Svoboda1-3/+1
2023-11-24[clang] Fix sorting module headers (#73146)Tulio Magno Quites Machado Filho1-4/+4
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-09[C++20] [Modules] Allow export from language linkageChuanqi Xu1-6/+6
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-10-27[clang] use relative paths for builtin headers during module compilation ↵Richard Howell1-1/+8
(#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-20[Modules] textual headers in submodules never resolve their `use`s (#69651)Ian Anderson1-6/+7
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[clang][modules] Use file name as requested (#68957)Jan Svoboda1-2/+3
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-03[NFC] remove duplicate ModuleId alias (#67899)David Stone1-2/+0
[clang] Remove duplicate `ModuleId` alias
2023-09-28[clang][modules] Use `FileEntryRef` in `ModuleMap` (2/2)Jan Svoboda1-3/+3
2023-09-28[clang][modules] Use `FileEntryRef` in `ModuleMap` (1/2)Jan Svoboda1-6/+5
2023-09-28[Modules] Add a flag to control builtin headers being in system modulesIan Anderson1-23/+46
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-13[clang] NFCI: Use `FileEntryRef` in `ModuleMap::InferredModuleAllowedBy`Jan Svoboda1-5/+5
2023-09-09[clang] NFCI: Use `FileEntryRef` in `ModuleMapCallbacks`Jan Svoboda1-1/+1
2023-09-09[clang] NFCI: Use `FileEntryRef` in `ModuleMap`Jan Svoboda1-1/+1
2023-09-09[clang] NFCI: Use `FileEntryRef` in `ModuleMapParser`Jan Svoboda1-4/+4
2023-08-10[clang][modules] Respect "-fmodule-name=" when serializing included files ↵Jan Svoboda1-2/+1
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-07-20[NFC] Remove needless nullchecks.Sindhu Chittireddy1-1/+1
Differential Revision: https://reviews.llvm.org/D155774
2023-07-17[clang][modules] Skip submodule & framework re-definitions in module mapsJan Svoboda1-2/+19
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[clang][modules] Serialize `Module::DefinitionLoc`Jan Svoboda1-3/+5
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-11[clang] Implement `PointerLikeTraits` for `{File,Directory}EntryRef`Jan Svoboda1-2/+2
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-10[llvm][vfs] For virtual directories, use the virtual path as the real pathJan Svoboda1-12/+2
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-06-15[clang] NFC: Use `DirectoryEntryRef` in `FileManager::getCanonicalName()`Jan Svoboda1-2/+2
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[clang] Use `{File,Directory}EntryRef` in modular header search (part 2/2)Jan Svoboda1-16/+11
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-01[clang] Use `FileEntryRef` in modular header search (part 1/2)Jan Svoboda1-19/+14
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[clang] NFCI: Split `HeaderSearch::findAllModulesForHeader()`Jan Svoboda1-2/+2
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