Commit Graph

96 Commits

Author SHA1 Message Date
Tom Honermann
53b4689006
[Clang] Mark WG14 N2939 (Identifier Syntax Fixes) as available in Clang 15 (#74666)
WG14 N2939 (Identifier Syntax Fixes) corrects a grammar issue in the C
standard but does not otherwise change intended behavior. This change
updates the C23 status to note this paper as implemented as of Clang 15;
the release in which support for N2836 (Identifier Syntax using Unicode
Standard Annex 31) was implemented.
2023-12-07 12:40:46 -05:00
Aaron Ballman
8bd06d5b65
[C23] Complete support for WG14 N2508 (#71398)
In Clang 16, we implemented the ability to add a label at the end of a
compound statement. These changes complete the implementation by
allowing a label to be followed by a declaration in C.

Note, this seems to have fixed an issue with some OpenMP stand-alone
directives not being properly diagnosed as per:
https://www.openmp.org/spec-html/5.1/openmpsu19.html#x34-330002.1.3
(The same requirement exists in OpenMP 5.2 as well.)
2023-11-20 10:52:11 -05:00
Jerin Philip
2e7913342e
[clang] Improve _Alignas on a struct declaration diagnostic (#65638)
Fixes https://github.com/llvm/llvm-project/issues/58637. 

Adds `isAlignas()` method on `AttributeCommonInfo` which accounts for
C++ `alignas` as well as C11 `_Alignas`.

The method is used to improve diagnostic in C when `_Alignas` is used in
C at the wrong location.  This corrects the previously suggested move
of `_Alignas` past the declaration specifier, now warns attribute
`_Alignas` is ignored.

Based on https://reviews.llvm.org/D141177.
2023-11-07 08:27:41 -05:00
Jessica Clarke
f9ead46931
[AST] Only dump desugared type when visibly different (#65214)
These are an artifact of how types are structured but serve little
purpose, merely showing that the type is sugared in some way. For
example, ElaboratedType's existence means struct S gets printed as
'struct S':'struct S' in the AST, which is unnecessary visual clutter.
Note that skipping the second print when the types have the same string
matches what we do for diagnostics, where the aka will be skipped.
2023-10-26 19:28:28 +01:00
zijunzhao
3694697003 [clang] Implement C23 <stdckdint.h>
https://github.com/llvm/llvm-project/issues/62248

Reviewed By: yabinc, aaron.ballman, #clang-language-wg

Differential Revision: https://reviews.llvm.org/D157331
2023-10-17 00:51:21 +00:00
Guillot Tony
d5444ab267
[clang][C2x] Remove confusing diagnostic auto storage class specifier (#68710)
When declaring `auto int` at local or file scope, we emit a warning
intended for C++11 and later, which is incorrect and confusing in C23.
See [Godbolt example](https://godbolt.org/z/j1acGhecd).
Now this diagnostic does not show up in C23.
2023-10-11 09:51:46 +02:00
Guillot Tony
13ffc61a42 [C2X] N3007 Type inference for object definitions
Re-landing 5d78b78c85 which was reverted.

This patches implements the auto keyword from the N3007 standard
specification.
This allows deducing the type of the variable like in C++:
```
auto nb = 1;
auto chr = 'A';
auto str = "String";
```
The list of statements which allows the usage of auto:

    * Basic variables declarations (int, float, double, char, char*...)
    * Macros declaring a variable with the auto type

The list of statements which will not work with the auto keyword:

    * auto arrays
    * sizeof(), alignas()
    * auto parameters, auto return type
    * auto as a struct/typedef member
    * uninitialized auto variables
    * auto in an union
    * auto as a enum type specifier
    * auto casts
    * auto in an compound literals

Differential Revision: https://reviews.llvm.org/D133289
2023-10-05 12:16:00 -04:00
Aaron Ballman
dc1000d5b2 Revert "[C2X] N3007 Type inference for object definitions"
This reverts commit 5d78b78c85.

Reverting due to the failure found by:
https://lab.llvm.org/buildbot/#/builders/245/builds/14999
2023-10-05 08:52:12 -04:00
Guillot Tony
5d78b78c85 [C2X] N3007 Type inference for object definitions
This patches implements the auto keyword from the N3007 standard
specification.
This allows deducing the type of the variable like in C++:
```
auto nb = 1;
auto chr = 'A';
auto str = "String";
```
The list of statements which allows the usage of auto:

    * Basic variables declarations (int, float, double, char, char*...)
    * Macros declaring a variable with the auto type

The list of statements which will not work with the auto keyword:

    * auto arrays
    * sizeof(), alignas()
    * auto parameters, auto return type
    * auto as a struct/typedef member
    * uninitialized auto variables
    * auto in an union
    * auto as a enum type specifier
    * auto casts
    * auto in an compound literals

Differential Revision: https://reviews.llvm.org/D133289
2023-10-05 08:11:02 -04:00
Aaron Ballman
7b8f5f7df7 No longer hang on typeof of a function type
We were calling `isFunctionProtoType()` on a `ParsedType` rather than
creating a valid semantic type first and calling the function on that.
The call to `isFunctionProtoType()` would eventually call
`getUnqualifiedDesugaredType()`, which loops indefinitely until we get
a desugared type and a `ParsedType` will never finish desugaring.

Fixes https://github.com/llvm/llvm-project/issues/64713
2023-08-17 07:15:30 -04:00
Aaron Ballman
9c4ade0623 [C23] Rename C2x->C23 in diagnostics
This renames C2x to C23 in diagnostic identifiers and messages. The
changes were made mechanically.
2023-08-11 08:42:01 -04:00
Aaron Ballman
13629b1408 [C2x] Support -std=c23 and -std=gnu23
C2x was finalized at the June 2023 WG14 meeting. The DIS is out for
balloting and the comment period for that closes later this year/early
next year. While that does leave an opportunity for more changes to the
standard during the DIS ballot resolution process, only editorial
changes are anticipated and as a result, the C committee considers C2x
to be final. The committee took a straw poll on what we'd prefer the
informal name of the standard be, and we decided it should be called
C23 regardless of what year ISO publishes it.

However, because the final publication is not out, this patch does not
add the language standard alias for the -std=iso9899:<year> spelling of
the standard mode; that will be added once ISO finally publishes the
document and the year chosen will match the publication date.

This also changes the value of __STDC_VERSION__ from the placeholder
value 202000L to the final value 202311L.

Subsequent patches will start renaming things from c2x to c23, cleaning
up documentation, etc.

Differential Revision: https://reviews.llvm.org/D157606
2023-08-10 13:57:40 -04:00
Shivam Gupta
a845252337 Revert "[Clang] Fix -Wconstant-logical-operand when LHS is a constant"
This reverts commit dfdfd306cf.

An issue is reported for wrong warning, this has to be reconsidered.

Differential Revision: https://reviews.llvm.org/D157352
2023-08-08 08:00:02 +05:30
Shivam Gupta
dfdfd306cf [Clang] Fix -Wconstant-logical-operand when LHS is a constant
This fix PR37919

The below code produces -Wconstant-logical-operand for the first statement,
but not the second.

void foo(int x) {
if (x && 5) {}
if (5 && x) {}
}

Reviewed By: nickdesaulniers

Differential Revision: https://reviews.llvm.org/D142609
2023-07-19 12:12:11 +05:30
Martin Storsjö
c2b256a990 Reapply [clang] [test] Narrow down MSVC specific behaviours from "any windows" to only MSVC/clang-cl
This fixes running tests with a toolchain that defaults to a MinGW
target.

After the previous attempt with this patch, this is now changed to
use !defined(__MINGW32__) instead of defined(_MSC_VER) to distinguish
between MSVC and MinGW mode; Clang doesn't define _MSC_VER when invoked
with "clang -cc1" as some of those tests do.

Differential Revision: https://reviews.llvm.org/D149997
2023-05-16 10:41:30 +03:00
Nico Weber
56bac6c87a Revert "[clang] [test] Narrow down MSVC specific behaviours from "any windows" to only MSVC/clang-cl"
This reverts commit 7f037e5645.
Breaks tests, see https://reviews.llvm.org/D149997#4331937
2023-05-10 16:49:18 +02:00
Martin Storsjö
7f037e5645 [clang] [test] Narrow down MSVC specific behaviours from "any windows" to only MSVC/clang-cl
This fixes running tests with a toolchain that defaults to a MinGW
target.

Differential Revision: https://reviews.llvm.org/D149997
2023-05-10 01:07:37 +03:00
Aaron Ballman
9bb28a18d9 [C2x] Update 'nullptr' implementation based on CD comments
We filed some CD ballot comments which WG14 considered during the
ballot comment resolution meetings in Jan and Feb 2023, and this
updates our implementation based on the decisions reached. Those
decisions were (paraphrased for brevity):

US 9-034 (REJECTED)
  allow (void *)nullptr to be a null pointer constant
US 10-035 (ACCEPTED)
  accept the following code, as in C++:
  void func(nullptr_t); func(0);
US 22-058 (REJECTED)
  accept the following code, as in C++:
  nullptr_t val; (void)(1 ? val : 0); (void)(1 ? nullptr : 0);
US 23-062 (REJECTED)
  reject the following code, as in C++:
  nullptr_t val; bool b1 = val; bool b2 = nullptr;
US 24-061 (ACCEPTED)
  accept the following code, as in C++:
  nullptr_t val; val = 0;
US 21-068 (ACCEPTED)
  accept the following code, as in C++:
  (nullptr_t)nullptr;
GB-071 (ACCEPTED)
  accept the following code, as in C++:
  nullptr_t val; (void)(val == nullptr);

This patch updates the implementation as appropriate, but is primarily
focused around US 10-035, US 24-061, and US 23-062 in terms of
functional changes.

Differential Revision: https://reviews.llvm.org/D148800
2023-05-03 14:50:15 -04:00
Aaron Ballman
78d0d59209 Add some additional comments to this test; NFC
It isn't immediately obvious why that code should be accepted given the
wording of C2x 6.7.10p4, so this adds a comment explaining that there
is an existing extension to support zero-sized arrays in C, and that
empty initialization of an unbounded array in C++ deduces the array
extent as zero, so C is exposing the same extension as in C++.
2023-04-05 13:15:34 -04:00
Aaron Ballman
5d8aaad445 [C2x] Implement support for empty brace initialization (WG14 N2900 and WG14 N3011)
This implements support for allowing {} to consistently zero initialize
objects. We already supported most of this work as a GNU extension, but
the C2x feature goes beyond what the GNU extension allowed.

The changes in this patch are:

* Removed the -Wgnu-empty-initializer warning group. The extension is
  now a C2x extension warning instead. Note that use of
  `-Wno-gnu-empty-initializer seems` to be quite low in the wild
(https://sourcegraph.com/search?q=context%3Aglobal+-file%3A.*test.*+%22-Wno-gnu-empty-initializer%22&patternType=standard&sm=1&groupBy=repo
  which currently only gives 8 hits total), so this is not expected to
  be an overly disruptive change. But I'm adding the clang vendors
  review group just in case this expectation is wrong.
* Reworded the diagnostic wording to be about a C2x extension, added a
  pre-C2x compat warning.
* Allow {} to zero initialize a VLA

This functionality is exposed as an extension in all older C modes
(same as the GNU extension was), but does *not* allow the extension for
VLA initialization in C++ due to concern about handling non-trivially
constructible types.

Differential Revision: https://reviews.llvm.org/D147349
2023-04-03 15:22:52 -04:00
Aaron Ballman
d2b768bba3 [C2x] Add test coverage for WG14 N2607
This adds test coverage for N2607, which makes arrays and their
elements identically qualified. Clang already implements much of the
functionality from this paper, but is still missing some support.

It also adds some details to the C status page so users have this
information as well.
2023-03-08 14:59:53 -05:00
Aaron Ballman
34d6a6e23a Fix bots by adding a triple to the test
Resolves the issue found by:
https://lab.llvm.org/buildbot/#/builders/245/builds/5384
2023-03-03 12:26:52 -05:00
Aaron Ballman
637ce0f713 [C2x] Claim support for WG14 N2838
This paper clarifies that complete object types need to be smaller than
SIZE_MAX. We already conformed to that requirement, so this adds some
test coverage to prove it.
2023-03-03 11:57:21 -05:00
Aaron Ballman
5680b75703 [C2x] Add tests for WG14 N3035 and update the C status page
The paper was making minor corrections to the standard that Clang had
already implemented. This adds (most of) the test coverage for the
paper and claims support. In my testing, we supported this in Clang 15.
2023-03-02 14:36:50 -05:00
Fanbo Meng
be646ef392 Make clang/test/C/C2x/n2934.c compatible with targets that do not support thread_local storage.
Add an optional error check to test case for it to pass on targets that do not support thread_local storage.

Reviewed By: aaron.ballman, abhina.sreeskantharajan

Differential Revision: https://reviews.llvm.org/D145158
2023-03-02 11:18:19 -05:00
Aaron Ballman
06174134e4 [C2x] Implement support for revised spelling of keywords
This implements WG14 N2934
(https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2934.pdf), which
adds keywords for alignas, alignof, bool, static_assert, and
thread_local in C, as aliases for _Alignas, _Alignof, _Bool,
_Static_assert, and _Thread_local. We already supported the keywords in
C2x mode, but this completes support by adding pre-C2x compat warnings
and updates the stdalign.h header in freestanding mode.
2023-02-25 09:30:10 -05:00
Aaron Ballman
898c673e08 [C2x] Remove the ATOMIC_VAR_INIT macro from stdatomic.h
This implements WG14 N2886 (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2886.htm)
which removed the macro entirely. (NB the macro was deprecated in C17.)
As the paper is not particularly clear on what alternative was picked,
here are my notes from the May 2022 meeting:

Does WG14 wish to adopt variant 1, change 3.2, 3.3, and 3.4 from N2886
into C23?
14/2/2 (consensus)
Does WG14 want to exchange Variant 1 with Variant 2 in N2886 in C23?
9/3/6 (consensus)

(There was no sentiment in the room for either Variant 3 or Variant 4
so those were not voted on.)

Does WG14 want to integrate change 3.5 in N2886 into C23?
8/1/9 (consensus)
Does WG14 want to integrate change 3.6 in N2886 into C23?
2/5/9 (no consensus)

Any code that is broken by the removal can remove the use of
ATOMIC_VAR_INIT and use regular initialization instead.

Differential Revision: https://reviews.llvm.org/D144196
2023-02-24 13:52:41 -05:00
Paul Robinson
32d441bfb4 [PS4/PS5] Specify no <complex.h> or <threads.h>
We've never provided these headers so set the preprocessor
toggles to reflect that.

Differential Revision: https://reviews.llvm.org/D144586
2023-02-23 07:09:30 -08:00
Aaron Ballman
a9797d7d2d [C2x] Implement the unreachable macro for WG14 N2826
This exposes __builtin_unreachable as the expansion for the unreachable
macro in C2x. I added this definition under __need_STDDEF_H_misc on the
assumption there is no need for a separate need macro to control adding
this.

Differential Revision: https://reviews.llvm.org/D143430
2023-02-15 14:43:01 -05:00
Aaron Ballman
63d6b8be6c Stop diagnosing member and array access in offsetof as an extension
This was a mistake from e7300e75b5
(https://reviews.llvm.org/D133574) caused by us accidentally tracking
an older copy of the C DR list for DR496. The text in
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2396.htm#dr_496 makes
it clear that subobjects are allowed, which means member and array
access expressions are allowed.

This backs out the changes from the previous commit that relate to this
diagnostic.
2023-01-27 11:01:58 -05:00
Aaron Ballman
fb9b3e9a5f Update the status of some C11 and C99 features we support
This updates the status of:

N505 Make qualifiers idempotent
N725 Integer promotion rules
N1311 Initializing static of external variables
2023-01-20 14:50:39 -05:00
Aaron Ballman
e7300e75b5 Diagnose extensions in 'offsetof'
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm made very
clear that it is an UB having type definitions with in offsetof.
Clang supports defining a type as the first argument as a conforming
extension due to how many projects use the construct in C99 and earlier
to calculate the alignment of a type. GCC also supports defining a type
as the first argument.

This adds extension warnings and documentation for the functionality
Clang explicitly supports.

Fixes #57065
Reverts the revert of 39da55e8f5

Co-authored-by: Yingchi Long <i@lyc.dev>
Co-authored-by: Aaron Ballman <aaron@aaronballman.com>

Differential Revision: https://reviews.llvm.org/D133574
2023-01-18 08:51:21 -05:00
Arthur Eubanks
39da55e8f5 Revert "Diagnose extensions in 'offsetof'"
This reverts commit f1f0a0d8e8.

Causes crashes on

$ echo 'typedef int a; void c() { __builtin_offsetof(struct {a b}, b); }' | bin/clang -cc1 -emit-llvm -o /dev/null - -x c
2023-01-17 22:35:21 -08:00
Aaron Ballman
f1f0a0d8e8 Diagnose extensions in 'offsetof'
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm made very
clear that it is an UB having type definitions with in offsetof.
Clang supports defining a type as the first argument as a conforming
extension due to how many projects use the construct in C99 and earlier
to calculate the alignment of a type. GCC also supports defining a type
as the first argument.

This adds extension warnings and documentation for the functionality
Clang explicitly supports.

Fixes #57065

Co-authored-by: Yingchi Long <i@lyc.dev>
Co-authored-by: Aaron Ballman <aaron@aaronballman.com>
2023-01-17 14:30:57 -05:00
Yingchi Long
596f76a799
Revert "[C2x] reject type definitions in offsetof"
This reverts commit e327b52766.
2023-01-16 16:52:50 +08:00
Yingchi Long
e327b52766
[C2x] reject type definitions in offsetof
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm made very
clear that it is an UB having type definitions with in offsetof. After
this patch clang will reject any type definitions in __builtin_offsetof.

Fixes https://github.com/llvm/llvm-project/issues/57065

```
local/offsetof.c:10:38: error: 'struct S' cannot be defined in '__builtin_offsetof'
    return __builtin_offsetof(struct S{ int a, b;}, a);
                                     ^
```

Reviewed By: aaron.ballman, #clang-language-wg

Differential Revision: https://reviews.llvm.org/D133574
2023-01-13 15:33:26 +08:00
Jordan Rupprecht
fc7b8e7131 [test] Fix dr324.c again for non-writeable source directories
In general, the source tree is not assumed to be writeable, so modifying `%s` does not work for all CI systems. Instead of touching `%s`, copy it to a writeable dir using `%t`, and touch it there.
Actually, `dr0xx.c` isn't really needed at all, so just create a new `dep.c` file in the build tree.

This was recently added in cb088e8c3a, fixed in 1481fcf780, and fixed again in d16c590130.
2022-12-12 10:11:19 -08:00
Chris Bieneman
d16c590130 Resolve possible test failure on Windows
If dr0xx.c's timestamp is newer than dr324.c the preprocessor will issue
an unexpected diagnostic in dr324.c. To prevent this diagnostic we can
have dr324.c run touch against itself to force its timestamp to be new.
2022-12-09 13:43:52 -06:00
Nico Weber
1481fcf780 [clang] Try to unbreak test/C/drs/dr324.c on Windows
The test uses

    #pragma GCC dependency "oops\..\dr0xx.c"

to test preprocessor "tokenization obscurities". However, that
pragma also emits a warning if the refered file is newer than the
current file -- and d694e2490a edited dr0xx.c, causing this warning
to fire.

As workaround, touch this file, so that it's newer than dr0xx.c again.
2022-12-09 08:57:28 -05:00
Aaron Ballman
d694e2490a Update the status of a few more C DRs
This includes tests for DR085 and DR259.
2022-12-08 13:19:29 -05:00
Aaron Ballman
e321c53f7b [C2x] Relaxing requirements for va_start
This implements WG14 N2975 relaxing requirements for va_start
(https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2975.pdf), which
does two things:

1) Allows the declaration of a variadic function without any named
arguments. e.g., void f(...) is now valid, as in C++.
2) Modified the signature of the va_start macro to be a variadic macro
that accepts one or more arguments; the second (and later) arguments
are not expanded or evaluated.

I followed the GCC implementation in terms of not modifying the
behavior of `__builtin_va_start` (it still requires exactly two
arguments), but this approach has led to several QoI issues that I've
documented with FIXME comments in the test. Specifically, the
requirement that we do not evaluate *or expand* the second and later
arguments means it's no longer possible to issue diagnostics for
compatibility with older C versions and C++. I am reaching out to
folks in WG14 to see if we can get an NB comment to address these
concerns (the US comment period has already closed, so I cannot file
the comment myself), so the diagnostic behavior may change in the
future.

I took this opportunity to add some documentation for all the related
builtins in this area, since there was no documentation for them yet.

Differential Revision: https://reviews.llvm.org/D139436
2022-12-08 07:36:07 -05:00
Aaron Ballman
2fc5a34100 Add test coverage for more C DRs
This completes the initial pass over all of the C DRs.
2022-11-29 14:44:37 -05:00
Aaron Ballman
7fc57d7c97 Add more tests for C DRs and update the status page 2022-11-18 14:15:41 -05:00
Matt Jacobson
bbe6bd724a Trivial fix to failing test on FreeBSD
This file can't use C99-style comments.
2022-11-17 00:20:23 -05:00
Aaron Ballman
a75879d9a9 Update the status of some C2x features
Only N2670 had testable changes in it, the rest can be trivially
assumed to be implemented as the changes are editorial.
2022-10-28 14:46:16 -04:00
Aaron Ballman
f1c9ef311f Add test coverage for WG14 N2322
The changes in this paper add a new recommended practice. I had
originally marked Clang as supporting this paper because we're not
obligated to follow a recommended practice. However, in retrospect, it
seems more useful to document whether we implement the recommendation
or not. This adds a test for those changes.
2022-10-28 12:45:56 -04:00
Aaron Ballman
76cf890984 Add some more C11 DR tests and update the status page 2022-10-28 08:17:03 -04:00
Aaron Ballman
5e676aa702 Add tests for C11 DRs and update the status page
This gets a start on testing how we're doing for C11 DRs
2022-10-26 13:53:50 -04:00
Aaron Ballman
6fcaaf89fe Whitespace fix in a comment; NFC 2022-10-25 12:35:35 -04:00
Aaron Ballman
5be51acbf0 Speculatively fix the test bots
This should hopefully fix the issues found on Linux hosts like:
https://lab.llvm.org/buildbot#builders/109/builds/49321
https://lab.llvm.org/buildbot/#/builders/139/builds/30061
2022-10-25 11:35:53 -04:00