From df5c121f8ed83da0df666734860762241ee450ca Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Mon, 5 Dec 2011 20:49:50 +0000 Subject: Add a warning for implicit conversion from function literals (and static methods) to bool. E.g. void foo() {} if (f) { ... // <- Warns here. } Only applies to non-weak functions, and does not apply if the function address is taken explicitly with the addr-of operator. llvm-svn: 145849 --- clang/lib/Sema/SemaChecking.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'clang/lib/Sema/SemaChecking.cpp') diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 0d640a8c123..1ec7e39398b 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -3758,6 +3758,25 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T, // by a check in AnalyzeImplicitConversions(). return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_string_literal_to_bool); + if (Source->isFunctionType()) { + // Warn on function to bool. Checks free functions and static member + // functions. Weakly imported functions are excluded from the check, + // since it's common to test their value to check whether the linker + // found a definition for them. + ValueDecl *D = 0; + if (DeclRefExpr* R = dyn_cast(E)) { + D = R->getDecl(); + } else if (MemberExpr *M = dyn_cast(E)) { + D = M->getMemberDecl(); + } + + if (D && !D->isWeak()) { + FunctionDecl* F = cast(D); + S.Diag(E->getExprLoc(), diag::warn_impcast_function_to_bool) + << F << E->getSourceRange() << SourceRange(CC); + return; + } + } return; // Other casts to bool are not checked. } -- cgit v1.2.3