r200356 - A new conversion warning for when an Objective-C object literal is implicitly
Richard Trieu
rtrieu at google.com
Tue Jan 28 15:40:26 PST 2014
Author: rtrieu
Date: Tue Jan 28 17:40:26 2014
New Revision: 200356
URL: http://llvm.org/viewvc/llvm-project?rev=200356&view=rev
Log:
A new conversion warning for when an Objective-C object literal is implicitly
cast into a boolean true value. This warning will catch code like:
if (@0) {}
if (@"foo") {}
Added:
cfe/trunk/test/SemaObjCXX/warn-objc-literal-conversion.mm
Modified:
cfe/trunk/include/clang/Basic/DiagnosticGroups.td
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/Sema/SemaChecking.cpp
cfe/trunk/test/Analysis/objc-string.mm
Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=200356&r1=200355&r2=200356&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Tue Jan 28 17:40:26 2014
@@ -44,6 +44,7 @@ def NonLiteralNullConversion : DiagGroup
def NullConversion : DiagGroup<"null-conversion">;
def ImplicitConversionFloatingPointToBool :
DiagGroup<"implicit-conversion-floating-point-to-bool">;
+def ObjCLiteralConversion : DiagGroup<"objc-literal-conversion">;
def BadArrayNewLength : DiagGroup<"bad-array-new-length">;
def BuiltinMacroRedefined : DiagGroup<"builtin-macro-redefined">;
def BuiltinRequiresHeader : DiagGroup<"builtin-requires-header">;
@@ -446,6 +447,7 @@ def Conversion : DiagGroup<"conversion",
LiteralConversion,
NonLiteralNullConversion, // (1-1)->pointer (etc)
NullConversion, // NULL->non-pointer
+ ObjCLiteralConversion,
SignConversion,
StringConversion]>,
DiagCategory<"Value Conversion Issue">;
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=200356&r1=200355&r2=200356&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Jan 28 17:40:26 2014
@@ -2296,6 +2296,10 @@ def note_function_to_bool_silence : Note
"prefix with the address-of operator to silence this warning">;
def note_function_to_bool_call : Note<
"suffix with parentheses to turn this into a function call">;
+def warn_impcast_objective_c_literal_to_bool : Warning<
+ "implicit boolean conversion of Objective-C object literal always "
+ "evaluates to true">,
+ InGroup<ObjCLiteralConversion>;
def warn_cast_align : Warning<
"cast from %0 to %1 increases required alignment from %2 to %3">,
Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=200356&r1=200355&r2=200356&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Tue Jan 28 17:40:26 2014
@@ -5358,6 +5358,13 @@ void CheckImplicitConversion(Sema &S, Ex
// prevented by a check in AnalyzeImplicitConversions().
return DiagnoseImpCast(S, E, T, CC,
diag::warn_impcast_string_literal_to_bool);
+ if (isa<ObjCStringLiteral>(E) || isa<ObjCArrayLiteral>(E) ||
+ isa<ObjCDictionaryLiteral>(E) || isa<ObjCBoxedExpr>(E)) {
+ // This covers the literal expressions that evaluate to Objective-C
+ // objects.
+ return DiagnoseImpCast(S, E, T, CC,
+ diag::warn_impcast_objective_c_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,
Modified: cfe/trunk/test/Analysis/objc-string.mm
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/objc-string.mm?rev=200356&r1=200355&r2=200356&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/objc-string.mm (original)
+++ cfe/trunk/test/Analysis/objc-string.mm Tue Jan 28 17:40:26 2014
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection -verify %s
+// RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection -verify -Wno-objc-literal-conversion %s
void clang_analyzer_eval(bool);
@class NSString;
Added: cfe/trunk/test/SemaObjCXX/warn-objc-literal-conversion.mm
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjCXX/warn-objc-literal-conversion.mm?rev=200356&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjCXX/warn-objc-literal-conversion.mm (added)
+++ cfe/trunk/test/SemaObjCXX/warn-objc-literal-conversion.mm Tue Jan 28 17:40:26 2014
@@ -0,0 +1,74 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wobjc-literal-conversion %s
+
+ at class NSString;
+
+ at interface NSNumber
++ (NSNumber *)numberWithChar:(char)value;
++ (NSNumber *)numberWithInt:(int)value;
++ (NSNumber *)numberWithLongLong:(long long)value;
++ (NSNumber *)numberWithFloat:(float)value;
++ (NSNumber *)numberWithDouble:(double)value;
++ (NSNumber *)numberWithBool:(bool)value;
+ at end
+
+ at interface NSArray
++ (id)arrayWithObjects:(const id [])objects count:(int)cnt;
+ at end
+
+ at interface NSDictionary
++ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id [])keys count:(unsigned long)cnt;
+ at end
+
+void char_test() {
+ if (@'a') {}
+ // expected-warning at -1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+}
+
+void int_test() {
+ if (@12) {}
+ // expected-warning at -1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+ if (@-12) {}
+ // expected-warning at -1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+ if (@12LL) {}
+ // expected-warning at -1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+ if (@-12LL) {}
+ // expected-warning at -1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+}
+
+void float_test() {
+ if (@12.55) {}
+ // expected-warning at -1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+ if (@-12.55) {}
+ // expected-warning at -1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+ if (@12.55F) {}
+ // expected-warning at -1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+ if (@-12.55F) {}
+ // expected-warning at -1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+}
+
+void bool_test() {
+ if (@true) {}
+ // expected-warning at -1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+ if (@false) {}
+ // expected-warning at -1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+}
+
+void string_test() {
+ if (@"asdf") {}
+ // expected-warning at -1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+}
+
+void array_test() {
+ if (@[ @313, @331, @367, @379 ]) {}
+ // expected-warning at -1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+}
+
+void dictionary_test() {
+ if (@{ @0: @0, @1: @1, @2: @1, @3: @3 }) {}
+ // expected-warning at -1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+}
+
+void objc_bool_test () {
+ if (__objc_yes) {}
+ if (__objc_no) {}
+}
More information about the cfe-commits
mailing list