]> granicus.if.org Git - clang/commitdiff
Add support for const pointer to literal-objc string as format attribute.
authorJean-Daniel Dupas <devlists@shadowlab.org>
Wed, 25 Jan 2012 10:35:33 +0000 (10:35 +0000)
committerJean-Daniel Dupas <devlists@shadowlab.org>
Wed, 25 Jan 2012 10:35:33 +0000 (10:35 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@148948 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaChecking.cpp
test/SemaObjC/format-strings-objc.m

index f5a8b448a17ec29a1887ad6211cc5aeb832deae0..950a57589189c9fb7872eb236546cd458008a0c3 100644 (file)
@@ -1441,6 +1441,10 @@ bool Sema::SemaCheckStringLiteral(const Expr *E, Expr **Args,
       } else if (const PointerType *PT = T->getAs<PointerType>()) {
         isConstant = T.isConstant(Context) &&
                      PT->getPointeeType().isConstant(Context);
+      } else if (T->isObjCObjectPointerType()) {
+        // In ObjC, there is usually no "const ObjectPointer" type,
+        // so don't check if the pointee type is constant.
+        isConstant = T.isConstant(Context);
       }
 
       if (isConstant) {
index a2fe841ced28fe5cd3a8ef47ce0f26f5816c96f2..b0c87fe243d393fdb4897d519987e7bae13bd7a7 100644 (file)
@@ -89,3 +89,21 @@ void rdar10743758(id x) {
   NSLog(@"%@ %@", x, (BOOL) 1); // expected-warning {{format specifies type 'id' but the argument has type 'BOOL' (aka 'signed char')}}
 }
 
+NSString *test_literal_propagation(void) {
+  const char * const s1 = "constant string %s"; // expected-note {{format string is defined here}}
+  printf(s1); // expected-warning {{more '%' conversions than data arguments}}
+  const char * const s5 = "constant string %s"; // expected-note {{format string is defined here}}
+  const char * const s2 = s5;
+  printf(s2); // expected-warning {{more '%' conversions than data arguments}}
+
+  const char * const s3 = (const char *)0;
+  printf(s3); // expected-warning {{format string is not a string literal}}
+
+  NSString * const ns1 = @"constant string %s"; // expected-note {{format string is defined here}}
+  NSLog(ns1); // expected-warning {{more '%' conversions than data arguments}}
+  NSString * const ns5 = @"constant string %s"; // expected-note {{format string is defined here}}
+  NSString * const ns2 = ns5;
+  NSLog(ns2); // expected-warning {{more '%' conversions than data arguments}}
+  NSString * ns3 = ns1;
+  NSLog(ns3); // expected-warning {{format string is not a string literal}}}
+}