130 Commits

Author SHA1 Message Date
Artem Chikin
68eb3b202f
[clang][deps] Collect discovered module dependencies' Link Libraries (#93588)
This will allow scanner clients to be able to compute e.g. auto-linking
dependencies of the scanned translation unit.
2024-06-04 09:16:49 -07:00
Alexandre Ganea
90e33e20a5
[clang-scan-deps] Expand response files before the argument adjuster (#89950)
Previously, since response (.rsp) files weren't expanded at the very
beginning of clang-scan-deps, we only parsed the command-line as
provided in the Clang .cdb file. Unfortunately, when using Unreal
Engine, arguments are always generated in a .rsp file (ie.
`/path/to/clang-cl.exe @/path/to/filename_args.rsp`).

After this patch, `/Fo` can be parsed and added to the final
command-line. Without this option, the make targets that are emitted are
made up from the input file name alone. We have some cases where the
same input in the project generates several output files, so we end up
with duplicate make targets in the scan-deps emitted dependency file.
2024-05-24 17:20:08 -04:00
Jan Svoboda
6a4eaf9b33
[clang][deps] Add -o flag to specify output path (#88767)
This makes it possible to pass "-o /dev/null" to `clang-scan-deps` and
skip some potentially expensive work, making timings less noisy. Also
removes the need for stream redirection.
2024-04-16 19:49:07 -07:00
Jan Svoboda
eafd515eca
[clang][deps] Support single-file mode for all formats (#88764)
The `clang-scan-deps` tool can be used for fast scanning of batches of
compilation commands passed in via the `-compilation-database` option.
This gets awkward in our tests where we have to resort to using
`.in`/`.template` JSON files and running them through `sed` in order to
embed LIT's `%t` variable into them. However, most of our tests only
need to pass single compilation command, so this dance is entirely
unnecessary.

This patch makes sure the existing "per-file" mode (where the
compilation command is passed in-line after the `--` argument) works for
all output formats, not only `P1689`.
2024-04-16 19:47:52 -07:00
Chuanqi Xu
9d6c43b4ae
[NFC] [C++20] [Modules] [P1689] [Scanner] Don't use thread pool in P1689 per file mode (#84285)
I suddenly found that the clang scan deps may use all concurrent threads
to scan the files. It makes sense in the batch mode. But in P1689 per
file mode, it simply wastes times and resources.

This patch itself should be a NFC patch. It simply moves codes.
2024-03-13 09:26:53 +08:00
Mehdi Amini
716042a63f
Rename llvm::ThreadPool -> llvm::DefaultThreadPool (NFC) (#83702)
The base class llvm::ThreadPoolInterface will be renamed
llvm::ThreadPool in a subsequent commit.

This is a breaking change: clients who use to create a ThreadPool must
now create a DefaultThreadPool instead.
2024-03-05 18:00:46 -08:00
Michael Spencer
d42de86eb3
reland: [clang][ScanDeps] Canonicalize -D and -U flags (#82568)
Canonicalize `-D` and `-U` flags by sorting them and only keeping the
last instance of a given name.

This optimization will only fire if all `-D` and `-U` flags start with a
simple identifier that we can guarantee a simple analysis of can
determine if two flags refer to the same identifier or not. See the
comment on `getSimpleMacroName()` for details of what the issues are.

Previous version of this had issues with sed differences between macOS,
Linux, and Windows. This test doesn't check paths, so just don't run
sed.
Other tests should use `sed -E 's:\\\\?:/:g'` to get portable behavior.

Windows has different command line parsing behavior than Linux for
compilation databases, so the test has been adjusted to ignore that
difference.
2024-02-23 17:44:32 -08:00
Nico Weber
84ed55e11f Revert "[clang][ScanDeps] Canonicalize -D and -U flags (#82298)"
This reverts commit 3ff805540173b83d73b673b39ac5760fc19bac15.

Test is failing on bots, see
https://github.com/llvm/llvm-project/pull/82298#issuecomment-1955664462
2024-02-20 20:24:32 -05:00
Michael Spencer
3ff8055401
[clang][ScanDeps] Canonicalize -D and -U flags (#82298)
Canonicalize `-D` and `-U` flags by sorting them and only keeping the
last instance of a given name.

This optimization will only fire if all `-D` and `-U` flags start with a
simple identifier that we can guarantee a simple analysis of can
determine if two flags refer to the same identifier or not. See the
comment on `getSimpleMacroName()` for details of what the issues are.
2024-02-20 15:20:40 -08:00
Mehdi Amini
744616b3ae
Rename ThreadPool::getThreadCount() to getMaxConcurrency() (NFC) (#82296)
This is addressing a long-time TODO to rename this misleading API. The
old one is preserved for now but marked deprecated.
2024-02-19 18:07:12 -08:00
Yaraslau
d783933bc9
[clang-scan-deps] Fix check for empty Compilation (#75545)
Closes https://github.com/llvm/llvm-project/issues/64144

Instead of checking for `nullptr` we need to ensure that `JobList` is
not empty to proceed
2024-01-31 09:55:05 +08:00
Michael Spencer
7847e44594
[clang][DependencyScanner] Remove unused -ivfsoverlay files (#73734)
`-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.
2024-01-30 15:39:18 -08:00
Alexandre Ganea
3c6f47d6b8
[llvm-driver] Fix usage of InitLLVM on Windows (#76306)
Previously, some tools such as `clang` or `lld` which require strict
order for certain command-line options, such as `clang -cc1` or `lld
-flavor`, would not longer work on Windows, when these tools were linked
as part of `llvm-driver`. This was caused by `InitLLVM` which was part
of the `*_main()` function of these tools, which in turn calls
`windows::GetCommandLineArguments`. That function completly replaces
argc/argv by new UTF-8 contents, so any ajustements to argc/argv made by
`llvm-driver` prior to calling these tools was reset.

`InitLLVM` is now called by the `llvm-driver`. Any tool that
participates in (or is part of) the `llvm-driver` doesn't call
`InitLLVM` anymore.
2024-01-11 19:08:28 -05: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
Michael Spencer
731152e18f
[clang][DependencyScanner] Remove all warning flags when suppressing warnings (#71612)
Since system modules don't emit most warnings, remove the warning flags
to increase module reuse.
2023-11-13 16:45:38 -08:00
Michael Spencer
fb07d9cc09
[clang][DepScan] Make OptimizeArgs a bit mask enum and enable by default (#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-11-07 16:06:59 -08:00
Jan Svoboda
3b1a68655e
[clang][deps] Generate command lines lazily (#65691)
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 17:45:53 -07:00
Takuya Shimizu
01b88dd66d [NFC] Remove unused variables declared in conditions
D152495 makes clang warn on unused variables that are declared in conditions like `if (int var = init) {}`
This patch is an NFC fix to suppress the new warning in llvm,clang,lld builds to pass CI in the above patch.

Differential Revision: https://reviews.llvm.org/D158016
2023-08-30 10:05:06 +09:00
Jan Svoboda
3f092f37b7 [llvm] Extract common OptTable bits into macros
All command-line tools using `llvm::opt` create an enum of option IDs and a table of `OptTable::Info` object. Most of the tools use the same ID (`OPT_##ID`), kind (`Option::KIND##Class`), group ID (`OPT_##GROUP`) and alias ID (`OPT_##ALIAS`). This patch extracts that common code into canonical macros. This results in fewer changes when tweaking the `OPTION` macros emitted by the TableGen backend.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D157028
2023-08-04 13:57:13 -07:00
Jan Svoboda
8a077cfe23 [clang][deps] Make the C++ API more type-safe
Scanner's C++ API accepts a set of modular dependencies the client has already seen and for which it doesn't need the full details. This is currently a set of strings, which somewhat implies that it should contain the set of module names. However, scanner internally expects the values to be in the format "{hash}{name}". Besides not being documented, this is very unintuitive. This patch makes this expectation explicit by changing the type to set of `ModuleID`.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D156492
2023-07-28 12:03:54 -07:00
Jan Svoboda
6b80f00bac [clang][deps] NFC: Refactor and comment ModuleDeps sorting
I once again stumbled across `IndexedModuleID` and was confused by the mutable `InputIndex` field.

This patch adds comments around that piece of code and unifies/simplifies related functions.

Reviewed By: Bigcheese

Differential Revision: https://reviews.llvm.org/D145197
2023-04-21 10:56:42 -07:00
Jan Svoboda
e766e3afed [clang][deps] Print timing information
This patch adds new `-print-timing` option to `clang-scan-deps`. It measures the wall and process time taken to scan dependencies for the compilation database. This provides more representative data compared to measuring the timing for the whole tool invocation, since that includes parsing and generating JSON files, which can be significant for larger inputs.

Reviewed By: akyrtzi

Differential Revision: https://reviews.llvm.org/D147815
2023-04-20 11:18:32 -07:00
NAKAMURA Takumi
a2d1611b7b [CMake] Reduce deps 2023-04-20 08:45:38 +09:00
Alex Brachet
c2423a3345 [clang-scan-deps] Add clang-scan-deps to llvm-driver build
Differential Revision: https://reviews.llvm.org/D148583
2023-04-18 15:02:23 +00:00
NAKAMURA Takumi
077a2a4bcd [CMake] Cleanup deps 2023-04-17 00:38:49 +09:00
NAKAMURA Takumi
7d5d987e93 [CMake] Reorder and reformat deps 2023-04-17 00:32:16 +09:00
Alex Brachet
384fca554a Reland "[clang-scan-deps] Migrate to OptTable"
Differential Revision: https://reviews.llvm.org/D139949
2023-04-14 19:16:10 +00:00
Jan Svoboda
9679075a1a [clang][deps] NFC: Simplify worker loop
This patch simplifies the loop inside each worker by extracting index retrieval into a lambda function.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D145101
2023-03-02 13:50:08 -08:00
Jan Svoboda
8640545008 [clang][deps] Preserve input ordering in the full output
This patch makes sure the ordering of TUs in the input CDB is preserved in the full output. Previously, the results were pushed into a global vector, potentially out-of-order and then sorted by the input file name. This is non-deterministic when the CDB contains multiple entries with the same input file. This patch fixes that by pre-allocating the output vector and writing directly to the position corresponding to the current input. This also eliminates one critical section.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D145098
2023-03-02 13:42:11 -08:00
Chuanqi Xu
64287d6982 Recommit [C++20] [Modules] [ClangScanDeps] Enable to print make-style dependency file within P1689 format (4/4)
Required in https://reviews.llvm.org/D137534.

The build systems needs the information to know that "header X changed,
scanning may have changed, so please rerun scanning". Although it is
possible to get the information by running clang-scan-deps for the
second time with make format, it is not user friendly clearly.

Reviewed By: jansvoboda11

Differential Revision: https://reviews.llvm.org/D139168
2023-02-13 11:26:34 +08:00
Chuanqi Xu
212d905643 Recommit [C++20] [Modules] [ClangScanDeps] Allow clang-scan-deps to without specified compilation database in P1689 (3/4)
In a private chat with @ben.boeckel , we get in consensus it would be
great for cmake if the invocation of clang-scan-deps can get rid of
compilation database. Due to the compilation database can't do very well
for the files which are not existed yet. @ben.boeckel may have more
context to add here.

This patch should be innocent for others usages.

Reviewed By: jansvoboda11

Differential Revision: https://reviews.llvm.org/D137534
2023-02-13 11:01:24 +08:00
Chuanqi Xu
eb70b38f83 Recommit [C++20] [Modules] [ClangScanDeps] Add ClangScanDeps support for C++20 Named Modules in P1689 format (2/4)
Close https://github.com/llvm/llvm-project/issues/51792
Close https://github.com/llvm/llvm-project/issues/56770

This patch adds ClangScanDeps support for C++20 Named Modules in P1689
format. We can find the P1689 format at:
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1689r5.html.
After we land the patch, we're able to compile C++20 Named
Modules with CMake! And although P1689 is written by kitware people,
other build systems should be able to use the format to compile C++20
Named Modules too.

TODO: Support header units in P1689 Format.
TODO2: Support C++20 Modules in the full dependency format of
ClangScanDeps. We also want to support C++20 Modules and clang modules
together according to
https://discourse.llvm.org/t/how-should-we-support-dependency-scanner-for-c-20-modules/66027.
But P1689 format cares about C++20 Modules only for now. So let's focus
on C++ Modules and P1689 format. And look at the full dependency format
later.

I'll add the ReleaseNotes and Documentations after the patch get landed.

Reviewed By: jansvoboda11

Differential Revision: https://reviews.llvm.org/D137527
2023-02-13 10:42:35 +08:00
NAKAMURA Takumi
069dd8768a Revert "[C++20] [Modules] [ClangScanDeps] Add ClangScanDeps support for C++20 Named Modules in P1689 format (2/4)"
This reverts commit de17c665e3f995c7f5a0e453461ce3a1b8aec196.

See also D137527
2023-02-12 18:38:25 +09:00
NAKAMURA Takumi
f8b5003cb3 Revert "[C++20] [Modules] [ClangScanDeps] Allow clang-scan-deps to without specified compilation database in P1689 (3/4)"
This reverts commit 591fdcde921334b2d502779006d7c168307a2106.

See also D137527
2023-02-12 18:38:17 +09:00
Archibald Elliott
d768bf994f [NFC][TargetParser] Replace uses of llvm/Support/Host.h
The forwarding header is left in place because of its use in
`polly/lib/External/isl/interface/extract_interface.cc`, but I have
added a GCC warning about the fact it is deprecated, because it is used
in `isl` from where it is included by Polly.
2023-02-10 09:59:46 +00:00
Chuanqi Xu
65f9719913 Revert "[C++20] [Modules] [ClangScanDeps] Enable to print make-style dependency file within P1689 format (4/4)"
This reverts commit e1354763b6e641e45cc2371270883fcd26edf705.

There is a build failure in m68k-linux testing bot
(https://lab.llvm.org/buildbot/#/builders/192/builds/267), which is
weird. Revert this for now and look at the reasons.
2023-02-10 17:56:02 +08:00
Chuanqi Xu
dd380f9243 [NFC] Remove the unused parameter introduced in e135476
I introduced an unused parameter due to an oversight during rebasing.
Now the patch removes the parameter.
2023-02-10 16:03:29 +08:00
Kai Luo
2db83b1b31 [CMake] Fix -DBUILD_SHARED_LIBS=on builds after D137534 2023-02-10 04:58:20 +00:00
Chuanqi Xu
e1354763b6 [C++20] [Modules] [ClangScanDeps] Enable to print make-style dependency file within P1689 format (4/4)
Required in https://reviews.llvm.org/D137534.

The build systems needs the information to know that "header X changed,
scanning may have changed, so please rerun scanning". Although it is
possible to get the information by running clang-scan-deps for the
second time with make format, it is not user friendly clearly.

Reviewed By: jansvoboda11

Differential Revision: https://reviews.llvm.org/D139168
2023-02-10 11:23:36 +08:00
Chuanqi Xu
591fdcde92 [C++20] [Modules] [ClangScanDeps] Allow clang-scan-deps to without specified compilation database in P1689 (3/4)
In a private chat with @ben.boeckel , we get in consensus it would be
great for cmake if the invocation of clang-scan-deps can get rid of
compilation database. Due to the compilation database can't do very well
for the files which are not existed yet. @ben.boeckel may have more
context to add here.

This patch should be innocent for others usages.

Reviewed By: jansvoboda11

Differential Revision: https://reviews.llvm.org/D137534
2023-02-10 10:42:04 +08:00
Chuanqi Xu
de17c665e3 [C++20] [Modules] [ClangScanDeps] Add ClangScanDeps support for C++20 Named Modules in P1689 format (2/4)
Close https://github.com/llvm/llvm-project/issues/51792
Close https://github.com/llvm/llvm-project/issues/56770

This patch adds ClangScanDeps support for C++20 Named Modules in P1689
format. We can find the P1689 format at:
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1689r5.html.
After we land the patch, we're able to compile C++20 Named
Modules with CMake! And although P1689 is written by kitware people,
other build systems should be able to use the format to compile C++20
Named Modules too.

TODO: Support header units in P1689 Format.
TODO2: Support C++20 Modules in the full dependency format of
ClangScanDeps. We also want to support C++20 Modules and clang modules
together according to
https://discourse.llvm.org/t/how-should-we-support-dependency-scanner-for-c-20-modules/66027.
But P1689 format cares about C++20 Modules only for now. So let's focus
on C++ Modules and P1689 format. And look at the full dependency format
later.

I'll add the ReleaseNotes and Documentations after the patch get landed.

Reviewed By: jansvoboda11

Differential Revision: https://reviews.llvm.org/D137527
2023-02-10 10:26:43 +08:00
Ben Langmuir
cf73d3f07b [clang][deps] Ensure module invocation can be serialized
When reseting modular options, propagate the values from certain options
that have ImpliedBy relations instead of setting to the default. Also,
verify in clang-scan-deps that the command line produced round trips
exactly.

Ideally we would automatically derive the set of options that need this
kind of propagation, but for now there aren't very many impacted.

rdar://105148590

Differential Revision: https://reviews.llvm.org/D143446
2023-02-08 14:23:39 -08:00
Jan Svoboda
152f291460 [clang][deps] Fix race condition
D140176 introduced new `FullDeps` API that's not thread-safe, breaking the class invariant. This was causing race condition when `clang-scan-deps` was run with multiple threads.

Reviewed By: steven_wu, akyrtzi

Differential Revision: https://reviews.llvm.org/D143428
2023-02-07 09:26:56 -08:00
Jan Svoboda
ba556660fe [clang][deps] NFC: Split out the module-based API from the TU-based API
For users of the C++ API, the return type of `getFullDependencies` doesn't make sense when asking for dependencies of a module. In the returned `FullDependenciesResult` instance, only `DiscoveredModules` is useful (the graph of modular dependecies). The `FullDeps` member is trying to describe a translation unit it was never given. Its command line also refers to a file in the in-memory VFS we create in the scanner, leaking the implementation detail.

This patch splits the API and improves layering and naming of the return types.

Depends on D140175.

Reviewed By: artemcm

Differential Revision: https://reviews.llvm.org/D140176
2023-02-01 13:46:26 -08:00
Jan Svoboda
e60fcfd6e5 [clang][deps] Remove support for the deprecated driver API
This API is no longer necessary, so let's remove it to simplify the internal APIs.

Reviewed By: benlangmuir, artemcm

Differential Revision: https://reviews.llvm.org/D140175
2023-02-01 13:46:26 -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
Alex Brachet
521554a7ee Revert "[clang-scan-deps] Migrate to OptTable"
This reverts commit 0c60ec699fc1ccca2e444ceb041cad9b1dca3a64.
2023-01-13 18:17:46 +00:00
Alex Brachet
0c60ec699f [clang-scan-deps] Migrate to OptTable
Differential Revision: https://reviews.llvm.org/D139949
2023-01-13 18:11:29 +00:00
Matt Arsenault
e748db0f7f Support: Convert Program APIs to std::optional 2022-12-01 17:00:44 -05:00