r245985 - Make sure that we evaluate __attribute__((enable_if)) on a method with no overloads. Patch by Ettore Speziale!

Nick Lewycky via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 25 15:33:16 PDT 2015


Author: nicholas
Date: Tue Aug 25 17:33:16 2015
New Revision: 245985

URL: http://llvm.org/viewvc/llvm-project?rev=245985&view=rev
Log:
Make sure that we evaluate __attribute__((enable_if)) on a method with no overloads. Patch by Ettore Speziale!

Modified:
    cfe/trunk/lib/Sema/SemaOverload.cpp
    cfe/trunk/test/SemaCXX/enable_if.cpp

Modified: cfe/trunk/lib/Sema/SemaOverload.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaOverload.cpp?rev=245985&r1=245984&r2=245985&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaOverload.cpp (original)
+++ cfe/trunk/lib/Sema/SemaOverload.cpp Tue Aug 25 17:33:16 2015
@@ -5826,10 +5826,11 @@ EnableIfAttr *Sema::CheckEnableIf(Functi
 
   SFINAETrap Trap(*this);
 
-  // Convert the arguments.
   SmallVector<Expr *, 16> ConvertedArgs;
   bool InitializationFailed = false;
   bool ContainsValueDependentExpr = false;
+
+  // Convert the arguments.
   for (unsigned i = 0, e = Args.size(); i != e; ++i) {
     if (i == 0 && !MissingImplicitThis && isa<CXXMethodDecl>(Function) &&
         !cast<CXXMethodDecl>(Function)->isStatic() &&
@@ -5863,6 +5864,28 @@ EnableIfAttr *Sema::CheckEnableIf(Functi
   if (InitializationFailed || Trap.hasErrorOccurred())
     return cast<EnableIfAttr>(Attrs[0]);
 
+  // Push default arguments if needed.
+  if (!Function->isVariadic() && Args.size() < Function->getNumParams()) {
+    for (unsigned i = Args.size(), e = Function->getNumParams(); i != e; ++i) {
+      ParmVarDecl *P = Function->getParamDecl(i);
+      ExprResult R = PerformCopyInitialization(
+          InitializedEntity::InitializeParameter(Context,
+                                                 Function->getParamDecl(i)),
+          SourceLocation(),
+          P->hasUninstantiatedDefaultArg() ? P->getUninstantiatedDefaultArg()
+                                           : P->getDefaultArg());
+      if (R.isInvalid()) {
+        InitializationFailed = true;
+        break;
+      }
+      ContainsValueDependentExpr |= R.get()->isValueDependent();
+      ConvertedArgs.push_back(R.get());
+    }
+
+    if (InitializationFailed || Trap.hasErrorOccurred())
+      return cast<EnableIfAttr>(Attrs[0]);
+  }
+
   for (AttrVec::iterator I = Attrs.begin(); I != E; ++I) {
     APValue Result;
     EnableIfAttr *EIA = cast<EnableIfAttr>(*I);
@@ -11604,6 +11627,16 @@ Sema::BuildCallToMemberFunction(Scope *S
     FoundDecl = MemExpr->getFoundDecl();
     Qualifier = MemExpr->getQualifier();
     UnbridgedCasts.restore();
+
+    if (const EnableIfAttr *Attr = CheckEnableIf(Method, Args, true)) {
+      Diag(MemExprE->getLocStart(),
+           diag::err_ovl_no_viable_member_function_in_call)
+          << Method << Method->getSourceRange();
+      Diag(Method->getLocation(),
+           diag::note_ovl_candidate_disabled_by_enable_if_attr)
+          << Attr->getCond()->getSourceRange() << Attr->getMessage();
+      return ExprError();
+    }
   } else {
     UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr);
     Qualifier = UnresExpr->getQualifier();

Modified: cfe/trunk/test/SemaCXX/enable_if.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/enable_if.cpp?rev=245985&r1=245984&r2=245985&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/enable_if.cpp (original)
+++ cfe/trunk/test/SemaCXX/enable_if.cpp Tue Aug 25 17:33:16 2015
@@ -11,6 +11,10 @@ struct X {
   void f(int n) __attribute__((enable_if(n == 0, "chosen when 'n' is zero")));
   void f(int n) __attribute__((enable_if(n == 1, "chosen when 'n' is one")));  // expected-note{{member declaration nearly matches}} expected-note{{candidate disabled: chosen when 'n' is one}}
 
+  void g(int n) __attribute__((enable_if(n == 0, "chosen when 'n' is zero")));  // expected-note{{candidate disabled: chosen when 'n' is zero}}
+
+  void h(int n, int m = 0) __attribute((enable_if(m == 0, "chosen when 'm' is zero")));  // expected-note{{candidate disabled: chosen when 'm' is zero}}
+
   static void s(int n) __attribute__((enable_if(n == 0, "chosen when 'n' is zero")));  // expected-note2{{candidate disabled: chosen when 'n' is zero}}
 
   void conflict(int n) __attribute__((enable_if(n+n == 10, "chosen when 'n' is five")));  // expected-note{{candidate function}}
@@ -40,6 +44,15 @@ void deprec2(int i) __attribute__((enabl
 void overloaded(int);
 void overloaded(long);
 
+struct Int {
+  constexpr Int(int i) : i(i) { }
+  constexpr operator int() const { return i; }
+  int i;
+};
+
+void default_argument(int n, int m = 0) __attribute__((enable_if(m == 0, "chosen when 'm' is zero")));  // expected-note{{candidate disabled: chosen when 'm' is zero}}
+void default_argument_promotion(int n, int m = Int(0)) __attribute__((enable_if(m == 0, "chosen when 'm' is zero")));  // expected-note{{candidate disabled: chosen when 'm' is zero}}
+
 struct Nothing { };
 template<typename T> void typedep(T t) __attribute__((enable_if(t, "")));  // expected-note{{candidate disabled:}}  expected-error{{value of type 'Nothing' is not contextually convertible to 'bool'}}
 template<int N> void valuedep() __attribute__((enable_if(N == 1, "")));
@@ -58,6 +71,12 @@ void test() {
   x.f(2);  // no error, suppressed by erroneous out-of-line definition
   x.f(3);  // expected-error{{no matching member function for call to 'f'}}
 
+  x.g(0);
+  x.g(1);  // expected-error{{no matching member function for call to 'g'}}
+
+  x.h(0);
+  x.h(1, 2);  // expected-error{{no matching member function for call to 'h'}}
+
   x.s(0);
   x.s(1);  // expected-error{{no matching member function for call to 's'}}
 
@@ -70,6 +89,12 @@ void test() {
 
   overloaded(x);
 
+  default_argument(0);
+  default_argument(1, 2);  // expected-error{{no matching function for call to 'default_argument'}}
+
+  default_argument_promotion(0);
+  default_argument_promotion(1, 2);  // expected-error{{no matching function for call to 'default_argument_promotion'}}
+
   int i = x(1);  // expected-error{{no matching function for call to object of type 'X'}}
 
   Nothing n;
@@ -88,6 +113,18 @@ template <class T> void test3() {
   fn3(sizeof(T) == 1);
 }
 
+template <typename T>
+struct Y {
+  T h(int n, int m = 0) __attribute__((enable_if(m == 0, "chosen when 'm' is zero")));  // expected-note{{candidate disabled: chosen when 'm' is zero}}
+};
+
+void test4() {
+  Y<int> y;
+
+  int t0 = y.h(0);
+  int t1 = y.h(1, 2);  // expected-error{{no matching member function for call to 'h'}}
+}
+
 // FIXME: issue an error (without instantiation) because ::h(T()) is not
 // convertible to bool, because return types aren't overloadable.
 void h(int);




More information about the cfe-commits mailing list