Commit Graph

5362 Commits

Author SHA1 Message Date
Aaron Puchert dea31f135c Consistent spelling in diagnostics: {l,r}value instead of {l,r}-value
As Richard Smith pointed out in the review of D90123, both the C and C++
standard call it lvalue and rvalue, so let's stick to the same spelling
in Clang.
2020-11-15 18:05:11 +01:00
Aaron Puchert 6f84779674 [Sema] Improve notes for value category mismatch in overloading
When an overloaded member function has a ref-qualifier, like:

class X {
    void f() &&;
    void f(int) &;
};

we would print strange notes when the ref-qualifier doesn't fit the value
category:

X x;
x.f();
X().f(0);

would both print a note "no known conversion from 'X' to 'X' for object
argument" on their relevant overload instead of pointing out the
mismatch in value category.

At first I thought the solution is easy: just use the FailureKind member
of the BadConversionSequence struct. But it turns out that we weren't
properly setting this for function arguments. So I went through
TryReferenceInit to make sure we're doing that right, and found a number
of notes in the existing tests that improved as well.

Fixes PR47791.

Reviewed By: rsmith

Differential Revision: https://reviews.llvm.org/D90123
2020-11-15 18:05:11 +01:00
Haojian Wu 7ad6c8414c [clang] Fix an assertion crash in delayed access check.
`TD->getTemplatedDecl()` might not be a DeclContext variant, which can
trigger an assertion inside `isa<>`.

Differential Revision: https://reviews.llvm.org/D91380
2020-11-13 14:02:57 +01:00
Hans Wennborg a088766508 [dllexport] Instantiate default ctor default args for explicit specializations (PR45811)
For dllexported default constructors with default arguments, we export
default constructor closures which pass in the default args. (See D8331
for a good explanation.)

For templates, that means those default args must be instantiated even
if the function isn't called. That is done by the
InstantiateDefaultCtorDefaultArgs() function, but it wasn't done for
explicit specializations, causing asserts (see bug).

Differential revision: https://reviews.llvm.org/D91089
2020-11-12 13:29:34 +01:00
Richard Smith e7f3e2103c Suppress printing template arguments that match default template
arguments of types by default.

This somewhat improves the worst-case printing of types like
std::string, std::vector, etc., where many irrelevant default arguments
can be included in the type as printed if we've lost the type sugar.
2020-11-11 15:05:51 -08:00
Baptiste Saleil f976ba6139 [PowerPC] Add Sema checks for MMA types
The use of the new types introduced for PowerPC MMA instructions needs to be restricted.
We add a PowerPC function checking that the given type is valid in a context in which we don't allow MMA types.
This function is called from various places in Sema where we want to prevent the use of these types.

Differential Revision: https://reviews.llvm.org/D82035
2020-11-04 17:01:47 -06:00
Mark de Wever b231396122 [Sema] Diagnose annotating `if constexpr` with a likelihood attribute
Adds a diagnostic when the user annotates an `if constexpr` with a
likelihood attribute. The `if constexpr` statement is evaluated at compile
time so the attribute has no effect. Annotating the accompanied `else`
with a likelihood attribute has the same effect as annotating a generic
statement. Since the attribute there is most likely not intended, a
diagnostic will be issued. Since the attributes can't conflict, the
"conflict" won't be diagnosed for an `if constexpr`.

Differential Revision: https://reviews.llvm.org/D90336
2020-10-31 17:51:36 +01:00
Richard Smith 2177e4555a PR47861: Expand dangling reference warning to look through copy
construction, and to assume that assignment operators return *this.
2020-10-30 10:19:50 -07:00
Erich Keane ec809e4cfe PR47372: Fix Lambda invoker calling conventions
As mentioned in the defect, the lambda static invoker does not follow
the calling convention of the lambda itself, which seems wrong. This
patch ensures that the calling convention of operator() is passed onto
the invoker and conversion-operator type.

This is accomplished by extracting the calling-convention determination
code out into a separate function in order to better reflect the 'thiscall'
work, as well as somewhat better support the future implementation of
https://devblogs.microsoft.com/oldnewthing/20150220-00/?p=44623

For any target (basically just win32) that has a different free and
static function calling convention, this generates BOTH alternatives.
This required some work to get the Windows mangler to work correctly for
this, as well as some tie-breaking for the unary operators.

Differential Revision: https://reviews.llvm.org/D89559
2020-10-30 06:39:55 -07:00
Aaron Puchert bbed8cfe80 Thread safety analysis: Consider static class members as inaccessible
This fixes the issue pointed out in D84604#2363134. For now we exclude
static members completely, we'll take them into account later.
2020-10-30 00:35:14 +01:00
Serge Pavlov 08bb5d9196 [FPEnv] Tests for rounding properties of constant evalution
These are moved from D88498.

Differential Revision: https://reviews.llvm.org/D90026
2020-10-29 13:53:13 +07:00
Aaron Puchert 5dbccc6c89 Better source location for -Wignored-qualifiers on trailing return types
We collect the source location of a trailing return type in the parser,
improving the location for regular functions and providing a location
for lambdas, where previously there was none.

Fixes PR47732.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D90129
2020-10-28 23:32:57 +01:00
Zahira Ammarguellat e562a40871 Fix for PR47544. Clang is crashing after generating the right
diagnostic for a re-declaration of a friend method.d
https://reviews.llvm.org/D88112
2020-10-27 05:57:39 -07:00
Richard Smith a5c7b46862 Fix checking for C++98 ICEs in C++11-and-later mode to not consider use
of a reference to be acceptable.
2020-10-26 16:59:48 -07:00
Zequan Wu e56e7bd469 Revert "Revert "Ensure that checkInitIsICE is called exactly once for every variable""
This reverts commit a2ac64dd90.
2020-10-26 12:08:57 -07:00
Zequan Wu a2ac64dd90 Revert "Ensure that checkInitIsICE is called exactly once for every variable"
This causing `Assertion Result && "Could not evaluate expression"' failed` at https://bugs.chromium.org/p/chromium/issues/detail?id=1142009

This reverts commit 76c0092665.
2020-10-26 11:59:55 -07:00
Haojian Wu efa9aaad70 [clang] Suppress "follow-up" diagnostics on recovery call expressions.
Because of typo-correction, the AST can be transformed, and the transformed
AST is marginally useful for diagnostics purpose, the following
diagnostics usually do harm than good (easily cause confusions).

Given the following code:

```
void abcc();
void test() {
  if (abc());
  // diagnostic 1 (for the typo-correction): the typo is correct to `abcc()`, so the code is treate as `if (abcc())` in AST perspective;
  // diagnostic 2 (for mismatch type): we perform an type-analysis on `if`, discover the type is not match
}
```

The secondary diagnostic "convertable to bool" is likely bogus to users.

The idea is to use RecoveryExpr (clang's dependent mechanism) to preserve the
recovery behavior but suppress all follow-up diagnostics.

Differential Revision: https://reviews.llvm.org/D89946
2020-10-26 12:40:00 +01:00
Tyker d3205bbca3 [Annotation] Allows annotation to carry some additional constant arguments.
This allows using annotation in a much more contexts than it currently has.
especially when annotation with template or constexpr.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D88645
2020-10-26 10:50:05 +01:00
Aaron Puchert 5250a03a99 Thread safety analysis: Consider global variables in scope
Instead of just mutex members we also consider mutex globals.
Unsurprisingly they are always in scope. Now the paper [1] says that

> The scope of a class member is assumed to be its enclosing class,
> while the scope of a global variable is the translation unit in
> which it is defined.

But I don't think we should limit this to TUs where a definition is
available - a declaration is enough to acquire the mutex, and if a mutex
is really limited in scope to a translation unit, it should probably be
only declared there.

The previous attempt in 9dcc82f34e was causing false positives because
I wrongly assumed that LiteralPtrs were always globals, which they are
not. This should be fixed now.

[1] https://static.googleusercontent.com/media/research.google.com/en/us/pubs/archive/42958.pdf

Fixes PR46354.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D84604
2020-10-25 19:32:26 +01:00
Richard Smith ccca93b5a2 Don't allow structured binding declarations to decompose a
lambda-expression's captures.

The built-in structured binding rules for classes require that all
fields can be accessed by name, and the fields introduced for lambda
captures are unnamed, so decomposing a capturing lambda is ill-formed.
2020-10-23 16:28:25 -07:00
Richard Smith cb9b9842d3 PR47954 / DR2126: permit temporary objects that are lifetime-extended by
variables that are usable in constant expressions to themselves be
usable in constant expressions.
2020-10-23 14:29:18 -07:00
Richard Smith af189c8ab1 Fix constant evaluation of zero-initialization of a union whose first
FieldDecl is an unamed bitfield.

Unnamed bitfields aren't non-static data member, so such a bitfield
isn't actually the first non-static data member.
2020-10-22 17:03:59 -07:00
Richard Smith ba4768c966 [c++20] For P0732R2 / P1907R1: Basic frontend support for class types as
non-type template parameters.

Create a unique TemplateParamObjectDecl instance for each such value,
representing the globally unique template parameter object to which the
template parameter refers.

No IR generation support yet; that will follow in a separate patch.
2020-10-21 13:21:41 -07:00
Richard Smith 08c8d5bc51 Properly track whether a variable is constant-initialized.
This fixes miscomputation of __builtin_constant_evaluated in the
initializer of a variable that's not usable in constant expressions, but
is readable when constant-folding.

If evaluation of a constant initializer fails, we throw away the
evaluated result instead of keeping it as a non-constant-initializer
value for the variable, because it might not be a correct value.
To avoid regressions for initializers that are foldable but not formally
constant initializers, we now try constant-evaluating some globals in
C++ twice: once to check for a constant initializer (in an mode where
is_constannt_evaluated returns true) and again to determine the runtime
value if the initializer is not a constant initializer.
2020-10-19 23:59:11 -07:00
Richard Smith 76c0092665 Ensure that checkInitIsICE is called exactly once for every variable
for which it matters.

This is a step towards separating checking for a constant initializer
(in which std::is_constant_evaluated returns true) and any other
evaluation of a variable initializer (in which it returns false).
2020-10-19 19:04:04 -07:00
Richard Smith 094e9f4779 PR47893: Synthesis of a comparison operator from an 'operator<=>'
inherits the SFINAEness of its enclosing context.
2020-10-18 14:15:12 -07:00
Richard Smith 552c6c2328 PR44406: Follow behavior of array bound constant folding in more recent versions of GCC.
Old GCC used to aggressively fold VLAs to constant-bound arrays at block
scope in GNU mode. That's non-conforming, and more modern versions of
GCC only do this at file scope. Update Clang to do the same.

Also promote the warning for this from off-by-default to on-by-default
in all cases; more recent versions of GCC likewise warn on this by
default.

This is still slightly more permissive than GCC, as pointed out in
PR44406, as we still fold VLAs to constant arrays in structs, but that
seems justifiable given that we don't support VLA-in-struct (and don't
intend to ever support it), but GCC does.

Differential Revision: https://reviews.llvm.org/D89523
2020-10-16 14:34:35 -07:00
Richard Smith 7e801ca0ef Treat constant contexts as being in the default rounding mode.
This addresses a regression where pretty much all C++ compilations using
-frounding-math now fail, due to rounding being performed in constexpr
function definitions in the standard library.

This follows the "manifestly constant evaluated" approach described in
https://reviews.llvm.org/D87528#2270676 -- evaluations that are required
to succeed at compile time are permitted even in regions with dynamic
rounding modes, as are (unfortunately) the evaluation of the
initializers of local variables of const integral types.

Differential Revision: https://reviews.llvm.org/D89360
2020-10-16 13:26:15 -07:00
Richard Smith fc031d29be Switch the default of VerifyIntegerConstantExpression from constant
folding to not constant folding.

Constant folding of ICEs is done as a GCC compatibility measure, but new
code was picking it up, presumably by accident, due to the bad default.

While here, also switch the flag from a bool to an enum to make it more
obvious what it means at call sites. This highlighted a couple of places
where our behavior is different between C++11 and C++14 due to switching
from checking for an ICE to checking for a converted constant
expression (where there is no 'fold' codepath).
2020-10-15 16:58:47 -07:00
Richard Smith 9dbb0886ea Perform lvalue conversions on the left of a pseudo-destructor call 'p->~T()'.
Previously we failed to convert 'p' from array/function to pointer type,
and to represent the load of 'p' in the AST. The latter causes problems
for constant evaluation.
2020-10-14 22:09:01 -07:00
Richard Smith f7f2e4261a PR47805: Use a single object for a function parameter in the caller and
callee in constant evaluation.

We previously made a deep copy of function parameters of class type when
passing them, resulting in the destructor for the parameter applying to
the original argument value, ignoring any modifications made in the
function body. This also meant that the 'this' pointer of the function
parameter could be observed changing between the caller and the callee.

This change completely reimplements how we model function parameters
during constant evaluation. We now model them roughly as if they were
variables living in the caller, albeit with an artificially reduced
scope that covers only the duration of the function call, instead of
modeling them as temporaries in the caller that we partially "reparent"
into the callee at the point of the call. This brings some minor
diagnostic improvements, as well as significantly reduced stack usage
during constant evaluation.
2020-10-14 17:43:51 -07:00
Richard Smith 69f7c006ff Revert "PR47805: Use a single object for a function parameter in the caller and"
Breaks a clangd unit test.

This reverts commit 8f8b9f2cca.
2020-10-13 19:32:03 -07:00
Richard Smith 8f8b9f2cca PR47805: Use a single object for a function parameter in the caller and
callee in constant evaluation.

We previously made a deep copy of function parameters of class type when
passing them, resulting in the destructor for the parameter applying to
the original argument value, ignoring any modifications made in the
function body. This also meant that the 'this' pointer of the function
parameter could be observed changing between the caller and the callee.

This change completely reimplements how we model function parameters
during constant evaluation. We now model them roughly as if they were
variables living in the caller, albeit with an artificially reduced
scope that covers only the duration of the function call, instead of
modeling them as temporaries in the caller that we partially "reparent"
into the callee at the point of the call. This brings some minor
diagnostic improvements, as well as significantly reduced stack usage
during constant evaluation.
2020-10-13 18:50:46 -07:00
Richard Smith ab870f3030 Revert "PR47805: Use a single object for a function parameter in the caller and"
The buildbots are displeased.

This reverts commit 8d03a972ce.
2020-10-13 15:59:00 -07:00
Richard Smith 8d03a972ce PR47805: Use a single object for a function parameter in the caller and
callee in constant evaluation.

We previously made a deep copy of function parameters of class type when
passing them, resulting in the destructor for the parameter applying to
the original argument value, ignoring any modifications made in the
function body. This also meant that the 'this' pointer of the function
parameter could be observed changing between the caller and the callee.

This change completely reimplements how we model function parameters
during constant evaluation. We now model them roughly as if they were
variables living in the caller, albeit with an artificially reduced
scope that covers only the duration of the function call, instead of
modeling them as temporaries in the caller that we partially "reparent"
into the callee at the point of the call. This brings some minor
diagnostic improvements, as well as significantly reduced stack usage
during constant evaluation.
2020-10-13 15:45:04 -07:00
Erich Keane ac73cafac0 Ensure TreeTransform considers ParmVarDecls as transformed Decls
See PR47804:

TreeTransform uses TransformedLocalDecls as a map of declarations that
have been transformed already. When doing a "TransformDecl", which
happens in the cases of updating a DeclRefExpr's target, the default
implementation simply returns the already transformed declaration.

However, this was not including ParmVarDecls. SO, any use of
TreeTransform that didn't re-implement TransformDecl would NOT properly
update the target of a DeclRefExpr, resulting in odd behavior.

In the case of Typo-recovery, the result was that a lambda that used its
own parameter would cause an error, since it thought that the
ParmVarDecl referenced was a different lambda. Additionally, this caused
a problem in the AST (a declrefexpr into another scope) such that a
future instantiation would cause an assertion.

This patch ensures that the ParmVarDecl transforming process records
into TransformedLocalDecls so that the DeclRefExpr is ALSO updated.
2020-10-12 14:38:04 -07:00
Ben Dunbobbin bb148ad426 [windows-itanium] make dllimport/export handling closer to MS behavior
Differential Revision: https://reviews.llvm.org/D86828
2020-10-09 13:24:07 +01:00
Ben Dunbobbin a9f1bb92bf Revert "first upstream review"
Pushed by accident :(

This reverts commit 4eb627ed96.
2020-10-09 13:22:46 +01:00
Ben Dunbobbin 4eb627ed96 first upstream review 2020-10-09 13:21:03 +01:00
Richard Smith d1751d14a6 PR47175: Ensure type-dependent function-style casts have dependent
types.

Previously, a type-dependent cast to a deduced class template
specialization type would end up with a non-dependent class template
specialization type, leading to confusion downstream.
2020-10-08 17:00:22 -07:00
Richard Smith 00d3e6c1b4 [c++17] Implement P0145R3 during constant evaluation.
Ensure that we evaluate assignment and compound-assignment
right-to-left, and array subscripting left-to-right.

Fixes PR47724.

This is a re-commit of ded79be, reverted in 37c74df, with a fix and test
for the crasher bug previously introduced.
2020-10-06 12:30:26 -07:00
Dmitri Gribenko 37c74dfe72 Revert "[c++17] Implement P0145R3 during constant evaluation."
This reverts commit ded79be635. It causes
a crash (I sent the crash reproducer directly to the author).
2020-10-06 15:49:44 +02:00
Richard Smith ded79be635 [c++17] Implement P0145R3 during constant evaluation.
Ensure that we evaluate assignment and compound-assignment
right-to-left, and array subscripting left-to-right.

Fixes PR47724.
2020-10-05 19:04:14 -07:00
Haojian Wu 7f05fe1aee [AST][RecoveryExpr] Fix a crash on undeduced type.
We should not capture the type if the function return type is undeduced.

Reviewed By: adamcz

Differential Revision: https://reviews.llvm.org/D87350
2020-10-05 12:52:04 +02:00
Akira Hatanaka 21cf2e6c26 Handle unknown OSes in DarwinTargetInfo::getExnObjectAlignment
rdar://problem/69727650
2020-09-30 16:05:17 -07:00
Richard Smith 892df30a7f Fix interaction of `constinit` and `weak`.
We previously took a shortcut and said that weak variables never have
constant initializers (because those initializers are never correct to
use outside the variable). We now say that weak variables can have
constant initializers, but are never usable in constant expressions.
2020-09-30 10:49:50 -07:00
Chris Hamilton 155d2d5300 Revert "[Sema] Address-space sensitive check for unbounded arrays (v2)"
This reverts commit d9ee935679.
2020-09-29 22:46:14 +02:00
Aaron Ballman 15fbae8ac3 Use "default member initializer" instead of "in-class initializer" for diagnostics.
This changes some diagnostics to use terminology from the standard
rather than invented terminology, which improves consistency with other
diagnostics as well. There are no functional changes intended other
than wording and naming.
2020-09-29 15:04:23 -04:00
Chris Hamilton d9ee935679 [Sema] Address-space sensitive check for unbounded arrays (v2)
Check applied to unbounded (incomplete) arrays and pointers to spot
cases where the computed address is beyond the largest possible
addressable extent of the array, based on the address space in which the
array is delcared, or which the pointer refers to.

Check helps to avoid cases of nonsense pointer math and array indexing
which could lead to linker failures or runtime exceptions.  Of
particular interest when building for embedded systems with small
address spaces.

This is version 2 of this patch -- version 1 had some testing issues
due to a sign error in existing code.  That error is corrected and
lit test for this chagne is extended to verify the fix.

Originally reviewed/accepted by: aaron.ballman
Original revision: https://reviews.llvm.org/D86796

Reviewed By: ebevhan

Differential Revision: https://reviews.llvm.org/D88174
2020-09-29 16:14:48 +02:00
Richard Smith c375635d05 Ensure that we don't compute linkage for an anonymous class too early if
it has a member whose name is the same as a builtin.

Fixes a regression from the introduction of BuiltinAttr.
2020-09-28 17:22:40 -07:00