Commit Graph

3893 Commits

Author SHA1 Message Date
Faisal Vali a734ab9808 [Cxx1z-constexpr-lambda-P0170R1] Support parsing of constexpr specifier (and its inference) on lambda expressions
Support the constexpr specifier on lambda expressions - and support its inference from the lambda call operator's body.

i.e.
  auto L = [] () constexpr { return 5; };
  static_assert(L() == 5); // OK
  auto Implicit = [] (auto a) { return a; };
  static_assert(Implicit(5) == 5); 

We do not support evaluation of lambda's within constant expressions just yet.

Implementation Strategy:
  - teach ParseLambdaExpressionAfterIntroducer to expect a constexpr specifier and mark the invented function call operator's declarator's decl-specifier with it; Have it emit fixits for multiple decl-specifiers (mutable or constexpr) in this location.
  - for cases where constexpr is not explicitly specified, have buildLambdaExpr check whether the invented function call operator satisfies the requirements of a constexpr function, by calling CheckConstexprFunctionDecl/Body.

Much obliged to Richard Smith for his patience and his care, in ensuring the code is clang-worthy.

llvm-svn: 264513
2016-03-26 16:11:37 +00:00
Richard Smith 0e32c5283a Don't warn on "use" of undefined inline function that isn't actually an ODR
use. In order for this to fire, the function needed to be a templated function
marked 'constexpr' and declared but not defined. This weird pattern appears in
libstdc++'s alloc_traits.h.

llvm-svn: 264471
2016-03-25 22:29:27 +00:00
Richard Smith d6a04d79c7 Store list of undefined-but-used objects in a deterministic order to fix
non-deterministic diagnostics (and non-deterministic PCH files). Check these
when building a module rather than serializing it; it's not reasonable for a
module's use to be satisfied by a definition in the user of the module.

llvm-svn: 264466
2016-03-25 21:49:43 +00:00
Richard Smith 95853077c3 Fix nondeterminism in computation of builtin operator overload sets.
llvm-svn: 264363
2016-03-25 00:08:53 +00:00
Richard Smith 6642bb3337 Change ADL to produce lookup results in a deterministic order. This fixes some
rare issues with nondeterministic diagnostic order, and some very common issues
with nondeterministic module builds.

llvm-svn: 264323
2016-03-24 19:12:22 +00:00
Richard Smith fd55fc86ed Make SemaAccess smarter about determining when a dependent class might
instantiate to match a friend class declaration. It's still pretty dumb,
though.

llvm-svn: 264189
2016-03-23 20:39:06 +00:00
Richard Smith c7649dc749 Make sure to perform dependent access checks when instantiating a
lambda-expression. We don't actually instantiate the closure type / operator()
in the template in order to produce the closure type / operator() in the
instantiation, so this isn't caught by the normal path.

llvm-svn: 264184
2016-03-23 20:07:07 +00:00
Manman Ren 75bc676160 Add replacement = "xxx" to AvailabilityAttr.
This commit adds a named argument to AvailabilityAttr, while r263652 adds an
optional string argument to __attribute__((deprecated)).

This was commited in r263687 and reverted in 263752 due to misaligned
access.

rdar://20588929

llvm-svn: 263958
2016-03-21 17:30:55 +00:00
Faisal Vali dc6b596ebb [Cxx1z] Implement Lambda Capture of *this by Value as [=,*this] (P0018R3)
Implement lambda capture of *this by copy.
For e.g.:
struct A {

  int d = 10;
  auto foo() { return [*this] (auto a) mutable { d+=a; return d; }; }

};

auto L = A{}.foo(); // A{}'s lifetime is gone.

// Below is still ok, because *this was captured by value.
assert(L(10) == 20);
assert(L(100) == 120);

If the capture was implicit, or [this] (i.e. *this was captured by reference), this code would be otherwise undefined.

Implementation Strategy:
  - amend the parser to accept *this in the lambda introducer
  - add a new king of capture LCK_StarThis
  - teach Sema::CheckCXXThisCapture to handle by copy captures of the
    enclosing object (i.e. *this)
  - when CheckCXXThisCapture does capture by copy, the corresponding 
    initializer expression for the closure's data member 
    direct-initializes it thus making a copy of '*this'.
  - in codegen, when assigning to CXXThisValue, if *this was captured by 
    copy, make sure it points to the corresponding field member, and
    not, unlike when captured by reference, what the field member points
    to.
  - mark feature as implemented in svn

Much gratitude to Richard Smith for his carefully illuminating reviews!   

llvm-svn: 263921
2016-03-21 09:25:37 +00:00
George Burgess IV cc2f355f71 [Sema] Make type deduction work with some overloadable functions
Some functions can't have their address taken. If we encounter an
overload set where only one of the candidates can have its address
taken, we should automatically select that candidate's type in type
deduction.

Differential Revision: http://reviews.llvm.org/D15591

llvm-svn: 263888
2016-03-19 21:51:45 +00:00
George Burgess IV 3cde9bf9d5 [Sema] Allow casting of some overloaded functions
Some functions can't have their address taken. If we encounter an
overload set where only one of the candidates can have its address
taken, we should automatically select that candidate in cast
expressions.

Differential Revision: http://reviews.llvm.org/D17701

llvm-svn: 263887
2016-03-19 21:36:10 +00:00
Manman Ren 34888f86ef Revert r263687 for ubsan bot failure.
llvm-svn: 263752
2016-03-17 22:13:50 +00:00
Manman Ren a7c4760c8e Add an optional named argument (replacement = "xxx") to AvailabilityAttr.
This commit adds a named argument to AvailabilityAttr, while r263652 adds an
optional string argument to __attribute__((deprecated)). This enables the
compiler to provide Fix-Its for deprecated declarations.

rdar://20588929

llvm-svn: 263687
2016-03-17 03:09:55 +00:00
Manman Ren c7890fed01 Add an optional string argument to DeprecatedAttr for Fix-It.
We only add this to __attribute__((deprecated)).

Differential Revision: http://reviews.llvm.org/D17865

llvm-svn: 263652
2016-03-16 18:50:49 +00:00
Olivier Goffart b94ed61452 Fix destructor definition of invalid classes
The declaration of the destructor of an invalid class was not properly marked
as noexcept. As a result, the definition of the same destructor, which was
properly implicitly marked as noexcept, would not match the definition.
This would cause the definition CXXDestructorDecl to be matked as invalid
and omited from the AST.

Differential Revision: http://reviews.llvm.org/D17988

llvm-svn: 263639
2016-03-16 14:36:11 +00:00
Bob Wilson 57819fc809 Move the fixit for -Wformat-security to a note.
r263299 added a fixit for the -Wformat-security warning, but that runs
into complications with our guideline that error recovery should be done
as-if the fixit had been applied. Putting the fixit on a note avoids that.

llvm-svn: 263584
2016-03-15 20:56:38 +00:00
Richard Smith 4d3d785e3f Add test for r263138.
llvm-svn: 263155
2016-03-10 19:22:21 +00:00
Dmitry Polukhin bf17ecf59a [GCC] PR23529 Sema part of attrbute abi_tag support
Original patch by Stefan Bühler http://reviews.llvm.org/D12834

Difference between original and this one:
- fixed all comments in original code review
- added more tests, all new diagnostics now covered by tests
- moved abi_tag on re-declaration checks to Sema::mergeDeclAttributes
  where they actually may work as designed
- clang-format + other stylistic changes

Mangle part will be sent for review as a separate patch.

Differential Revision: http://reviews.llvm.org/D17567

llvm-svn: 263015
2016-03-09 15:30:53 +00:00
Richard Smith bbbe618467 Fix crash in access check for aggregate initialization of base classes. It's
not obvious how to access-check these, so pick a conservative rule until we get
feedback from CWG.

llvm-svn: 262966
2016-03-08 23:17:35 +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
Duncan P. N. Exon Smith 8536392a83 Sema: Methods in unavailable classes are unavailable
Similar to the template cases in r262050, when a C++ method in an
unavailable struct/class calls unavailable API, don't diagnose an error.
I.e., this case was failing:

    void foo() __attribute__((unavailable));
    struct __attribute__((unavailable)) A {
      void bar() { foo(); }
    };

Since A is unavailable, A::bar is allowed to call foo.  However, we were
emitting a diagnostic here.  This commit checks up the context chain
from A::bar, in a manner inspired by SemaDeclAttr.cpp:isDeclUnavailable.

I expected to find other related issues but failed to trigger them:

- I wondered if DeclBase::getAvailability should check for
  `TemplateDecl` instead of `FunctionTemplateDecl`, but I couldn't find
  a way to trigger this.  I left behind a few extra tests to make sure
  we don't regress.

- I wondered if Sema::isFunctionConsideredUnavailable should be
  symmetric, checking up the context chain of the callee (this commit
  only checks up the context chain of the caller).  However, I couldn't
  think of a testcase that didn't require first referencing the
  unavailable type; this, we already diagnose.

rdar://problem/25030656

llvm-svn: 262921
2016-03-08 10:28:52 +00:00
Richard Smith e513cd25bd Move [[nodiscard]] tests into test/CXX tree.
llvm-svn: 262888
2016-03-08 00:44:49 +00:00
Richard Smith d5d796ecde Define __has_cpp_attribute(fallthrough) to a more reasonable value. (What year is it?!)
llvm-svn: 262887
2016-03-08 00:40:32 +00:00
Richard Smith 4f902c7ecc P0188R1: add support for standard [[fallthrough]] attribute. This is almost
exactly the same as clang's existing [[clang::fallthrough]] attribute, which
has been updated to have the same semantics. The one significant difference
is that [[fallthrough]] is ill-formed if it's not used immediately before a
switch label (even when -Wimplicit-fallthrough is disabled). To support that,
we now build a CFG of any function that uses a '[[fallthrough]];' statement
to check.

In passing, fix some bugs with our support for statement attributes -- in
particular, diagnose their use on declarations, rather than asserting.

llvm-svn: 262881
2016-03-08 00:32:55 +00:00
Aaron Ballman e7964789da Implement support for [[nodiscard]] in C++1z that is based off existing support for warn_unused_result, and treat it as an extension pre-C++1z. This also means extending the existing warn_unused_result attribute so that it can be placed on an enum as well as a class.
llvm-svn: 262872
2016-03-07 22:44:55 +00:00
Richard Trieu 2334a30e15 Add null check to diagnostic path for lambda captures.
Previously, the failed capture of a variable in nested lambdas may crash when
the lambda pointer is null.  Only give the note if a location can be retreived
from the lambda pointer.

llvm-svn: 262765
2016-03-05 04:04:57 +00:00
David Blaikie ac92893a93 PR5941 - improve diagnostic for * vs & confusion when choosing overload candidate with a parameter of incomplete (ref or pointer) type
Reviewers: dblaikie

Differential Revision: http://reviews.llvm.org/D16949

llvm-svn: 262752
2016-03-04 22:29:11 +00:00
John McCall 477f2bb0d5 Semantic analysis for the swiftcall calling convention.
I've tried to keep the infrastructure behind parameter ABI
treatments fairly general.

llvm-svn: 262587
2016-03-03 06:39:32 +00:00
John McCall 3b5a8f5ffc Improve some infrastructure for extended parameter infos and
fix a bug with the instantiation of ns_consumed parameter
attributes in ARC.

llvm-svn: 262551
2016-03-03 00:10:03 +00:00
Richard Smith e5a91464bc Fix bug in using shadow decl checking: a using shadow decl should not conflict
with a prior UsingDecl -- those should not even really be found by the lookup
here, except that we use the same lookup results for two different checks, and
the other check needs them.

This happens to work in *almost all* cases, because either the lookup results
list the UsingDecl first (and the NonTag result gets replaced by something
else) or because the problematic declaration is a function (which causes us to
use different logic to detect conflicts). This can also be triggered from a
state only reachable through modules (where the name lookup results can contain
multiple UsingDecls in the same scope).

llvm-svn: 262105
2016-02-27 02:36:43 +00:00
Paul Robinson 65ab102be3 Fix Clang tests that used CHECK-NEXT-NOT and CHECK-DAG-NOT.
FileCheck actually doesn't support combo suffixes.

Differential Revision: http://reviews.llvm.org/D17589

llvm-svn: 262052
2016-02-26 19:34:01 +00:00
Duncan P. N. Exon Smith ec599a906b SemaCXX: Support templates in availability attributes
If the availability context is `FunctionTemplateDecl`, we should look
through it to the `FunctionDecl`.  This prevents a diagnostic in the
following case:

    class C __attribute__((unavailable));
    template <class T> void foo(C&) __attribute__((unavailable));

This adds tests for availability in templates in many other cases, but
that was the only case that failed before this patch.

I added a feature `__has_feature(attribute_availability_in_templates)`
so users can test for this.

rdar://problem/24561029

llvm-svn: 262050
2016-02-26 19:27:00 +00:00
David Majnemer e9807b28af [MSVC Compat] Don't evaluate member base expressions w/o side effects
A member expression's base doesn't always have an impact on what the
member decl would evaluate to.  In such a case, the base is used as a
poor man's scope qualifier.

This fixes PR26738.

Differential Revision: http://reviews.llvm.org/D17619

llvm-svn: 261975
2016-02-26 04:23:19 +00:00
Nico Weber 72c57f49c4 Fix rejects-valid caused by r261297.
r261297 called hasUserProvidedDefaultConstructor() to check if defining a
const object is ok.  This is incorrect for this example:

  struct X { template<typename ...T> X(T...); int n; };
  const X x; // formerly OK, now bogus error

Instead, track if a class has a defaulted default constructor, and disallow
a const object for classes that either have defaulted default constructors or
if they need an implicit constructor.

Bug report and fix approach by Richard Smith, thanks!

llvm-svn: 261770
2016-02-24 20:58:14 +00:00
Faisal Vali e7f8fb9835 Fix PR24473 : Teach clang to remember to substitute into member variable templates referred to within dependent qualified ids.
In passing also fix a semi-related bug that allows access to variable templates through member access notation.

llvm-svn: 261506
2016-02-22 02:24:29 +00:00
David Majnemer c919f5f964 Correct typos after acting on invalid subscript expressions
llvm-svn: 261312
2016-02-19 07:15:33 +00:00
Nico Weber 6a6376b17c Implement the likely resolution of core issue 253.
C++11 requires const objects to have a user-provided constructor, even for
classes without any fields. DR 253 relaxes this to say "If the implicit default
constructor initializes all subobjects, no initializer should be required."

clang is currently the only compiler that implements this C++11 rule, and e.g.
libstdc++ relies on something like DR 253 to compile in newer versions.  This
change  makes it possible to build code that says `const vector<int> v;' again
when using libstdc++5.2 and _GLIBCXX_DEBUG
(https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60284).

Fixes PR23381.

http://reviews.llvm.org/D16552

llvm-svn: 261297
2016-02-19 01:52:46 +00:00
Reid Kleckner bde5ede526 [Sema] PR25181 Fix crash when method declaration with throw spec fails to parse correctly
Fixes crash referenced in PR25181 where dyn_cast is called on a null
instance of LM.Method.

Reviewers: majnemer, rnk

Patch by Don Hinton

Differential Revision: http://reviews.llvm.org/D17072

llvm-svn: 261292
2016-02-19 01:15:08 +00:00
Richard Trieu faca2d83b1 Add -Wcomma warning to Clang.
-Wcomma will detect and warn on most uses of the builtin comma operator.  It
currently whitelists the first and third statements of the for-loop.  For other
cases, the warning can be silenced by casting the first operand of the comma
operator to void.

Differential Revision: http://reviews.llvm.org/D3976

llvm-svn: 261278
2016-02-18 23:58:40 +00:00
David Majnemer bc24cb5fb5 [Parse] Make sure we don't forget to diagnose typos in exprs
If ActOn*Op fails, we will forget to diagnose typos in the LHS of
expressions.

llvm-svn: 261191
2016-02-18 06:37:44 +00:00
David Majnemer 2eb74e278d Correct more typos in conditional expressions
We didn't correctly handle some edge cases, causing us to bail out
before correcting all the typos.

llvm-svn: 261109
2016-02-17 17:19:00 +00:00
Richard Smith c28aee6a51 Improve diagnostics for ill-formed literal operator declarations.
Patch by Erik Pilkington!

llvm-svn: 261034
2016-02-17 00:04:04 +00:00
Reid Kleckner 4ce625c814 [typo-correction] Apply name specifier corrections when forming a NNS
Previously we would leave behind the old name specifier prefix, which
creates an invalid AST.  Other callers of CorrectTypo update their
CXXScopeSpec objects with the correction specifier if one is present.

llvm-svn: 260993
2016-02-16 19:16:20 +00:00
Andrey Bokhanko c4feaf0272 PR26449: Tests for builtin_classify_type fix
In my previous commit (rL260881) I forget to svn add tests. This commit adds
them.

Differential Revision: http://reviews.llvm.org/D16846

llvm-svn: 260882
2016-02-15 10:44:27 +00:00
Saleem Abdulrasool cfd4553a7c Sema: prevent assertion on stack return checking
In the case that the array indexing itself is within a type dependent context,
bail out of the evaluation.  We would previously try to symbolically evaluate
the expression which would then try to evaluate a non-address expression as an
address, triggering an assertion in Asserts builds.

We only need to consider the array subscript expression itself as in the case
that the base itself being type dependent is handled appropriately in EvalAddr.

Resolves PR26599.

llvm-svn: 260867
2016-02-15 01:51:24 +00:00
Richard Trieu 0a5e166a0b Make -Wnull-conversion more useful.
When a null constant is used in a macro, walk through the macro stack to
determine where the null constant is written and where the context is located.
Only warn if both locations are within the same macro expansion.  This helps
function-like macros which involve pointers be treated as if they were
functions.

llvm-svn: 260776
2016-02-13 00:58:53 +00:00
Olivier Goffart 8bc0caa2e9 Fix ICE with constexpr and friend functions
Fix a crash while parsing this code:

  struct X  {
    friend constexpr int foo(X*) { return 12; }
    static constexpr int j = foo(static_cast<X*>(nullptr));
  };

Differential Revision: http://reviews.llvm.org/D16973

llvm-svn: 260675
2016-02-12 12:34:44 +00:00
Manman Ren b4e8a1b308 Fix a crash when there is a typo in the return statement.
If the typo happens after a successful deduction for an earlier
return statement, we should check if the deduced type is null
before using it.

The typo correction happens after we try to deduce the return
type and we ignore the deduction from the typo and continue
to typo correction.

rdar://24342247

llvm-svn: 259820
2016-02-04 20:05:40 +00:00
Douglas Gregor 0fc3a00168 [Sema debugger support] Require non-void types to be complete in unknown-anytype casts.
When performing a cast from an __unknown_anytype function call to a
non-void type, we need to make sure that type is complete. Fixes
rdar://problem/23959960.

llvm-svn: 259681
2016-02-03 19:13:08 +00:00
Richard Smith 59b982e1be PR24989: Stop trying to use the C++11 rules for lambda return type inference in
C++14 generic lambdas. It conflicts with the C++14 return type deduction
mechanism, and results in us failing to actually deduce the lambda's return
type in some cases.

llvm-svn: 259609
2016-02-02 23:58:56 +00:00