summaryrefslogtreecommitdiffstats
path: root/clang/lib/Sema/SemaChecking.cpp
diff options
context:
space:
mode:
authorLang Hames <lhames@gmail.com>2011-12-05 20:49:50 +0000
committerLang Hames <lhames@gmail.com>2011-12-05 20:49:50 +0000
commitdf5c121f8ed83da0df666734860762241ee450ca (patch)
tree17d4b75aea26f96167b8fac54fcb451a1ff8e1fa /clang/lib/Sema/SemaChecking.cpp
parentab7940f6e1ae76a6eb8adf64babf3563c9550830 (diff)
downloadbcm5719-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.cpp19
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.
}
OpenPOWER on IntegriCloud