diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 400098444826..2db253a89333 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -6814,6 +6814,13 @@ ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, if (!resultType->isScalarType()) // C99 6.5.3.3p1 return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) << resultType << Input->getSourceRange()); + + // Do not accept &f if f is overloaded + // i.e. void f(int); void f(char); bool b = &f; + if (resultType == Context.OverloadTy && + PerformContextuallyConvertToBool(Input)) + return ExprError(); // Diagnostic is uttered above + // LNot always has type int. C99 6.5.3.3p5. // In C++, it's bool. C++ 5.3.1p8 resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; diff --git a/clang/test/CXX/expr/expr.unary/expr.unary.op/p6.cpp b/clang/test/CXX/expr/expr.unary/expr.unary.op/p6.cpp new file mode 100644 index 000000000000..eea2c33ffe1b --- /dev/null +++ b/clang/test/CXX/expr/expr.unary/expr.unary.op/p6.cpp @@ -0,0 +1,36 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +// -- prvalue of arithmetic + +bool b = !0; + +bool b2 = !1.2; + +bool b3 = !4; + +// -- unscoped enumeration +enum { E, F }; + +bool b4 = !E; +bool b5 = !F; + +// -- pointer, +bool b6 = !&b4; +void f(); +bool b61 = !&f; + +// -- or pointer to member type can be converted to a prvalue of type bool. +struct S { void f() { } }; + +bool b7 = !&S::f; + + +bool b8 = !S(); //expected-error {{invalid argument type 'S'}} + +namespace PR8181 +{ + void f() { } + void f(char) { } + bool b = !&f; //expected-error {{value of type '' is not contextually convertible to 'bool'}} + +}