aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-08-01[clang][ScanDeps] Clear compilation directory if needed (#150129)Steven Wu1-24/+2
During explicit module build, clear the compilation directory flags when CWD optimization is on. Now clang CodeGen will not add compilation directories to DIFile when the corresponding flags are not passed and make debug info not dependent on CWD.
2025-07-11[clang][scan-deps] Report a scanned TU's visible modules (#147969)Cyndy Ishida1-1/+31
Clients of the dependency scanning service may need to add dependencies based on the visibility of importing modules, for example, when determining whether a Swift overlay dependency should be brought in based on whether there's a corresponding **visible** clang module for it. This patch introduces a new field `VisibleModules` that contains all the visible top-level modules in a given TU. Because visibility is determined by which headers or (sub)modules were imported, and not top-level module dependencies, the scanner now performs a separate DFS starting from what was directly imported for this computation. In my local performance testing, there was no observable performance impact. resolves: rdar://151416358 --------- Co-authored-by: Jan Svoboda <jan@svoboda.ai>
2025-06-24[clang][clang-scan-deps] Add named modules to format 'experimental-full' ↵Naveen Seth Hanig1-3/+2
(#145221)
2025-06-11[Clang][Tooling][NFC] Use move to avoid copies of large objects (#143603)Shafik Yaghmour1-2/+2
Static analysis flagged these cases in which can use std::move and avoid copies of large objects.
2025-05-31[Tooling] Remove unused includes (NFC) (#142257)Kazu Hirata1-1/+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-29NFCI: [clang][deps] Simplify handling of main TU context hash (#141966)Jan Svoboda1-7/+3
Instead of handling the context hash in a preprocessor callback on each file change, do it once at the end of the scan.
2025-05-29Revert "[clang][Dependency Scanning] Report What a Module Exports during ↵Qiongsi Wu1-38/+27
Scanning (#137421)" (#140820) This reverts commit ea1bfbf3f6399b7d2d840722f0e87542d00f6a35. The commit did not solve the fundamental issue we need to handle and is no longer necessary. rdar://144794793
2025-05-04[clang] Remove unused local variables (NFC) (#138453)Kazu Hirata1-1/+0
2025-04-30[clang][Dependency Scanning] Report What a Module Exports during Scanning ↵Qiongsi Wu1-27/+38
(#137421) We would like to report, for a module, which direct dependencies it exports during dependency scanning. This PR implements this reporting by augmenting `ModuleDep`'s `ClangModuleDeps` variable. `ClangModuleDeps` now contains instances of `DepInfo`, which is made of a `ModuleID` and a boolean flag that indicates if a particular dependence is exported. rdar://144794793
2025-04-17Reland [clang] Unify `SourceLocation` and `IdentifierInfo*` pair-like data ↵yronglin1-1/+1
structures to `IdentifierLoc` (#136077) This PR reland https://github.com/llvm/llvm-project/pull/135808, fixed some missed changes in LLDB. I found this issue when I working on https://github.com/llvm/llvm-project/pull/107168. Currently we have many similiar data structures like: - std::pair<IdentifierInfo *, SourceLocation>. - Element type of ModuleIdPath. - IdentifierLocPair. - IdentifierLoc. This PR unify these data structures to IdentifierLoc, moved IdentifierLoc definition to SourceLocation.h, and deleted other similer data structures. --------- Signed-off-by: yronglin <yronglin777@gmail.com>
2025-04-16Revert "[clang] Unify `SourceLocation` and `IdentifierInfo*` pair-like data ↵Michael Buch1-1/+1
structures to `IdentifierLoc`" (#135974) Reverts llvm/llvm-project#135808 Example from the LLDB macOS CI: https://green.lab.llvm.org/job/llvm.org/view/LLDB/job/as-lldb-cmake/24084/execution/node/54/log/?consoleFull ``` /Users/ec2-user/jenkins/workspace/llvm.org/as-lldb-cmake/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.cpp:360:49: error: no viable conversion from 'std::pair<clang::IdentifierInfo *, clang::SourceLocation>' to 'clang::ModuleIdPath' (aka 'ArrayRef<IdentifierLoc>') clang::Module *top_level_module = DoGetModule(clang_path.front(), false); ^~~~~~~~~~~~~~~~~~ /Users/ec2-user/jenkins/workspace/llvm.org/as-lldb-cmake/llvm-project/llvm/include/llvm/ADT/ArrayRef.h:41:40: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'std::pair<clang::IdentifierInfo *, clang::SourceLocation>' to 'const llvm::ArrayRef<clang::IdentifierLoc> &' for 1st argument class LLVM_GSL_POINTER [[nodiscard]] ArrayRef { ^ /Users/ec2-user/jenkins/workspace/llvm.org/as-lldb-cmake/llvm-project/llvm/include/llvm/ADT/ArrayRef.h:41:40: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'std::pair<clang::IdentifierInfo *, clang::SourceLocation>' to 'llvm::ArrayRef<clang::IdentifierLoc> &&' for 1st argument /Users/ec2-user/jenkins/workspace/llvm.org/as-lldb-cmake/llvm-project/llvm/include/llvm/ADT/ArrayRef.h:70:18: note: candidate constructor not viable: no known conversion from 'std::pair<clang::IdentifierInfo *, clang::SourceLocation>' to 'std::nullopt_t' for 1st argument /*implicit*/ ArrayRef(std::nullopt_t) {} ```
2025-04-16[clang] Unify `SourceLocation` and `IdentifierInfo*` pair-like data ↵yronglin1-1/+1
structures to `IdentifierLoc` (#135808) I found this issue when I working on https://github.com/llvm/llvm-project/pull/107168. Currently we have many similiar data structures like: - `std::pair<IdentifierInfo *, SourceLocation>`. - Element type of `ModuleIdPath`. - `IdentifierLocPair`. - `IdentifierLoc`. This PR unify these data structures to `IdentifierLoc`, moved `IdentifierLoc` definition to SourceLocation.h, and deleted other similer data structures. --------- Signed-off-by: yronglin <yronglin777@gmail.com>
2025-04-15[clang][depscan] Centralize logic for populating StableDirs, NFC (#135704)Cyndy Ishida1-12/+8
Pass a reference to `StableDirs` when creating ModuleDepCollector. This avoids needing to create one from the same ScanInstance for each call to `handleTopLevelModule` & reduces the amount of potential downstream changes needed for handling StableDirs.
2025-04-08 [clang][DependencyScanning] Track dependencies from prebuilt modules to ↵Cyndy Ishida1-42/+58
determine IsInStableDir (#132237) When a module is being scanned, it can depend on modules that have already been built from a pch dependency. When this happens, the pcm files are reused for the module dependencies. When this is the case, check if input files recorded from the PCMs come from the provided stable directories transitively since the scanner will not have access to the full set of file dependencies from prebuilt modules.
2025-03-25[clang][DepScan] Allow ModuleDep to be const (#132968)Cyndy Ishida1-1/+3
This type can be exposed from C APIs, where instantiations of this type are not expected to mutate after creation. To support this, mark the lazy computation of build arguments mutable, as that is not intended to otherwise mutate the state of these objects. This was reviewed separately by @jansvoboda11
2025-03-17[clang][DepScan] Pass references to ModuleDeps instead of ModuleID in ↵Cyndy Ishida1-7/+11
lookupModuleOutput callbacks, NFCI (#131688) This allows clients to reference more read-only attributes, like IsInStableDirectories.
2025-03-17[clang][DependencyScanning] Track modules that resolve from "stable" ↵Cyndy Ishida1-3/+82
locations (#130634) That patch tracks whether all the file & module dependencies of a module resolve to a stable location. This information will later be queried by build systems for determining where to store the accompanying pcms.
2025-02-27[clang][deps] Propagate the entire service (#128959)Jan Svoboda1-22/+23
Shared state between dependency scanning workers is managed by the dependency scanning service. Right now, the members are individually threaded through the worker, action, and collector. This makes any change to the service and its members a very laborious process. Moreover, this situation causes frequent merge conflicts in our downstream repo where the service does have some extra members that need to be passed around. To ease the maintenance burden, this PR starts passing a reference to the entire service.
2025-02-26[clang modules] Setting `DebugCompilationDir` when it is safe to ignore ↵Qiongsi Wu1-7/+43
current working directory (#128446) This PR explicitly sets `DebugCompilationDir` to the system's root directory if it is safe to ignore the current working directory. This fixes a problem where a PCM file's embedded debug information can lead to compilation failure. The compiler may have decided it is indeed safe to ignore the current working directory. In this case, the PCM file's content is functionally correct regardless of the current working directory because no inputs use relative paths (see https://github.com/llvm/llvm-project/pull/124786). However, a PCM may contain debug info. If debug info is requested, the compiler uses the current working directory value to set `DW_AT_comp_dir`. This may lead to the following situation: 1. Two different compilations need the same PCM file. 2. The PCM file is compiled assuming a working directory, which is embedded in the debug info, but otherwise has no effect. 3. The second compilation assumes a different working directory, and expects an identically-sized pcm file. However, it cannot find such a PCM, because the existing PCM file has been compiled assuming a different `DW_AT_comp_dir `, which is embedded in the debug info. This PR resets the `DebugCompilationDir` if it is functionally safe to ignore the working directory so the above situation is avoided, since all debug information will share the same working directory. rdar://145249881
2025-02-04[clang module] Current Working Directory Pruning (#124786)Qiongsi Wu1-4/+92
When computing the context hash, `clang` always includes the compiler's working directory. This can lead to situations when the only difference between two compilations is the working directory, different module variants are generated. These variants are redundant. This PR implements an optimization that ignores the working directory when computing the context hash when safe. Specifically, `clang` checks if it is safe to ignore the working directory in `isSafeToIgnoreCWD`. The check involves going through compile command options to see if any paths specified are relative. The definition of relative path used here is that the input path is not empty, and `llvm::sys::path::is_absolute` is false. If all the paths examined are not relative, `clang` considers it safe to ignore the current working directory and does not consider the working directory when computing the context hash.
2025-01-30[Clang] Remove ARCMigrate (#119269)Sirraide1-3/+0
In the discussion around #116792, @rjmccall mentioned that ARCMigrate has been obsoleted and that we could go ahead and remove it from Clang, so this patch does just that.
2024-11-11[clang][deps][modules] Allocate input file paths lazily (#114457)Jan Svoboda1-16/+25
This PR builds on top of #113984 and attempts to avoid allocating input file paths eagerly. Instead, the `InputFileInfo` type used by `ASTReader` now only holds `StringRef`s that point into the PCM file buffer, and the full input file paths get resolved on demand. The dependency scanner makes use of this in a bit of a roundabout way: `ModuleDeps` now only holds (an owning copy of) the short unresolved input file paths, which get resolved lazily. This can be a big win, I'm seeing up to a 5% speedup.
2024-10-28[clang][modules] Preserve the module map that allowed inferring (#113389)Jan Svoboda1-11/+7
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-04[DependencyScanning] Avoid repeated hash lookups (NFC) (#111088)Kazu Hirata1-1/+1
2024-09-25[clang] Make deprecations of some `FileManager` APIs formal (#110014)Jan Svoboda1-3/+5
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-09-05[clang][scan] Report module dependencies in topological order (#107474)Jan Svoboda1-5/+6
2024-06-04[clang][deps] Collect discovered module dependencies' Link Libraries (#93588)Artem Chikin1-0/+4
This will allow scanner clients to be able to compute e.g. auto-linking dependencies of the scanned translation unit.
2024-04-19[clang][modules] Only avoid pruning module maps when asked to (#89428)Jan Svoboda1-0/+5
Pruning non-affecting module maps is useful even when passing module maps explicitly via `-fmodule-map-file=<path>`. For this situation, this patch reinstates the behavior we had prior to #87849. For the situation where the explicit module map file arguments were generated by the dependency scanner (which already pruned the non-affecting ones), this patch introduces new `-cc1` flag `-fno-modules-prune-non-affecting-module-map-files` that avoids the extra work.
2024-04-15[clang/DependencyScanning/ModuleDepCollector] Refactor part of ↵Argyrios Kyrtzidis1-12/+24
`makeCommonInvocationForModuleBuild` into its own function (#88447) The new function is about clearing out benign codegen options and can be applied for PCH invocations as well.
2024-04-05[clang][deps] Remove pgo profile flags from modules (#87724)Argyrios Kyrtzidis1-0/+3
These are not necessary when not performing codegen.
2024-02-23[clang][ScanDeps] Allow PCHs to have different VFS overlays (#82294)Michael Spencer1-8/+26
It turns out it's not that uncommon for real code to pass a different set of VFSs while building a PCH than while using the PCH. This can cause problems as seen in `test/ClangScanDeps/optimize-vfs-pch.m`. If you scan `compile-commands-tu-no-vfs-error.json` without -Werror and run the resulting commands, Clang will emit a fatal error while trying to emit a note saying that it can't find a remapped header. This also adds textual tracking of VFSs for prebuilt modules that are part of an included PCH, as the same issue can occur in a module we are building if we drop VFSs. This has to be textual because we have no guarantee the PCH had the same list of VFSs as the current TU. This uses the `PrebuiltModuleListener` to collect `VFSOverlayFiles` instead of trying to extract it out of a `serialization::ModuleFile` each time it's needed. There's not a great way to just store a pointer to the list of strings in the serialized AST.
2024-02-08[clang][lex] Always pass suggested module to `InclusionDirective()` callback ↵Jan Svoboda1-4/+4
(#81061) This patch provides more information to the `PPCallbacks::InclusionDirective()` hook. We now always pass the suggested module, regardless of whether it was actually imported or not. The extra `bool ModuleImported` parameter then denotes whether the header `#include` will be automatically translated into import the the module. The main change is in `clang/lib/Lex/PPDirectives.cpp`, where we take care to not modify `SuggestedModule` after it's been populated by `LookupHeaderIncludeOrImport()`. We now exclusively use the `SM` (`ModuleToImport`) variable instead, which has been equivalent to `SuggestedModule` until now. This allows us to use the original non-modified `SuggestedModule` for the callback itself. (This patch turns out to be necessary for https://github.com/apple/llvm-project/pull/8011).
2024-01-31[clang][DependencyScanner] Remove unused -fmodule-map-file arguments (#80090)Michael Spencer1-0/+4
Since we already add a `-fmodule-map-file=` argument for every used modulemap, we can remove all `ModuleMapFiles` entries before adding them. This reduces the number of module variants when `-fmodule-map-file=` appears on the original command line.
2024-01-30[clang][DependencyScanner] Remove unused -ivfsoverlay files (#73734)Michael Spencer1-21/+53
`-ivfsoverlay` files are unused when building most modules. Enable removing them by, * adding a way to visit the filesystem tree with extensible RTTI to access each `RedirectingFileSystem`. * Adding tracking to `RedirectingFileSystem` to record when it actually redirects a file access. * Storing this information in each PCM. Usage tracking is only enabled when iterating over the source manager and affecting modulemaps. Here each path is stated to cause an access. During scanning these stats all hit the cache.
2023-12-14[clang][modules] Strip LLVM options (#75405)Juergen Ributzka1-0/+2
Currently, the dep scanner does not remove LLVM options from the argument list. Since LLVM options shouldn't affect the AST, it is safe to remove them all.
2023-12-13[clang] Use StringRef::{starts,ends}_with (NFC) (#75149)Kazu Hirata1-2/+2
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-08[clang] NFC: Remove `OptionalFileEntryRefDegradesToFileEntryPtr` (#74899)Jan Svoboda1-1/+1
2023-11-30[clang][DependencyScanner] Include the working directory in the context hash ↵Michael Spencer1-2/+7
(#73719) The working directory is included in the PCM, but is not currently part of the context hash. This causes problems because different builds of a PCM with exactly the same command line can end up with different binary content for a PCM. If a build system tracks tasks by both working directory and command line, it may build a given PCM multiple times, causing a "module file out of date" error when loading the PCM due to different sizes.
2023-11-13[clang][DependencyScanner] Remove all warning flags when suppressing ↵Michael Spencer1-0/+26
warnings (#71612) Since system modules don't emit most warnings, remove the warning flags to increase module reuse.
2023-11-07[clang][DepScan] Make OptimizeArgs a bit mask enum and enable by default ↵Michael Spencer1-2/+3
(#71588) Make it easier to control which optimizations are enabled by making OptimizeArgs a bit masked enum. There's currently only one such optimization, but more will be added in followup commits.
2023-10-31[clang-scan-deps] [P1689] Keep consistent behavior for make dependencies ↵Chuanqi Xu1-0/+12
with clang (#69551) Close https://github.com/llvm/llvm-project/issues/69439. This patch tries to reuse the codes to generate make style dependencies information with P1689 format directly.
2023-10-09Use llvm::endianness{,::little,::native} (NFC)Kazu Hirata1-2/+1
Now that llvm::support::endianness has been renamed to llvm::endianness, we can use the shorter form. This patch replaces llvm::support::endianness with llvm::endianness.
2023-09-10[clang][deps] NFCI: Use `FileEntryRef` in `ModuleDepCollectorPP`Jan Svoboda1-1/+1
2023-09-07[clang][deps] Generate command lines lazily (#65691)Jan Svoboda1-1/+9
This patch makes the generation of command lines for modular dependencies lazy/on-demand. That operation is somewhat expensive and prior to this patch used to be performed multiple times for the identical `ModuleDeps` (i.e. when they were imported from multiple different TUs).
2023-09-07[clang][deps] Store common, partially-formed invocation (#65677)Jan Svoboda1-47/+71
We create one `CompilerInvocation` for each modular dependency we discover. This means we create a lot of copies, even though most of the invocation is the same between modules. This patch makes use of the copy-on-write flavor of `CompilerInvocation` to share the common parts, reducing memory usage and speeding up the scan.
2023-09-05[clang] NFCI: Change returned LanguageOptions pointer to referenceJan Svoboda1-2/+2
2023-08-09[clang][modules] Add -Wsystem-headers-in-module=Ben Langmuir1-0/+8
Add a way to enable -Wsystem-headers only for a specific module. This is useful for validating a module that would otherwise not see system header diagnostics without being flooded by diagnostics for unrelated headers/modules. It's relatively common for a module to be marked [system] but still wish to validate itself explicitly. rdar://113401565 Differential Revision: https://reviews.llvm.org/D156948
2023-08-09[clang][modules][deps] Create more efficient API for visitation of ↵Jan Svoboda1-8/+13
`ModuleFile` inputs The current `ASTReader::visitInputFiles()` function calls into `FileManager` to create `FileEntryRef` objects. This ends up being fairly costly in `clang-scan-deps`, where we mostly only care about file paths. This patch introduces new `ASTReader` API that gives clients access to just the serialized paths. Since the scanner needs both the as-requested path and the on-disk one (and doesn't want to transform the former into the latter via `FileManager`), this patch starts serializing both of them into the PCM file if they differ. This increases the size of scanning PCMs by 0.1% and speeds up scanning by 5%. Reviewed By: benlangmuir, vsapsai Differential Revision: https://reviews.llvm.org/D157066
2023-08-03[clang][deps] NFC: Speed up canonical context hash computationJan Svoboda1-6/+7
This patch makes use of the infrastructure established in D157046 to speed up computation of the canonical context hash in the dependency scanner. This is somewhat hot code, since it's ran for all modules in the dependency graph of every TU. I also tried an alternative approach that tried to avoid allocations as much as possible (essentially doing `HashBuilder.add(Arg.toStringRef(ArgVec))`), but that turned out to be slower than approach in this patch. Note that this is not problematic in the same way command-line hashing used to be prior D143027. The lambda is now being called even for constant strings. Depends on D157046. Reviewed By: benlangmuir Differential Revision: https://reviews.llvm.org/D157052
2023-07-28[clang][deps] Remove `ModuleDeps::ImportedByMainFile`Jan Svoboda1-5/+11
This information is already exposed via `TranslationUnitDeps::ClangModuleDeps` on the `DependencyScanningTool` level, and this patch also adds it on the `DependencyScanningWorker` level via `DependencyConsumer::handleDirectModuleDependency()`. Besides being redundant, this bit of information is misleading for clients that share single `ModuleDeps` instance between multiple TUs (by using the `AlreadySeen` set). The module can be imported directly in some TUs but transitively in others. Reviewed By: benlangmuir Differential Revision: https://reviews.llvm.org/D156563