Commit Graph

65 Commits

Author SHA1 Message Date
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
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
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
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
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
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
Aaron Ballman
cb088e8c3a Add more C99 DR test cases and update the status page
This mostly completes the C99 set of DRs, though there are a few still
marked as "unknown".
2022-10-25 11:14:52 -04:00
Aaron Ballman
0de10a60af Fix a failing C DR test case found by post-commit CI
This should address the failure found by:
https://lab.llvm.org/buildbot/#/builders/231/builds/4152
2022-10-24 16:25:10 -04:00
Aaron Ballman
5635f005f9 Fix failing test case
This amends f43ef6b2dc with some warnings
that got dropped before I commit the test.
2022-10-24 15:19:41 -04:00
Aaron Ballman
f43ef6b2dc Update the status of more C99 DRs
This adds test coverage and updates the related entries for five more
C99 DRs.
2022-10-24 15:11:04 -04:00
Aaron Ballman
91babd3516 Update the status of a few more C99 DRs 2022-09-26 08:20:56 -04:00
Jun Zhang
e07ead85a3
[Clang] Warn when trying to dereference void pointers in C
Previously we only have an extension that warn void pointer deferencing
in C++, but for C we did nothing.

C2x 6.5.3.2p4 says The unary * operator denotes indirection. If it points
to an object, the result is an lvalue designating the object. However, there
is no way to form an lvalue designating an object of an incomplete type as
6.3.2.1p1 says "an lvalue is an expression (with an object type other than
void)", so the behavior is undefined.

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

Signed-off-by: Jun Zhang <jun@junz.org>

Differential Revision: https://reviews.llvm.org/D134461
2022-09-24 22:18:04 +08:00
Aaron Ballman
b63b4d8966 Add tests & update the C99 DR statuses for dr315 and dr316 2022-09-21 15:33:03 -04:00
Aaron Ballman
5d92d0b0f8 Correctly diagnose use of long long literals w/o a suffix
We would diagnose use of `long long` as an extension in C89 and C++98
modes when the user spelled the type `long long` or used the `LL`
literal suffix, but failed to diagnose when the literal had no suffix
but required a `long long` to represent the value.
2022-09-17 07:55:10 -04:00
Aaron Ballman
486a3c4662 Update the status of some more C DRs
Update some of the C99-era DRs starting in the 300s.
2022-08-06 11:53:40 -04:00
Muhammad Usman Shahid
76476efd68 Rewording "static_assert" diagnostics
This patch rewords the static assert diagnostic output. Failing a
_Static_assert in C should not report that static_assert failed. This
changes the wording to be more like GCC and uses "static assertion"
when possible instead of hard coding the name. This also changes some
instances of 'static_assert' to instead be based on the token in the
source code.

Differential Revision: https://reviews.llvm.org/D129048
2022-07-25 07:22:54 -04:00
Aaron Ballman
7068aa9841 Strengthen -Wint-conversion to default to an error
Clang has traditionally allowed C programs to implicitly convert
integers to pointers and pointers to integers, despite it not being
valid to do so except under special circumstances (like converting the
integer 0, which is the null pointer constant, to a pointer). In C89,
this would result in undefined behavior per 3.3.4, and in C99 this rule
was strengthened to be a constraint violation instead. Constraint
violations are most often handled as an error.

This patch changes the warning to default to an error in all C modes
(it is already an error in C++). This gives us better security posture
by calling out potential programmer mistakes in code but still allows
users who need this behavior to use -Wno-error=int-conversion to retain
the warning behavior, or -Wno-int-conversion to silence the diagnostic
entirely.

Differential Revision: https://reviews.llvm.org/D129881
2022-07-22 15:24:54 -04:00
Erich Keane
1da3119025 Revert "Rewording the "static_assert" to static assertion"
Looks like we again are going to have problems with libcxx tests that
are overly specific in their dependency on clang's diagnostics.

This reverts commit 6542cb55a3.
2022-07-21 06:40:14 -07:00
Muhammad Usman Shahid
6542cb55a3 Rewording the "static_assert" to static assertion
This patch is basically the rewording of the static assert statement's
output(error) on screen after failing. Failing a _Static_assert in C
should not report that static_assert failed. It’d probably be better to
reword the diagnostic to be more like GCC and say “static assertion”
failed in both C and C++.

consider a c file having code

_Static_assert(0, "oh no!");

In clang the output is like:

<source>:1:1: error: static_assert failed: oh no!
_Static_assert(0, "oh no!");
^              ~
1 error generated.
Compiler returned: 1

Thus here the "static_assert" is not much good, it will be better to
reword it to the "static assertion failed" to more generic. as the gcc
prints as:

<source>:1:1: error: static assertion failed: "oh no!"
    1 | _Static_assert(0, "oh no!");
          | ^~~~~~~~~~~~~~
          Compiler returned: 1

The above can also be seen here. This patch is about rewording
the static_assert to static assertion.

Differential Revision: https://reviews.llvm.org/D129048
2022-07-21 06:34:14 -07:00
Mitch Phillips
041d4012e4 Revert "Rewording "static_assert" diagnostics"
This reverts commit b7e77ff25f.

Reason: Broke sanitizer builds bots + libcxx. 'static assertion
expression is not an integral constant expression'. More details
available in the Phabricator review: https://reviews.llvm.org/D129048
2022-07-14 10:59:20 -07:00
Muhammad Usman Shahid
b7e77ff25f Rewording "static_assert" diagnostics
This patch rewords the static assert diagnostic output. Failing a
_Static_assert in C should not report that static_assert failed. This
changes the wording to be more like GCC and uses "static assertion"
when possible instead of hard coding the name. This also changes some
instances of 'static_assert' to instead be based on the token in the
source code.

Differential Revision: https://reviews.llvm.org/D129048
2022-07-14 07:47:37 -04:00
Aaron Ballman
2346d19a26 Speculatively fix this C DR test
There is a failing bot:
http://45.33.8.238/macm1/40002/step_7.txt

It looks to be failing because of a regex and how it handles whitespace,
so modifying the CHECK line slightly to account for that.
2022-07-12 15:18:03 -04:00
Aaron Ballman
514dd3c3c3 Update the status for more C DRs
This mostly finishes the DRs in the 200s. There are a few DRs left
which will require more thought and work to test.
2022-07-12 14:06:39 -04:00
Aaron Ballman
548f3f9061 Update the status & add tests for some more C99 DRs 2022-07-06 14:34:36 -04:00
Aaron Ballman
6450daddd2 Test a few more C99 DRs
This updates the status for another 8 DRs.
2022-07-01 13:54:11 -04:00
Aaron Ballman
ac8dab8e09 Add some more expected warnings to this C99 DR test
This should address the issue found by:
https://lab.llvm.org/buildbot/#/builders/171/builds/16835
2022-07-01 08:11:46 -04:00
Aaron Ballman
83fdf0c34e Ensure that the generic associations aren't redundant
This should hopefully address the test failure found in:
https://lab.llvm.org/buildbot/#/builders/171/builds/16833
2022-07-01 07:48:59 -04:00
Aaron Ballman
14035d5147 Fix this C99 DR to be more robust
This should fix the following test issue on ARM:
https://lab.llvm.org/buildbot/#/builders/171/builds/16815
2022-07-01 07:33:37 -04:00
Hubert Tong
6bd53df9b6 [clang][NFC][tests] dr208.c optional signext handling
Fixes llvm/llvm-project#56325.
2022-07-01 00:03:58 -04:00
Corentin Jabot
da1609ad73 Improve the formatting of static_assert messages
Display 'static_assert failed: message' instead of
'static_assert failed "message"' to be consistent
with other implementations and be slightly more
readable.

Reviewed By: #libc, aaron.ballman, philnik, Mordante

Differential Revision: https://reviews.llvm.org/D128844
2022-06-30 23:59:21 +02:00