]> granicus.if.org Git - clang/commitdiff
Produce a diagnostic for unused overloaded expressions, from Faisal Vali!
authorDouglas Gregor <dgregor@apple.com>
Mon, 7 Mar 2011 02:05:23 +0000 (02:05 +0000)
committerDouglas Gregor <dgregor@apple.com>
Mon, 7 Mar 2011 02:05:23 +0000 (02:05 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@127148 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaExprCXX.cpp
test/CXX/over/over.over/p2-resolve-single-template-id.cpp

index e4ac3ed6bbbb4be688e749dceb725962866dcc8f..4e018276b6da5b07ae5136ff0eb375a32d5e0be6 100644 (file)
@@ -3911,8 +3911,31 @@ ExprResult Sema::ActOnFinishFullExpr(Expr *FullExpr) {
   if (DiagnoseUnexpandedParameterPack(FullExpr))
     return ExprError();
 
+  // 13.4.1 ... An overloaded function name shall not be used without arguments 
+  //         in contexts other than those listed [i.e list of targets].
+  //  
+  //  void foo(); void foo(int);
+  //  template<class T> void fooT(); template<class T> void fooT(int);
+  
+  //  Therefore these should error:
+  //  foo; 
+  //  fooT<int>;
+  
+  if (FullExpr->getType() == Context.OverloadTy) {
+    if (!ResolveSingleFunctionTemplateSpecialization(FullExpr, 
+                                                     /* Complain */ false)) {
+      OverloadExpr* OvlExpr = OverloadExpr::find(FullExpr).Expression; 
+      Diag(FullExpr->getLocStart(), diag::err_addr_ovl_ambiguous)
+        << OvlExpr->getName();
+      NoteAllOverloadCandidates(OvlExpr);
+      return ExprError();
+    }  
+  }
+
+
   IgnoredValueConversions(FullExpr);
   CheckImplicitConversions(FullExpr);
+  
   return MaybeCreateExprWithCleanups(FullExpr);
 }
 
index f38a74e6e4953bd33b5a086d69e249cafd986309..64871edcbc6b05cf38f438188b13456502283e26 100644 (file)
@@ -22,6 +22,36 @@ namespace DontResolveTooEarly_WaitForOverloadResolution
 
 } // End namespace
 
+namespace DontAllowUnresolvedOverloadedExpressionInAnUnusedExpression
+{
+  void one() { }
+  template<class T> void oneT() { }
+
+  void two() { } //expected-note 2{{candidate}}
+  void two(int) { } //expected-note 2{{candidate}}
+  template<class T> void twoT() { } //expected-note 2{{candidate}}
+  template<class T> void twoT(T) { } //expected-note 2{{candidate}}
+
+  void check()
+  {
+    one; // expected-warning {{expression result unused}}
+    two; // expected-error{{address of overloaded function}}
+    oneT<int>; // expected-warning {{expression result unused}}
+    twoT<int>; // expected-error {{address of overloaded function}}
+  }
+
+  // check the template function case
+  template<class T> void check()
+  {
+    one; // expected-warning {{expression result unused}}
+    two; // expected-error{{address of overloaded function}}
+    oneT<int>; // expected-warning {{expression result unused}}
+    twoT<int>; // expected-error {{address of overloaded function}}
+  }
+
+}
+
   template<typename T>
     void twoT() { }
   template<typename T, typename U>
@@ -45,6 +75,7 @@ namespace DontResolveTooEarly_WaitForOverloadResolution
 
 int main()
 {
+  
   { static_cast<void>(one); }
   { (void)(one); }
   { static_cast<void>(oneT<int>); }