Commit Graph

225 Commits

Author SHA1 Message Date
Yuki Okushi e32a92c6fe Remove unnecessary triple from test
PR27098(https://bugs.llvm.org/show_bug.cgi?id=27098) has been fixed so
the avoidance seems unnecessary.

Differential Revision: https://reviews.llvm.org/D68891
2021-06-15 09:49:45 -07:00
Aaron Puchert 825f80e111 [Sema] Introduce function reference conversion, NFC
Technically 'noexcept' isn't a qualifier, so this should be a separate conversion.

Also make the test a pure frontend test.

Reviewed By: rsmith

Differential Revision: https://reviews.llvm.org/D67112
2020-11-22 20:51:57 +01:00
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
Bruno Ricci eb10b065f2
[clang] Pass the NamedDecl* instead of the DeclarationName into many diagnostics.
Background:
-----------
There are two related argument types which can be sent into a diagnostic to
display the name of an entity: DeclarationName (ak_declarationname) or
NamedDecl* (ak_nameddecl) (there is also ak_identifierinfo for
IdentifierInfo*, but we are not concerned with it here).

A DeclarationName in a diagnostic will just be streamed to the output,
which will directly result in a call to DeclarationName::print.

A NamedDecl* in a diagnostic will also ultimately result in a call to
DeclarationName::print, but with two customisation points along the way:

The first customisation point is NamedDecl::getNameForDiagnostic which is
overloaded by FunctionDecl, ClassTemplateSpecializationDecl and
VarTemplateSpecializationDecl to print the template arguments, if any.

The second customisation point is NamedDecl::printName. By default it just
streams the stored DeclarationName into the output but it can be customised
to provide a user-friendly name for an entity. It is currently overloaded by
DecompositionDecl and MSGuidDecl.

What this patch does:
---------------------
For many diagnostics a DeclarationName is used instead of the NamedDecl*.
This bypasses the two customisation points mentioned above. This patches fix
this for diagnostics in Sema.cpp, SemaCast.cpp, SemaChecking.cpp, SemaDecl.cpp,
SemaDeclAttr.cpp, SemaDecl.cpp, SemaOverload.cpp and SemaStmt.cpp.

I have only modified diagnostics where I could construct a test-case which
demonstrates that the change is appropriate (either with this patch or the next
one).

Reviewed By: erichkeane, aaron.ballman

Differential Revision: https://reviews.llvm.org/D84656
2020-07-28 10:30:35 +01:00
Bruno Ricci be8e5fee91
[clang][NFC] Tests showing the problems with some uses of NamedDecl::getDeclName in diagnostics, SemaExpr.cpp part 2020-07-18 20:39:16 +01:00
Bruno Ricci 40ea01f654
[clang] Convert a default argument expression to the parameter type...
...before checking that the default argument is valid with
CheckDefaultArgumentVisitor.

Currently the restrictions on a default argument are checked with the visitor
CheckDefaultArgumentVisitor in ActOnParamDefaultArgument before
performing the conversion to the parameter type in SetParamDefaultArgument.

This was fine before the previous patch but now some valid code post-CWG 2346
is rejected:

void test() {
  const int i2 = 0;
  extern void h2a(int x = i2);     // FIXME: ok, not odr-use
  extern void h2b(int x = i2 + 0); // ok, not odr-use
}

This is because the reference to i2 in h2a has not been marked yet with
NOUR_Constant. i2 is marked NOUR_Constant when the conversion to the parameter
type is done, which is done just after.

The solution is to do the conversion to the parameter type before checking
the restrictions on default arguments with CheckDefaultArgumentVisitor.
This has the side-benefit of improving some diagnostics.

Differential Revision: https://reviews.llvm.org/D81616

Reviewed By: rsmith
2020-06-11 13:18:45 +01:00
Bruno Ricci 5951ff4512
[clang] CWG 2082 and 2346: loosen the restrictions on parameters and local variables in default arguments.
This patch implements the resolution of CWG 2082 and CWG 2346.

The resolution of CWG 2082 changed [dcl.fct.default]p7 and p9 to allow
a parameter or local variable to appear in a default argument if not
in a potentially-evaluated expression.

The resolution of CWG 2346 changed [dcl.fct.default]p7 to allow a local
variable to appear in a default argument if not odr-used.

An issue remains after this patch
(see the FIXME in test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p7.cpp).
This is addressed by the next patch.

Differential Revision: https://reviews.llvm.org/D81615

Reviewed By: rsmith, erichkeane
2020-06-11 12:41:08 +01:00
Richard Smith b5f2c4e45b PR23029 / C++ DR2233: Allow expanded parameter packs to follow
parameters with default arguments.

Directly follow the wording by relaxing the AST invariant that all
parameters after one with a default arguemnt also have default
arguments, and removing the diagnostic on missing default arguments
on a pack-expanded parameter following a parameter with a default
argument.

Testing also revealed that we need to special-case explicit
specializations of templates with a pack following a parameter with a
default argument, as such explicit specializations are otherwise
impossible to write. The standard wording doesn't address this case; a
issue has been filed.

This exposed a bug where we would briefly consider a parameter to have
no default argument while we parse a delay-parsed default argument for
that parameter, which is also fixed.

Partially incorporates a patch by Raul Tambre.
2020-06-02 13:48:59 -07:00
Fangrui Song d0d076fed9 [Driver] Flip the CC1 default of -fdiagnostics-show-option
The driver enables -fdiagnostics-show-option by default, so flip the CC1
default to reduce the lengths of common CC1 command lines.

This change also makes ParseDiagnosticArgs() consistently enable
-fdiagnostics-show-option by default.
2020-03-31 21:59:27 -07:00
Michele Scandale bd5b22070b Fix TryParsePtrOperatorSeq.
The syntax rules for ptr-operator allow attributes after *, &,
&&, therefore we should be able to parse the following:

void fn() {
    void (*[[attr]] x)() = &fn;
    void (&[[attr]] y)() = fn;
    void (&&[[attr]] z)() = fn;
}
However the current logic in TryParsePtrOperatorSeq does not consider
the presence of attributes leading to unexpected parsing errors.

Moreover we should also consider _Atomic a possible qualifier that can
appear after the sequence of attribute specifiers.
2020-02-24 08:08:47 -05:00
Richard Smith 24ad121582 Add -std=c++20 flag, replace C++2a with C++20 throughout the Clang
user interface and documentation, and update __cplusplus for C++20.

WG21 considers the C++20 standard to be finished (even though it still
has some more steps to pass through in the ISO process).

The old flag names are accepted for compatibility, as usual, and we
still have lots of references to C++2a in comments and identifiers;
those can be cleaned up separately.
2020-02-18 16:16:37 -08:00
Richard Smith 7ef45f45f6 P1957R2: conversion from a pointer to bool is considered narrowing.
This is being implemented somewhat speculatively, to match GCC's
behavior.
2020-02-11 06:52:44 -08:00
Richard Smith 1b5404aff3 PR44540: Prefer an inherited default constructor over an initializer
list constructor when initializing from {}.

We would previously pick between calling an initializer list constructor
and calling a default constructor unstably in this situation, depending
on whether the inherited default constructor had already been used
elsewhere in the program.
2020-01-14 19:29:50 -08:00
Richard Smith 2519554134 When diagnosing the lack of a viable conversion function, also list
explicit functions that are not candidates.

It's not always obvious that the reason a conversion was not possible is
because the function you wanted to call is 'explicit', so explicitly say
if that's the case.

It would be nice to rank the explicit candidates higher in the
diagnostic if an implicit conversion sequence exists for their
arguments, but unfortunately we can't determine that without potentially
triggering non-immediate-context errors that we're not permitted to
produce.
2020-01-09 15:15:02 -08:00
Richard Smith d052a578de [c++2a] Allow comparison functions to be explicitly defaulted.
This adds some initial syntactic checking that only the appropriate
function signatures can be defaulted. No implicit definitions are
generated yet.
2019-10-22 18:16:17 -07:00
Richard Smith 457226e02a For P0784R7: add support for constexpr destructors, and call them as
appropriate during constant evaluation.

Note that the evaluator is sometimes invoked on incomplete expressions.
In such cases, if an object is constructed but we never reach the point
where it would be destroyed (and it has non-trivial destruction), we
treat the expression as having an unmodeled side-effect.

llvm-svn: 372538
2019-09-23 03:48:44 +00:00
Richard Smith 7a6182d48d PR40674: fix assertion failure if a structured binding declaration has a
tuple-like decomposition that produces value-dependent reference
bindings.

llvm-svn: 369829
2019-08-24 01:23:57 +00:00
Reid Kleckner ed399a69e2 [Sema] Implement DR2386 for C++17 structured binding
Allow implementations to provide complete definitions of
std::tuple_size<T>, but to omit the 'value' member to signal that T is
not tuple-like. The Microsoft standard library implements
std::tuple_size<const T> this way.

If the value member exists, clang still validates that it is an ICE, but
if it does not, then the type is considered to not be tuple-like.

Fixes PR33236

Reviewers: rsmith

Differential Revision: https://reviews.llvm.org/D66040

llvm-svn: 369043
2019-08-15 19:45:28 +00:00
Anastasia Stulova 1da9e4c910 [Sema] Improved diagnostic for qualifiers in reference binding
Improved wording and also simplified by using printing
method from qualifiers.

Differential Revision: https://reviews.llvm.org/D62914

llvm-svn: 364023
2019-06-21 10:50:02 +00:00
Richard Smith 2b45b267da P1286R2: Remove restriction that the exception specification of a
defaulted special member matches the implicit exception specification.

llvm-svn: 360011
2019-05-06 05:04:56 +00:00
Richard Smith 9b2c5e7c44 [cxx2a] P0641R2: (Some) type mismatches on defaulted functions only
render the function deleted instead of rendering the program ill-formed.

This change also adds an enabled-by-default warning for the case where
an explicitly-defaulted special member function of a non-template class
is implicitly deleted by the type checking rules. (This fires either due
to this language change or due to pre-C++20 reasons for the member being
implicitly deleted). I've tested this on a large codebase and found only
bugs (where the program means something that's clearly different from
what the programmer intended), so this is enabled by default, but we
should revisit this if there are problems with this being enabled by
default.

llvm-svn: 343285
2018-09-28 01:16:43 +00:00
Richard Smith 5c9b3b7576 P0969R0: allow structured binding of accessible members, not only public members.
llvm-svn: 343036
2018-09-25 22:12:44 +00:00
Erik Pilkington be168a9d34 [Sema] P0961R1: Relaxing the structured bindings customization point finding rules
Differential revision: https://reviews.llvm.org/D50418

llvm-svn: 339375
2018-08-09 20:11:13 +00:00
Richard Smith b368ea865f Per C++ [over.match.copy]p1, direct-initialization of a reference can
only invoke converting constructors of the reference's underlying type.

llvm-svn: 336153
2018-07-02 23:25:22 +00:00
Richard Smith c00e44656d [c++17] If a class inherits virtual functions from a base class, it is
not an aggregtae.

llvm-svn: 334763
2018-06-14 20:03:22 +00:00
Mikhail Maltsev 2373516cad [Sema] Improve test coverage of narrowing conversion diagnostics
Summary:
This patch adds tests of narrowing conversion diagnostics for the
'unscoped enum -> integer' case.

Reviewers: faisalv, rsmith, rogfer01

Reviewed By: rogfer01

Subscribers: cfe-commits, rogfer01

Differential Revision: https://reviews.llvm.org/D43572

llvm-svn: 326551
2018-03-02 10:03:02 +00:00
Mikhail Maltsev 7b1a950c4a [Sema] Classify conversions from enum to float as narrowing
Summary:
According to [dcl.init.list]p7:
  A narrowing conversion is an implicit conversion
  - ...
  - from an integer type or unscoped enumeration type to a
    floating-point type, except where the source is a constant
    expression and the actual value after conversion will fit into
    the target type and will produce the original value when
    converted back to the original type, or
  - ...

Currently clang does not handle the 'unscoped enumeration' case. This
patch fixes the corresponding check.

Reviewers: faisalv, rsmith, rogfer01

Reviewed By: rogfer01

Subscribers: rogfer01, cfe-commits

Differential Revision: https://reviews.llvm.org/D42545

llvm-svn: 325668
2018-02-21 10:08:18 +00:00
Richard Smith 9fb4e60d7d PR33503: When a qualified name in a redeclaration names a prior declaration in
an inline namespace, update its semantic DeclContext to match.

We would previously get the semantic DeclContext wrong (pointing to the named
scope rather than the inline namespace within it), resulting in wrong lookup
results and linkage-related problems if the inline namespace was an anonymous
namespace.

llvm-svn: 321770
2018-01-03 23:03:54 +00:00
Aaron Ballman 8c20828b5c Re-commit r321223, which adds a printing policy to the ASTDumper.
This allows you to dump C++ code that spells bool instead of _Bool, leaves off the elaborated type specifiers when printing struct or class names, and other C-isms.

Fixes the -Wreorder issue and fixes the ast-dump-color.cpp test.

llvm-svn: 321310
2017-12-21 21:42:42 +00:00
Aaron Ballman 9d6501f6cd Reverting r321223 and its follow-up commit because of failing bots due to Misc/ast-dump-color.cpp.
llvm-svn: 321229
2017-12-20 23:17:29 +00:00
Aaron Ballman 207ee3d0a7 Add a printing policy to the ASTDumper.
This allows you to dump C++ code that spells bool instead of _Bool, leaves off the elaborated type specifiers when printing struct or class names, and other C-isms.

llvm-svn: 321223
2017-12-20 22:04:54 +00:00
Serge Pavlov 79271ab154 Do not inherit default arguments for friend function in class template.
A function declared in a friend declaration may have declarations prior
to the containing class definition. If such declaration defines default
argument, the friend function declaration inherits them. This behavior
causes problems if the class where the friend is declared is a template:
during the class instantiation the friend function looks like if it had
default arguments, so error is triggered.

With this change friend functions declared in class templates do not
inherit default arguments. Actual set of them will be defined at the
point where the containing class is instantiated.

This change fixes PR12724.

Differential Revision: https://reviews.llvm.org/D30393

llvm-svn: 304965
2017-06-08 06:31:19 +00:00
Vedant Kumar f4217f862a [Sema] Add lvalue-to-rvalue cast in direct-list-initialization of enum
After r264564, we allowed direct-list-initialization of an enum from an
integral value in C++1z mode, so long as that value can convert to the
enum's underlying type.

In this kind of initialization, we need a lvalue-to-rvalue conversion
for the initializer value if it is not a rvalue. This lets us accept the
following code:

  enum class A : unsigned {};
  A foo(unsigned x) { return A{x}; }

Differential Revision: https://reviews.llvm.org/D29723

llvm-svn: 295266
2017-02-16 01:20:00 +00:00
Richard Smith 836a3b416d PR31606: Generalize our tentative DR resolution for inheriting copy/move
constructors to better match the pre-P0136R1 behavior.

llvm-svn: 291955
2017-01-13 20:46:54 +00:00
Richard Smith 378b8c8f01 [c++1z] P0217R3: Allow by-value structured binding of arrays.
llvm-svn: 289630
2016-12-14 03:22:16 +00:00
Richard Smith b8c0f553ed DR1295 and cleanup for P0135R1: Make our initialization code more directly
mirror the description in the standard. Per DR1295, this means that binding a
const / rvalue reference to a bit-field no longer "binds directly", and per
P0135R1, this means that we materialize a temporary in reference binding
after adjusting cv-qualifiers and before performing a derived-to-base cast.

In C++11 onwards, this should have fixed the last case where we would
materialize a temporary of the wrong type (with a subobject adjustment inside
the MaterializeTemporaryExpr instead of outside), but we still have to deal
with that possibility in C++98, unless we want to start using xvalues to
represent materialized temporaries there too.

llvm-svn: 289250
2016-12-09 18:49:13 +00:00
Richard Smith 208732e954 [c++1z] P0490R0, NB comment GB 20: if std::tuple_size<T> is complete, use the
tuple-like interpretation of decomposition declaration even if there is no
::value member. We already did this, anticipating this resolution, just update
comments and tweak a testcase.

llvm-svn: 289021
2016-12-08 03:24:55 +00:00
Eric Fiselier 283d8d45db [Sema] Don't perform aggregate initialization for types with explicit constructors
Summary:
The C++17 rules for aggregate initialization changed to disallow types with explicit constructors [dcl.init.aggr]p1. This patch implements that new rule.


Reviewers: rsmith

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D25654

llvm-svn: 288565
2016-12-03 01:26:47 +00:00
Richard Smith 58b4ab845a Add another testcase missed from r284905.
llvm-svn: 284906
2016-10-22 01:32:45 +00:00
Richard Smith 3c4f8d2e96 P0012R1: Make exception specifications be part of the type system. This
implements the bulk of the change (modifying the type system to include
exception specifications), but not all the details just yet.

llvm-svn: 284337
2016-10-16 17:54:23 +00:00
Richard Smith 7c2bcc9eb0 Fix clang's handling of the copy performed in the second phase of class
copy-initialization. We previously got this wrong in a couple of ways:
 - we only looked for copy / move constructors and constructor templates for
   this copy, and thus would fail to copy in cases where doing so should use
   some other constructor (but see core issue 670),
 - we mishandled the special case for disabling user-defined conversions that
   blocks infinite recursion through repeated application of a copy constructor
   (applying it in slightly too many cases) -- though as far as I can tell,
   this does not ever actually affect the result of overload resolution, and
 - we misapplied the special-case rules for constructors taking a parameter
   whose type is a (reference to) the same class type by incorrectly assuming
   that only happens for copy/move constructors (it also happens for
   constructors instantiated from templates and those inherited from base
   classes).

These changes should only affect strange corner cases (for instance, where the
copy constructor exists but has a non-const-qualified parameter type), so for
the most part it only causes us to produce more 'candidate' notes, but see the
test changes for other cases whose behavior is affected.

llvm-svn: 280776
2016-09-07 02:14:33 +00:00
Richard Smith 53851923d5 Fix all tests under test/CXX (and test/Analysis) to pass if clang's default
C++ language standard is not C++98.

llvm-svn: 280309
2016-08-31 23:24:08 +00:00
Benjamin Kramer df8bf140dd [C++1z] Fix crash when decomposing structs with anonymous members.
The diagnostic format was invalid.

llvm-svn: 278487
2016-08-12 09:19:34 +00:00
Richard Smith dca60b4958 P0217R3: Constant expression evaluation for decomposition declarations.
llvm-svn: 278447
2016-08-12 00:39:32 +00:00
Richard Smith 7873de0cf6 P0217R3: Perform semantic checks and initialization for the bindings in a
decomposition declaration for arrays, aggregate-like structs, tuple-like
types, and (as an extension) for complex and vector types.

llvm-svn: 278435
2016-08-11 22:25:46 +00:00
Richard Trieu 891f0f176c Revert the bool portion of r267054
Remove the floating point to bool conversion warnings.  Some of these
conversions will be caught by -Wliteral-conversion and -Wfloat-conversion

llvm-svn: 267234
2016-04-22 22:14:32 +00:00
Richard Trieu be234c30ad Split interesting warnings off from -Wfloat-conversion
Restructure the implict floating point to integer conversions so that
interesting sub-groups are under different flags.  Breakdown of warnings:

No warning:
Exact conversions from floating point to integer:
int x = 10.0;
int x = 1e10;

-Wliteral-conversion - Floating point literal to integer with rounding:
int x = 5.5;
int x = -3.4;

-Wfloat-conversion - All conversions not covered by the above two:
int x = GetFloat();
int x = 5.5 + 3.5;

-Wfloat-zero-conversion - The expression converted has a non-zero floating
point value that gets converted to a zero integer value, excluded the cases
falling under -Wliteral-conversion.  Subset of -Wfloat-conversion.
int x = 1.0 / 2.0;

-Wfloat-overflow-conversion - The floating point value is outside the range
of the integer type, exluding cases from -Wliteral conversion.  Subset of
-Wfloat-conversion.
char x = 500;
char x = -1000;

-Wfloat-bool-conversion - Any conversion of a floating point type to bool.
Subset of -Wfloat-conversion.
if (GetFloat()) {}
bool x = 5.0;

-Wfloat-bool-constant-conversion - Conversion of a compile time evaluatable
floating point value to bool.  Subset of -Wfloat-bool-conversion.
bool x = 1.0;
bool x = 4.0 / 20.0;

Also add EvaluateAsFloat to Sema, which is similar to EvaluateAsInt, but for
floating point values.

llvm-svn: 267054
2016-04-21 21:04:55 +00:00
Reid Kleckner 25955f8590 Paper over the Windows-only enum initialization test failure until the bug is fixed
llvm-svn: 264642
2016-03-28 20:13:55 +00:00
Richard Smith ed638864d3 P0138R2: Allow direct-list-initialization of an enumeration from an integral
value that can convert to the enum's underlying type.

llvm-svn: 264564
2016-03-28 06:08:37 +00:00
Richard Smith 872307e2ac P0017R1: In C++1z, an aggregate class can have (public non-virtual) base classes; these are initialized as if they were data members.
llvm-svn: 262963
2016-03-08 22:17:41 +00:00