6818 Commits

Author SHA1 Message Date
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
Chuanqi Xu
7201cae106
[C++20] [Modules] Support module level lookup (#122887)
Close https://github.com/llvm/llvm-project/issues/90154

This patch is also an optimization to the lookup process to utilize the
information provided by `export` keyword.

Previously, in the lookup process, the `export` keyword only takes part
in the check part, it doesn't get involved in the lookup process. That
said, previously, in a name lookup for 'name', we would load all of
declarations with the name 'name' and check if these declarations are
valid or not. It works well. But it is inefficient since it may load
declarations that may not be wanted.

Note that this patch actually did a trick in the lookup process instead
of bring module information to DeclarationName or considering module
information when deciding if two declarations are the same. So it may
not be a surprise to me if there are missing cases. But it is not a
regression. It should be already the case. Issue reports are welcomed.

In this patch, I tried to split the big lookup table into a lookup table
as before and a module local lookup table, which takes a combination of
the ID of the DeclContext and hash value of the primary module name as
the key. And refactored `DeclContext::lookup()` method to take the
module information. So that a lookup in a DeclContext won't load
declarations that are local to **other** modules.

And also I think it is already beneficial to split the big lookup table
since it may reduce the conflicts during lookups in the hash table.

BTW, this patch introduced a **regression** for a reachability rule in
C++20 but it was false-negative. See
'clang/test/CXX/module/module.interface/p7.cpp' for details.

This patch is not expected to introduce any other
regressions for non-c++20-modules users since the module local lookup
table should be empty for them.

---

On the API side, this patch unfortunately add a maybe-confusing argument
`Module *NamedModule` to
`ExternalASTSource::FindExternalVisibleDeclsByName()`. People may think
we can get the information from the first argument `const DeclContext
*DC`. But sadly there are declarations (e.g., namespace) can appear in
multiple different modules as a single declaration. So we have to add
additional information to indicate this.
2025-01-15 15:15:35 +08:00
sstwcw
fbef1f835f
[clang-format][NFC] Make formatting Verilog faster (#121139)
A regular expression was used in the lexing process. It made the program
take more than linear time with regards to the length of the input. It
looked like the entire buffer could be scanned for every token lexed.
Now the regular expression is replaced with code. Previously it took 20
minutes for the program to format 125 000 lines of code on my computer.
Now it takes 315 milliseconds.
2025-01-14 15:37:06 +00:00
Balázs Kéri
7e01a322f8
[clang][ASTImporter] Fix unused variable warning (NFC) (#122686) 2025-01-13 11:35:38 +01:00
Balázs Kéri
b270525f73
[clang][ASTImporter] Not using primary context in lookup table (#118466)
`ASTImporterLookupTable` did use the `getPrimaryContext` function to get
the declaration context of the inserted items. This is problematic
because the primary context can change during import of AST items, most
likely if a definition of a previously not defined class is imported.
(For any record the primary context is the definition if there is one.)
The use of primary context is really not important, only for namespaces
because these can be re-opened and lookup in one namespace block is not
enough. This special search is now moved into ASTImporter instead of
relying on the lookup table.
2025-01-13 09:46:45 +01:00
Kazu Hirata
a3e62d849f [AST] Fix a warning
This patch fixes:

  clang/unittests/AST/ASTImporterTest.cpp:3397:9: error: unused
  variable 'ToTU' [-Werror,-Wunused-variable]
2025-01-11 00:11:42 -08:00
Timm Baeder
cfe26358e3
Reapply "[clang] Avoid re-evaluating field bitwidth" (#122289) 2025-01-11 07:12:37 +01:00
Owen Pan
2ea34cdf2b
[clang-format] Stop fixing indentation on namespace closing brace (#122234)
Fixes #119790.
2025-01-09 19:10:15 -08:00
Amr Hesham
9ba6e8dc91
[Clang][ASTMatcher] Extend hasDependentName to match DependentNameType name (#121975)
Extend `hasDependentName` to be a polymorphic matcher that
matches the name of either `DependentNameType` or
`DependentScopeDeclRefExpr`
2025-01-09 01:18:10 -05:00
Timm Bäder
59bdea24b0 Revert "[clang] Avoid re-evaluating field bitwidth (#117732)"
This reverts commit 81fc3add1e627c23b7270fe2739cdacc09063e54.

This breaks some LLDB tests, e.g.
SymbolFile/DWARF/x86/no_unique_address-with-bitfields.cpp:

lldb: ../llvm-project/clang/lib/AST/Decl.cpp:4604: unsigned int clang::FieldDecl::getBitWidthValue() const: Assertion `isa<ConstantExpr>(getBitWidth())' failed.
2025-01-08 15:09:52 +01:00
Timm Baeder
81fc3add1e
[clang] Avoid re-evaluating field bitwidth (#117732)
Save the bitwidth value as a `ConstantExpr` with the value set. Remove
the `ASTContext` parameter from `getBitWidthValue()`, so the latter
simply returns the value from the `ConstantExpr` instead of
constant-evaluating the bitwidth expression every time it is called.
2025-01-08 14:45:19 +01:00
Jan Voung
72a28a3bf0
[clang][dataflow] Use smart pointer caching in unchecked optional accessor (#120249)
Part 2 (and final part) following
https://github.com/llvm/llvm-project/pull/120102
Allows users to do things like:

```
if (o->x.has_value()) {
  ((*o).x).value();
}
```
where the `->` and `*` are operator overload calls.

A user could instead extract the nested optional into a local variable
once instead of doing two accessor calls back to back, but currently
they are unsure why the code is flagged.
2025-01-08 08:27:16 -05:00
Balazs Benics
5f6b714507
[analyzer][NFC] Simplify PositiveAnalyzerOption handling (#121910)
This simplifies #120239
Addresses my comment at:
https://github.com/llvm/llvm-project/pull/120239#issuecomment-2574600543

CPP-5920
2025-01-07 15:19:16 +01:00
Balazs Benics
55391f85ac
[analyzer] Retry UNDEF Z3 queries 2 times by default (#120239)
If we have a refutation Z3 query timed out (UNDEF), allow a couple of
retries to improve stability of the query. By default allow 2 retries,
which will give us in maximum of 3 solve attempts per query.

Retries should help mitigating flaky Z3 queries.
See the details in the following RFC:

https://discourse.llvm.org/t/analyzer-rfc-retry-z3-crosscheck-queries-on-timeout/83711

Note that with each attempt, we spend more time per query.
Currently, we have a 15 seconds timeout per query - which are also in
effect for the retry attempts.

---

Why should this help?
In short, retrying queries should bring stability because if a query
runs long
it's more likely that it did so due to some runtime anomaly than it's on
the edge of succeeding. This is because most queries run quick, and the
queries that run long, usually run long by a fair amount.
Consequently, retries should improve the stability of the outcome of the
Z3 query.

In general, the retries shouldn't increase the overall analysis time
because it's really rare we hit the 0.1% of the cases when we would do
retries. But keep in mind that the retry attempts can add up if many
retries are allowed, or the individual query timeout is large.

CPP-5920
2025-01-06 18:08:12 +01:00
Owen Pan
9e4774b934
[clang-format] Add LT_RequiresExpression and LT_SimpleRequirement (#121681)
The new line types help to annotate */&/&& in simple requirements as
binary operators.

Fixes #121675.
2025-01-06 01:46:17 -08:00
Amr Hesham
f3590c16da
[Clang][ASTMatcher] Add a matcher for the name of a DependentScopeDeclRefExpr (#121656)
Add the `hasDependentName` matcher to match the name of
`DependentScopeDeclRefExpr`

Fixes https://github.com/llvm/llvm-project/issues/121610
2025-01-05 21:17:06 -05:00
Owen Pan
00934505d4
[clang-format] Add TT_CompoundRequirementLBrace for better annotation (#121539)
Also, add `ST_CompoundRequirement` to help annotating */&/&& in compound
requirement expressions as `TT_BinaryOperator`.

Fixes #121471.
2025-01-04 20:51:00 -08:00
Owen Pan
c1ea05eaf0
[clang-format] Don't break short macro call followed by l_paren (#121626)
Fixes #105658.
2025-01-04 16:24:41 -08:00
Owen Pan
44b83e81b5
[clang-format] Add TT_AfterPPDirective for better annotation (#121622)
For now, we only need to annotate the token after #error or #warning.

Fixes #117706.
2025-01-04 16:22:54 -08:00
Owen Pan
66f16e682f
[clang-format][NFC] Add missing config tests for List of Strings (#121451)
Also, simplify the existing test for NamespaceMacros. Like the options
tested by the added tests, it's also a list of arbitrary strings and
initialized to an empty list. (The other existing tests for list of
strings either are initialized to a list of one or more strings or
require specific strings.)
2025-01-04 16:14:25 -08:00
kefan cao
d85b22ed5d
[Clang][ASTMatcher] Add dependentTemplateSpecializationType matcher (#121435)
Fixes https://github.com/llvm/llvm-project/issues/121307
2025-01-03 16:32:02 -05:00
dmasloff
1c997feff1
[clang-format] Add option WrapNamespaceBodyWithNewlines (#106145)
It wraps the body of namespace with additional newlines, turning this code:
```
namespace N {
int function();
}
```
into the following:
```
namespace N {

int function();

}
```

---------

Co-authored-by: Owen Pan <owenpiano@gmail.com>
2025-01-02 21:52:01 -08:00
Owen Pan
1a0d0ae234
[clang-format] Add VariableTemplates option (#121318)
Closes #120148.
2025-01-01 18:24:56 -08:00
Owen Pan
cd239493c1
[clang-format] Support globstar in .clang-format-ignore (#121404)
Closes #110160.
Closes #114969.
2025-01-01 15:37:59 -08:00
Galen Elias
486ec4bd74
[clang-format] Add AllowShortNamespacesOnASingleLine option (#105597)
This fixes #101363 which is a resurrection of a previously opened but
never completed review: https://reviews.llvm.org/D11851

The feature is to allow code like the following not to be broken across
multiple lines:

```
namespace foo { class bar; }
namespace foo { namespace bar { class baz; } }
```

Code like this is commonly used for forward declarations, which are
ideally kept compact. This is also apparently the format that
include-what-you-use will insert for forward declarations.

Also, fix an off-by-one error in `CompactNamespaces` code. For nested
namespaces with 3 or more namespaces, it was incorrectly compacting
lines which were 1 or two spaces over the `ColumnLimit`, leading to
incorrect formatting results.
2024-12-30 01:28:03 -08:00
Amr Hesham
6230f1ba94
[Clang][ASTMatcher] Add dependentNameType AST matcher (#121263)
Fixes: https://github.com/llvm/llvm-project/issues/121240
2024-12-29 19:27:10 -05:00
Amr Hesham
48bf0a9457
[Clang][ASTMatcher] Add dependentScopeDeclRefExpr matcher (#120996)
Fixes https://github.com/llvm/llvm-project/issues/120937
2024-12-27 19:10:24 -05:00
Owen Pan
141c544c03
[clang-format] Skip line splices when sorting C++ includes (#120680)
Fixes #109864.
2024-12-24 21:47:53 -08:00
天音あめ
cac6777ca1
[clang-format] Fix crashes when the macro expansion is empty (#119428)
An empty expansion should be valid, like `echo 'A()' | clang-format
--style='{Macros: [A(x)=x]}'`.

Fixes #119258.
2024-12-21 22:21:27 -08:00
Owen Pan
40acaa394f
[clang-format] Fix a crash in QualifierAlignment: Right (#120821)
Fixes #120793.
2024-12-21 17:16:29 -08:00
Jan Voung
54309b1c2f
[clang][dataflow] Add matchers for smart pointer accessors to be cached (#120102)
This is part 1 of caching for smart pointer accessors, building on top
of the CachedConstAccessorsLattice, which caches "normal" accessors.

Smart pointer accessors are a bit different in that they may:
- have aliases to access the same underlying data (but potentially
  returning slightly different types like `&` vs `*`). Within a
  "checked" sequence users may mix uses of the different aliases and the
  check should apply to any of the spellings.
- may have non-const overloads in addition to the const version, where
  the non-const doesn't actually modify the container

Part 2 will follow and add transfer functions utilities. It will also
add a user UncheckedOptionalAccessModel. We'd seen false positives when
nesting StatusOr<optional<T>> and optional<StatusOr<T>>, etc. which this
can help address.
2024-12-20 09:01:13 -05:00
Owen Pan
fe2685303b [clang-format] Fix a crash caused by commit f03bf8c45f43 2024-12-18 19:41:50 -08:00
Owen Pan
b1b60d4c1d
[clang-format] Fix a bug in annotating arrows after init braces (#119958)
Fixes #59066.
2024-12-18 12:44:53 -08:00
Owen Pan
f03bf8c45f
[clang-format] Don't change breaking before CtorInitializerColon (#119522)
Don't change breaking before CtorInitializerColon with `ColumnLimit: 0`.

Fixes #119519.
2024-12-18 12:42:26 -08:00
Gedare Bloom
57c161a647
[clang-format] Detect nesting in template strings (#119989)
The helper to check if a token is in a template string scans too far
backward. It should stop if a different scope is found.

Fixes #107571
2024-12-17 08:05:01 -08:00
Owen Pan
8c681a929b [clang-format][NFC] Add a TypeScript test case
See #108530.
2024-12-14 19:42:43 -08:00
Kazu Hirata
c35108e244
[AST] Migrate away from PointerUnion::get (NFC) (#119949)
Note that PointerUnion::get has been soft deprecated in
PointerUnion.h:

  // FIXME: Replace the uses of is(), get() and dyn_cast() with
  //        isa<T>, cast<T> and the llvm::dyn_cast<T>
2024-12-14 09:17:56 -08:00
Kristóf Umann
ea8e328ae2
[analyzer][Z3] Restore the original timeout of 15s (#118291)
Discussion here:

https://discourse.llvm.org/t/analyzer-rfc-taming-z3-query-times/79520/15?u=szelethus

The original patch, #97298 introduced new timeouts backed by thorough
testing and measurements to keep the running time of Z3 within
reasonable limits. The measurements also showed that only certain
reports and certain TUs were responsible for the poor performance of Z3
refutation.

Unfortunately, it seems like that on machines with different
characteristics (slower machines) the current timeouts don't just axe
0.01% of reports, but many more as well. Considering that timeouts are
inherently nondeterministic as a cutoff point, this lead reports sets
being vastly different on the same projects with the same configuration.
The discussion link shows that all configurations introduced in the
patch with their default values lead to severa nondeterminism of the
analyzer. As we, and others use the analyzer as a gating tool for PRs,
we should revert to the original defaults.

We should respect that
* There are still parts of the analyzer that are either proven or
suspected to contain nondeterministic code (like pointer sets),
* A 15s timeout is more likely to hit the same reports every time on a
wider range of machines, but is still inherently nondeterministic, but
an infinite timeout leads to the tool hanging,
* If you measure the performance of the analyzer on your machines, you
can and should achieve some speedup with little or no observable
nondeterminism.

---------

Co-authored-by: Balazs Benics <benicsbalazs@gmail.com>
2024-12-13 14:31:06 +01:00
macurtis-amd
8bf19ec444
[clang] Fix use of dangling ptr in CommandLineTest (#119798)
If 'GeneratedArgsStorage' ever grows, contained strings may get copied
and data pointers stored in 'GeneratedArgs' may become invalid, pointing
to deallocated memory.
2024-12-13 05:27:00 -06:00
Clement Courbet
4aacafd49b
[clang][ASTVisitor] Visit HoldingVar from BindingDecl. (#117858)
Tuple-like types introduce `VarDecl`s in the AST for their "holding
vars", but AST visitors do not visit those. As a result the `VarDecl`
for the holding var is orphaned when trying to retreive its parents.

Fix a `FlowSensitive` test that assumes that only a `BindingDecl` is
introduced with the given name (the matcher now can also reach the
`VarDecl` for the holding var).
2024-12-12 17:01:59 +01:00
Ilya Biryukov
7f43120152 [Serialization] Free memory in LoadSpecLazilyTest
Default Clang invocations set DisableFree = true, which causes ASAN to
complain. Override it in tests that are not supposed to leak.
2024-12-12 11:19:11 +01:00
Chuanqi Xu
20e9049509
[Serialization] Support loading template specializations lazily (#119333)
Reland https://github.com/llvm/llvm-project/pull/83237

---

(Original comments)

Currently all the specializations of a template (including
instantiation, specialization and partial specializations) will be
loaded at once if we want to instantiate another instance for the
template, or find instantiation for the template, or just want to
complete the redecl chain.

This means basically we need to load every specializations for the
template once the template declaration got loaded. This is bad since
when we load a specialization, we need to load all of its template
arguments. Then we have to deserialize a lot of unnecessary
declarations.

For example,

```
// M.cppm
export module M;
export template <class T>
class A {};

export class ShouldNotBeLoaded {};

export class Temp {
   A<ShouldNotBeLoaded> AS;
};

// use.cpp
import M;
A<int> a;
```

We have a specialization ` A<ShouldNotBeLoaded>` in `M.cppm` and we
instantiate the template `A` in `use.cpp`. Then we will deserialize
`ShouldNotBeLoaded` surprisingly when compiling `use.cpp`. And this
patch tries to avoid that.

Given that the templates are heavily used in C++, this is a pain point
for the performance.

This patch adds MultiOnDiskHashTable for specializations in the
ASTReader. Then we will only deserialize the specializations with the
same template arguments. We made that by using ODRHash for the template
arguments as the key of the hash table.

To review this patch, I think `ASTReaderDecl::AddLazySpecializations`
may be a good entry point.
2024-12-11 09:40:47 +08:00
Owen Pan
54ca1c4212
[clang-format] Fix idempotent format of hash in macro body (#118513)
Fixes #118334.
2024-12-10 16:47:21 -08:00
Gedare Bloom
46bf67d8fa
[clang-format] Reorder TokenAnnotator::canBreakBefore (#119044)
Move the checks related to breaking before right braces and right parens
earlier to avoid conflicting checks that prevent breaking based on the
left-hand token. This allows properly formatting declarations with
pointers and references at a minimum.
2024-12-09 21:05:28 -08:00
Nathan Ridge
70c1764d7a
[clang] [Sema] Preserve nested name specifier prefix in MemberPointerType (#118236)
Fixes https://github.com/llvm/llvm-project/issues/118198
Fixes https://github.com/clangd/clangd/issues/2235
2024-12-07 20:17:07 -05:00
Owen Pan
cb61a5e420
[clang-format] Fix an assertion failure in RemoveSemicolon (#117472)
Fixes #117290.
2024-12-07 16:47:35 -08:00
Haowei Wu
12bdeba76e Revert "[Serialization] Support load lazy specialization lazily"
This reverts commit b5bd19211118c6d43bc525a4e3fb65d2c750d61e.
It brokes multiple llvm bots including clang-x64-windows-msvc
2024-12-06 10:33:57 -08:00
kadir çetinkaya
d74214cc8c
[clang][NFC] Change suppression mapping interfaces to use SourceLocation (#118960)
This way we can delay getting a presumed location even further, only
performing it for diagnostics that are mapped.
2024-12-06 15:50:32 +01:00
Chuanqi Xu
b5bd192111 [Serialization] Support load lazy specialization lazily
Currently all the specializations of a template (including
instantiation, specialization and partial specializations)  will be
loaded at once if we want to instantiate another instance for the
template, or find instantiation for the template, or just want to
complete the redecl chain.

This means basically we need to load every specializations for the
template once the template declaration got loaded. This is bad since
when we load a specialization, we need to load all of its template
arguments. Then we have to deserialize a lot of unnecessary
declarations.

For example,

```
// M.cppm
export module M;
export template <class T>
class A {};

export class ShouldNotBeLoaded {};

export class Temp {
   A<ShouldNotBeLoaded> AS;
};

// use.cpp
import M;
A<int> a;
```

We should a specialization ` A<ShouldNotBeLoaded>` in `M.cppm` and we
instantiate the template `A` in `use.cpp`. Then we will deserialize
`ShouldNotBeLoaded` surprisingly when compiling `use.cpp`. And this
patch tries to avoid that.

Given that the templates are heavily used in C++, this is a pain point
for the performance.

This patch adds MultiOnDiskHashTable for specializations in the
ASTReader. Then we will only deserialize the specializations with the
same template arguments. We made that by using ODRHash for the template
arguments as the key of the hash table.

To review this patch, I think `ASTReaderDecl::AddLazySpecializations`
may be a good entry point.

The patch was reviewed in
https://github.com/llvm/llvm-project/pull/83237 but that PR is a stacked
PR. But I feel the intention of the stacked PRs get lost during the
review process. So I feel it is better to merge the commits into a
single commit instead of merging them in the PR page. It is better for
us to cherry-pick and revert.
2024-12-06 10:52:35 +08:00
Samira Bazuzi
f7e8be7c66
Skip escaped newlines before checking for whitespace in Lexer::getRawToken. (#117548)
The Lexer used in getRawToken is not told to keep whitespace, so when it
skips over escaped newlines, it also ignores whitespace, regardless of
getRawToken's IgnoreWhiteSpace parameter. 

Instead of letting this case fall through to lexing, check
for whitespace after skipping over any escaped newlines.
2024-12-05 09:37:46 -05:00