From 353ee246e754e38db9b738240d18f1ecf2bb389b Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Mon, 7 Mar 2011 02:05:23 +0000 Subject: [PATCH] Produce a diagnostic for unused overloaded expressions, from Faisal Vali! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@127148 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaExprCXX.cpp | 23 ++++++++++++++ .../p2-resolve-single-template-id.cpp | 31 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp index e4ac3ed6bb..4e018276b6 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -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 void fooT(); template void fooT(int); + + // Therefore these should error: + // foo; + // fooT; + + 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); } diff --git a/test/CXX/over/over.over/p2-resolve-single-template-id.cpp b/test/CXX/over/over.over/p2-resolve-single-template-id.cpp index f38a74e6e4..64871edcbc 100644 --- a/test/CXX/over/over.over/p2-resolve-single-template-id.cpp +++ b/test/CXX/over/over.over/p2-resolve-single-template-id.cpp @@ -22,6 +22,36 @@ namespace DontResolveTooEarly_WaitForOverloadResolution } // End namespace +namespace DontAllowUnresolvedOverloadedExpressionInAnUnusedExpression +{ + void one() { } + template void oneT() { } + + void two() { } //expected-note 2{{candidate}} + void two(int) { } //expected-note 2{{candidate}} + template void twoT() { } //expected-note 2{{candidate}} + template void twoT(T) { } //expected-note 2{{candidate}} + + void check() + { + one; // expected-warning {{expression result unused}} + two; // expected-error{{address of overloaded function}} + oneT; // expected-warning {{expression result unused}} + twoT; // expected-error {{address of overloaded function}} + } + + // check the template function case + template void check() + { + one; // expected-warning {{expression result unused}} + two; // expected-error{{address of overloaded function}} + oneT; // expected-warning {{expression result unused}} + twoT; // expected-error {{address of overloaded function}} + + } + +} + template void twoT() { } template @@ -45,6 +75,7 @@ namespace DontResolveTooEarly_WaitForOverloadResolution int main() { + { static_cast(one); } { (void)(one); } { static_cast(oneT); } -- 2.40.0