Commit Graph

30621 Commits

Author SHA1 Message Date
Aaron Ballman b8266f512a Correct the __has_c_attribute value for fallthrough
The original proposal was seen in Apr 2019 and we accidentally used
that date (201904L) as the feature testing value. However, WG14 N2408
was adopted at the Oct 2019 meeting and so that's the correct date for
the feature testing macro. The committee draft for C2x shows 201910L
for this value, so this changes brings us in line with the standard.
2022-09-13 11:08:58 -04:00
Aaron Ballman 0f28cf416d Correct the __has_c_attribute value for maybe_unused
The original proposal was adopted in Apr 2019, but was subsequently
updated by WG14 N2662 in June 2021. We already supported the attribute
on a label and it behaved as expected, but we had not bumped the
feature test value.
2022-09-13 09:33:20 -04:00
Aaron Ballman 1b19df12b8 Correct the __has_c_attribute value for nodiscard
The original proposal was adopted in Apr 2019 and so the previous value
was 201904L. However, a subsequent proposal (N2448) was adopted to add
an optional message argument to the attribute. We already support that
functionality, but had not bumped the feature test value.
2022-09-13 08:13:01 -04:00
Alexander Kornienko 637da9de4c Revert "[clang] template / auto deduction deduces common sugar"
This reverts commit d200db3863, which causes a
clang crash. See https://reviews.llvm.org/D111283#3785755

Test case for convenience:
```
template <typename T>
using P = int T::*;

template <typename T, typename... A>
void j(P<T>, T, A...);

template <typename T>
void j(P<T>, T);

struct S {
  int b;
};
void g(P<S> k, S s) { j(k, s); }
```
2022-09-13 12:18:07 +02:00
Sylvestre Ledru cd20a18286 Revert "[clang, llvm] Add __declspec(safebuffers), support it in CodeView"
Causing:
https://github.com/llvm/llvm-project/issues/57709

This reverts commit ab56719acd.
2022-09-13 10:53:59 +02:00
Martin Storsjö fbfe1db4a9 [clang] Explicitly set the EmulatedTLS codegen option. NFC.
Set the EmulatedTLS option based on `Triple::hasDefaultEmulatedTLS()`
if the user didn't specify it; set `ExplicitEmulatedTLS` to true
in `llvm::TargetOptions` and set `EmulatedTLS` to Clang's
opinion of what the default or preference is.

This avoids any risk of deviance between the two.

This affects one check of `getCodeGenOpts().EmulatedTLS` in
`shouldAssumeDSOLocal` in CodeGenModule, but as that check only
is done for `TT.isWindowsGNUEnvironment()`, and
`hasDefaultEmulatedTLS()` returns false for such environments
it doesn't make any current testable difference - thus NFC.

Some mingw distributions carry a downstream patch, that enables
emulated TLS by default for mingw targets in `hasDefaultEmulatedTLS()`
- and for such cases, this patch does make a difference and fixes the
detection of emulated TLS, if it is implicitly enabled.

Differential Revision: https://reviews.llvm.org/D132916
2022-09-13 10:40:54 +03:00
Balazs Benics f8643a9b31 [analyzer] Prefer wrapping SymbolicRegions by ElementRegions
It turns out that in certain cases `SymbolRegions` are wrapped by
`ElementRegions`; in others, it's not. This discrepancy can cause the
analyzer not to recognize if the two regions are actually referring to
the same entity, which then can lead to unreachable paths discovered.

Consider this example:

```lang=C++
struct Node { int* ptr; };
void with_structs(Node* n1) {
  Node c = *n1; // copy
  Node* n2 = &c;
  clang_analyzer_dump(*n1); // lazy...
  clang_analyzer_dump(*n2); // lazy...
  clang_analyzer_dump(n1->ptr); // rval(n1->ptr): reg_$2<int * SymRegion{reg_$0<struct Node * n1>}.ptr>
  clang_analyzer_dump(n2->ptr); // rval(n2->ptr): reg_$1<int * Element{SymRegion{reg_$0<struct Node * n1>},0 S64b,struct Node}.ptr>
  clang_analyzer_eval(n1->ptr != n2->ptr); // UNKNOWN, bad!
  (void)(*n1);
  (void)(*n2);
}
```

The copy of `n1` will insert a new binding to the store; but for doing
that it actually must create a `TypedValueRegion` which it could pass to
the `LazyCompoundVal`. Since the memregion in question is a
`SymbolicRegion` - which is untyped, it needs to first wrap it into an
`ElementRegion` basically implementing this untyped -> typed conversion
for the sake of passing it to the `LazyCompoundVal`.
So, this is why we have `Element{SymRegion{.}, 0,struct Node}` for `n1`.

The problem appears if the analyzer evaluates a read from the expression
`n1->ptr`. The same logic won't apply for `SymbolRegionValues`, since
they accept raw `SubRegions`, hence the `SymbolicRegion` won't be
wrapped into an `ElementRegion` in that case.

Later when we arrive at the equality comparison, we cannot prove that
they are equal.

For more details check the corresponding thread on discourse:
https://discourse.llvm.org/t/are-symbolicregions-really-untyped/64406

---

In this patch, I'm eagerly wrapping each `SymbolicRegion` by an
`ElementRegion`; basically canonicalizing to this form.
It seems reasonable to do so since any object can be thought of as a single
array of that object; so this should not make much of a difference.

The tests also underpin this assumption, as only a few were broken by
this change; and actually fixed a FIXME along the way.

About the second example, which does the same copy operation - but on
the heap - it will be fixed by the next patch.

Reviewed By: martong

Differential Revision: https://reviews.llvm.org/D132142
2022-09-13 08:58:46 +02:00
Fangrui Song 6f9c4851ab [MinGW] Reject explicit hidden visibility applied to dllexport and hidden/protected applied to dllimport
Hidden visibility is incompatible with dllexport.
Hidden and protected visibilities are incompatible with dllimport.
(PlayStation uses dllexport protected.)

When an explicit visibility attribute applies on a dllexport/dllimport
declaration, report a Frontend error (Sema does not compute visibility).

Reviewed By: mstorsjo

Differential Revision: https://reviews.llvm.org/D133266
2022-09-12 15:56:36 -07:00
David Majnemer ab56719acd [clang, llvm] Add __declspec(safebuffers), support it in CodeView
__declspec(safebuffers) is equivalent to
__attribute__((no_stack_protector)).  This information is recorded in
CodeView.

While we are here, add support for strict_gs_check.
2022-09-12 21:15:34 +00:00
Corentin Jabot 7eead180b9 [Clang] NFC: Make UnqualifiedId::Kind private for consistency.
Differential Revision: https://reviews.llvm.org/D133703
2022-09-12 19:14:06 +02:00
Jun Zhang 1d51bb824f
[Clang] Reword diagnostic for scope identifier with linkage
If the declaration of an identifier has block scope, and the identifier has
external or internal linkage, the declaration shall have no initializer for
the identifier.

Clang now gives a more suitable diagnosis for this case.
Fixes https://github.com/llvm/llvm-project/issues/57478

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

 Differential Revision: https://reviews.llvm.org/D133088
2022-09-12 22:40:54 +08:00
Muhammad Usman Shahid ea26ed1f9c Rewording note note_constexpr_invalid_cast
The diagnostics here are correct, but the note is really silly. It
talks about reinterpret_cast in C code. So rewording it for c mode by
using another %select{}.
```
int array[(long)(char *)0];
```
previous note:
```
cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression
```
reworded note:
```
this conversion is not allowed in a constant expression
```

Differential Revision: https://reviews.llvm.org/D133194
2022-09-12 07:47:39 -04:00
Xiang Li c9d2b6b92d [HLSL] Preserve vec3 for HLSL.
Preserve vec3 for HLSL by set -fpreserve-vec3-type.

Reviewed By: beanz

Differential Revision: https://reviews.llvm.org/D132913
2022-09-09 10:55:45 -07:00
mydeveloperday 28bd7945ea [clang-format] NFC remove incorrect whitespace causing documentation issue 2022-09-09 16:03:48 +01:00
Chris Bieneman d3c54a172d [HLSL] Call global constructors inside entry
HLSL doesn't have a runtime loader model that supports global
construction by a loader or runtime initializer. To allow us to leverage
global constructors with minimal code generation impact we put calls to
the global constructors inside the generated entry function.

Differential Revision: https://reviews.llvm.org/D132977
2022-09-09 09:01:28 -05:00
Alvin Wong a3a8bd00c8 [clang][MinGW] Add `-mguard=cf` and `-mguard=cf-nochecks`
This option can be used to enable Control Flow Guard checks and
generation of address-taken function table. They are equivalent to
`/guard:cf` and `/guard:cf,nochecks` in clang-cl. Passing this flag to
the Clang driver will also pass `--guard-cf` to the MinGW linker.

This feature is disabled by default. The option `-mguard=none` is also
available to explicitly disable this feature.

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D132810
2022-09-09 09:55:40 +03:00
Joe Loser 1b3a78d1d5 [clang] Use std::size instead of llvm::array_lengthof
LLVM contains a helpful function for getting the size of a C-style
array: `llvm::array_lengthof`. This is useful prior to C++17, but not as
helpful for C++17 or later: `std::size` already has support for C-style
arrays.

Change call sites to use `std::size` instead. Leave the few call sites that
use a locally defined `array_lengthof` that are meant to test previous bugs
with NTTPs in clang analyzer and SemaTemplate.

Differential Revision: https://reviews.llvm.org/D133520
2022-09-08 17:20:25 -06:00
Fangrui Song 4d4ca6c9d0 [Driver] Rename Z to Zlib 2022-09-08 16:14:31 -07:00
Fangrui Song 48203b327d Revert D130308 "[clang] extend getCommonSugaredType to merge sugar nodes"
This reverts commit 16e5d6d7f9.
There are multiple complaints on the review.

In addition, it may cause spurious
```
error: invalid operands to binary expression ('SinkPrinter' and 'char[cluster_name_length]')
note: candidate template ignored: substitution failure: variably modified type 'char *' cannot be used as a template argument SinkPrinter operator<<(const SinkPrinter &s, T) {
```
for some C++ code
2022-09-08 15:09:39 -07:00
Tom Honermann 8d03328e7b [clang] NFC: Add a missing const qualifier to Decl::isLocalExternDecl(). 2022-09-08 13:21:10 -07:00
Matheus Izvekov 16e5d6d7f9
[clang] extend getCommonSugaredType to merge sugar nodes
This continues D111283 by extending the getCommonSugaredType
implementation to also merge non-canonical type nodes.

We merge these nodes by going up starting from the canonical
node, calculating their merged properties on the way.

If we reach a pair that is too different, or which we could not
otherwise unify, we bail out and don't try to keep going on to
the next pair, in effect striping out all the remaining top-level
sugar nodes. This avoids mismatching 'companion' nodes, such as
ElaboratedType, so that they don't end up elaborating some other
unrelated thing.

Depends on D111509

Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>

Differential Revision: https://reviews.llvm.org/D130308
2022-09-08 19:17:53 +02:00
Matheus Izvekov d200db3863
[clang] template / auto deduction deduces common sugar
After upgrading the type deduction machinery to retain type sugar in
D110216, we were left with a situation where there is no general
well behaved mechanism in Clang to unify the type sugar of multiple
deductions of the same type parameter.

So we ended up making an arbitrary choice: keep the sugar of the first
deduction, ignore subsequent ones.

In general, we already had this problem, but in a smaller scale.
The result of the conditional operator and many other binary ops
could benefit from such a mechanism.

This patch implements such a type sugar unification mechanism.

The basics:

This patch introduces a `getCommonSugaredType(QualType X, QualType Y)`
method to ASTContext which implements this functionality, and uses it
for unifying the results of type deduction and return type deduction.
This will return the most derived type sugar which occurs in both X and
Y.

Example:

Suppose we have these types:
```
using Animal = int;
using Cat = Animal;
using Dog = Animal;

using Tom = Cat;
using Spike = Dog;
using Tyke = Dog;
```
For `X = Tom, Y = Spike`, this will result in `Animal`.
For `X = Spike, Y = Tyke`, this will result in `Dog`.

How it works:

We take two types, X and Y, which we wish to unify as input.
These types must have the same (qualified or unqualified) canonical
type.

We dive down fast through top-level type sugar nodes, to the
underlying canonical node. If these canonical nodes differ, we
build a common one out of the two, unifying any sugar they had.
Note that this might involve a recursive call to unify any children
of those. We then return that canonical node, handling any qualifiers.

If they don't differ, we walk up the list of sugar type nodes we dived
through, finding the last identical pair, and returning that as the
result, again handling qualifiers.

Note that this patch will not unify sugar nodes if they are not
identical already. We will simply strip off top-level sugar nodes that
differ between X and Y. This sugar node unification will instead be
implemented in a subsequent patch.

This patch also implements a few users of this mechanism:
* Template argument deduction.
* Auto deduction, for functions returning auto / decltype(auto), with
  special handling for initializer_list as well.

Further users will be implemented in a subsequent patch.

Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>

Differential Revision: https://reviews.llvm.org/D111283
2022-09-08 19:17:48 +02:00
Thomas Lively ac3b8df8f2 [WebAssembly] Prototype `f32x4.relaxed_dot_bf16x8_add_f32`
As proposed in https://github.com/WebAssembly/relaxed-simd/issues/77. Only an
LLVM intrinsic and a clang builtin are implemented. Since there is no bfloat16
type, use u16 to represent the bfloats in the builtin function arguments.

Differential Revision: https://reviews.llvm.org/D133428
2022-09-08 08:07:49 -07:00
Fangrui Song a0365abad8 [Driver] Support -gz=zstd
The driver option translates to --compress-debug-sections=zstd cc1/cc1as/GNU
assembler/linker options.

`clang -g -gz=zstd -c a.c` generates ELFCOMPRESS_ZSTD compressed debug info
sections if compression decreases size.
2022-09-08 01:39:06 -07:00
Fangrui Song bc502d9c24 Revert D133266 "[MinGW] Reject explicit non-default visibility applied to dllexport/dllimport declaration"
This reverts commit 91d8324366.

The combo dllexport protected makes sense and is used by PlayStation.
Will change the patch to allow dllexport protected.
2022-09-07 16:06:19 -07:00
Volodymyr Sapsai 3e7350f317 [ODRHash diagnostics] Move `ODRDiagsEmitter` to libAST in separate files. NFC.
Intend to use `ODRDiagsEmitter` during parsing to diagnose a parsed
definition differing from a definition with the same name from a hidden
[sub]module.

Differential Revision: https://reviews.llvm.org/D128695
2022-09-07 14:40:37 -07:00
Marco Elver c4842bb2e9 [Clang] Introduce -fexperimental-sanitize-metadata=
Introduces the frontend flag -fexperimental-sanitize-metadata=, which
enables SanitizerBinaryMetadata instrumentation.

The first intended user of the binary metadata emitted will be a variant
of GWP-TSan [1]. The plan is to open source a stable and production
quality version of GWP-TSan. The development of which, however, requires
upstream compiler support.

[1] https://llvm.org/devmtg/2020-09/slides/Morehouse-GWP-Tsan.pdf

Until the tool has been open sourced, we mark this kind of
instrumentation as "experimental", and reserve the option to change
binary format, remove features, and similar.

Reviewed By: vitalybuka, MaskRay

Differential Revision: https://reviews.llvm.org/D130888
2022-09-07 21:25:40 +02:00
Craig Topper cc14e195e7 [RISCV] Remove space before colon in error message. 2022-09-07 11:34:33 -07:00
Craig Topper 9ea7e4f7c1 [RISCV] Remove unnecessary word from error message. 2022-09-07 10:31:48 -07:00
Argyrios Kyrtzidis aa484c90cf [Lex/DependencyDirectivesScanner] Keep track of the presence of tokens between the last scanned directive and EOF
Directive `dependency_directives_scan::tokens_present_before_eof` is introduced to indicate there were tokens present before
the last scanned dependency directive and EOF.
This is useful to ensure we correctly identify the macro guards when lexing using the dependency directives.

Differential Revision: https://reviews.llvm.org/D133357
2022-09-07 10:31:29 -07:00
Richard Smith a002063de3 Enforce module decl-use restrictions and private header restrictions in textual headers
Per the documentation, these restrictions were intended to apply to textual headers but previously this didn't work because we decided there was no requesting module when the `#include` was in a textual header.

A `-cc1` flag is provided to restore the old behavior for transitionary purposes.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D132779
2022-09-06 17:12:57 -07:00
Craig Topper 7440e2274f [RISCV] Add '32bit' feature to rv32 only builtins.
The backend now has a 32bit feature as part of the recent mtune
patch. We can now use that make our rv32-only builtin error checking
work the same way as rv64-only errors.

Reviewed By: kito-cheng

Differential Revision: https://reviews.llvm.org/D132192
2022-09-06 14:46:35 -07:00
Passw 2adf241592 [clang-format] [doc] Fix example of wrapping class definitions
Example of

BraceWrapping
  AfterClass
is wrong

Differential Revision: https://reviews.llvm.org/D133087
2022-09-06 12:24:18 +02:00
Nikita Popov 71797797f7 [OpenMP] Mark -fopenmp-implicit-rpath as NoArgumentUnused
This matches the behavior for all the other -fopenmp options,
as well as -frtlib-add-rpath.

For context, Fedora passes this flag by default in case OpenMP is
used, and this results in a warning if it (usually) isn't, which
causes build failures for some programs with unnecessarily strict
build systems (like Ruby).

Differential Revision: https://reviews.llvm.org/D133316
2022-09-06 09:44:45 +02:00
Chuanqi Xu 7628f19262 [NFC] Remove invisible character in Diagnostic message and tests 2022-09-06 11:08:42 +08:00
Fangrui Song 91d8324366 [MinGW] Reject explicit non-default visibility applied to dllexport/dllimport declaration
dllimport/dllexport is incompatible with protected/hidden visibilities.
(Arguably dllexport semantics is compatible with protected but let's reject the
combo for simplicity.)

When an explicit visibility attribute applies on a dllexport/dllimport
declaration, report a Frontend error (Sema does not compute visibility).

Reviewed By: mstorsjo

Differential Revision: https://reviews.llvm.org/D133266
2022-09-05 10:17:19 -07:00
Krystian Kuzniarek 961fd77687 [clang-format][docs] Fix incorrect 'clang-format 4' option markers
Introduced by 23a5090c6, some style option markers indicated
'clang-format 4', though their respective options were available in
earlier releases.

Differential Revision: https://reviews.llvm.org/D129934
2022-09-05 09:38:22 +02:00
Chuanqi Xu 69e920d426 [Coroutines] Use LookupAllocationFunction to find allocation functions for coroutines consistently
Previously we may call Sema::FindAllocationFunctions directly to lookup
allocation functions directly instead of using our wrapped lambda
LookupAllocationFunction, which is slightly incosnsistent. It will be
helpful to refactor this for further changes.

Also previously, when we lookup 'operator new(std::size_t, std::nothrow_t)' in
case we found `get_­return_­object_­on_­allocation_­failure` in the
promise_type, the compiler will try to look at the allocation function
in promise_type. However, this is not wanted actually. According to
[dcl.fct.def.coroutine]p10:

> if a global allocation function is selected, the
> ::operator new(size_­t, nothrow_­t) form is used.

So we should only lookup for `::operator (size_t, nothrow_t)` for the
global allocation function. For the allocation function in the
promise_type, the requirement is that it shouldn't throw, which has
already been checked.

Given users generally include headers from standard libs so it will
generally include the <new> header, so this change should be a trivial
one and shouldn't affect almost any user.
2022-09-05 15:20:09 +08:00
isuckatcs a46154cb1c [analyzer] Warn if the size of the array in `new[]` is undefined
This patch introduces a new checker, called NewArraySize checker,
which detects if the expression that yields the element count of
the array in new[], results in an Undefined value.

Differential Revision: https://reviews.llvm.org/D131299
2022-09-04 23:06:58 +02:00
Vitaly Buka 9905dae5e1 Revert "[Clang][CodeGen] Avoid __builtin_assume_aligned crash when the 1st arg is array type"
Breakes windows bot.

This reverts commit 3ad2fe913a.
2022-09-03 13:12:49 -07:00
Fangrui Song 89df4e4825 [Driver] Remove unused -Ttext -Tdata -Tbss
They lead to -Wunused-command-line-argument and should be written as -Ttext=
instead, but the driver options end with a space. -Ttext=0 can be accepted by
the JoinedOrSeparate -T, so the JoinedOrSeparate -Ttext/etc are unneeded.
2022-09-03 10:57:29 -07:00
Fangrui Song a2ef7654ec [Driver] Change some cc1 only JoinedOrSeparate long options to Separate
They are error-prone as they do not end with `=`.
Note: for driver -isystem/etc, we have to support the Joined form.
2022-09-03 10:45:30 -07:00
yronglin 3ad2fe913a [Clang][CodeGen] Avoid __builtin_assume_aligned crash when the 1st arg is array type
Avoid __builtin_assume_aligned crash when the 1st arg is array type(or string literal).

Open issue: https://github.com/llvm/llvm-project/issues/57169

Reviewed By: rjmccall

Differential Revision: https://reviews.llvm.org/D133202
2022-09-03 23:26:01 +08:00
Volodymyr Sapsai 246c5a994b [ODRHash diagnostics] Transform method `ASTReader::diagnoseOdrViolations` into a class `ODRDiagsEmitter`. NFC.
Preparing to use diagnostics about ODR hash mismatches outside of ASTReader.

Differential Revision: https://reviews.llvm.org/D128490
2022-09-02 16:20:05 -07:00
Fangrui Song 2f7da9d317 [Driver] Remove cc1 Separate form -fvisibility 2022-09-02 12:40:00 -07:00
Chris Bieneman 5b5329bd41 [NFC] Make MultiplexExternalSemaSource own sources
This change refactors the MuiltiplexExternalSemaSource to take ownership
of the underlying sources. As a result it makes a larger cleanup of
external source ownership in Sema and the ChainedIncludesSource.

Reviewed By: aaron.ballman, aprantl

Differential Revision: https://reviews.llvm.org/D133158
2022-09-02 13:57:39 -05:00
Fangrui Song 1491282165 [clang] Change cc1 -fvisibility's canonical spelling to -fvisibility= 2022-09-02 11:49:38 -07:00
Chris Bieneman 10194a51a9 [HLSL] Restrict to supported targets
Someday we would like to support HLSL on a wider range of targets, but
today targeting anything other than `dxil` is likly to cause lots of
headaches. This adds an error and tests to validate that the expected
target is `dxil-?-shadermodel`.

We will continue to do a best effort to ensure the code we write makes
it easy to support other targets (like SPIR-V), but this error will
prevent users from hitting frustrating errors for unsupported cases.

Reviewed By: jcranmer-intel, Anastasia

Differential Revision: https://reviews.llvm.org/D132056
2022-09-02 13:36:23 -05:00
Fangrui Song 8899c3c4e1 [docs] -fivisibility= allows protected and internal 2022-09-02 10:49:10 -07:00
Fangrui Song e05edb19ad [Driver] Unsupport --print-multiarch
* If GCC is configured with `--disable-multi-arch`, `--print-multiarch` output is an empty line.
* If GCC is configured with `--enable-multi-arch`, `--print-multiarch` output may be a normalized triple or (on Debian, 'vendor' is omitted) `x86_64-linux-gnu`.

The Clang support D101400 just prints the Debian multiarch style triple
unconditionally, but the string is not really expected for non-Debian systems.

AIUI many Linux distributions and non-Linux OSes don't configure GCC with `--enable-multi-arch`.
Instead of getting us in the trouble of supporting all kinds of variants, drop the support as before D101400.

Close https://github.com/llvm/llvm-project/issues/51469

Reviewed By: phosek

Differential Revision: https://reviews.llvm.org/D133170
2022-09-02 09:51:02 -07:00