1317 Commits

Author SHA1 Message Date
Kazu Hirata
c2d6c7cea7
[clang] Use llvm::append_range (NFC) (#136448) 2025-04-19 12:21:14 -07:00
Kazu Hirata
0ed1c9862d
[clang] llvm::append_range (NFC) (#136440) 2025-04-19 10:37:25 -07:00
Qiongsi Wu
9ef91677a4
[clang][Dependency Scanning] Adding an API to Diagnose Invalid Negative Stat Cache Entries (#135703)
We have had numerous situations where the negatively stat cached paths
are invalidated during the build, and such invalidations lead to build
errors.

This PR adds an API to diagnose such cases.
`DependencyScanningFilesystemSharedCache::diagnoseNegativeStatCachedPaths`
allows users of the cache to ask the cache to examine all negatively
stat cached paths, and re-stat the paths using the passed-in file
system. If any re-stat succeeds, the API emits diagnostics.

rdar://149147920
2025-04-18 10:52:39 -07:00
yronglin
d83b639b4c
Reland [clang] Unify SourceLocation and IdentifierInfo* pair-like data 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-17 22:40:47 +08:00
Michael Buch
99c08ff1cb
Revert "[clang] Unify SourceLocation and IdentifierInfo* pair-like data 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 17:05:53 +02:00
yronglin
d3153ad66c
[clang] Unify SourceLocation and IdentifierInfo* pair-like data 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-16 20:53:25 +08:00
Cyndy Ishida
40050888a1
[clang][depscan] Centralize logic for populating StableDirs, NFC (#135704)
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-15 09:59:23 -07:00
Cyndy Ishida
1365b5b1ad
[clang][DependencyScanning] Track dependencies from prebuilt modules to 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-04-08 15:48:25 -07:00
Matheus Izvekov
dc17429ae6
[clang] improved preservation of template keyword (#133610) 2025-04-01 17:15:18 -03:00
Kazu Hirata
7cc17fb085
[ADT] Remove old range constructors of SmallSet and StringSet (#133205)
This patch removes the old range constructors of SmallSet and
StringSet that do not take the llvm::from_range tag.  Since there are
so few uses, this patch directly removes them without going through
the deprecation process.
2025-03-27 07:52:13 -07:00
Cyndy Ishida
9aecbdf8ed
[clang][DepScan] Allow ModuleDep to be const (#132968)
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-25 13:13:58 -07:00
Jan Svoboda
15ad84fffd [clang][deps] Explicitly link pthreads
This is a speculative fix for link failure found by the RHEL 8 bot: https://lab.llvm.org/buildbot/#/builders/204/builds/3862.

FAILED: lib/libclangDependencyScanning.so.21.0git
/InProcessModuleCache.cpp.o: In function `(anonymous namespace)::ReaderWriterLock::tryLock()':
InProcessModuleCache.cpp:(.text._ZN12_GLOBAL__N_116ReaderWriterLock7tryLockEv+0x1d): undefined reference to `pthread_rwlock_trywrlock'
2025-03-20 09:37:10 -07:00
Kazu Hirata
934ef30522 [clang] Fix warnings
This patch fixes:

  clang/lib/Tooling/DependencyScanning/InProcessModuleCache.cpp:38:5:
  error: 'shared_lock' may not intend to support class template
  argument deduction [-Werror,-Wctad-maybe-unsupported]

  clang/lib/Tooling/DependencyScanning/InProcessModuleCache.cpp:68:7:
  error: 'lock_guard' may not intend to support class template
  argument deduction [-Werror,-Wctad-maybe-unsupported]
2025-03-18 14:33:03 -07:00
Jan Svoboda
056264b838
[clang][deps] Implement efficient in-process ModuleCache (#129751)
The dependency scanner uses implicitly-built Clang modules under the
hood. This system was originally designed to handle multiple concurrent
processes working on the same module cache, and mutual exclusion was
implemented using file locks. The scanner, however, runs within single
process, making file locks unnecessary. This patch virtualizes the
interface for module cache locking and provides an implementation based
on `std::shared_mutex`. This reduces `clang-scan-deps` runtime by ~17%
on my benchmark.

Note that even when multiple processes run a scan on the same module
cache (and therefore don't coordinate efficiently), this should still be
correct due to the strict context hash, the write-through
`InMemoryModuleCache` and the logic for rebuilding out-of-date or
incompatible modules.
2025-03-18 14:01:04 -07:00
Cyndy Ishida
ad8f0e2760
[clang][DepScan] Pass references to ModuleDeps instead of ModuleID in lookupModuleOutput callbacks, NFCI (#131688)
This allows clients to reference more read-only attributes, like IsInStableDirectories.
2025-03-17 16:30:09 -07:00
Cyndy Ishida
584f8cc305
[clang][DependencyScanning] Track modules that resolve from "stable" 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-03-17 15:33:23 -07:00
Kazu Hirata
4acfeafd8f
[Tooling] Avoid repeated hash lookups (NFC) (#131264) 2025-03-14 07:20:54 -07:00
Ben Langmuir
f8ba0df878
[clang][deps] Remove dependency on llvm targets from lib DependencyScanning (#129774)
In d64eccf we split the object file reader from the writer and removed
the dependency on clangCodeGen from the dependency scanner. However, we
were still initializing all the llvm targets, which was needed for
writing object file pcms but not reading them. This caused us to link
more llvm target code than necessary. Shrinks clangd by nearly 50% for
me.

rdar://144790713
2025-03-05 08:25:39 -08:00
Jan Svoboda
d2e66625bc
[clang][deps] Propagate the entire service (#128959)
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-27 10:06:26 -08:00
Qiongsi Wu
7f482aa848
[clang modules] Setting DebugCompilationDir when it is safe to ignore 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-26 13:21:14 -08:00
Ben Langmuir
e3cab30ab9
[clang][deps] Ensure DiagnosticConsumer::finish is always called (#127110)
When using the clang dependency scanner with an arbitrary
DiagnosticConsumer, it is important that we always call finish().
Previously, if there was an error preventing us from reaching the
scanning action, or if the command line contained no scannable actions
we would fail to finish(), which would break some consumers (e.g.
serialized diag consumer).
2025-02-13 14:06:17 -08:00
Qiongsi Wu
54acda2e0e
[clang module] Current Working Directory Pruning (#124786)
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-02-04 20:04:39 -08:00
Steven Wu
7a52b93837
[DependencyScanning] Add ability to scan TU with a buffer input (#125111)
Update Dependency scanner so it can scan the dependency of a TU with
a provided buffer rather than relying on the on disk file system to
provide the input file.
2025-02-04 16:37:29 -08:00
Sirraide
c4a019747c
[Clang] Remove ARCMigrate (#119269)
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.
2025-01-30 05:32:25 +01:00
Kadir Cetinkaya
4b0df28a68
[clang][Tooling] Prefer <atomic> for atomic_* family in C++ 2025-01-23 10:06:33 +01:00
Boaz Brickner
6ca560a909
[clang] Add support for passing FileSystem to buildASTFromCodeWithArgs() (#123042)
This would allow tools that don't use the real file system to use this
function.
2025-01-15 14:56:07 +01:00
Vadim D.
92a4f4dda5
[Tooling/Inclusion] Update std symbol mapping (#118174)
This adds new symbols to the generated mapping and removes special
mappings for missing symbols introduced in #113612, as these symbols are
now included in the generated mapping.
2024-12-16 10:11:22 +01:00
Kadir Cetinkaya
df9a14d7bb
Reapply "[NFC] Explicitly pass a VFS when creating DiagnosticsEngine (#115852)"
This reverts commit a1153cd6fedd4c906a9840987934ca4712e34cb2 with fixes
to lldb breakages.

Fixes https://github.com/llvm/llvm-project/issues/117145.
2024-11-21 14:55:30 +01:00
Sylvestre Ledru
a1153cd6fe Revert "[NFC] Explicitly pass a VFS when creating DiagnosticsEngine (#115852)"
Reverted for causing:
https://github.com/llvm/llvm-project/issues/117145

This reverts commit bdd10d9d249bd1c2a45e3de56a5accd97e953458.
2024-11-21 13:04:30 +01:00
kadir çetinkaya
bdd10d9d24
[NFC] Explicitly pass a VFS when creating DiagnosticsEngine (#115852)
Starting with 41e3919ded78d8870f7c95e9181c7f7e29aa3cc4 DiagnosticsEngine
creation might perform IO. It was implicitly defaulting to
getRealFileSystem. This patch makes it explicit by pushing the decision
making to callers.

It uses ambient VFS if one is available, and keeps using
`getRealFileSystem` if there aren't any VFS.
2024-11-21 12:11:41 +01:00
Jan Svoboda
25d1ac11d5
[clang][deps] Only write preprocessor info into PCMs (#115239)
This patch builds on top of
https://github.com/llvm/llvm-project/pull/115237 and
https://github.com/llvm/llvm-project/pull/115235, only passing the
`Preprocessor` object to `ASTWriter`. This reduces the size of scanning
PCM files by 1/3 and speeds up scans by 16%.
2024-11-11 13:07:08 -08:00
Jan Svoboda
9d4837f47c
[clang][deps][modules] Allocate input file paths lazily (#114457)
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-11-11 09:46:50 -08:00
Jan Svoboda
a6637ae2cc
[clang][deps] Share FileManager between modules (#115065)
The `FileManager` sharing between module-building `CompilerInstance`s
was disabled a while ago due to `FileEntry::getName()` being unreliable.
Now that we use `FileEntryRef::getNameAsRequested()` in places where it
matters, re-enabling `FileManager` is sound and improves performance of
`clang-scan-deps` by ~6.2%.
2024-11-06 14:21:01 -08:00
Vadim D.
c0ce44e8fc
[Tooling/Inclusion] Update std symbols mapping (#113612)
Fixes #113494
2024-11-04 14:55:38 +01:00
c8ef
68daf7d27e
[Tooling/Inclusion] Add binary search related std::ranges symbols to the mapping. (#113796)
Fixes #94459.

This patch adds binary search related `std::ranges` symbols to the mapping.
2024-10-30 22:04:14 +08:00
Jan Svoboda
da1a16ae10
[clang][modules] Preserve the module map that allowed inferring (#113389)
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-28 11:24:27 -07:00
Kazu Hirata
f13d3f7211
[Tooling] Simplify code with StringMap::operator[] (NFC) (#113071) 2024-10-20 09:05:55 -07:00
Kazu Hirata
a8289a35d0
[Tooling] Avoid repeated hash lookups (NFC) (#111469) 2024-10-08 07:28:53 -07:00
Kazu Hirata
c7895f0d72
[DependencyScanning] Avoid repeated hash lookups (NFC) (#111088) 2024-10-04 07:37:54 -07:00
Jan Svoboda
b1aea98cfa
[clang] Make deprecations of some FileManager APIs formal (#110014)
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-25 10:36:44 -07:00
Abhina Sreeskantharajan
efdb3ae232 Revert "[SystemZ][z/OS] Propagate IsText parameter to open text files as text (#107906)"
This reverts commit edf3b277a5f2ebe144827ed47463c22743cac5f9.
2024-09-20 08:18:16 -04:00
Youngsuk Kim
7db641af13 [clang] Don't call raw_string_ostream::flush() (NFC)
Don't call raw_string_ostream::flush(), which is essentially a no-op.
As specified in the docs, raw_string_ostream is always unbuffered
2024-09-19 17:18:10 -05:00
Abhina Sree
edf3b277a5
[SystemZ][z/OS] Propagate IsText parameter to open text files as text (#107906)
This patch adds an IsText parameter to the following functions
openFileForRead, getBufferForFile, getBufferForFileImpl and determines
whether a file is text by querying the file tag on z/OS. The default is
set to OF_Text instead of OF_None, this change in value does not affect
any other platforms other than z/OS.
2024-09-19 14:30:10 -04:00
Martin Storsjö
a26ec54237
[clang-scan-deps] Infer the tool locations from PATH (#108539)
This allows the clang driver to know which tool is meant to be executed,
which allows the clang driver to load the right clang config files, and
allows clang to find colocated sysroots.

This makes sure that doing `clang-scan-deps -- <tool> ...` looks up
things in the same way as if one just would execute `<tool> ...`, when
`<tool>` isn't an absolute or relative path.
2024-09-13 23:18:10 +03:00
Jan Svoboda
6e4dcbb21d
[clang][deps] Print tracing VFS data (#108056)
Clang's `-cc1 -print-stats` shows lots of useful internal data including
basic `FileManager` stats. Since this layer caches some results, it is
unclear how that information translates to actual filesystem accesses.
This PR uses `llvm::vfs::TracingFileSystem` to provide that missing
information.

Similar mechanism is implemented for `clang-scan-deps`'s verbose mode
(`-v`). IO contention proved to be a real bottleneck a couple of times
already and this new feature should make those easier to detect in the
future. The tracing VFS is inserted below the caching FS and above the
real FS.
2024-09-11 16:04:56 -07:00
Clement Courbet
512cecad4c
[clang][transformer] Make describe() terser for NamedDecls. (#108215)
Right now `describe()`ing a `FunctionDecl` dups the whole code of the
function. Dump only its name.
2024-09-11 17:02:47 +02:00
Chuanqi Xu
62fec3d23d [NFCI] [ClangScanDeps] [P1689] Use PreprocessorOnly Action for P1689
It is fine enough to use PreprocessorOnly action for P1689 format. We
don't need to read any PCH or module files.
2024-09-06 15:20:59 +08:00
Jan Svoboda
5acd9d1137
[clang][scan] Report module dependencies in topological order (#107474) 2024-09-05 19:13:08 -07:00
c8ef
4f4bd41f70
[NFC] Fix typos (#106817)
Fixes #106761.
2024-08-31 19:12:57 -07:00
Jacques Pienaar
0d150db214
[llvm][clang] Move RewriterBuffer to ADT. (#99770)
These classes are not specific to clang and useful for other rewriter
tools (flagged in previous review).
2024-08-18 09:46:51 -07:00