Commit Graph

115 Commits

Author SHA1 Message Date
Ben Langmuir 4a3a9a5fa0 [clang][deps] Sort submodules when calculating dependencies
Dependency scanning does not care about the order of submodules for
correctness, so sort the submodules so that we get the same
command-lines to build the module across different TUs. The order of
inferred submodules can vary depending on the order of #includes in the
including TU.

Differential Revision: https://reviews.llvm.org/D128008
2022-06-17 07:55:27 -07:00
Ben Langmuir 509223da61 [clang][deps] Further canonicalize implicit modules options in dep scan
Disable or canonicalize compiler options that are not relevant in
explicit module builds, similar to what we already did for the modules
cache path. This reduces uninteresting differences between
command-lines, which is particularly useful if there is a tool that can
cache the compilations.

Differential Revision: https://reviews.llvm.org/D127883
2022-06-15 13:29:47 -07:00
Ben Langmuir 7a72dca74a [clang][deps] Set -disable-free for module compilations
The command-line arguments for module builds are cc1 commands, so they
do not implicitly set -disable-free like a driver invocation, and
Tooling will disable it for the scanning instance itself. Set
-disable-free explicitly so that separate invocations for building
modules will not pay for freeing memory unnecessarily.

Differential Revision: https://reviews.llvm.org/D127229
2022-06-08 11:09:17 -07:00
Argyrios Kyrtzidis fad6e37995 [Lex] Fix crash during dependency scanning while skipping an unmatched `#if` 2022-05-27 23:59:30 -07:00
Argyrios Kyrtzidis edcd06ba8b [test/ClangScanDeps] Add a target triple for `macro-expansions.cpp`
This should fix the `clang-ppc64-aix` builder.
2022-05-26 17:18:32 -07:00
Argyrios Kyrtzidis b58a420ff4 [Tooling/DependencyScanning] Rename refactorings towards transitioning dependency scanning to use pre-lexed preprocessor directive tokens
This is first of a series of patches for making the special lexing for dependency scanning a first-class feature of the `Preprocessor` and `Lexer`.
This patch only includes NFC renaming changes to make reviewing of the functionality changing parts easier.

Differential Revision: https://reviews.llvm.org/D125484
2022-05-26 12:49:51 -07:00
Argyrios Kyrtzidis 42823beb1d [Tooling/DependencyScanning] Make skipping excluded PP ranges during dependency scanning the default
This is to improve maintenance a bit and remove need to maintain the additional option and related code-paths.

Differential Revision: https://reviews.llvm.org/D124558
2022-04-28 15:23:03 -07:00
Jan Svoboda b672638dbc [clang][deps] Ensure deterministic filename case
The dependency scanner can reuse single FileManager instance across multiple translation units. This may lead to non-deterministic output depending on which TU gets processed first.

One of the problems is that Clang uses DirectoryEntry::getName in the header search algorithm. This function returns the path that was first used to construct the (shared) entry in FileManager. Using DirectoryEntryRef::getName instead preserves the case as it was spelled out for the current "get directory entry" request.

rdar://90647508

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D123229
2022-04-08 09:18:00 +02:00
Gulfem Savrun Yeniceri 6bb836af2a Revert "[clang][deps] NFC: De-duplicate clang-cl tests"
This reverts commit 30cb49b44e.
It caused test failures on Fuchsia Toolchain Mac builds:
https://luci-milo.appspot.com/ui/p/fuchsia/builders/toolchain.ci/clang-mac-x64/b8818888168677577537/overview
2022-03-24 18:10:08 +00:00
Jan Svoboda 30cb49b44e [clang][deps] NFC: De-duplicate clang-cl tests
In D92191, a bunch of test cases were added to check `clang-scan-deps` works in `clang-cl` mode as well.

We don't need to duplicate all test cases, though. Testing the few special cases we have in `clang-scan-deps` for `clang-cl` should be good enough:

1. Deducing output path (and therefore target name in our make output).
2. Ignoring `-Xclang` arguments in step 1.
3. Deducing resource directory by invoking the compiler executuable.

This test de-duplicates the extra clang-cl test cases.

Reviewed By: dexonsmith, saudi

Differential Revision: https://reviews.llvm.org/D121812
2022-03-23 15:42:11 +01:00
Jan Svoboda 26053ce05a [clang][deps] Create lit substitution for deps-to-rsp
This patch gets rid of the ridiculous relative path we use to invoke the `module-deps-to-rsp.py` script and creates proper lit substitution, cleaning up the tests.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D121525
2022-03-23 15:42:10 +01:00
Jan Svoboda f9096b89dd [clang][deps] NFC: Align path separator conversion in tests
This is the only test in the ClangScanDeps suite that doesn't convert (double) backslashes to forward slashes.
2022-03-16 13:40:09 +01:00
Jan Svoboda 85db3a27cf [clang][deps] Attempt to fix test on Windows
This test fails on Windows due to path separators
2022-03-16 13:36:32 +01:00
Jan Svoboda 1e25ff84d8 [clang][deps] Fix traversal of precompiled dependencies
The code for traversing precompiled dependencies is somewhat complicated and contains a dangling iterator bug.

This patch simplifies the code and fixes the bug.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D121533
2022-03-16 12:17:53 +01:00
Jan Svoboda d73daa9135 [clang][deps] Don't prune search paths used by dependencies
When pruning header search paths (to reduce the number of modules we need to build explicitly), we can't prune the search paths used in (transitive) dependencies of a module. Otherwise, we could end up with either of the following dependency graphs:

```
X:<hash1> -> Y:<hash2>
X:<hash1> -> Y:<hash3>
```

depending on the search paths of the translation unit we discovered `X` and `Y` from.

This patch fixes that.

Depends on D121295.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D121303
2022-03-16 12:17:53 +01:00
Jake Egan d1873d395d [NFC][AIX] Disable precompiled module file test on AIX
This patch follows the same reasoning as D114481. The PCH reader looks for `__clangast` section in the precompiled module file, which is not present in the file on AIX and not supported in XCOFF yet.

Reviewed By: daltenty

Differential Revision: https://reviews.llvm.org/D121709
2022-03-15 15:16:15 -04:00
Jan Svoboda c4b13f7a2f [clang][deps] Simplify PREFIX definitions in tests
Instead of outputting the test directory into the JSON result file, parsing it with `FileCheck` and then potentially stripping it, simply use `FileCheck`'s `-D` option.

Note that we use `%/t` instead of `%t` in order to normalize to forward slashes on Windows, which matches what we do with `sed 's:\\\\\?:/:g'`.

Differential Revision: https://reviews.llvm.org/D121516
2022-03-12 16:45:09 +01:00
Jan Svoboda 4d6835f96d [clang][deps] Attempt to fix Windows test failure 2022-03-12 12:09:22 +01:00
Jan Svoboda cf4a31fc0f [clang][deps] Remove '-fmodules-cache-path=' arguments
With explicit modules build, the '-fmodules-cache-path=' argument is unused.

This patch removes the argument to avoid warnings or errors (with '-Werror') stemming from that.

Depends on D118915.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D120474
2022-03-12 11:42:07 +01:00
Jan Svoboda 7f6af60746 [clang][deps] Generate '-fmodule-file=' only for direct dependencies
The `clang-scan-deps` tool currently generates `-fmodule-file=` command-line arguments for the whole transitive closure of modular dependencies. This is not necessary, we only need to provide the direct dependencies on the command line. Information about transitive dependencies is stored within the `.pcm` files of direct dependencies. This makes the command lines shorter, but should be a NFC otherwise (unless there are bugs in the loading mechanism for explicit modules).

Depends on D120465.

Reviewed By: Bigcheese

Differential Revision: https://reviews.llvm.org/D118915
2022-03-12 11:32:51 +01:00
Jan Svoboda a6ef363546 [clang][deps] Disable implicit module maps
Since D113473, we don't report any module map files via `-fmodule-map-file=` in explicit builds. The ultimate goal here is to make sure Clang doesn't open/read/parse/evaluate unnecessary module maps.

However, implicit module maps still end up reading all reachable module maps. This patch disables implicit module maps in explicit builds.

Unfortunately, we still need to report some module map files that aren't encoded in PCM files of dependencies: module maps that are necessary to correctly evaluate includes in modules marked as `[no_undeclared_includes]`.

Depends on D120464.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D120465
2022-03-12 11:07:21 +01:00
Jan Svoboda 19017c2435 [clang][deps] Return the whole TU command line
The dependency scanner already generates canonical -cc1 command lines that can be used to compile discovered modular dependencies.

For translation unit command lines, the scanner only generates additional driver arguments the build system is expected to append to the original command line.

While this works most of the time, there are situations where that's not the case. For example with `-Wunused-command-line-argument`, Clang will complain about the `-fmodules-cache-path=` argument that's not being used in explicit modular builds. Combine that with `-Werror` and the build outright fails.

To prevent such failures, this patch changes the dependency scanner to return the full driver command line to compile the original translation unit. This gives us more opportunities to massage the arguments into something reasonable.

Reviewed By: Bigcheese

Differential Revision: https://reviews.llvm.org/D118986
2022-02-23 15:46:20 +01:00
Steven Wan 760e69223d [NFC][AIX]Disable new pcm tests on AIX
Same as D114481, the PCH reader looks for a `__clangast` section in the precompiled module file, which isn't present on AIX, and we don't support writing this custom section in XCOFF yet.

Reviewed By: Jake-Egan, daltenty, sfertile

Differential Revision: https://reviews.llvm.org/D118477
2022-01-28 12:39:09 -05:00
Jake Egan 6f4f745668 [clang][deps] Adapt test to be compatible when the assembler is called by default
When `-fno-integrated-as` is in effect (the default on AIX) the cc1 job produces a `.s` file instead. This patch adapts the test to accept `.s` or `.o` files.

Reviewed By: jansvoboda11

Differential Revision: https://reviews.llvm.org/D118152
2022-01-28 11:40:16 -05:00
Hans Wennborg c1335166b2 Don't run test/ClangScanDeps/modules-symlink.c on Windows
'ln -s' isn't Windows friendly.
2022-01-24 16:52:01 +01:00
Jan Svoboda 8cc2a13727 [clang][deps] Handle symlinks in minimizing FS
The minimizing and caching filesystem used by the dependency scanner can be configured to **not** minimize some files. That's necessary when scanning a TU with prebuilt inputs (i.e. PCH) that refer to the original (non-minimized) files. Minimizing such files in the dependency scanner would cause discrepancy between the current perceived state of the filesystem and the file sizes stored in the AST file. By not minimizing such files, we avoid creating the discrepancy.

The problem with the current approach is that files that should not be minimized are identified by their path. This breaks down when the prebuilt input (PCH) and the current TU refer to the same file via different paths (i.e. symlinks). This patch switches from paths to `llvm::sys::fs::UniqueID` when identifying ignored files. This is consistent with how the rest of Clang treats files.

Depends on D114966.

Reviewed By: dexonsmith, arphaman

Differential Revision: https://reviews.llvm.org/D114971
2022-01-21 13:04:25 +01:00
Jake Egan fbf52caf58 [AIX] Disable tests that fail because of no 64-bit XCOFF object file support
The modified tests fail because 64-bit XCOFF object files are not currently supported on AIX. This patch disables these tests on 64-bit AIX for now.

This patch is similar to D111887 except the failures on this patch are on a 64-bit build.

Reviewed By: shchenz, #powerpc

Differential Revision: https://reviews.llvm.org/D113049
2022-01-09 12:25:24 -05:00
Michael Spencer 37e6e022d2 Re-land "[Clang][ScanDeps] Use the virtual path for module maps"
This re-lands:
- 04192422c4
- 015e08c6ba

Which I reverted in ea83517138 in error.

Differential Revision: https://reviews.llvm.org/D114206
2022-01-06 21:05:05 +00:00
Archibald Elliott ea83517138 Revert "[Clang][ScanDeps] Use the virtual path for module maps"
This reverts commits:
- 04192422c4.
- 015e08c6ba

D114206 was landed before it was approved - and was landed knowing that
the test crashed on windows, without an xfail. The promised follow-up
commit with fixes has not appeared since it was promised on December 14th.
2022-01-05 12:17:06 +00:00
Archibald Elliott 015e08c6ba [clang][scandeps] Update Module Cache Path in Test
This fixes the test introduced in D114206 so it no longer writes to the current working directory.

Reviewed By: simon_tatham

Differential Revision: https://reviews.llvm.org/D116611
2022-01-05 10:42:38 +00:00
Michael Spencer 04192422c4 [Clang][ScanDeps] Use the virtual path for module maps
Make clang-scan-deps use the virtual path for module maps instead of the on disk
path. This is needed so that modulemap relative lookups are done correctly in
the actual module builds. The file dependencies still use the on disk path as
that's what matters for build invalidation.

Differential Revision: https://reviews.llvm.org/D114206
2021-12-14 11:21:42 -07:00
Jan Svoboda 47eec789ed [clang][deps] Remove hard-coded resource directory from tests
Dependency scanner test for resource directory deduction doesn't account for LLVM builds with custom `CLANG_RESOURCE_DIR`.

This patch ensures we don't hardcode the default behavior into the test and take into account the actual value. This is done by running `%clang -print-resource-dir` and using that as the expected value in test assertions.
New comment also clarifies this is different from running that command as part of the dependency scan.

Reviewed By: mgorny

Differential Revision: https://reviews.llvm.org/D115628
2021-12-14 10:59:47 +01:00
Jan Svoboda 5b6c08379b [clang][deps] Reset some benign codegen options
Some command-line codegen arguments are likely to differ between identical modules discovered from different translation units. This patch removes them to make builds deterministic and/or reduce the number of built modules.

Reviewed By: Bigcheese

Differential Revision: https://reviews.llvm.org/D112923
2021-12-08 11:53:50 +01:00
Steven Wan 06d4a76309 [NFC][AIX]Disable precompiled module file tests on AIX
The PCH reader looks for `__clangast` section in the precompiled module file, which is not present in the file on AIX, and we don't support writing this custom section in XCOFF yet.

Reviewed By: daltenty

Differential Revision: https://reviews.llvm.org/D114481
2021-11-24 13:22:09 -05:00
Jan Svoboda 17ec9d1f6b [clang][deps] Don't emit `-fmodule-map-file=`
During explicit modules build, when all modules are provided via `-fmodule-file=<path>` and implicit modules and implicit module maps are disabled (`-fno-implicit-modules`, `-fno-implicit-module-maps`), we don't need to load the original module map files at all. This patch stops emitting the `-fmodule-map-file=` arguments we don't need, saving some compilation time due to avoiding parsing such module maps and making the command line shorter.

Reviewed By: bnbarham

Differential Revision: https://reviews.llvm.org/D113473
2021-11-18 12:31:24 +01:00
Sylvain Audi a82a844961 [clang][deps] Keep #pragma push_macro, pop_macro and include_alias when minimizing source code.
The #pragma directives push_macro/pop_macro and include_alias may influence the #include / import directives encountered by dependency scanning tools like clang-scan-deps.

This patch ensures that those directives are not removed during source code minimization.

Differential Revision: https://reviews.llvm.org/D112088
2021-11-01 16:04:52 -04:00
Jan Svoboda e76a9f1636 [clang][deps] Fix fragile test
This test was failing on GreenDragon.
2021-10-27 14:00:13 +02:00
Jan Svoboda b8b14b682c [clang][deps] Make resource directory deduction configurable
The `clang-scan-deps` CLI tool invokes the compiler with `-print-resource-dir` in case the `-resource-dir` argument is missing from the compilation command line. This is to enable running the tool on compilation databases that use compiler from a different toolchain than `clang-scan-deps` itself. While this doesn't make sense when scanning modular builds (due to the `-cc1` arguments the tool generates), the tool can can be used to efficiently scan for file dependencies of non-modular builds too.

This patch stops deducing the resource directory by invoking the compiler by default. This mode can still be enabled by invoking `clang-scan-deps` with `--resource-dir-recipe invoke-compiler`. The new default is `--resource-dir-recipe modify-compiler-path` which relies on the resource directory deduction taking place in `Driver::Driver` which is based on the compiler path. This makes the default more aligned with the intended usage of the tool while still allowing it to serve other use-cases.

Note that this functionality was also influenced by D108979, where the dependency scanner stopped going through `ClangTool::run`. The function tried to deduce the resource directory based on the current executable path, which might not be what the users expect when invoked from within a shared library.

Depends on D108979.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D108366
2021-10-21 14:06:52 +02:00
Jan Svoboda 954d77b98d [clang][deps] Ensure reported context hash is strict
One of main goals of the dependency scanner is to be strict about module compatibility. This is achieved through strict context hash. This patch ensures that strict context hash is enabled not only during the scan itself (and its minimized implicit build), but also when actually reporting the dependency.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D111720
2021-10-21 13:49:47 +02:00
Jan Svoboda 08c8016cfb [clang][modules] Cache loads of modules imported by PCH
During explicit modular build, PCM files are typically specified via the `-fmodule-file=<path>` command-line option. Early during the compilation, Clang uses the `ASTReader` to read their contents and caches the result so that the module isn't loaded implicitly later on. A listener is attached to the `ASTReader` to collect names of the modules read from the PCM files. However, if the PCM has already been loaded previously via PCH:
1. the `ASTReader` doesn't do anything for the second time,
2. the listener is not invoked at all,
3. the module load result is not cached,
4. the compilation fails when attempting to load the module implicitly later on.

This patch solves this problem by attaching the listener to the `ASTReader` for PCH reading as well.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D111560
2021-10-13 18:09:52 +02:00
Jan Svoboda 6a1f50b84a [clang][deps] Prune unused header search paths
To reduce the number of explicit builds of a single module, we can try to squash multiple occurrences of the module with different command-lines (and context hashes) by removing benign command-line options. The greatest contributors to benign differences between command-lines are the header search paths.

In this patch, the lookup cache in `HeaderSearch` is used to identify paths that were actually used when implicitly building the module during scanning. This information is serialized into the unhashed control block of the implicitly-built PCM. The dependency scanner then loads this and may use it to prune the header search paths before computing the context hash of the module and generating the command-line.

We could also prune the header search paths when serializing `HeaderSearchOptions` into the PCM. That way, we could do it only once instead of every load of the PCM file by dependency scanner. However, that would result in a PCM file whose contents don't produce the same context hash as the original build, which is probably highly surprising.

There is an alternative approach to storing extra information into the PCM: wire up preprocessor callbacks to capture the used header search paths on-the-fly during preprocessing of modularized headers (similar to what we currently do for the main source file and textual headers). Right now, that's not compatible with the fact that we do an actual implicit build producing PCM files during dependency scanning. The second run of dependency scanner loads the PCM from the first run, skipping the preprocessing altogether, which would result in different results between runs. We can revisit this approach when we stop building implicitly during dependency scanning.

Depends on D102923.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D102488
2021-10-12 12:39:23 +02:00
Fangrui Song 0867c017e5 [ClangScanDeps][test] Add -fmodules-cache-path=DIR/cache to make diagnostics.c hermetic
Otherwise it may access sys::path::cache_directory/clang/ModuleCache which may not be writable.
2021-09-13 13:17:55 -07:00
Jan Svoboda 285ec53730 [clang][deps] Move tests to the Clang subdirectory 2021-09-10 21:36:49 +02:00
Jan Svoboda 993f60ae32 [clang][deps] Sanitize both instances of DiagnosticOptions
During dependency scanning, we generally want to suppress -Werror. Apply the same logic to the DiagnosticOptions instance used for command-line parsing.

This fixes a test failure on the PS4 bot, where the system header directory could not be found, which was reported due to -Werror being on the command line and not being sanitized.
2021-09-10 14:47:21 +02:00
Akira Hatanaka 17c2948d04 [clang-scan-deps] Add an API for clang dependency scanner to perform
module lookup by name alone

This removes the need to create a fake source file that imports a
module.

rdar://64538073

Differential Revision: https://reviews.llvm.org/D109485
2021-09-09 08:52:50 -07:00
Jan Svoboda 6da811fd5c [clang][deps] Reset non-modular language and preprocessor options
There are a number of language and preprocessor options that are reset in the `CompilerInvocation` that describes the build of an implicit module. This patch uses the logic for explicit modules as well.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D108710
2021-08-26 08:43:21 +02:00
Jan Svoboda b5088cb408 [clang][deps] Ensure deterministic order of TU '-fmodule-file=' arguments
Translation units with multiple direct modular dependencies trigger a non-deterministic ordering in `clang-scan-deps`. This boils down to usage of `std::unordered_map`, which gets replaced by `std::map` in this patch.

Depends on D103526.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D103807
2021-08-25 11:14:16 +02:00
Jan Svoboda 3b8f536fec [clang][deps] Use top-level modules as precompiled dependencies
The `ASTReader` populates `Module::PresumedModuleMapFile` only for top-level modules, not submodules. To avoid generating empty `-fmodule-map-file=` arguments, make discovered modules depend on top-level precompiled modules. The granularity of submodules is not important here.

The documentation of `Module::PresumedModuleMapFile` says this field is non-empty only when building from preprocessed source. This means there can still be cases where the dependency scanner generates empty `-fmodule-map-file=` arguments. That's being addressed in separate patch: D108544.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D108647
2021-08-25 10:51:34 +02:00
Jan Svoboda 83c633ea1a [clang][deps] Collect precompiled deps from submodules too
In this patch, the dependency scanner starts collecting precompiled dependencies from all encountered submodules, not only from top-level modules.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D108540
2021-08-25 10:35:34 +02:00
Alex Lorenz c68f247275 [clang-scan-deps] ignore top-level module dependencies that aren't actually imported
Whenever -fmodule-name=top_level_module name is parsed, and clang actually tries to
import top_level_module, the headers are imported textually and the module isn't actually
built. However, the dependency scanner could still record it as a potential dependency
if the module was reimported and thus recorded by the preprocessor callbacks.
This change avoids collecting this kind of module as a dependency by verifying that we don't
collect top level modules without actual PCM files.

Differential Revision: https://reviews.llvm.org/D106100
2021-07-20 11:11:28 -07:00