5956 Commits

Author SHA1 Message Date
Donát Nagy
bb27d5e5c6
[analyzer] Don't assume third iteration in loops (#119388)
This commit ensures that if the loop condition is opaque (the analyzer
cannot determine whether it's true or false) and there were at least two
iterations, then the analyzer doesn't make the unjustified assumption
that it can enter yet another iteration.

Note that the presence of a loop suggests that the developer thought
that two iterations can happen (otherwise an `if` would've been
sufficient), but it does not imply that the developer expected three or
four iterations -- and in fact there are many false positives where a
loop iterates over a two-element (or three-element) data structure, but
the analyzer cannot understand the loop condition and blindly assumes
that there may be three or more iterations. (In particular, analyzing
the FFMPEG project produces 100+ such false positives.)

Moreover, this provides some performance improvements in the sense that
the analyzer won't waste time on traversing the execution paths with 3
or 4 iterations in a loop (which are very similar to the paths with 2
iterations) and therefore will be able to traverse more branches
elsewhere on the `ExplodedGraph`.

This logic is disabled if the user enables the widen-loops analyzer
option (which is disabled by default), because the "simulate one final
iteration after the invalidation" execution path would be suppressed by
the "exit the loop if the loop condition is opaque and there were at
least two iterations" logic. If we want to support loop widening, we
would need to create a follow-up commit which ensures that it "plays
nicely" with this logic.
2025-01-02 15:51:03 +01: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
TilakChad
1623c43594
[Clang] Resolved type of expression indexing into pack of values of a non-dependent type (#121405) 2025-01-01 15:50:05 +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
TilakChad
70965ef259
[Clang] Prevent assignment to captured structured bindings inside immutable lambda (#120849)
For structured bindings, a call to getCapturedDeclRefType(...) was
missing. This PR fixes that behavior and adds the related diagnostics
too.

This fixes https://github.com/llvm/llvm-project/issues/95081.
2024-12-25 18:59:33 +01:00
Kinoshita Kotaro
88d04be815
[AArch64][docs] Add release notes for FUJITSU-MONAKA support (#120684)
Adds release notes for the FUJITSU-MONAKA support introduced in PR #118432.
These notes were missing from the original PR.
2024-12-25 10:59:59 +09:00
Joseph Huber
34f8573a51
[OpenMP] Use generic IR for the OpenMP DeviceRTL (#119091)
Summary:
We previously built this for every single architecture to deal with
incompatibility. This patch updates it to use the 'generic' IR that
`libc` and other projects use. Who knows if this will have any
side-effects, probably worth testing more but it passes the tests I
expect to pass on my side.
2024-12-24 18:05:28 -06:00
c8ef
dd577c05ad
[clang] constexpr built-in reduce min/max function. (#120866)
Part of #51787.

This patch adds constexpr support for the built-in reduce min/max
function.
2024-12-23 09:06:38 +08:00
Malek Ben Slimane
c1e7e4500c Thread Safety Analysis: Support passing scoped locks between functions with appropriate annotations (#110523)
This is helpful when multiple functions operate on the same
capabilities, but we still want to use scoped lockable types for
readability and exception safety.
- Introduce support for thread safety annotations on function parameters
  marked with the 'scoped_lockable' attribute.
- Add semantic checks for annotated function parameters, ensuring
  correct usage.
- Enhance the analysis to recognize and handle parameters annotated for
  thread safety, extending the scope of analysis to track these across
  function boundries.
- Verify that the underlying mutexes of function arguments match the
  expectations set by the annotations.

Limitation: This does not work when the attribute arguments are class
members, because attributes on function parameters are parsed
differently from attributes on functions.
2024-12-20 23:49:03 +01:00
Chris White
994457f81f
[Clang-CL][DXC] Expose -fdiagnostic-color= to clang-cl and clang-dxc (#120644)
Exposing `-fdiagnostic-color=` to clang-cl and clang-dxc.
`-fcolor-diagnostics` and `-fno-color-diagnostics` are already allowed
in both of these and `-fdiagnostics-color=` allows one additional value,
`auto`.

I've added the tests for clang-cl to `cl-options.c` as per the comments
in the issue linked below. I couldn't finding a suitable existing file
to add the clang-dxc tests to so I've created a new one.

Resolves #119184
2024-12-20 23:12:25 +01:00
Thurston Dang
5bb650345d
Remove -bounds-checking-unique-traps (replace with -fno-sanitize-merge=local-bounds) (#120682)
#120613 removed -ubsan-unique-traps and replaced it with
-fno-sanitize-merge (introduced in #120511), which allows fine-grained
control of which UBSan checks to prevent merging. This analogous patch
removes -bound-checking-unique-traps, and allows it to be controlled via
-fno-sanitize-merge=local-bounds.

Most of this patch is simply plumbing through the compiler flags into
the bounds checking pass.

Note: this patch subtly changes -fsanitize-merge (the default) to also
include -fsanitize-merge=local-bounds. This is different from the
previous behavior, where -fsanitize-merge (or the old
-ubsan-unique-traps) did not affect local-bounds (requiring the separate
-bounds-checking-unique-traps). However, we argue that the new behavior
is more intuitive.

Removing -bounds-checking-unique-traps and merging its functionality
into -fsanitize-merge breaks backwards compatibility; we hope that this
is acceptable since '-mllvm -bounds-checking-unique-traps' was an
experimental flag.
2024-12-20 10:07:44 -08:00
Qiongsi Wu
1418018502
[clang][ObjectiveC] Fix Parsing the :: Optional Scope Specifier (#119908)
The parser hangs when processing types/variables prefixed by `::` as an
optional scope specifier. For example,
```
- (instancetype)init:(::A *) foo;
```

The parser should not hang, and it should emit an error. This PR
implements the error check.

rdar://140885078
2024-12-20 09:47:26 -08:00
Haojian Wu
a6d26c56ff
[clang] Fix dangling false positives for conditional operators. (#120233)
When analyzing a dangling gsl pointer, we currently filter out all field
access `MemberExpr` to avoid common false positives (`string_view sv =
Temp().sv`), However, this filter only applies to direct MemberExpr
instances, leaving the conditional operator as an escaping example
(`GSLPointer pointer(Cond ? Owner().ptr : GSLPointer());`).

This patch extends the MemberExpr logic to handle the conditional
operator. The heuristic is intentionally simple, which may result in
some false negatives. However, it effectively covers common cases like
`std::string_view sv = cond ? "123" : std::string();`, which is a
reasonable trade-off.

Fixes https://github.com/llvm/llvm-project/issues/120206
2024-12-20 09:26:38 +01:00
Vitaly Buka
c2aee50620
[ubsan] Runtime and driver support for local-bounds (#120515)
Implements ``-f[no-]sanitize-trap=local-bounds``,
and ``-f[no-]sanitize-recover=local-bounds``.

LLVM part is here #120513.
2024-12-19 16:38:07 -08:00
Thurston Dang
cb8a90b7d1
[ubsan] Remove -ubsan-unique-traps (replace with -fno-sanitize-merge) (#120613)
-fno-sanitize-merge (introduced in
https://github.com/llvm/llvm-project/pull/120511) duplicates the
functionality of -ubsan-unique-traps but also allows individual checks
to be specified e.g.,
* "-fno-sanitize-merge" without arguments is equivalent to
-ubsan-unique-traps
* "-fno-sanitize-merge=bool,enum" will apply it only to those two checks

Additionally, the naming is more consistent with the rest of the
-fsanitize- family.

This patch therefore removes -ubsan-unique-traps. This breaks backwards
compatibility; we hope that this is acceptable since '-mllvm
-ubsan-unique-traps' was an experimental flag.

This patch also adds negative test examples to bounds-checking.c, and
strengthens the NOOPTARRAY assertion to prevent spurious matches.

"-bounds-checking-unique-traps" is unaffected by this patch.
2024-12-19 12:53:48 -08:00
Younan Zhang
5c55f9664f
[Clang] Don't assume unexpanded PackExpansions' size when expanding packs (#120380)
CheckParameterPacksForExpansion() previously assumed that template
arguments don't include PackExpansion types when attempting another pack
expansion (i.e. when NumExpansions is present). However, this assumption
doesn't hold for type aliases, whose substitution might involve
unexpanded packs. This can lead to incorrect diagnostics during
substitution because the pack size is not yet determined.

To address this, this patch calculates the minimum pack size (ignoring
unexpanded PackExpansionTypes) and compares it to the previously
expanded size. If the minimum pack size is smaller, then there's still a
chance for future substitution to expand it to a correct size, so we
don't diagnose it too eagerly.

Fixes #61415
Fixes #32252
Fixes #17042
2024-12-19 13:12:01 +08:00
cor3ntin
72e58e00c2
[Clang] Set __cpp_explicit_this_parameter (#107451)
There are not a lot of outstanding known issues
with deducing this (besides #95112), so it
seems reasonable to claim full support.

Fixes #82780
2024-12-18 17:36:47 +01:00
Oleksandr T.
9daf10ff8f
Reland [Clang] skip default argument instantiation for non-defining friend declarations to meet [dcl.fct.default] p4 (#115487)
This fixes a crash when instantiating default arguments for templated
friend function declarations which lack a definition.
There are implementation limits which prevents us from finding the
pattern for such functions, and this causes difficulties
setting up the instantiation scope for the function parameters.

This patch skips instantiating the default argument in these cases,
which causes a minor regression in error recovery, but otherwise avoids
the crash.

The previous attempt #113777 accidentally skipped all default argument
constructions, causing some regressions. This patch resolves that by
moving the guard to InstantiateDefaultArgument() where the handling of
templates takes place.

Fixes https://github.com/llvm/llvm-project/issues/113324
2024-12-18 12:36:23 +02:00
cor3ntin
db93ef14ae
[Clang] Implement CWG2813: Class member access with prvalues (#120223)
This is a rebase of #95112 with my own feedback apply as @MitalAshok has
been inactive for a while.
It's fairly important this makes clang 20 as it is a blocker for #107451

--- 

[CWG2813](https://cplusplus.github.io/CWG/issues/2813.html)

prvalue.member_fn(expression-list) now will not materialize a temporary
for prvalue if member_fn is an explicit object member function, and
prvalue will bind directly to the object parameter.

The E1 in E1.static_member is now a discarded-value expression, so if E1
was a call to a [[nodiscard]] function, there will now be a warning.
This also affects C++98 with [[gnu::warn_unused_result]] functions.

This should not affect C where TemporaryMaterializationConversion is a
no-op.

Closes #100314
Fixes #100341

---------

Co-authored-by: Mital Ashok <mital@mitalashok.co.uk>
2024-12-18 10:44:42 +01:00
Nikita Popov
6d34cfac53
[Sema] Diagnose tautological bounds checks (#120222)
This diagnoses comparisons like `ptr + unsigned_index < ptr` and `ptr +
unsigned_index >= ptr`, which are always false/true because addition of
a pointer and an unsigned index cannot wrap (or the behavior is
undefined).

This warning is intended to help find broken bounds checks (which must
be implemented in terms of uintptr_t instead).

Fixes https://github.com/llvm/llvm-project/issues/120214.
2024-12-18 11:39:12 +08:00
Florian Hahn
c135f6ffe2
[TySan] Add initial Type Sanitizer support to Clang) (#76260)
This patch introduces the Clang components of type sanitizer: a
sanitizer for type-based aliasing violations.

It is based on Hal Finkel's https://reviews.llvm.org/D32198.

The Clang changes are mostly formulaic, the one specific change being
that when the TBAA sanitizer is enabled, TBAA is always generated, even
at -O0.

It goes together with the corresponding LLVM changes
(https://github.com/llvm/llvm-project/pull/76259) and compiler-rt
changes (https://github.com/llvm/llvm-project/pull/76261)

PR: https://github.com/llvm/llvm-project/pull/76260
2024-12-17 15:13:42 +00:00
Sirraide
eb5c21108f
[Clang] [Sema] Support matrix types in pseudo-destructor expressions (#117483)
We already support vector types, and since matrix element types have to
be scalar types, there should be no problem w/ just enabling this.

This now also allows matrix types to be stored in STL containers.
2024-12-17 06:49:31 +01:00
Sirraide
6e58e99a07
[Clang] [Sema] Reject non-power-of-2 _BitInt matrix element types (#117487)
Essentially, this makes this ill-formed:
```c++
using mat4 = _BitInt(12) [[clang::matrix_type(3, 3)]];
```

This matches preexisting behaviour for vector types (e.g.
`ext_vector_type`), and given that LLVM IR intrinsics for matrices also
take vector types, it seems like a sensible thing to do.

This is currently especially problematic since we sometimes lower matrix
types to LLVM array types instead, and while e.g. `[4 x i32]` and `<4 x
i32>` *probably* have the same similar memory layout (though I don’t
think it’s sound to rely on that either, see #117486), `[4 x i12]` and
`<4 x i12>` definitely don’t.
2024-12-17 02:07:29 +01:00
GeorgeKA
473e2518e8
[clang] Document the return value of __builtin_COLUMN (#118360)
PR for issue #78657

Updated clang/docs/LanguageExtensions.rst to detail the return value of
__builtin_COLUMN for this implementation.

--

Fyi, this is my first contribution, so please bear with me.

There already appears to be a unit test for __builtin_COLUMN in
clang/test/SemaCXX/source_location.cpp.
2024-12-13 10:50:12 +01:00
Owen Pan
ea44647a0b [clang-format] Write in text mode with LF in dump_format_[help|style].py 2024-12-12 23:22:26 -08:00
Mingming Liu
02bcaca599
[docs]Fix a typo around '#pragma clang section' (#119791) 2024-12-12 19:43:25 -08:00
Haojian Wu
33b910cde3
[clang] Fix the post-filtering heuristic for GSLPointer. (#114044)
The lifetime analyzer processes GSL pointers:

- when encountering a constructor for a `gsl::pointer`, the analyzer
continues traversing the constructor argument, regardless of whether the
parameter has a `lifetimebound` annotation. This aims to catch cases
where a GSL pointer is constructed from a GSL owner, either directly
(e.g., `FooPointer(FooOwner)`) or through a chain of GSL pointers (e.g.,
`FooPointer(FooPointer(FooOwner))`);
- When a temporary object is reported in the callback, the analyzer has
heuristics to exclude non-owner types, aiming to avoid false positives
(like `FooPointer(FooPointer())`).

In the problematic case (discovered in
https://github.com/llvm/llvm-project/pull/112751#issuecomment-2441055471)
of `return foo.get();`:

- When the analyzer reports the local object `foo`, the `Path` is
`[GslPointerInit, Lifetimebound]`.
- The `Path` goes through
[`pathOnlyHandlesGslPointer`](https://github.com/llvm/llvm-project/blob/main/clang/lib/Sema/CheckExprLifetime.cpp#L1136)
and isn’t filtered out by the [[heuristics]](because `foo` is an owner
type), the analyzer treats it as the `FooPointer(FooOwner())` scenario,
thus triggering a diagnostic.

Filtering out base on the object 'foo' is wrong, because the GSLPointer
is constructed from the return result of the `foo.get()`. The patch
fixes this by teaching the heuristic to use the return result (only
`const GSLOwner&` is considered) of the lifetimebound annotated
function.
2024-12-12 20:57:39 +01:00
Haojian Wu
f229ea2ffe
[clang] Enable the -Wdangling-capture diagnostic by default. (#119685)
We have tested this diagnostics internally, and we don't find see any
issues.
2024-12-12 16:38:17 +01:00
Mariya Podchishchaeva
1d65c35ce1
[clang] Reject _Complex _BitInt (#119402)
The C standard doesn't require support for these types and Codegen for
these types is incorrect ATM.
See https://github.com/llvm/llvm-project/issues/119352
2024-12-12 12:19:48 +01:00
jijjijj
9f1e9f682d
[C++20][modules] Fix std::initializer_list recognition if it's exported out of a module (#118537)
If the std::initializer_list is exported out of module, its
`DeclContext` is not a namespace as `Sema::isStdInitializerList`
expects, but an `Decl::Kind::Export` and only its parent is a namespace.
So this commit makes `Sema::isStdInitializerList` account for that.

I'm really new to clang so I'm not 100% sure that was the issue, it
seems so and it fixes compilation. Also I probably need to add tests but
I'd like someone to approve the idea first.

Fixes https://github.com/llvm/llvm-project/issues/118218
2024-12-12 09:38:47 +08:00
Younan Zhang
d44518c1cc
[Clang] Don't check incomplete CXXRecordDecl's members when transforming sizeof...(expr) (#119344)
For a FunctionParmPackExpr that is used as the argument of a
sizeof...(pack) expression, we might exercise the logic that checks the
CXXRecordDecl's members regardless of the type being incomplete, when
rebuilding the DeclRefExpr into non-ODR-used forms.

Fixes https://github.com/llvm/llvm-project/issues/81436
2024-12-10 18:37:30 +08:00
Chuanqi Xu
411196b9bb
[C++20] [Modules] Convert '-fexperimental-modules-reduced-bmi' to '-fmodules-reduced-bmi' (#114382)
According to our previous consensus in
https://clang.llvm.org/docs/StandardCPlusPlusModules.html#reduced-bmi,
the reduced BMI will be the default and recommend users to use the new
option.

The `-fexperimental-modules-reduced-bmi ` option is introduced in
https://github.com/llvm/llvm-project/pull/85050 in Mar13 and released in
19.x. And now we are in 20's release cycle. Also I rarely receive issue
reports about reduced BMI. No matter it is due to the quality of reduced
BMI is really good or no one uses it.

This patch literally did the second point in
https://clang.llvm.org/docs/StandardCPlusPlusModules.html#reduced-bmi
2024-12-10 15:18:26 +08:00
ykiko
77a08a73cc
[Clang] Fix wrong call location of DefaultArgExpr (#119212)
Fix https://github.com/llvm/llvm-project/issues/119129.
2024-12-10 14:01:40 +08:00
Carlo Cabrera
511e84ff26
[clang][docs] fix rendering of $-prefixed options (#119249)
This was added in #117573 but the options were not being rendered
correctly due to the missing newline after `::`.
2024-12-10 03:24:04 +08:00
Oleksandr T.
1094641bc0
[Clang] allow usage of placement new operator in [[msvc::constexpr]] context outside of the std namespace (#119153)
Fixes #74924
2024-12-09 18:47:38 +02:00
cor3ntin
6d759f83eb
[Clang] Deleting an incomplete enum type is not an error (#119077)
The changes introduced in #97733 accidentally prevented to delete an
incomplete enum
(the validity of which has been confirmed by CWG2925
    
Fixes #99278
2024-12-09 10:20:16 +01:00
Owen Pan
411df3bb49 [clang-format] Make command lines of dump_format_[style|help].py consistent 2024-12-09 00:56:24 -08:00
Younan Zhang
a4506bb340
[Clang] Recurse into parsing when using pack-indexing as a specifier (#119123)
Pack indexing type that introduces a scope, e.g. `T...[0]::value` would
be annotated as `annot_cxxscope`. This is something we didn't handle in
`ParseCastExpression`, causing it to mistakely fall through to the logic
for `raw_identifier`.

We should recurse into parsing the following specifiers for such cases.

Closes #119072
2024-12-09 09:58:37 +08:00
c8ef
f145ff3f70
[clang] constexpr built-in elementwise add_sat/sub_sat functions. (#119082)
Part of #51787.

This patch adds constexpr support for the built-in elementwise add_sat
and sub_sat functions.
2024-12-09 09:28:12 +08:00
Chris Apple
8843d2b469
[rtsan] Add verify_interceptors flag to docs (#119074) 2024-12-08 09:34:40 -08:00
Owen Pan
eeadd0128d [clang-format] Also check ClangFormat.rst is up to date in docs_updated.test 2024-12-08 01:15:45 -08:00
Amr Hesham
3f458cd9ab
[Clang] Warning as error Array Comparisons from C++26 (#118872)
Starting from C++26 the array comparison warning should converted to an
error.

Fixes: #117859
2024-12-07 18:28:09 +01:00
Chris Apple
342c8db381
[rtsan] NFC: Docs update adding links to Disabling, adding a few ` marks (#119075) 2024-12-07 08:25:08 -08:00
Paul Osmialowski
755519f7f6
[clang][driver] Use $ prefix with config file options to have them added after all of the command line options (#117573)
Currently, if a -l (or -Wl,) flag is added into a config file
(e.g. clang.cfg), it is situated before any object file in the
effective command line. If the library requested by given -l flag is
static, its symbols will not be made visible to any of the object
files provided by the user. Also, the presence of any of the linker
flags in a config file confuses the driver whenever the user invokes
clang without any parameters (see issue #67209).

This patch attempts to solve both of the problems, by allowing a split
of the arguments list into two parts. The head part of the list will
be used as before, but the tail part will be appended after the
command line flags provided by the user and only when it is known
that the linking should occur. The $-prefixed arguments will be added
to the tail part.
2024-12-07 11:18:44 +00:00
cor3ntin
bcf6f84763
Revert "[Clang] Deleting an incomplete enum type is not an error (#118455) (#118980)
This reverts commit 8271195de05742ed7079d7882fbebc2daecbd7e2.
2024-12-07 04:22:47 +01:00
Owen Pan
9a5946cdba [clang-format] Add --directory option to dump_format_help.py 2024-12-05 20:16:09 -08:00
Owen Pan
74d29c6393 [clang-format] Open plurals.txt in read-only mode in dump_format_style.py 2024-12-05 19:25:32 -08:00