diff options
author | Lang Hames <lhames@gmail.com> | 2011-12-05 20:49:50 +0000 |
---|---|---|
committer | Lang Hames <lhames@gmail.com> | 2011-12-05 20:49:50 +0000 |
commit | df5c121f8ed83da0df666734860762241ee450ca (patch) | |
tree | 17d4b75aea26f96167b8fac54fcb451a1ff8e1fa /clang/lib/Sema/SemaChecking.cpp | |
parent | ab7940f6e1ae76a6eb8adf64babf3563c9550830 (diff) | |
download | bcm5719-llvm-df5c121f8ed83da0df666734860762241ee450ca.tar.gz bcm5719-llvm-df5c121f8ed83da0df666734860762241ee450ca.zip |
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
Diffstat (limited to 'clang/lib/Sema/SemaChecking.cpp')
-rw-r--r-- | clang/lib/Sema/SemaChecking.cpp | 19 |
1 files changed, 19 insertions, 0 deletions
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<DeclRefExpr>(E)) { + D = R->getDecl(); + } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) { + D = M->getMemberDecl(); + } + + if (D && !D->isWeak()) { + FunctionDecl* F = cast<FunctionDecl>(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. } |