3115 Commits

Author SHA1 Message Date
owenca
6cfedea492
[clang-format] Add SpaceInEmptyBraces option (#153765)
Also set it to SIEB_Always for WebKit style.

Closes #85525.
Closes #93635.
2025-08-17 12:56:22 -07:00
mdenson
65ffa53cb7
[Clang] unrecognized html tag causing undesirable comment lexing (#152944)
Simple fix for this particular html tag. A more complete solution should
be implemented.

1. Add all html tags to table so they are recognized. Some input on what
is desirable/safe would be appreciated
2. Change the lex strategy to deal with this in a different manner

Fixes #32680

---------

Co-authored-by: Brock Denson <brock.denson@virscient.com>
2025-08-17 15:59:47 +02:00
Carlos Galvez
bd77e9acf0
[clang-tidy] Avoid matching nodes in system headers (#151035)
This commit is a re-do of e4a8969e56572371201863594b3a549de2e23f32,
which got reverted, with the same goal: dramatically speed-up clang-tidy
by avoiding doing work in system headers (which is wasteful as warnings
are later discarded). This proposal was already discussed here with
favorable feedback: https://github.com/llvm/llvm-project/pull/132725

The novelty of this patch is:

- It's less aggressive: it does not fiddle with AST traversal. This
solves the issue with the previous patch, which impacted the ability to
inspect parents of a given node.

- Instead, what we optimize for is exitting early in each `Traverse*`
function of `MatchASTVisitor` if the node is in a system header, thus
avoiding calling the `match()` function with its corresponding callback
(when there is a match).

- It does not cause any failing tests.

- It does not move `MatchFinderOptions` - instead we add a user-defined
default constructor which solves the same problem.

- It introduces a function `shouldSkipNode` which can be extended for
adding more conditions. For example there's a PR open about skipping
modules in clang-tidy where this could come handy:
https://github.com/llvm/llvm-project/pull/145630

As a benchmark, I ran clang-tidy with all checks activated, on a single
.cpp file which #includes all the standard C++ headers, then measure the
time as well as found warnings.

On trunk:

```
Suppressed 75413 warnings (75413 in non-user code).

real	0m12.418s
user	0m12.270s
sys	0m0.129s
```

With this patch:

```
Suppressed 11448 warnings (11448 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.

real	0m1.666s
user	0m1.538s
sys	0m0.129s
```

With the original patch that got reverted:

```
Suppressed 11428 warnings (11428 in non-user code).

real	0m1.193s
user	0m1.096s
sys	0m0.096s
```

We therefore get a dramatic reduction in number of warnings and runtime,
with no change in functionality.

The remaining warnings are due to `PPCallbacks` - implementing a similar
system-header exclusion mechanism there can lead to almost no warnings
left in system headers. This does not bring the runtime down as much,
though, so it's probably not worth the effort.

Fixes #52959

Co-authored-by: Carlos Gálvez <carlos.galvez@zenseact.com>
2025-08-17 11:40:48 +02:00
keinflue
af96ed6bf6
[clang] Inject IndirectFieldDecl even if name conflicts. (#153140)
This modifies InjectAnonymousStructOrUnionMembers to inject an
IndirectFieldDecl and mark it invalid even if its name conflicts with
another name in the scope.

This resolves a crash on a further diagnostic
diag::err_multiple_mem_union_initialization which via
findDefaultInitializer relies on these declarations being present.

Fixes #149985
2025-08-15 09:43:29 -07:00
Yanzuo Liu
3b27d50cc7
[LLVM][utils] Add script which clears release notes (#153593)
The script copies `ReleaseNotesTemplate.txt` to corresponding
`ReleaseNotes.rst`/`.md` to clear release notes.

The suffix of `ReleaseNotesTemplate.txt` must be `.txt`. If it is
`.rst`/`.md`, it will be treated as a documentation source file when
building documentation.
2025-08-15 19:00:08 +08:00
Vladimir Vuksanovic
9d1dd9a264
[Sema] Fix false positive warnings for misaligned member access (#150025)
These warnings are reported on a per expression basis, however some
potential misaligned accesses are discarded before that happens. The
problem is when a new expression starts while processing another
expression. The new expression will end first and emit all potential
misaligned accesses collected up to that point. That includes candidates
that were found in the parent expression, even though they might have
gotten discarded later.

Fixed by checking if the candidate is located withing the currently
processed expression.

Fixes #144729
2025-08-14 06:14:43 -07:00
Vincent
d3bbdc7bde
[clang] constexpr __builtin_elementwise_abs support (#152497)
Added constant evaluation support for `__builtin_elementwise_abs` on integer, float and vector type.

fixes #152276

---------

Co-authored-by: Simon Pilgrim <llvm-dev@redking.me.uk>
2025-08-14 12:34:23 +01:00
mdenson
f5b36eb3a4
[clang] fix comment lexing of command names with underscore (#152943)
Comment lexer fails to parse non-alphanumeric names.

fixes #33296

---------

Co-authored-by: Brock Denson <brock.denson@virscient.com>
2025-08-14 13:03:55 +02:00
Corentin Jabot
186176de45
[Clang] Do not consider a variadic function ellipsis part of a default arg (#153496)
When stashing the tokens of a parameter of a member function, we would
munch an ellipsis, as the only considered terminal conditions were `,`
and `)`.

Fixes #153445
2025-08-14 12:51:58 +02:00
Matheus Izvekov
9255580a3a
[clang] fix skipped parsing of late parsed attributes (#153558) 2025-08-14 06:42:55 -03:00
DeanSturtevant1
4cdc3388b3
Fix Clang bug that -Wformat-signedness is not reported properly. (#150962)
The goal is to correctly identify diagnostics that are emitted by virtue
of -Wformat-signedness.

Before this change, diagnostic messages triggered by -Wformat-signedness
might look like:
format specifies type 'unsigned int' but the argument has type 'int'
[-Wformat]
signedness of format specifier 'u' is incompatible with 'c' [-Wformat]
With this change:
format specifies type 'unsigned int' but the argument has type 'int',
which differs in signedness [-Wformat-signedness]
signedness of format specifier 'u' is incompatible with 'c'
[-Wformat-signedness]

Fix:
- handleFormatSignedness can now return NoMatchSignedness. Callers
handle this.
- warn_format_conversion_argument_type extends the message it used to
emit by a string that
  mentions "signedness".
- warn_format_cmp_specifier_sign_mismatch is now correctly categorized
as a
  diagnostic controlled by -Wformat-signedness.
2025-08-13 08:03:28 -07:00
Jongmyeong Choi
385f83c774
[clang] Fix assertion failure with explicit(bool) in pre-C++11 modes (#152985)
Allow CCEKind::ExplicitBool in BuildConvertedConstantExpression for
pre-C++11 contexts, similar to the existing TempArgStrict exception.
This enables explicit(bool) to work as a C++20 extension in earlier
language modes without triggering assertion failures.

Fixes #152729

---------

Co-authored-by: Jongmyeong Choi <cheesechoi@gmail.com>
2025-08-13 07:03:09 -07:00
Amit Tiwari
2074e1320f
[Clang][OpenMP] Non-contiguous strided update (#144635)
This patch handles the strided update in the `#pragma omp target update
from(data[a🅱️c])` directive where 'c' represents the strided access
leading to non-contiguous update in the `data` array when the offloaded
execution returns the control back to host from device using the `from`
clause.

Issue: Clang CodeGen where info is generated for the particular
`MapType` (to, from, etc), it was failing to detect the strided access.
Because of this, the `MapType` bits were incorrect when passed to
runtime. This led to incorrect execution (contiguous) in the
libomptarget runtime code.

Added a minimal testcase that verifies the working of the patch.
2025-08-12 19:32:15 +05:30
Chaitanya Koparkar
c3bf73bc4a
[clang] Add elementwise fshl/fshr builtins (#153113)
This patch implements `__builtin_elementwise_fshl` and
`__builtin_elementwise_fshr` builtins.

These map to the fshl/fshr intrinsics described here:
- https://llvm.org/docs/LangRef.html#llvm-fshl-intrinsic
- https://llvm.org/docs/LangRef.html#llvm-fshr-intrinsic

Fixes https://github.com/llvm/llvm-project/issues/152555.
2025-08-12 20:57:55 +09:00
Iris Shi
713ec58dec
[clang] constexpr integer __builtin_elementwise_{max,min} (#152294)
- Closes #152278
- Part of #51787

This PR adds support for the bulitin elementwise max/min functions for
integer types.
2025-08-12 09:49:05 +08:00
Oleksandr T.
5544492d6a
[Clang] Fixed a crash when parsing #embed parameters with unmatched closing brackets (#152877)
Fixes #152829

--- 

This patch addresses the issue where the preprocessor could crash when
parsing `#embed` parameters containing unmatched closing brackets

```cpp
#embed "file" prefix(])
#embed "file" prefix(})
```
2025-08-11 17:08:10 +03:00
Jimmy Z
e20dd94bb3
[libclang/python] Expose clang_getCursorLanguage via Cursor.language (#152897) 2025-08-11 14:50:29 +02:00
keinflue
4c0a7b72f7
[clang] Distinguish NTTPs with deduced types in variable template partial specializations (#152864)
If a template argument in a partial specialization of a variable
template directly refers to a NTTP of the specialization without
implicit type conversion it was assumed that the NTTP is identical to
that of the primary template.

This doesn't hold if the primary template's NTTP uses a deduced type, so
instead compare the types explicitly as well.

The affected function is used only to provide an improved early error if
the partial specialization has identical template arguments to the
primary template. The actual check that the partial specialization is
more specialized happens later.

Fixes #118190
Fixes #152750
2025-08-09 20:03:02 -03:00
Craig Topper
92a966bd8f
[RISCV] Add -march=unset to cancel and ignore a previous -march. (#148321)
-mcpu is used to determine the ISA string if an explicit -march is not
present on the command line. If there is a -march present it always has
priority over -mcpu regardless of where it appears in the command line.

This can cause issues if -march appears in a Makefile and a user wants
to override it with an -mcpu on the command line. The user would need to
provide a potentially long ISA string to -march that matches the -mcpu
in order to override the MakeFile.

This issue can also be seen on Compiler Explorer where the rv64gc
toolchain is passed -march=rv64gc before any user command line options
are added. If you pass -mcpu=spacemit-x60, vectors will not be enabled
due to the hidden -march.

This patch adds a new option for -march, "unset" that makes the -march
ignored for purposes of prioritizing over -mcpu. Now a user can write
-march=unset -mcpu=<cpu_name>. This is also implemented by gcc for
ARM32.

An alternative would be to allow -march to take a cpu name, but that
requires "-march=<cpu_name> -mcpu=<cpu_name>" or "-march=<cpu_name>
-mtune=<cpu_name>" to ensure the tune cpu also gets updated. IMHO,
needing to repeat the CPU name twice isn't friendly and invites
mistakes.

gcc has implemented this as well.
2025-08-09 09:23:04 -05:00
Matheus Izvekov
91cdd35008
[clang] Improve nested name specifier AST representation (#147835)
This is a major change on how we represent nested name qualifications in
the AST.

* The nested name specifier itself and how it's stored is changed. The
prefixes for types are handled within the type hierarchy, which makes
canonicalization for them super cheap, no memory allocation required.
Also translating a type into nested name specifier form becomes a no-op.
An identifier is stored as a DependentNameType. The nested name
specifier gains a lightweight handle class, to be used instead of
passing around pointers, which is similar to what is implemented for
TemplateName. There is still one free bit available, and this handle can
be used within a PointerUnion and PointerIntPair, which should keep
bit-packing aficionados happy.
* The ElaboratedType node is removed, all type nodes in which it could
previously apply to can now store the elaborated keyword and name
qualifier, tail allocating when present.
* TagTypes can now point to the exact declaration found when producing
these, as opposed to the previous situation of there only existing one
TagType per entity. This increases the amount of type sugar retained,
and can have several applications, for example in tracking module
ownership, and other tools which care about source file origins, such as
IWYU. These TagTypes are lazily allocated, in order to limit the
increase in AST size.

This patch offers a great performance benefit.

It greatly improves compilation time for
[stdexec](https://github.com/NVIDIA/stdexec). For one datapoint, for
`test_on2.cpp` in that project, which is the slowest compiling test,
this patch improves `-c` compilation time by about 7.2%, with the
`-fsyntax-only` improvement being at ~12%.

This has great results on compile-time-tracker as well:

![image](https://github.com/user-attachments/assets/700dce98-2cab-4aa8-97d1-b038c0bee831)

This patch also further enables other optimziations in the future, and
will reduce the performance impact of template specialization resugaring
when that lands.

It has some other miscelaneous drive-by fixes.

About the review: Yes the patch is huge, sorry about that. Part of the
reason is that I started by the nested name specifier part, before the
ElaboratedType part, but that had a huge performance downside, as
ElaboratedType is a big performance hog. I didn't have the steam to go
back and change the patch after the fact.

There is also a lot of internal API changes, and it made sense to remove
ElaboratedType in one go, versus removing it from one type at a time, as
that would present much more churn to the users. Also, the nested name
specifier having a different API avoids missing changes related to how
prefixes work now, which could make existing code compile but not work.

How to review: The important changes are all in
`clang/include/clang/AST` and `clang/lib/AST`, with also important
changes in `clang/lib/Sema/TreeTransform.h`.

The rest and bulk of the changes are mostly consequences of the changes
in API.

PS: TagType::getDecl is renamed to `getOriginalDecl` in this patch, just
for easier to rebasing. I plan to rename it back after this lands.

Fixes #136624
Fixes https://github.com/llvm/llvm-project/issues/43179
Fixes https://github.com/llvm/llvm-project/issues/68670
Fixes https://github.com/llvm/llvm-project/issues/92757
2025-08-09 05:06:53 -03:00
Oliver Hunt
c548c47476
[clang] Fix crash in dynamic_cast final class optimization (#152076)
This corrects the codegen for the final class optimization to
correct handle the case where there is no path to perform the
cast, and also corrects the codegen to handle ptrauth protected
vtable pointers.

As part of this fix we separate out the path computation as
that makes it easier to reason about the failure code paths
and more importantly means we can know what the type of the
this object is during the cast.

The allows us to use the GetVTablePointer interface which
correctly performs the authentication operations required when
pointer authentication is enabled. This still leaves incorrect
authentication behavior in the multiple inheritance case but
currently the optimization is disabled entirely if pointer
authentication is enabled.

Fixes #137518
2025-08-06 09:52:34 -07:00
Vincent
3339a0045d
[clang] Respect [[gnu::error]] on functions passed to [[gnu::cleanup]] (#152082)
Forward SourceLocation to `EmitCall` so that clang triggers an error
when a function inside `[[gnu::cleanup(func)]]` is annotated with
`[[gnu::error("some message")]]`.

resolves #146520
2025-08-05 21:21:36 -07:00
keinflue
81ed75679d
[clang] Fix constant evaluation of member pointer access into sibling class. (#150829)
HandleMemberPointerAccess considered whether the lvalue path in a member
pointer access matched the bases of the containing class of the member,
but neglected to check the same for the containing class of the member
itself, thereby ignoring access attempts to members in direct sibling
classes.

Fixes #150705.
Fixes #150709.
2025-08-05 14:44:15 -07:00
Matt
2e404d1f80
clang: Make the type_info builtin declaration a singleton (#151277)
This fixes an ambiguous type type_info when you try and reference the
`type_info` type while using clang modulemaps with `-fms-compatibility`
enabled

Fixes #38400
2025-08-04 06:44:10 -07:00
Gergely Futo
1454db130a
[RISCV] Support resumable non-maskable interrupt handlers (#148134)
The `rnmi` interrupt attribute value has been added for the `Smrnmi`
extension.

---------

Co-authored-by: Sam Elliott <sam@lenary.co.uk>
2025-08-04 10:54:50 +02:00
Iris Shi
eefc3d275f
[clang][diagnostics] Fix fix-it hint parenthesis placement for fold expressions (#151790)
- Closes #151787

Insert the right parenthesis one token later to correctly enclose the
expression.

---------

Co-authored-by: Corentin Jabot <corentinjabot@gmail.com>
2025-08-02 16:53:06 +08:00
Aaron Ballman
17327482f0
[Analyzer] No longer crash with VLA operands to unary type traits (#151719)
sizeof was handled correctly, but __datasizeof and _Countof were not.

Fixes #151711
2025-08-01 12:31:56 -04:00
Iris Shi
f9088f1eb5
[static analyzer] Fix crash on parenthesized expression in assume attribute (#151682)
- Closes #151529

`ParenExpr` should be ignored before reaching `ExprEngine::Visit`.
Failing to do so triggers the assertion.
2025-08-01 19:29:06 +08:00
M. Zeeshan Siddiqui
70471f08ee
[Sema] Fix -Wunreachable-code false negative when operands differ only by implicit casts (#149972)
## Motivation  
`-Wunreachable-code` missed—or in rare cases crashed on—tautological
comparisons such as

```cpp
x != 0 || x != 1.0   // always true
x == 0 && x == 1.0   // always false
```

when the *same* variable appears on both sides but one operand goes
through a floating‑rank promotion that is target‑dependent. On back‑ends
with **native half‑precision** (`_Float16` / `__fp16`) such as
AArch64 `+fullfp16`, no promotion occurs, so the cast stacks between the
two operands differ and the existing heuristic bails out.

## Technical description 
* **Extends `Expr::isSameComparisonOperand()`** – the helper now ignores
parentheses **and value‑preserving implicit casts**
(`CK_LValueToRValue`, floating‑rank `CK_FloatingCast`) before comparing
the underlying operands. This prevents floating‑rank promotions and
lvalue‑to‑rvalue conversions from blocking the unreachable‑code
diagnostic on targets with native FP16.
*No change needed in `CheckIncorrectLogicOperator`; it simply benefits
from the improved helper.*
* **Regression test** – `warn-unreachable_crash.cpp` updated to cover
both the promoted case (x86‑64) and the native‑half case
(AArch64 `+fullfp16`).
* **Docs** – release‑note bullet added under *Bug Fixes in This
Version*.

@ziqingluo-90 @yronglin Could you please review promptly? (feel free to
also merge it on my behalf) Thanks!

Fixes #149967

Co-authored-by: Zeeshan Siddiqui <mzs@ntdev.microsoft.com>
2025-07-31 09:21:59 +08:00
Yanzuo Liu
b39160ddfb
[Clang][Sema] Expression in assumption attribute should be full expression (#150814)
Add missing `ActOnFinishFullExpr` to `BuildCXXAssumeExpr`. We did it
during template instantiation but forgot non-template case.
2025-07-29 11:53:02 +08:00
Nick Sarnie
0efcb83626
[Clang] Reland '__has_builtin should return false for aux triple builtins' (#126324)
Reland https://github.com/llvm/llvm-project/pull/121839 based on the
results of the Discourse discussion
[here](https://discourse.llvm.org/t/rfc-has-builtin-behavior-on-offloading-targets/84964).

---------

Signed-off-by: Sarnie, Nick <nick.sarnie@intel.com>
2025-07-28 17:54:52 +00:00
halbi2
a63bbf2f1e
[clang] Diagnose [[nodiscard]] return types in Objective-C++ (#142541)
My solution was to copy-paste getUnusedResultAttr and
hasUnusedResultAttr from CallExpr into ObjCMessageExpr too.

Fixes #141504
2025-07-28 09:26:28 -04:00
Anthony Tran
29992cfd62
[Clang][CodeGen] Emit “trap reasons” on UBSan traps (#145967)
This patch adds a human readable trap category and message to UBSan
traps. The category and message are encoded in a fake frame in the debug
info where the function is a fake inline function where the name encodes
the trap category and message. This is the same mechanism used by
Clang’s `__builtin_verbose_trap()`.

This change allows consumers of binaries built with trapping UBSan to
more easily identify the reason for trapping. In particular LLDB already
has a frame recognizer that recognizes the fake function names emitted
in debug info by this patch. A patch testing this behavior in LLDB will
be added in a separately.

The human readable trap messages are based on the messages currently
emitted by the userspace runtime for UBSan in compiler-rt. Note the
wording is not identical because the userspace UBSan runtime has access
to dynamic information that is not available during Clang’s codegen.

Test cases for each UBSan trap kind are included.

This complements the [`-fsanitize-annotate-debug-info`
feature](https://github.com/llvm/llvm-project/pull/141997). While
`-fsanitize-annotate-debug-info` attempts to annotate all UBSan-added
instructions, this feature (`-fsanitize-debug-trap-reasons`) only
annotates the final trap instruction using SanitizerHandler information.

This work is part of a GSoc 2025 project.
2025-07-26 08:50:25 -07:00
Harald van Dijk
2228b4b46c
clang: Handle deleting pointers to incomplete array types (#150359)
CodeGenFunction::EmitCXXDeleteExpr contains logic to go from a pointer
to an array to a pointer to the first element of the array using a
getelementptr LLVM IR instruction. This was done for pointers that were
not variable length arrays, as pointers to variable length arrays never
existed in LLVM IR, but rather than checking for arrays that were not
variable length arrays, it checked for arrays that had a constant bound.
This caused incomplete arrays to be inadvertently omitted.

This getelementptr was necessary back when LLVM IR used typed pointers,
but they have been gone for a while, a gep with a constant zero offset
does nothing now, so we can simplify the code by removing that.
2025-07-24 18:43:17 +01:00
Aaron Ballman
db8a389f88 [Docs] Add back lost release notes; NFC
When release notes were cleared on ``main``, there were some notes lost
because the changes happened after the branch point for 21.x. This
restores the lost notes.
2025-07-24 07:12:54 -04:00
Tobias Hieta
3d8db8ef50 Clear release notes on main for LLVM 22 2025-07-24 11:36:11 +02:00
Shashi Shankar
31db0f0a7a
[Clang] Suppress deprecated field warnings in implicit functions definitions (#147400)
Do not warn on deprecated member used in an implicit definition (such as a defaulted special member function).

Co-authored-by: Corentin Jabot <corentinjabot@gmail.com>

Fixes #147293
2025-07-24 08:48:27 +02:00
David Pagan
45d99c26c3
[clang][OpenMP] In 6.0, can omit length in array section (#148048)
In OpenMP 6.0 specification, section 5.2.5 Array Sections, page 166,
lines 28-28:

When the length is absent and the size of the dimension is not known,
the array section is an assumed-size array.

Testing
- Updated LIT test
- check-all
- OpenMP_VV (formerly sollve) test case
tests/6.0/target/test_target_assumed_array_size.c
2025-07-23 10:53:38 -07:00
Fazlay Rabbi
5daaaf8d7d
[OpenMP 6.0] Allow only byref arguments with need_device_addr modifier on adjust_args clause (#149586)
If the need_device_addr adjust-op modifier is present, each list item
that appears in the clause must refer to an argument in the declaration
of the function variant that has a reference type.

Reference:
OpenMP 6.0 [Sec 9.6.2, page 332, line 31-33, adjust_args clause,
Restrictions]
2025-07-23 09:38:13 -07:00
Wenju He
e0dd22fab1
[Clang] Add elementwise maximumnum/minimumnum builtin functions (#149775)
Addresses https://github.com/llvm/llvm-project/issues/112164. minimumnum
and maximumnum intrinsics were added in 5bf81e53dbea.

The new built-ins can be used for implementing OpenCL math function fmax
and fmin in #128506.
2025-07-23 08:34:35 +08:00
Volodymyr Sapsai
dbc41dd5d8
[clang][deps] Add a release note for fixing crashes in clang-scan-deps. (#149857) 2025-07-22 20:15:54 -04:00
yronglin
44a6e0099b
[clang] Check empty macro name in #pragma push_macro("") or #pragma pop_macro("") (#149982)
Fixes https://github.com/llvm/llvm-project/issues/149762.

---------

Signed-off-by: yronglin <yronglin777@gmail.com>
2025-07-22 22:57:34 +08:00
Chuanqi Xu
255a163e51 [C++20] [Modules] Enable reduced BMI by default
As documented in 20.x, we'd like to keep reduced BMI off by default for
1~2 versions. And now we're in 22.x.

I rarely receive bug reports for reduced BMI. I am not sure about the
reason. Maybe not a lot of people are using it. Or it is really stable
enough.

And also, we've been enabling the reduced BMI internally for roughly half a
year.

So I think it's the time to move on. See the document changes for other
information.
2025-07-21 18:57:38 +08:00
YexuanXiao
7c402b8b81
Reland [Clang] Make the SizeType, SignedSizeType and PtrdiffType be named sugar types (#149613)
The checks for the 'z' and 't' format specifiers added in the original
PR #143653 had some issues and were overly strict, causing some build
failures and were consequently reverted at
4c85bf2fe8.

In the latest commit
27c58629ec,
I relaxed the checks for the 'z' and 't' format specifiers, so warnings
are now only issued when they are used with mismatched types.

The original intent of these checks was to diagnose code that assumes
the underlying type of `size_t` is `unsigned` or `unsigned long`, for
example:

```c
printf("%zu", 1ul); // Not portable, but not an error when size_t is unsigned long
```  

However, it produced a significant number of false positives. This was
partly because Clang does not treat the `typedef` `size_t` and
`__size_t` as having a common "sugar" type, and partly because a large
amount of existing code either assumes `unsigned` (or `unsigned long`)
is `size_t`, or they define the equivalent of size_t in their own way
(such as
sanitizer_internal_defs.h).2e67dcfdcd/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h (L203)
2025-07-19 03:44:14 -03:00
Oleksandr T.
cda28e203d
[analyzer] Support parenthesized list initialization (CXXParenListInitExpr) (#148988)
This patch addresses the lack of support for parenthesized
initialization in the Clang Static Analyzer's `ExprEngine`. Previously,
initializations such as `V v(1, 2);` were not modeled properly, which
could lead to false negatives in analyses like `DivideZero`.

```cpp
struct A {
  int x;
  A(int v) : x(v) {}
};

int t() {
  A a(42);
  return 1 / (a.x - 42); // expected-warning {{Division by zero}}
}
```

Fixes #148875
2025-07-18 09:34:15 +02:00
Younan Zhang
beec840822
[Clang] Ensure correct parameters are in the scope for constraint equivalence checking (#149264)
This is another case where untransformed constraint expressions led to
inconsistent transforms.

We did fix some of those issues by looking at parent scopes, however the
parent instantiation scope is not always available because we could also
reach here after the parents get instantiated.

Fixes #146614
2025-07-18 15:23:32 +08:00
Kazu Hirata
4c85bf2fe8 Revert "[Clang] Make the SizeType, SignedSizeType and PtrdiffType be named sugar types instead of built-in types (#143653)"
This reverts commit c27e283cfbca2bd22f34592430e98ee76ed60ad8.

A builbot failure has been reported:
https://lab.llvm.org/buildbot/#/builders/186/builds/10819/steps/10/logs/stdio

I'm also getting a large number of warnings related to %zu and %zx.
2025-07-17 21:04:01 -07:00
YexuanXiao
c27e283cfb
[Clang] Make the SizeType, SignedSizeType and PtrdiffType be named sugar types instead of built-in types (#143653)
Including the results of `sizeof`, `sizeof...`, `__datasizeof`,
`__alignof`, `_Alignof`, `alignof`, `_Countof`, `size_t` literals, and
signed `size_t` literals, the results of pointer-pointer subtraction and
checks for standard library functions (and their calls).

The goal is to enable clang and downstream tools such as clangd and
clang-tidy to provide more portable hints and diagnostics.

The previous discussion can be found at #136542.

This PR implements this feature by introducing a new subtype of `Type`
called `PredefinedSugarType`, which was considered appropriate in
discussions. I tried to keep `PredefinedSugarType` simple enough yet not
limited to `size_t` and `ptrdiff_t` so that it can be used for other
purposes. `PredefinedSugarType` wraps a canonical `Type` and provides a
name, conceptually similar to a compiler internal `TypedefType` but
without depending on a `TypedefDecl` or a source file.

Additionally, checks for the `z` and `t` format specifiers in format
strings for `scanf` and `printf` were added. It will precisely match
expressions using `typedef`s or built-in expressions.

The affected tests indicates that it works very well.

Several code require that `SizeType` is canonical, so I kept `SizeType`
to its canonical form.

The failed tests in CI are allowed to fail. See the
[comment](https://github.com/llvm/llvm-project/pull/135386#issuecomment-3049426611)
in another PR #135386.
2025-07-17 22:45:57 -03:00
Doug Wyatt
cfc20ea15e
[Clang] FunctionEffects: Make a separate diagnostic group for redeclarations/overrides where effects are implicit. (#148690)
The current function effect diagnostics include these behaviors:

When you declare a function `nonblocking` (typically in a header) and
then omit the attribute on the implementation (or any other
redeclaration), Clang warns: attribute 'nonblocking' on function does
not match previous declaration.

But if a `nonblocking` function is a C++ virtual method, then overrides
are implicitly nonblocking; the attribute doesn't need to be explicitly
stated.

These behaviors are arguably inconsistent -- and also, both, more
pedantic than the rest of the function effect diagnostics.

This PR accomplishes two things:

- Separates the diagnostic on a redeclaration into a new group,
`-Wfunction-effect-redeclarations`, so it can be disabled independently.

- Adds a second diagnostic to this new group, for the case of an
override method missing the attribute. (This helps in a situation where
I'm trying to add `nonblocking` via a macro that does other things and I
want to know that the macro is missing on an override declaration.)

---------

Co-authored-by: Doug Wyatt <dwyatt@apple.com>
Co-authored-by: Sirraide <aeternalmail@gmail.com>
2025-07-16 08:24:28 -07:00
Corentin Jabot
9e5470e7d6
[Clang] Diagnose forming references to nullptr (#143667)
Per [decl.ref],

> Because a null pointer value or a pointer past the end of an object
does not point to an object, a reference in a well-defined program
cannot refer to such things.

Note this does not fixes the new bytecode interpreter.

Fixes #48665
2025-07-16 14:25:24 +02:00