6269 Commits

Author SHA1 Message Date
Matheus Izvekov
761787d425
Reland: [clang] Improved canonicalization for template specialization types (#135414)
This relands https://github.com/llvm/llvm-project/pull/135119, after
fixing crashes seen in LLDB CI reported here:
https://github.com/llvm/llvm-project/pull/135119#issuecomment-2794910840

Fixes https://github.com/llvm/llvm-project/pull/135119

This changes the TemplateArgument representation to hold a flag
indicating whether a tempalte argument of expression type is supposed to
be canonical or not.

This gets one step closer to solving
https://github.com/llvm/llvm-project/issues/92292

This still doesn't try to unique as-written TSTs. While this would
increase the amount of memory savings and make code dealing with the AST
more well-behaved, profiling template argument lists is still too
expensive for this to be worthwhile, at least for now.

This also fixes the context creation of TSTs, so that they don't in some
cases get incorrectly flagged as sugar over their own canonical form.
This is captured in the test expectation change of some AST dumps.

This fixes some places which were unnecessarily canonicalizing these
TSTs.
2025-04-12 14:26:30 -03:00
Aaron Ballman
d1ea4799d9
[C23] Fix failed assertions with invalid #embed parameters (#135368)
If the invalid parameter was not the last parameter given, we would fail
to skip to the end of the directive and trip a failed assertion.

Fixes #126940
2025-04-11 10:44:37 -04:00
Dmitry Vasilyev
cbba9600ae
Revert "[clang] Improved canonicalization for template specialization types" (#135354)
Reverts llvm/llvm-project#135119 because of the assert in ASTContext.cpp, line 5619.
See #135352 for details.
2025-04-11 17:55:11 +04:00
Mészáros Gergely
c58777c2ad
[clang][ReleaseNotes] Fix code block not rendering (#135330)
Bullet lists require a blank line between paragraphs therefore the `..
code-block::` directive only renders correctly if preceded by a blank
line.
2025-04-11 13:53:33 +02:00
offsetof
9604bdf118
[clang] Allow parentheses around CTAD declarators (#132829)
Fixes #39811
2025-04-11 08:47:07 +02:00
Yingwei Zheng
04c38981a9
[Clang][CodeGen] Do not set inbounds flag in EmitMemberDataPointerAddress when the base pointer is null (#130952)
See also https://github.com/llvm/llvm-project/pull/130734 for the
original motivation.

This pattern (`container_of`) is also widely used by real-world
programs.
Examples:

1d89d7d5d7/llvm/include/llvm/IR/SymbolTableListTraits.h (L77-L87)

a2a53cb728/src/util-inl.h (L134-L137)
https://github.com/search?q=*%29nullptr-%3E*&type=code
2025-04-11 10:51:08 +08:00
Yingwei Zheng
1711996805
[Clang][CodeGen] Do not set inbounds flag for struct GEP with null base pointers (#130734)
In the LLVM middle-end we want to fold `gep inbounds null, idx -> null`:
https://alive2.llvm.org/ce/z/5ZkPx-
This pattern is common in real-world programs
(https://github.com/dtcxzyw/llvm-opt-benchmark/pull/55#issuecomment-1870963906).
Generally, it exists in some (actually) unreachable blocks, which is
introduced by JumpThreading.

However, some old-style offsetof macros are still widely used in
real-world C/C++ code (e.g., hwloc/slurm/luajit). To avoid breaking
existing code and inconvenience to downstream users, this patch removes
the inbounds flag from the struct gep if the base pointer is null.
2025-04-11 09:04:23 +08:00
Oliver Hunt
1cd59264aa
[RFC] Initial implementation of P2719 (#113510)
This is a basic implementation of P2719: "Type-aware allocation and
deallocation functions" described at http://wg21.link/P2719

The proposal includes some more details but the basic change in
functionality is the addition of support for an additional implicit
parameter in operators `new` and `delete` to act as a type tag. Tag is
of type `std::type_identity<T>` where T is the concrete type being
allocated. So for example, a custom type specific allocator for `int`
say can be provided by the declaration of

  void *operator new(std::type_identity<int>, size_t, std::align_val_t);
  void  operator delete(std::type_identity<int>, void*, size_t, std::align_val_t);

However this becomes more powerful by specifying templated declarations,
for example

template <typename T> void *operator new(std::type_identity<T>, size_t, std::align_val_t);
template <typename T> void operator delete(std::type_identity<T>, void*, size_t, std::align_val_t););

Where the operators being resolved will be the concrete type being
operated over (NB. A completely unconstrained global definition as above
is not recommended as it triggers many problems similar to a general
override of the global operators).

These type aware operators can be declared as either free functions or
in class, and can be specified with or without the other implicit
parameters, with overload resolution performed according to the existing
standard parameter prioritisation, only with type parameterised
operators having higher precedence than non-type aware operators. The
only exception is destroying_delete which for reasons discussed in the
paper we do not support type-aware variants by default.
2025-04-10 17:13:10 -07:00
Tim Clephas
c893f1d18d
Treat ipynb as json (#135137)
Fixes #110727
2025-04-10 20:40:43 +02:00
Matheus Izvekov
3954d258a5
[clang] Improved canonicalization for template specialization types (#135119)
This changes the TemplateArgument representation to hold a flag
indicating whether a template argument of expression type is supposed to
be canonical or not.

This gets one step closer to solving
https://github.com/llvm/llvm-project/issues/92292

This still doesn't try to unique as-written TSTs. While this would
increase the amount of memory savings and make code dealing with the AST
more well-behaved, profiling template argument lists is still too
expensive for this to be worthwhile, at least for now. Without this
uniquing, this patch stands neutral in terms of performance impact.

This also fixes the context creation of TSTs, so that they don't in some
cases get incorrectly flagged as sugar over their own canonical form.
This is captured in the test expectation change of some AST dumps.

This fixes some places which were unnecessarily canonicalizing these
TSTs.
2025-04-10 14:23:02 -03:00
Aaron Ballman
5c8ba28c75
[C11] Implement WG14 N1285 (temporary lifetimes) (#133472)
This feature largely models the same behavior as in C++11. It is
technically a breaking change between C99 and C11, so the paper is not
being backported to older language modes.

One difference between C++ and C is that things which are rvalues in C
are often lvalues in C++ (such as the result of a ternary operator or a
comma operator).

Fixes #96486
2025-04-10 08:12:14 -04:00
Aaron Ballman
543e5f5842
Fix crash with align_value diagnostic reporting (#135013)
We were passing the address of a local variable to a call to Diag()
which then tried to use the object after its lifetime ended, resulting
in crashes. We no longer pass the temporary object any longer.

Fixes #26612
2025-04-09 12:39:03 -04:00
Oleksandr T.
9f463056e6
[Clang] add ext warning for missing return in 'main' for C89 mode (#134617)
Fixes #21650 

--- 

Clang currently inserts an implicit `return 0;` in `main()` when
compiling in `C89` mode, even though the `C89` standard doesn't require
this behavior. This patch changes that behavior by emitting a warning
instead of silently inserting the implicit return under `-pedantic`.
2025-04-08 23:21:53 +03:00
Aaron Ballman
9ba1a3fcb5
Reject invalid integer constants in unevaluated preprocessor operands (#134884)
Clang was previously accepting invalid code like:
```
  #if 1 ? 1 : 999999999999999999999
  #endif
```
because the integer constant (which is too large to fit into any
standard or extended integer type) was in an unevaluated branch of the
conditional operator. Similar invalid code involving || or && was also
accepted and is now rejected.

Fixes #134658
2025-04-08 14:25:59 -04:00
Matheus Izvekov
d057811655
[clang] fix diagnostic printing of expressions ignoring LangOpts (#134693)
Currently when printing a template argument of expression type, the
expression is converted immediately into a string to be sent to the
diagnostic engine, unsing a fake LangOpts.

This makes the expression printing look incorrect for the current
language, besides being inneficient, as we don't actually need to print
the expression if the diagnostic would be ignored.

This fixes a nastiness with the TemplateArgument constructor for
expressions being implicit, and all current users just passing an
expression to a diagnostic were implicitly going through the template
argument path.

The expressions are also being printed unquoted. This will be fixed in a
subsequent patch, as the test churn is much larger.
2025-04-07 23:19:32 -03:00
Matheus Izvekov
954ccee5d5
[clang] fix partial ordering of NTTP packs (#134461)
This fixes partial ordering of pack expansions of NTTPs, by procedding
with the check using the pattern of the NTTP through the rules of the
non-pack case.

This also unifies almost all of the different versions of
FinishTemplateArgumentDeduction (except the function template case).
This makes sure they all follow the rules consistently, instantiating
the parameters and comparing those with the argument.

Fixes #132562
2025-04-07 12:30:51 -03:00
Aaron Ballman
b9ec68431b
Correctly diagnose incomplete arrays with static storage in C (#134374)
A file scope declaration without an initializer which is neither extern
nor thread_local is a tentative definition. If the declaration of an
identifier for an object is a tentative definition and has internal
linkage, the declared type shall not be an incomplete type.

Clang was previously failing to diagnose this in -pedantic mode.

Fixes #50661

---------

Co-authored-by: Mariya Podchishchaeva <mariya.podchishchaeva@intel.com>
2025-04-07 07:12:33 -04:00
Aaron Ballman
a2d983cffb
Improve diagnostic wording for invalid callback attribute uses (#134423)
We were previously telling the user how many arguments were passed to
the attribute rather than saying how many arguments were expected to be
passed to the callback function. This rewords the diagnostic to
hopefully be a bit more clear.

Fixes #47451
2025-04-04 15:01:36 -04:00
Aaron Ballman
b6b0257972
Fix the signature for __builtin___clear_cache (#134376)
The signature was changed from void(char *, char *) to void(void *, void
*) to match GCC's signature for the same builtin.

Fixes #47833
2025-04-04 13:53:45 -04:00
Matheus Izvekov
49fd0bf35d
[clang] support pack expansions for trailing requires clauses (#133190) 2025-04-03 12:36:15 -03:00
Julian Brown
c1ada72b09
[OpenMP] Mark 'map-type modifiers in arbitrary position' done (#133906)
I think #90499 already implements support for the listed OpenMP 6.0
feature mentioned in the title. This patch just marks it done (for
C/C++).
2025-04-03 16:34:35 +01:00
GeorgeKA
7145ead280
[Clang] Add warning message for C++17 alias template CTAD (#133806)
Alias template class template argument deduction is a documented C++20
feature. C++17 also happens to support it, but there is no message
output to indicate the officially supported version. This PR adds that.

Also updated relevant CTAD test cases.

Closes #125913
2025-04-03 15:58:52 +02:00
Alex Bradbury
2a9948f038
Revert "[CLANG-CL] ignores wpadded" (#134239)
Reverts llvm/llvm-project#130182

This is causing failures on RISC-V and ppc builders as mentioned on
https://github.com/llvm/llvm-project/pull/130182#issuecomment-2775516899

Reverting so the issue can be fixed by the path author without time pressure (as noted in that PR, it seems a value is uninitialised).
2025-04-03 13:47:59 +01:00
Aaron Ballman
7febd78f1e
No longer diagnose __auto_type as the auto extension (#134129)
Given:

  __auto_type x = 12;
  decltype(auto) y = 12;

-Wc++98-compat would diagnose both x and y with:

'auto' type specifier is incompatible with C++98

This patch silences the diagnostic in those cases. decltype(auto) is
still diagnosed with:

'decltype(auto)' type specifier is incompatible with C++ standards
before C++14

as expected but no longer produces the extraneous diagnostic about use
of 'auto'.

Fixes #47900
2025-04-03 07:13:30 -04:00
Ankur Ahir
fb7135ec52
[Clang] fixed clang frontend crash with friend class declaration and overload == (#133878) 2025-04-03 09:11:27 +08:00
Theo de Magalhaes
76fa9530c9
[clang] add support for -Wpadded on Windows (#130182)
Implements the -Wpadded warning for --target=x86_64-windows-msvc etc.

Fixes #61702 .
2025-04-02 14:46:58 -07:00
Juan Manuel Martinez Caamaño
0375ef07c3
[Clang][AMDGPU] Add __builtin_amdgcn_cvt_off_f32_i4 (#133741)
This built-in maps to `V_CVT_OFF_F32_I4` which treats its input as
a 4-bit signed integer and returns `0.0625f * src`.

SWDEV-518861
2025-04-02 19:51:40 +02:00
Lyle Dean
a0b75b9d99
[Clang] add emit -Wignored-base-class-qualifiers diagnostic for cv-qualified base classes (#132116)
Split diagnosing base class qualifiers from the ``-Wignored-Qualifiers``
diagnostic group into a new ``-Wignored-base-class-qualifiers``
diagnostic group (which is grouped under ``-Wignored-qualifiers``).

Fixes #131935
2025-04-02 07:31:42 -04:00
Aaron Ballman
574e43dffd
[C23] Allow casting from a null pointer constant to nullptr_t (#133742)
C23 allows a cast of a null pointer constant to nullptr_t. e.g.,
(nullptr_t)0 or (nullptr_t)(void *)0.

Fixes #133644
2025-04-02 07:28:45 -04:00
Aaron Ballman
53f3031005
[C99] Fix definitions of INTn_C macros (#133916)
C99 introduced macros of the form `INTn_C(v)` which expand to a signed
or unsigned integer constant with the specified value `v` and the type
`int_leastN_t`. Clang's initial implementation of these macros used
token pasting to form the integer constants, but this means that users
cannot define a macro named `L`, `U`, `UL`, etc before including
`<stdint.h>` (in freestanding mode, where Clang's header is being used)
because that could form invalid token pasting results. The new
definitions now use the predefined `__INTn_C` macros instead of using
token pasting. This matches the behavior of GCC.

Fixes #85995
2025-04-02 07:21:15 -04:00
Matheus Izvekov
ad1ca5f4a2
[clang] Concepts: support pack expansions for type constraints (#132626)
This reverts an earlier attempt
(adb0d8ddceb143749c519d14b8b31b481071da77 and
50e5411e4247421fd606f0a206682fcdf0303ae3) to support these expansions,
which was limited to type arguments and which subverted the purpose
of SubstTemplateTypeParmType.

This propagates the ArgumentPackSubstitutionIndex along with the
AssociatedConstraint, so that the pack expansion works, without
needing any new transforms or otherwise any changes to the template
instantiation process.

This keeps the tests from the reverted commits, and adds a few more
showing the new solution also works for NTTPs.

Fixes https://github.com/llvm/llvm-project/issues/131798
2025-04-01 21:11:56 -03:00
Matheus Izvekov
dc17429ae6
[clang] improved preservation of template keyword (#133610) 2025-04-01 17:15:18 -03:00
Aaron Ballman
41b83b48e3
No longer assert on incorrect attribute argument index (#133766)
Fixes an assertion when referencing an out-of-bounds parameter via a
function attribute whose argument list refers to parameters by index and
the function is variadic. e.g.,

    __attribute__ ((__format_arg__(2))) void test (int i, ...) { }

Fixes #61635
2025-04-01 07:56:36 -04:00
offsetof
b0a7906517
[clang] Fix crash on invalid std::initializer_list<T> template-id (#132284)
In `Sema::BuildStdInitializerList`, check that the synthesized
template-id `std::initializer_list<T>` is valid (which might not be the
case if the template has associated constraints or subsequent parameters
with default arguments) before forming the type.

Fixes #132256
2025-04-01 12:44:10 +02:00
Phoebe Wang
c7572ae213
[X86][AVX10] Re-target mavx10.1 and emit warning for mavx10.x-256/512 and m[no-]evex512 (#132542)
The 256-bit maximum vector register size control was removed from AVX10
whitepaper, ref: https://cdrdv2.intel.com/v1/dl/getContent/784343

- Re-target m[no-]avx10.1 to enable AVX10.1 with 512-bit maximum vector
register size;
- Emit warning for mavx10.x-256, noting AVX10/256 is not supported;
- Emit warning for mavx10.x-512, noting to use m[no-]avx10.x instead;
- Emit warning for m[no-]evex512, noting AVX10/256 is not supported;

This patch only changes Clang driver behavior. The features
avx10.x-256/512 keep unchanged and will be removed in the next release.
2025-03-31 22:05:50 +08:00
Mariya Podchishchaeva
842b57b775
Reland [MS][clang] Add support for vector deleting destructors (#133451)
Whereas it is UB in terms of the standard to delete an array of objects
via pointer whose static type doesn't match its dynamic type, MSVC
supports an extension allowing to do it.
Aside from array deletion not working correctly in the mentioned case,
currently not having this extension implemented causes clang to generate
code that is not compatible with the code generated by MSVC, because
clang always puts scalar deleting destructor to the vftable. This PR
aims to resolve these problems.

It was reverted due to link time errors in chromium with sanitizer
coverage enabled,
which is fixed by https://github.com/llvm/llvm-project/pull/131929 .

The second commit of this PR also contains a fix for a runtime failure
in chromium reported
in
https://github.com/llvm/llvm-project/pull/126240#issuecomment-2730216384
.

Fixes https://github.com/llvm/llvm-project/issues/19772
2025-03-31 10:03:39 +02:00
Owen Pan
e5fcbfa2aa
[clang-format] Add an option for editing enum trailing commas (#133576)
Also refactor the code that removes/replaces a token.
2025-03-30 16:02:49 -07:00
Alcaro
be7f185ca1
[NFC][analyzer] Fix typo in VirtualCall checker docs (#133593) 2025-03-29 21:34:15 +01:00
Qinkun Bao
0cd82327ff
Fix some typos (NFC) (#133558) 2025-03-29 20:54:15 +01:00
Qiu Chaofan
b3f01a6aa4
[Clang] Check PP presence when printing stats (#131608)
Front-end option `-print-stats` can be used to print statistics around
the compilation process. But clang with this options will crash when
input is IR file. This patch fixes the crash by checking preprocessor
presence before invoking it.
2025-03-29 01:54:14 +08:00
Paul Schwabauer
8a3fe30a0a
[PATCH] [clang][frontend] Fix serialization for CXXConstructorDecl (refs llvm#132794) (#133077)
When retrieving the ExplicitSpecifier from a CXXConstructorDecl, one of
its canonical declarations is returned. To correctly write the
declaration record the ExplicitSpecifier of the current declaration must
be used.

Failing to do so results in a crash during deserialization.
2025-03-28 11:34:24 +01:00
cor3ntin
ae54f476f7
[Clang] Improve subsumption. (#132849)
The main goal of this patch is to improve the
performance of concept subsumption by

- Making sure literal (atomic) clauses are de-duplicated (Whether 2
atomic constraint is established during the initial normal form
production).
 - Eagerly removing duplicated clauses.

This should minimize the risks of exponentially large formulas that can
be produced by a naive {C,D}NF transformation.

While at it, I restructured that part of the code to be a bit clearer.

Subsumption of fold expanded constraint is also cached.

---

Note that removing duplicated clauses seems to be necessary and
sufficient to have acceptable performance on anything that could be
construed as reasonable code.

Ultimately, the number of clauses is always going to be fairly small
(but $2^{fairly\ small}$ is quickly *fairly large*..).

I went too far in the rabbit hole of Tseitin transformations etc, which
was much faster but would then require to check satisfiabiliy to
establish subsumption between some constraints (although it was good
enough to pass all but ones of our tests...).

It doesn't help that the C++ standard has a very specific definition of
subsumption that is really more of an implication...

While that sort of musing is fascinating, it was ultimately a fool's
errand, at least until such time that there is more motivation for a SAT
solver in clang (clang-tidy can after all use z3!).

Here be dragons.

Fixes #122581
2025-03-27 18:23:58 +01:00
Aaron Ballman
00c43ae235
[C2y] Implement WG14 N3369 and N3469 (_Countof) (#133125)
C2y adds the `_Countof` operator which returns the number of elements in
an array. As with `sizeof`, `_Countof` either accepts a parenthesized
type name or an expression. Its operand must be (of) an array type. When
passed a constant-size array operand, the operator is a constant
expression which is valid for use as an integer constant expression.

This is being exposed as an extension in earlier C language modes, but
not in C++. C++ already has `std::extent` and `std::size` to cover these
needs, so the operator doesn't seem to get the user enough benefit to
warrant carrying this as an extension.

Fixes #102836
2025-03-27 13:23:16 -04:00
Aaron Ballman
85c54a519f
[Docs] Document freestanding requirements (#132232)
This adds some initial documentation about freestanding requirements for
Clang. The most critical part of the documentation is spelling out that
a conforming freestanding C Standard Library is required; Clang will not
be providing the headers for <string.h> in C23 which expose a number of
symbols in freestanding mode.

The docs also make it clear that in addition to a conforming
freestanding C standard library, the library must provide some
additional symbols which LLVM requires.

These docs are not comprehensive, this is just getting the bare bones in
place so that they can be expanded later.

This also updates the C status page to make it clear that we don't have
anything to do for WG14 N2524 which adds string interfaces to
freestanding mode.
2025-03-27 13:17:05 -04:00
Younan Zhang
a9672515ce
[Clang] Correct the DeclRefExpr's Type after the initializer gets instantiated (#133212)
The instantiation of a VarDecl's initializer might be deferred until the
variable is actually used. However, we were still building the
DeclRefExpr with a type that could later be changed by the initializer's
instantiation, which is incorrect when incomplete arrays are involved.

Fixes #79750
Fixes #113936
Fixes #133047
2025-03-27 19:40:02 +08:00
Balázs Kéri
322b2fe61e
[clang][analyzer] Move 'alpha.core.FixedAddressDereference' out of alpha (#132404) 2025-03-26 09:24:18 +01:00
marius doerner
4067581aea
[clang] Placement new error when modifying consts (#132460)
Raise an error when placement new is used to modify a const-qualified
variable in a constexpr function.

Fixes #131432
2025-03-25 20:39:12 +01:00
Matheus Izvekov
dfb6c761f7
[clang] ASTContex: fix getCommonSugaredType for array types (#132559)
This corrects the behaviour for getCommonSugaredType with regards to
array top level qualifiers: remove differing top level qualifiers, as
they must be redundant with element qualifiers.

Fixes https://github.com/llvm/llvm-project/issues/97005
2025-03-25 15:13:50 -03:00
offsetof
5105ecc413
[clang] Refine handling of C++20 aggregate initialization (#131320)
* Move parts of `InitializationSequence::InitializeFrom` corresponding
to C++ [dcl.init.general] p16.6.1 and p16.6.2 into a separate
function,`TryConstructorOrParenListInitialization`
* Use it in `TryListInitialization` to implement [dcl.init.list] p3.2
* Fix parenthesized aggregate initialization being attempted in
copy-initialization contexts or when the constructor call is ambiguous

Co-authored-by: cor3ntin <corentinjabot@gmail.com>
2025-03-25 15:30:16 +01:00
Jannick Kremer
20fc2d5aa5
[libclang/python] Add some bindings to the Cursor interface (#132377)
Make Cursor hashable
Add `Cursor.has_attr()`
Add `Cursor.specialized_template`

This covers the Cursor interface changes added by #120590

---------

Co-authored-by: Mathias Stearn <redbeard0531@gmail.com>
2025-03-25 00:08:32 +01:00