Add the diagnose_if attribute to clang.

`diagnose_if` can be used to have clang emit either warnings or errors
for function calls that meet user-specified conditions. For example:

```
constexpr int foo(int a)
  __attribute__((diagnose_if(a > 10, "configurations with a > 10 are "
                                      "expensive.", "warning")));

int f1 = foo(9);
int f2 = foo(10); // warning: configuration with a > 10 are expensive.
int f3 = foo(f2);
```

It currently only emits diagnostics in cases where the condition is
guaranteed to always be true. So, the following code will emit no
warnings:

```
constexpr int bar(int a) {
  foo(a);
  return 0;
}

constexpr int i = bar(10);
```

We hope to support optionally emitting diagnostics for cases like that
(and emitting runtime checks) in the future.

Release notes will appear shortly. :)

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

llvm-svn: 291418
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 1550303..c0321d3 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -29,6 +29,7 @@
 #include "clang/Sema/Template.h"
 #include "clang/Sema/TemplateDeduction.h"
 #include "llvm/ADT/DenseSet.h"
+#include "llvm/ADT/Optional.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallString.h"
@@ -830,12 +831,20 @@
 
 void OverloadCandidateSet::clear() {
   destroyCandidates();
-  ConversionSequenceAllocator.Reset();
-  NumInlineSequences = 0;
+  // DiagnoseIfAttrs are just pointers, so we don't need to destroy them.
+  SlabAllocator.Reset();
+  NumInlineBytesUsed = 0;
   Candidates.clear();
   Functions.clear();
 }
 
+DiagnoseIfAttr **
+OverloadCandidateSet::addDiagnoseIfComplaints(ArrayRef<DiagnoseIfAttr *> CA) {
+  auto *DIA = slabAllocate<DiagnoseIfAttr *>(CA.size());
+  std::uninitialized_copy(CA.begin(), CA.end(), DIA);
+  return DIA;
+}
+
 namespace {
   class UnbridgedCastsSet {
     struct Entry {
@@ -5814,6 +5823,28 @@
   return false;
 }
 
+static void initDiagnoseIfComplaint(Sema &S, OverloadCandidateSet &CandidateSet,
+                                    OverloadCandidate &Candidate,
+                                    FunctionDecl *Function,
+                                    ArrayRef<Expr *> Args,
+                                    bool MissingImplicitThis = false,
+                                    Expr *ExplicitThis = nullptr) {
+  SmallVector<DiagnoseIfAttr *, 8> Results;
+  if (DiagnoseIfAttr *DIA = S.checkArgDependentDiagnoseIf(
+          Function, Args, Results, MissingImplicitThis, ExplicitThis)) {
+    Results.clear();
+    Results.push_back(DIA);
+  }
+
+  Candidate.NumTriggeredDiagnoseIfs = Results.size();
+  if (Results.empty())
+    Candidate.DiagnoseIfInfo = nullptr;
+  else if (Results.size() == 1)
+    Candidate.DiagnoseIfInfo = Results[0];
+  else
+    Candidate.DiagnoseIfInfo = CandidateSet.addDiagnoseIfComplaints(Results);
+}
+
 /// AddOverloadCandidate - Adds the given function to the set of
 /// candidate functions, using the given function call arguments.  If
 /// @p SuppressUserConversions, then don't allow user-defined
@@ -5847,8 +5878,8 @@
       // is irrelevant.
       AddMethodCandidate(Method, FoundDecl, Method->getParent(),
                          QualType(), Expr::Classification::makeSimpleLValue(),
-                         Args, CandidateSet, SuppressUserConversions,
-                         PartialOverloading);
+                         /*ThisArg=*/nullptr, Args, CandidateSet,
+                         SuppressUserConversions, PartialOverloading);
       return;
     }
     // We treat a constructor like a non-member function, since its object
@@ -6008,6 +6039,8 @@
     Candidate.FailureKind = ovl_fail_ext_disabled;
     return;
   }
+
+  initDiagnoseIfComplaint(*this, CandidateSet, Candidate, Function, Args);
 }
 
 ObjCMethodDecl *
@@ -6120,66 +6153,87 @@
   return Result;
 }
 
-EnableIfAttr *Sema::CheckEnableIf(FunctionDecl *Function, ArrayRef<Expr *> Args,
-                                  bool MissingImplicitThis) {
-  auto EnableIfAttrs = getOrderedEnableIfAttrs(Function);
-  if (EnableIfAttrs.empty())
-    return nullptr;
-
-  SFINAETrap Trap(*this);
-  SmallVector<Expr *, 16> ConvertedArgs;
-  bool InitializationFailed = false;
+static bool
+convertArgsForAvailabilityChecks(Sema &S, FunctionDecl *Function, Expr *ThisArg,
+                                 ArrayRef<Expr *> Args, Sema::SFINAETrap &Trap,
+                                 bool MissingImplicitThis, Expr *&ConvertedThis,
+                                 SmallVectorImpl<Expr *> &ConvertedArgs) {
+  if (ThisArg) {
+    CXXMethodDecl *Method = cast<CXXMethodDecl>(Function);
+    assert(!isa<CXXConstructorDecl>(Method) &&
+           "Shouldn't have `this` for ctors!");
+    assert(!Method->isStatic() && "Shouldn't have `this` for static methods!");
+    ExprResult R = S.PerformObjectArgumentInitialization(
+        ThisArg, /*Qualifier=*/nullptr, Method, Method);
+    if (R.isInvalid())
+      return false;
+    ConvertedThis = R.get();
+  } else {
+    if (auto *MD = dyn_cast<CXXMethodDecl>(Function)) {
+      (void)MD;
+      assert((MissingImplicitThis || MD->isStatic() ||
+              isa<CXXConstructorDecl>(MD)) &&
+             "Expected `this` for non-ctor instance methods");
+    }
+    ConvertedThis = nullptr;
+  }
 
   // Ignore any variadic arguments. Converting them is pointless, since the
-  // user can't refer to them in the enable_if condition.
+  // user can't refer to them in the function condition.
   unsigned ArgSizeNoVarargs = std::min(Function->param_size(), Args.size());
 
   // Convert the arguments.
   for (unsigned I = 0; I != ArgSizeNoVarargs; ++I) {
     ExprResult R;
-    if (I == 0 && !MissingImplicitThis && isa<CXXMethodDecl>(Function) &&
-        !cast<CXXMethodDecl>(Function)->isStatic() &&
-        !isa<CXXConstructorDecl>(Function)) {
-      CXXMethodDecl *Method = cast<CXXMethodDecl>(Function);
-      R = PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/nullptr,
-                                              Method, Method);
-    } else {
-      R = PerformCopyInitialization(InitializedEntity::InitializeParameter(
-                                        Context, Function->getParamDecl(I)),
+    R = S.PerformCopyInitialization(InitializedEntity::InitializeParameter(
+                                        S.Context, Function->getParamDecl(I)),
                                     SourceLocation(), Args[I]);
-    }
 
-    if (R.isInvalid()) {
-      InitializationFailed = true;
-      break;
-    }
+    if (R.isInvalid())
+      return false;
 
     ConvertedArgs.push_back(R.get());
   }
 
-  if (InitializationFailed || Trap.hasErrorOccurred())
-    return EnableIfAttrs[0];
+  if (Trap.hasErrorOccurred())
+    return false;
 
   // 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,
+      ExprResult R = S.PerformCopyInitialization(
+          InitializedEntity::InitializeParameter(S.Context,
                                                  Function->getParamDecl(i)),
           SourceLocation(),
           P->hasUninstantiatedDefaultArg() ? P->getUninstantiatedDefaultArg()
                                            : P->getDefaultArg());
-      if (R.isInvalid()) {
-        InitializationFailed = true;
-        break;
-      }
+      if (R.isInvalid())
+        return false;
       ConvertedArgs.push_back(R.get());
     }
 
-    if (InitializationFailed || Trap.hasErrorOccurred())
-      return EnableIfAttrs[0];
+    if (Trap.hasErrorOccurred())
+      return false;
   }
+  return true;
+}
+
+EnableIfAttr *Sema::CheckEnableIf(FunctionDecl *Function, ArrayRef<Expr *> Args,
+                                  bool MissingImplicitThis) {
+  SmallVector<EnableIfAttr *, 4> EnableIfAttrs =
+      getOrderedEnableIfAttrs(Function);
+  if (EnableIfAttrs.empty())
+    return nullptr;
+
+  SFINAETrap Trap(*this);
+  SmallVector<Expr *, 16> ConvertedArgs;
+  // FIXME: We should look into making enable_if late-parsed.
+  Expr *DiscardedThis;
+  if (!convertArgsForAvailabilityChecks(
+          *this, Function, /*ThisArg=*/nullptr, Args, Trap,
+          /*MissingImplicitThis=*/true, DiscardedThis, ConvertedArgs))
+    return EnableIfAttrs[0];
 
   for (auto *EIA : EnableIfAttrs) {
     APValue Result;
@@ -6195,6 +6249,87 @@
   return nullptr;
 }
 
+static bool gatherDiagnoseIfAttrs(FunctionDecl *Function, bool ArgDependent,
+                                  SmallVectorImpl<DiagnoseIfAttr *> &Errors,
+                                  SmallVectorImpl<DiagnoseIfAttr *> &Nonfatal) {
+  for (auto *DIA : Function->specific_attrs<DiagnoseIfAttr>())
+    if (ArgDependent == DIA->getArgDependent()) {
+      if (DIA->isError())
+        Errors.push_back(DIA);
+      else
+        Nonfatal.push_back(DIA);
+    }
+
+  return !Errors.empty() || !Nonfatal.empty();
+}
+
+template <typename CheckFn>
+static DiagnoseIfAttr *
+checkDiagnoseIfAttrsWith(const SmallVectorImpl<DiagnoseIfAttr *> &Errors,
+                         SmallVectorImpl<DiagnoseIfAttr *> &Nonfatal,
+                         CheckFn &&IsSuccessful) {
+  // Note that diagnose_if attributes are late-parsed, so they appear in the
+  // correct order (unlike enable_if attributes).
+  auto ErrAttr = llvm::find_if(Errors, IsSuccessful);
+  if (ErrAttr != Errors.end())
+    return *ErrAttr;
+
+  llvm::erase_if(Nonfatal, [&](DiagnoseIfAttr *A) { return !IsSuccessful(A); });
+  return nullptr;
+}
+
+DiagnoseIfAttr *
+Sema::checkArgDependentDiagnoseIf(FunctionDecl *Function, ArrayRef<Expr *> Args,
+                                  SmallVectorImpl<DiagnoseIfAttr *> &Nonfatal,
+                                  bool MissingImplicitThis,
+                                  Expr *ThisArg) {
+  SmallVector<DiagnoseIfAttr *, 4> Errors;
+  if (!gatherDiagnoseIfAttrs(Function, /*ArgDependent=*/true, Errors, Nonfatal))
+    return nullptr;
+
+  SFINAETrap Trap(*this);
+  SmallVector<Expr *, 16> ConvertedArgs;
+  Expr *ConvertedThis;
+  if (!convertArgsForAvailabilityChecks(*this, Function, ThisArg, Args, Trap,
+                                        MissingImplicitThis, ConvertedThis,
+                                        ConvertedArgs))
+    return nullptr;
+
+  return checkDiagnoseIfAttrsWith(Errors, Nonfatal, [&](DiagnoseIfAttr *DIA) {
+    APValue Result;
+    // It's sane to use the same ConvertedArgs for any redecl of this function,
+    // since EvaluateWithSubstitution only cares about the position of each
+    // argument in the arg list, not the ParmVarDecl* it maps to.
+    if (!DIA->getCond()->EvaluateWithSubstitution(
+            Result, Context, DIA->getParent(), ConvertedArgs, ConvertedThis))
+      return false;
+    return Result.isInt() && Result.getInt().getBoolValue();
+  });
+}
+
+DiagnoseIfAttr *Sema::checkArgIndependentDiagnoseIf(
+    FunctionDecl *Function, SmallVectorImpl<DiagnoseIfAttr *> &Nonfatal) {
+  SmallVector<DiagnoseIfAttr *, 4> Errors;
+  if (!gatherDiagnoseIfAttrs(Function, /*ArgDependent=*/false, Errors,
+                             Nonfatal))
+    return nullptr;
+
+  return checkDiagnoseIfAttrsWith(Errors, Nonfatal, [&](DiagnoseIfAttr *DIA) {
+    bool Result;
+    return DIA->getCond()->EvaluateAsBooleanCondition(Result, Context) &&
+           Result;
+  });
+}
+
+void Sema::emitDiagnoseIfDiagnostic(SourceLocation Loc,
+                                    const DiagnoseIfAttr *DIA) {
+  auto Code = DIA->isError() ? diag::err_diagnose_if_succeeded
+                             : diag::warn_diagnose_if_succeeded;
+  Diag(Loc, Code) << DIA->getMessage();
+  Diag(DIA->getLocation(), diag::note_from_diagnose_if)
+      << DIA->getParent() << DIA->getCond()->getSourceRange();
+}
+
 /// \brief Add all of the function declarations in the given function set to
 /// the overload candidate set.
 void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns,
@@ -6210,7 +6345,7 @@
         AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(),
                            cast<CXXMethodDecl>(FD)->getParent(),
                            Args[0]->getType(), Args[0]->Classify(Context),
-                           Args.slice(1), CandidateSet,
+                           Args[0], Args.slice(1), CandidateSet,
                            SuppressUserConversions, PartialOverloading);
       else
         AddOverloadCandidate(FD, F.getPair(), Args, CandidateSet,
@@ -6219,13 +6354,12 @@
       FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D);
       if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) &&
           !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic())
-        AddMethodTemplateCandidate(FunTmpl, F.getPair(),
-                              cast<CXXRecordDecl>(FunTmpl->getDeclContext()),
-                                   ExplicitTemplateArgs,
-                                   Args[0]->getType(),
-                                   Args[0]->Classify(Context), Args.slice(1),
-                                   CandidateSet, SuppressUserConversions,
-                                   PartialOverloading);
+        AddMethodTemplateCandidate(
+            FunTmpl, F.getPair(),
+            cast<CXXRecordDecl>(FunTmpl->getDeclContext()),
+            ExplicitTemplateArgs, Args[0]->getType(),
+            Args[0]->Classify(Context), Args[0], Args.slice(1), CandidateSet,
+            SuppressUserConversions, PartialOverloading);
       else
         AddTemplateOverloadCandidate(FunTmpl, F.getPair(),
                                      ExplicitTemplateArgs, Args,
@@ -6240,6 +6374,7 @@
 void Sema::AddMethodCandidate(DeclAccessPair FoundDecl,
                               QualType ObjectType,
                               Expr::Classification ObjectClassification,
+                              Expr *ThisArg,
                               ArrayRef<Expr *> Args,
                               OverloadCandidateSet& CandidateSet,
                               bool SuppressUserConversions) {
@@ -6255,12 +6390,12 @@
     AddMethodTemplateCandidate(TD, FoundDecl, ActingContext,
                                /*ExplicitArgs*/ nullptr,
                                ObjectType, ObjectClassification,
-                               Args, CandidateSet,
+                               ThisArg, Args, CandidateSet,
                                SuppressUserConversions);
   } else {
     AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext,
                        ObjectType, ObjectClassification,
-                       Args,
+                       ThisArg, Args,
                        CandidateSet, SuppressUserConversions);
   }
 }
@@ -6276,7 +6411,7 @@
 Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl,
                          CXXRecordDecl *ActingContext, QualType ObjectType,
                          Expr::Classification ObjectClassification,
-                         ArrayRef<Expr *> Args,
+                         Expr *ThisArg, ArrayRef<Expr *> Args,
                          OverloadCandidateSet &CandidateSet,
                          bool SuppressUserConversions,
                          bool PartialOverloading) {
@@ -6393,6 +6528,9 @@
     Candidate.DeductionFailure.Data = FailedAttr;
     return;
   }
+
+  initDiagnoseIfComplaint(*this, CandidateSet, Candidate, Method, Args,
+                          /*MissingImplicitThis=*/!ThisArg, ThisArg);
 }
 
 /// \brief Add a C++ member function template as a candidate to the candidate
@@ -6405,6 +6543,7 @@
                                  TemplateArgumentListInfo *ExplicitTemplateArgs,
                                  QualType ObjectType,
                                  Expr::Classification ObjectClassification,
+                                 Expr *ThisArg,
                                  ArrayRef<Expr *> Args,
                                  OverloadCandidateSet& CandidateSet,
                                  bool SuppressUserConversions,
@@ -6445,8 +6584,9 @@
   assert(isa<CXXMethodDecl>(Specialization) &&
          "Specialization is not a member function?");
   AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl,
-                     ActingContext, ObjectType, ObjectClassification, Args,
-                     CandidateSet, SuppressUserConversions, PartialOverloading);
+                     ActingContext, ObjectType, ObjectClassification,
+                     /*ThisArg=*/ThisArg, Args, CandidateSet,
+                     SuppressUserConversions, PartialOverloading);
 }
 
 /// \brief Add a C++ function template specialization as a candidate
@@ -6702,6 +6842,8 @@
     Candidate.DeductionFailure.Data = FailedAttr;
     return;
   }
+
+  initDiagnoseIfComplaint(*this, CandidateSet, Candidate, Conversion, None, false, From);
 }
 
 /// \brief Adds a conversion function template specialization
@@ -6854,6 +6996,8 @@
     Candidate.DeductionFailure.Data = FailedAttr;
     return;
   }
+
+  initDiagnoseIfComplaint(*this, CandidateSet, Candidate, Conversion, None);
 }
 
 /// \brief Add overload candidates for overloaded operators that are
@@ -6902,10 +7046,8 @@
          Oper != OperEnd;
          ++Oper)
       AddMethodCandidate(Oper.getPair(), Args[0]->getType(),
-                         Args[0]->Classify(Context), 
-                         Args.slice(1),
-                         CandidateSet,
-                         /* SuppressUserConversions = */ false);
+                         Args[0]->Classify(Context), Args[0], Args.slice(1),
+                         CandidateSet, /*SuppressUserConversions=*/false);
   }
 }
 
@@ -8936,6 +9078,17 @@
   }
 }
 
+static bool isCandidateUnavailableDueToDiagnoseIf(const OverloadCandidate &OC) {
+  ArrayRef<DiagnoseIfAttr *> Info = OC.getDiagnoseIfInfo();
+  if (!Info.empty() && Info[0]->isError())
+    return true;
+
+  assert(llvm::all_of(Info,
+                      [](const DiagnoseIfAttr *A) { return !A->isError(); }) &&
+         "DiagnoseIf info shouldn't have mixed warnings and errors.");
+  return false;
+}
+
 /// \brief Computes the best viable function (C++ 13.3.3)
 /// within an overload candidate set.
 ///
@@ -9014,13 +9167,19 @@
   // Best is the best viable function.
   if (Best->Function &&
       (Best->Function->isDeleted() ||
-       S.isFunctionConsideredUnavailable(Best->Function)))
+       S.isFunctionConsideredUnavailable(Best->Function) ||
+       isCandidateUnavailableDueToDiagnoseIf(*Best)))
     return OR_Deleted;
 
   if (!EquivalentCands.empty())
     S.diagnoseEquivalentInternalLinkageDeclarations(Loc, Best->Function,
                                                     EquivalentCands);
 
+  for (const auto *W : Best->getDiagnoseIfInfo()) {
+    assert(W->isWarning() && "Errors should've been caught earlier!");
+    S.emitDiagnoseIfDiagnostic(Loc, W);
+  }
+
   return OR_Success;
 }
 
@@ -9861,7 +10020,7 @@
   EnableIfAttr *Attr = static_cast<EnableIfAttr*>(Cand->DeductionFailure.Data);
 
   S.Diag(Callee->getLocation(),
-         diag::note_ovl_candidate_disabled_by_enable_if_attr)
+         diag::note_ovl_candidate_disabled_by_function_cond_attr)
       << Attr->getCond()->getSourceRange() << Attr->getMessage();
 }
 
@@ -9891,21 +10050,28 @@
   FunctionDecl *Fn = Cand->Function;
 
   // Note deleted candidates, but only if they're viable.
-  if (Cand->Viable && (Fn->isDeleted() ||
-      S.isFunctionConsideredUnavailable(Fn))) {
-    std::string FnDesc;
-    OverloadCandidateKind FnKind =
+  if (Cand->Viable) {
+    if (Fn->isDeleted() || S.isFunctionConsideredUnavailable(Fn)) {
+      std::string FnDesc;
+      OverloadCandidateKind FnKind =
         ClassifyOverloadCandidate(S, Cand->FoundDecl, Fn, FnDesc);
 
-    S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted)
-      << FnKind << FnDesc
-      << (Fn->isDeleted() ? (Fn->isDeletedAsWritten() ? 1 : 2) : 0);
-    MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl);
-    return;
-  }
+      S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted)
+        << FnKind << FnDesc
+        << (Fn->isDeleted() ? (Fn->isDeletedAsWritten() ? 1 : 2) : 0);
+      MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl);
+      return;
+    }
+    if (isCandidateUnavailableDueToDiagnoseIf(*Cand)) {
+      auto *A = Cand->DiagnoseIfInfo.get<DiagnoseIfAttr *>();
+      assert(A->isError() && "Non-error diagnose_if disables a candidate?");
+      S.Diag(Cand->Function->getLocation(),
+             diag::note_ovl_candidate_disabled_by_function_cond_attr)
+          << A->getCond()->getSourceRange() << A->getMessage();
+      return;
+    }
 
-  // We don't really have anything else to say about viable candidates.
-  if (Cand->Viable) {
+    // We don't really have anything else to say about viable candidates.
     S.NoteOverloadCandidate(Cand->FoundDecl, Fn);
     return;
   }
@@ -12460,6 +12626,16 @@
       TemplateArgs = &TemplateArgsBuffer;
     }
 
+    // Poor-programmer's Lazy<Expr *>; isImplicitAccess requires stripping
+    // parens/casts, which would be nice to avoid potentially doing multiple
+    // times.
+    llvm::Optional<Expr *> UnresolvedBase;
+    auto GetUnresolvedBase = [&] {
+      if (!UnresolvedBase.hasValue())
+        UnresolvedBase =
+          UnresExpr->isImplicitAccess() ? nullptr : UnresExpr->getBase();
+      return *UnresolvedBase;
+    };
     for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(),
            E = UnresExpr->decls_end(); I != E; ++I) {
 
@@ -12480,14 +12656,15 @@
           continue;
 
         AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType,
-                           ObjectClassification, Args, CandidateSet,
+                           ObjectClassification,
+                           /*ThisArg=*/GetUnresolvedBase(), Args, CandidateSet,
                            /*SuppressUserConversions=*/false);
       } else {
-        AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func),
-                                   I.getPair(), ActingDC, TemplateArgs,
-                                   ObjectType,  ObjectClassification,
-                                   Args, CandidateSet,
-                                   /*SuppressUsedConversions=*/false);
+        AddMethodTemplateCandidate(
+            cast<FunctionTemplateDecl>(Func), I.getPair(), ActingDC,
+            TemplateArgs, ObjectType, ObjectClassification,
+            /*ThisArg=*/GetUnresolvedBase(), Args, CandidateSet,
+            /*SuppressUsedConversions=*/false);
       }
     }
 
@@ -12600,10 +12777,20 @@
            diag::err_ovl_no_viable_member_function_in_call)
           << Method << Method->getSourceRange();
       Diag(Method->getLocation(),
-           diag::note_ovl_candidate_disabled_by_enable_if_attr)
+           diag::note_ovl_candidate_disabled_by_function_cond_attr)
           << Attr->getCond()->getSourceRange() << Attr->getMessage();
       return ExprError();
     }
+
+    SmallVector<DiagnoseIfAttr *, 4> Nonfatal;
+    if (const DiagnoseIfAttr *Attr = checkArgDependentDiagnoseIf(
+            Method, Args, Nonfatal, false, MemE->getBase())) {
+      emitDiagnoseIfDiagnostic(MemE->getMemberLoc(), Attr);
+      return ExprError();
+    }
+
+    for (const auto *Attr : Nonfatal)
+      emitDiagnoseIfDiagnostic(MemE->getMemberLoc(), Attr);
   }
 
   if ((isa<CXXConstructorDecl>(CurContext) || 
@@ -12683,7 +12870,7 @@
        Oper != OperEnd; ++Oper) {
     AddMethodCandidate(Oper.getPair(), Object.get()->getType(),
                        Object.get()->Classify(Context),
-                       Args, CandidateSet,
+                       Object.get(), Args, CandidateSet,
                        /*SuppressUserConversions=*/ false);
   }
 
@@ -12959,7 +13146,8 @@
   for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end();
        Oper != OperEnd; ++Oper) {
     AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context),
-                       None, CandidateSet, /*SuppressUserConversions=*/false);
+                       Base, None, CandidateSet,
+                       /*SuppressUserConversions=*/false);
   }
 
   bool HadMultipleCandidates = (CandidateSet.size() > 1);