diff options
| author | Richard Trieu <rtrieu@google.com> | 2011-09-23 20:10:00 +0000 |
|---|---|---|
| committer | Richard Trieu <rtrieu@google.com> | 2011-09-23 20:10:00 +0000 |
| commit | 021baa373f58a21a3e2169c2f22f7dc58a987e96 (patch) | |
| tree | b470e17430a8811dfb0e012a8e11630906a3ad37 /clang/lib/Sema/SemaChecking.cpp | |
| parent | 0c254a00c1c7e64ca26de4e6f8a0d13510a8b1aa (diff) | |
| download | bcm5719-llvm-021baa373f58a21a3e2169c2f22f7dc58a987e96.tar.gz bcm5719-llvm-021baa373f58a21a3e2169c2f22f7dc58a987e96.zip | |
Add a new warning to -Wliteral-conversion to catch cases where a string literal
is cast to a boolean. An exception has been made for string literals in
logical expressions to allow the common case of use in assert statements.
bool x;
x = "hi"; // Warn here
void foo(bool x);
foo("hi"); // Warn here
assert(0 && "error");
assert("error); // Warn here
llvm-svn: 140405
Diffstat (limited to 'clang/lib/Sema/SemaChecking.cpp')
| -rw-r--r-- | clang/lib/Sema/SemaChecking.cpp | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 052ef3bef2b..5de43bb619d 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -3237,9 +3237,17 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T, if (CC.isInvalid()) return; - // Never diagnose implicit casts to bool. - if (Target->isSpecificBuiltinType(BuiltinType::Bool)) - return; + // Diagnose implicit casts to bool. + if (Target->isSpecificBuiltinType(BuiltinType::Bool)) { + if (isa<StringLiteral>(E)) + // Warn on string literal to bool. Checks for string literals in logical + // expressions, for instances, assert(0 && "error here"), is prevented + // by a check in AnalyzeImplicitConversions(). + return DiagnoseImpCast(S, E, T, CC, + diag::warn_impcast_string_literal_to_bool); + else // Other casts to bool are not checked. + return; + } // Strip vector types. if (isa<VectorType>(Source)) { @@ -3508,8 +3516,16 @@ void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) { // Now just recurse over the expression's children. CC = E->getExprLoc(); - for (Stmt::child_range I = E->children(); I; ++I) - AnalyzeImplicitConversions(S, cast<Expr>(*I), CC); + BinaryOperator *BO = dyn_cast<BinaryOperator>(E); + bool IsLogicalOperator = BO && BO->isLogicalOp(); + for (Stmt::child_range I = E->children(); I; ++I) { + Expr *ChildExpr = cast<Expr>(*I); + if (IsLogicalOperator && + isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts())) + // Ignore checking string literals that are in logical operators. + continue; + AnalyzeImplicitConversions(S, ChildExpr, CC); + } } } // end anonymous namespace |

