]> granicus.if.org Git - clang/commitdiff
A new conversion warning for when an Objective-C object literal is implicitly
authorRichard Trieu <rtrieu@google.com>
Tue, 28 Jan 2014 23:40:26 +0000 (23:40 +0000)
committerRichard Trieu <rtrieu@google.com>
Tue, 28 Jan 2014 23:40:26 +0000 (23:40 +0000)
cast into a boolean true value.  This warning will catch code like:

if (@0) {}
if (@"foo") {}

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@200356 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticGroups.td
include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaChecking.cpp
test/Analysis/objc-string.mm
test/SemaObjCXX/warn-objc-literal-conversion.mm [new file with mode: 0644]

index 110c53e2291353f96e63b806f16ae24bbef9c3c4..8e9ef688c6f901d0687d88a23d975f61f162be09 100644 (file)
@@ -44,6 +44,7 @@ def NonLiteralNullConversion : DiagGroup<"non-literal-null-conversion">;
 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">;
index b682505299f1b75d711df70f470ea3c99bdefd7e..67f5837246ff07ba873540671940040baf8a8299 100644 (file)
@@ -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">,
index 0dffa53679e6585cd385d175791220ba6077f85a..ee0e6702e0f341fa1007230fea7371e3b5f29b85 100644 (file)
@@ -5358,6 +5358,13 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
       // 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,
index c67ab5e8919cbba859b3426266142bb322c16e91..a32b740bba02cd26c7bb1f9a900e842b770e8388 100644 (file)
@@ -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;
diff --git a/test/SemaObjCXX/warn-objc-literal-conversion.mm b/test/SemaObjCXX/warn-objc-literal-conversion.mm
new file mode 100644 (file)
index 0000000..4464561
--- /dev/null
@@ -0,0 +1,74 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wobjc-literal-conversion %s
+
+@class NSString;
+
+@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;
+@end
+
+@interface NSArray
++ (id)arrayWithObjects:(const id [])objects count:(int)cnt;
+@end
+
+@interface NSDictionary
++ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id [])keys count:(unsigned long)cnt;
+@end
+
+void char_test() {
+  if (@'a') {}
+  // expected-warning@-1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+}
+
+void int_test() {
+  if (@12) {}
+  // expected-warning@-1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+  if (@-12) {}
+  // expected-warning@-1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+  if (@12LL) {}
+  // expected-warning@-1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+  if (@-12LL) {}
+  // expected-warning@-1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+}
+
+void float_test() {
+  if (@12.55) {}
+  // expected-warning@-1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+  if (@-12.55) {}
+  // expected-warning@-1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+  if (@12.55F) {}
+  // expected-warning@-1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+  if (@-12.55F) {}
+  // expected-warning@-1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+}
+
+void bool_test() {
+  if (@true) {}
+  // expected-warning@-1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+  if (@false) {}
+  // expected-warning@-1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+}
+
+void string_test() {
+  if (@"asdf") {}
+  // expected-warning@-1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+}
+
+void array_test() {
+  if (@[ @313, @331, @367, @379 ]) {}
+  // expected-warning@-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@-1{{implicit boolean conversion of Objective-C object literal always evaluates to true}}
+}
+
+void objc_bool_test () {
+  if (__objc_yes) {}
+  if (__objc_no) {}
+}