225 Commits

Author SHA1 Message Date
Nikita Popov
c23b4fbdbb
[IR] Remove size argument from lifetime intrinsics (#150248)
Now that #149310 has restricted lifetime intrinsics to only work on
allocas, we can also drop the explicit size argument. Instead, the size
is implied by the alloca.

This removes the ability to only mark a prefix of an alloca alive/dead.
We never used that capability, so we should remove the need to handle
that possibility everywhere (though many key places, including stack
coloring, did not actually respect this).
2025-08-08 11:09:34 +02:00
Aaron Ballman
d2361e43d1
[C23] More improved type compatibility for enumerations (#150946)
The structural equivalence checker was not paying attention to whether
enumerations had compatible fixed underlying types or not.

Fixes #150594
2025-07-29 13:30:52 -04:00
Aaron Ballman
315e2e28b1
[C23] Handle type compatibility for enumerations better (#150282)
An enumeration is compatible with its underlying type, which means that
code like the following should be accepted:

  struct A { int h; };
  void func() {
    extern struct A x;
    enum E : int { e };
    struct A { enum E h; };
    extern struct A x;
  }

because the structures are declared in different scopes, the two
declarations of 'x' are both compatible.

Note, the structural equivalence checker does not take scope into
account, but that is something the C standard requires. This means we
are accepting code we should be rejecting per the standard, like:

  void func() {
    struct A { int h; };
    extern struct A x;
    enum E : int { e };
    struct A { enum E h; };
    extern struct A x;
  }

Because the structures are declared in the same scope, the type
compatibility rule require the structures to use the same types, not
merely compatible ones.

Fixes #149965
2025-07-29 08:20:59 -04:00
Timothy Herchen
8366dc207a
[clang] Don't warn on zero literals with -std=c2y (#149688)
Fixes #149669; the old check compared with the end of the literal, but
we can just check that after parsing digits, we're pointing to one
character past the token start.
2025-07-21 12:15:09 -04:00
Aaron Ballman
a2246eebca
[C23] Accept an _Atomic underlying type (#147802)
The underlying type of an enumeration is the non-atomic, unqualified
version of the specified type. Clang was rejecting such enumerations,
with a hard error, but now has the ability to downgrade the error into a
warning. Additionally, we diagnose (as a warning) dropping other
qualifiers. _Atomic is special given that an atomic type need not have
the same size as its non-atomic counterpart, and that the C++ version
of <stdatomic.h> defines _Atomic to std::atomic for easing cross-
language atomic use and std::atomic is an invalid enum base in C++.
(Note: we expose _Atomic in C++ even without including
<stdatomic,h>.)

Fixes #147736
2025-07-11 07:28:03 -04:00
Oleksandr T.
2e8e254d18
[Clang] include attribute scope in diagnostics (#144619)
This patch updates diagnostics to print fully qualified attribute names,
including scope when present.
2025-07-08 11:36:52 +03:00
yronglin
bd6ee6ac21
[C23][Parser] Accept single variadic parameter function declarator in type name (#145362)
Fixs: https://github.com/llvm/llvm-project/issues/145250.

This PR makes clang accept single variadic parameter function declarator
in type name.
Eg.
```c
int va_fn(...); // ok

// As typeof() argument
typeof(int(...))*fn_ptr = &va_fn;  // ok

// As _Generic association type
int i = _Generic(typeof(va_fn), int(...):1); // ok
```

Signed-off-by: yronglin <yronglin777@gmail.com>
2025-06-24 09:40:01 +08:00
Guillot Tony
b896d262eb
[C23][N3006] Documented behavior of underspecified object declarations (#140911)
This PR is documenting the behavior of Clang towards underspecified
object declarations in C23 as advised by @AaronBallman.
2025-06-09 11:02:20 -04:00
Naveen Seth Hanig
f482b9677e
[C2y] Handle FP-suffixes on prefixed octals (#141230) (#141695)
Fixes https://github.com/llvm/llvm-project/issues/141230.

Currently, prefixed octal literals used with floating-point suffixes are
not
rejected, causing Clang to crash.
This adds proper handling to reject invalid literals such as `0o0.1` or
`0.0e1`.

No release note because this is fixing an issue with a new change.
2025-06-06 09:47:34 +02:00
Aaron Ballman
6769a836e9
[C23] Handle type compatibility of unnamed records (#141783)
At the top-level, both types need to have a tag in order for them to be
compatible within the same TU in C23. An unnamed structure has no tag,
so it cannot be compatible with another type within the TU.

Fixes #141724
2025-05-29 06:59:34 -04:00
Aaron Ballman
5ab944a8c6
[C2y] Add stdcountof.h (#140890)
WG14 N3469 changed _Lengthof to _Countof but it also introduced the
<stdcountof.h> header to expose a macro with a non-ugly identifier. GCC
vends this header as part of the compiler implementation, so Clang
should do the same.

Suggested-by: Alejandro Colomar <alx@kernel.org>
2025-05-28 06:41:01 -04:00
Aaron Ballman
d1a6327b2b
[C2y] Fix _Countof handling of VLAs (#141621)
It turns out that getVLASize() does not get you the size of a single
dimension of the VLA, it gets you the full count of all elements. This
caused _Countof to return invalid values on VLA ranks. Now switched to
using getVLAElements1D() instead, which only gets a single dimension.

Fixes #141409
2025-05-27 14:59:30 -04:00
Aaron Ballman
cc5237c7af
[C2y] Correctly handle incomplete types in generic selections (#141596)
We were emitting a non-error diagnostic but claiming we emitted an
error, which caused some obvious follow-on problems.

Fixes #141549
2025-05-27 11:29:30 -04:00
Alejandro Colomar
4ff9db68a3
Add more tests for _Countof (#133333)
Link: <https://github.com/llvm/llvm-project/issues/102836>
Link: <https://github.com/llvm/llvm-project/pull/133125>
2025-05-07 13:33:40 -04:00
Aaron Ballman
15f7e02940
[C23] Disable diagnostic on struct defn in prototype (#138516)
Thanks to changes to type compatibility rules via WG14 N3007, these
functions can now be called with a compatible type even within the same
TU, which makes the -Wvisibility diagnostic too chatty to have on by
default.

So in C23 mode, -Wvisibility will only diagnose an incomplete tag type
declared in a function prototype. If the tag is defined in the
prototype, the diagnostic is silenced.
2025-05-05 10:40:05 -04:00
Aaron Ballman
a4ceac7e3e
[C23] Implement WG14 N3037 (#132939)
This changes the type compatibility rules so that it is permitted to
redefine tag types within the same TU so long as they are equivalent
definitions.

It is intentionally not being exposed as an extension in older C
language modes. GCC does not do so and the feature doesn't seem
compelling enough to warrant it.
2025-05-02 09:19:21 -04:00
Aaron Ballman
f2b8539803
[C2y] Correctly handle 0 in the preprocessor (#137844)
We do not diagnose 0 as a deprecated octal literal outside of the
preprocessor. This fixes a bug where we were accidentally diagnosing 0
from a preprocessor conditional, however.

No release note because this is fixing an issue with a new change.
2025-04-30 06:55:20 -04:00
Aaron Ballman
576161cb60
[C] Warn on uninitialized const objects (#137166)
Unlike C++, C allows the definition of an uninitialized `const` object.
If the object has static or thread storage duration, it is still
zero-initialized, otherwise, the object is left uninitialized. In either
case, the code is not compatible with C++.

This adds a new diagnostic group, `-Wdefault-const-init-unsafe`, which
is on by default and diagnoses any definition of a `const` object which
remains uninitialized.

It also adds another new diagnostic group, `-Wdefault-const-init` (which
also enabled the `unsafe` variant) that diagnoses any definition of a
`const` object (including ones which are zero-initialized). This
diagnostic is off by default.

Finally, it adds `-Wdefault-const-init` to `-Wc++-compat`. GCC diagnoses
these situations under this flag.

Fixes #19297
2025-04-25 08:21:41 -04:00
Aaron Ballman
a84a6f7dd6 Fix this _Countof test for 32 vs 64-bits
This should address the issues found by:
https://lab.llvm.org/buildbot/#/builders/154/builds/14958
2025-04-17 07:36:48 -04:00
Aaron Ballman
4bd9b9e9ad Fix failing bot with changes to _Countof testing
This addresses the issue found by:
https://lab.llvm.org/buildbot/#/builders/190/builds/18484
2025-04-17 07:32:12 -04:00
Aaron Ballman
c536967af1 Split _Countof tests into two files; NFC
Post-commit review feedback during the language WG meeting requested
that I try to generalize the testing for this rather than only test on
a single target as we previously did.

The tests which are hard to generalize are the VLA tests, so those
still have specific triples in the RUN line, but have more coverage and
a comment explaining that the test should generalize to all targets.
2025-04-17 07:13:31 -04:00
Matheus Izvekov
fceb9cecdf
[clang] consistently quote expressions in diagnostics (#134769) 2025-04-15 04:18:23 -03:00
Aaron Ballman
5a1b4ec6f4 Fix broken bots
https://lab.llvm.org/buildbot/#/builders/190/builds/18038

This adds a triple and regenerates the test results.
2025-04-10 08:36:13 -04: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
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
Sirraide
50f0b30cff
[Clang] [Sema] Allow static assertions in the first part of a for loop in C (#134415)
No release note for this one because the one added by #129737 already
mentions ‘non-variable declarations’.

Fixes #56471.
2025-04-04 20:54:13 +02:00
Aaron Ballman
fb9deab74e Add additional test coverage for WG14 N3042
This addresses a post-commit request for some additional tests
2025-04-04 09:28:07 -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
Aaron Ballman
66b540d861
[C11] Claim conformance to WG14 N1518 (#133749)
This paper introduced ranges of valid start and continuation characters
for identifiers. C23 made further changes to these sets.
2025-04-01 11:45:56 -04:00
Aaron Ballman
4480f26e93 Fix failing test case for _Countof
Test just needs an explicit triple that was missed.
2025-03-27 14:07:57 -04: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
c65fa9163e
[C23] Fix compound literals within function prototype (#132097)
WG14 N2819 clarified that a compound literal within a function prototype
has a lifetime similar to that of a local variable within the function,
not a file scope variable.
2025-03-20 08:03:52 -04:00
Aaron Ballman
449cdfacc0
Suppress pedantic diagnostic for a file not ending in EOL (#131794)
WG14 added N3411 to the list of papers which apply to older versions of
C in C2y, and WG21 adopted CWG787 as a Defect Report in C++11. So we no
longer should be issuing a pedantic diagnostic about a file which does
not end with a newline character.

We do, however, continue to support -Wnewline-eof as an opt-in
diagnostic.
2025-03-19 07:49:16 -04:00
Aaron Ballman
9cf46fb230
[C2y] Add octal prefixes, deprecate unprefixed octals (#131626)
WG14 N3353 added support for 0o and 0O as octal literal prefixes. It
also deprecates use of octal literals without a prefix, except for the
literal 0.

This feature is being exposed as an extension in older C language modes
as well as in all C++ language modes.
2025-03-18 07:28:59 -04:00
Aaron Ballman
d781ac1cf0
[C23] Add __builtin_c23_va_start (#131166)
This builtin is supported by GCC and is a way to improve diagnostic
behavior for va_start in C23 mode. C23 no longer requires a second
argument to the va_start macro in support of variadic functions with no
leading parameters. However, we still want to diagnose passing more than
two arguments, or diagnose when passing something other than the last
parameter in the variadic function.

This also updates the freestanding <stdarg.h> header to use the new
builtin, same as how GCC works.

Fixes #124031
2025-03-15 11:01:53 -04:00
Aaron Ballman
cbbcc3d13b
[C2y] Claim conformance to WG14 N3460 (#131196)
This moves some Annex G requirements for complex numbers into the main
body of the standard.
2025-03-14 12:37:07 -04:00
Imad Aldij
b43620661d
[clang] Fix inaccurate wording of warn_second_arg_of_va_start_not_last_named_param (#131238)
Rename and update the wording of
`warn_second_arg_of_va_start_not_last_named_param` to best indicate that
it's actually the last non-variadic parameter instead.

Fixes #131171
2025-03-14 09:50:40 -04:00
Aaron Ballman
5af5fb0555 [C2y] Claim conformance to WG14 N3482
This paper makes it a constraint violation (instead of UB) to call
va_start from a function with a fixed (non-variadic) parameter list.
This is something Clang has always diagnosed as an error.
2025-03-13 13:59:17 -04:00
Aaron Ballman
ab53e1c8e5
[C2y] Claim conformance to WG14 N3363 (#130980)
This paper clarifies two things:
  * a call to va_start must be within the body of a variadic function,
  * the va_list arguments must be lvalues

Clang has correctly diagnosed the first point since Clang 5.0. Clang
generally diagnoses the second point, except for va_copy. However, the
second point is not a constraint violation, so a diagnostic is not
strictly required. That said, it would be good for us to properly
diagnose va_copy.
2025-03-12 14:38:54 -04:00
Aaron Ballman
456fa47bab [C2y] Claim conformance to WG14 N3505
This paper clarified that conditional inclusion in the preprocessor
requires the integer constant expression to be comprised only of
integer and character literals, punctuators, or implementation-defined
tokens.

This is something Clang has always required.
2025-03-11 15:04:11 -04:00
Aaron Ballman
51850db4bf [C2y] Claim conformance to WG14 N3451
This paper allows initialization of anonymous structures and unions via
braced initializers, which is something Clang has supported since
forever.
2025-03-10 15:25:59 -04:00
Aaron Ballman
0ea02e7721 [C2y] Claim nonconformance to WG14 N3410
This paper made it a constraint violation for the same identifier
within a TU to have both internal and external linkage. It was
previously UB.

Clang does not correctly diagnose the constraint in some cases,
documented in the added test case.
2025-03-07 15:15:53 -05:00
Aaron Ballman
b19ed9c043
[C2y] Implement WG14 N3409 (#130299)
This paper removes UB around use of void expressions. Previously, code
like this had undefined behavior:
```
  void foo(void) {
    (void)(void)1;
    extern void x;
    x;
  }
```
and this is now well-defined in C2y. Functionally, this now means that
it is valid to use `void` as a `_Generic` association.
2025-03-07 14:46:29 -05:00
Aaron Ballman
d1bd1c7e82 [C2y] Claim conformance to WG14 N3496
This was a clarification for C23's N2412, primarily about the
BOOL_WIDTH macro. It should expand to the number of bits in the value
representation, not the object representation. Clang 20 implements the
correct value.
2025-03-07 09:51:33 -05:00
Aaron Ballman
40ee5a0bcc [C2y] Claim conformance to WG14 N3481
This paper made it a constraint violation to have an incomplete type
during lvalue conversion. Previously, this was undefined behavior.

Clang has always diagnosed this for non-void incomplete types. However,
Clang does allow derefencing a void pointer (with a diagnostic, so we
still meet the conformance requirements), but not for a value
computation.
2025-03-07 09:37:15 -05:00
Aaron Ballman
9fc3310798
[C2y] Implement WG14 N3411 (#130180)
This paper (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3411.pdf)
allows a source file to end without a newline. Clang has supported this
as a conforming extension for a long time, so this suppresses the
diagnotic in C2y mode but continues to diagnose as an extension in
earlier language modes. It also continues to diagnose if the user passes
-Wnewline-eof explicitly.
2025-03-07 08:34:22 -05:00
Aaron Ballman
2c8b824ff5 [C2y] Claim conformance to WG14 N3478
This paper made it a constraint violation for a TU to end with an
unterminated multiline comment. This is something Clang has always
diagnosed as an error.
2025-03-07 08:31:56 -05:00
Aaron Ballman
f3effc2400 [C2y] Claim conformance to N3347
This paper made it a constraint violation to have a tentative
definition with internal linkage which is not completed by the end of
the translation unit.

This has been diagnosed as an error since at least Clang 3.0, so no
changes are needed.
2025-03-06 16:21:40 -05:00
Aaron Ballman
00770489e4
[C23] Fixed the value of BOOL_WIDTH (#117364)
The standard mandates that this returns the width of the type, which is
the number of bits in the value. For bool, that's required to be `1`
explicitly.

Fixes #117348
2024-11-25 13:01:25 -05:00