]> granicus.if.org Git - clang/commitdiff
Sema: When merging objc string literals, give the result a constant array type.
authorBenjamin Kramer <benny.kra@googlemail.com>
Tue, 25 Feb 2014 12:26:20 +0000 (12:26 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Tue, 25 Feb 2014 12:26:20 +0000 (12:26 +0000)
Also assert that we never create non-array string literals again.

PR18939.

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

lib/AST/Expr.cpp
lib/Sema/SemaExprObjC.cpp
test/SemaObjC/format-strings-objc.m

index 3f5833c50156f1b6d607c7f6a254011b54b6def7..4492d02b7a7934751ea5c9c3efd39ceb3ee7496d 100644 (file)
@@ -781,6 +781,9 @@ StringLiteral *StringLiteral::Create(const ASTContext &C, StringRef Str,
                                      StringKind Kind, bool Pascal, QualType Ty,
                                      const SourceLocation *Loc,
                                      unsigned NumStrs) {
+  assert(C.getAsConstantArrayType(Ty) &&
+         "StringLiteral must be of constant array type!");
+
   // Allocate enough space for the StringLiteral plus an array of locations for
   // any concatenated string tokens.
   void *Mem = C.Allocate(sizeof(StringLiteral)+
index 54aa6a610e076b8d277d972b8aeae19295b5cb19..3a7e83c5ac1c4ad4981a99fa0911241ee09837cc 100644 (file)
@@ -67,10 +67,14 @@ ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
 
     // Create the aggregate string with the appropriate content and location
     // information.
-    S = StringLiteral::Create(Context, StrBuf,
-                              StringLiteral::Ascii, /*Pascal=*/false,
-                              Context.getPointerType(Context.CharTy),
-                              &StrLocs[0], StrLocs.size());
+    const ConstantArrayType *CAT = Context.getAsConstantArrayType(S->getType());
+    assert(CAT && "String literal not of constant array type!");
+    QualType StrTy = Context.getConstantArrayType(
+        CAT->getElementType(), llvm::APInt(32, StrBuf.size() + 1),
+        CAT->getSizeModifier(), CAT->getIndexTypeCVRQualifiers());
+    S = StringLiteral::Create(Context, StrBuf, StringLiteral::Ascii,
+                              /*Pascal=*/false, StrTy, &StrLocs[0],
+                              StrLocs.size());
   }
   
   return BuildObjCStringLiteral(AtLocs[0], S);
index 2667e3047aae817e7a3f6c1a696aa16ed6b4d69d..49567a710c3a5a1fa966613d9632d90500383c4f 100644 (file)
@@ -116,6 +116,9 @@ NSString *test_literal_propagation(void) {
   NSLog(ns2); // expected-warning {{more '%' conversions than data arguments}}
   NSString * ns3 = ns1;
   NSLog(ns3); // expected-warning {{format string is not a string literal}}}
+
+  NSString * const ns6 = @"split" " string " @"%s"; // expected-note {{format string is defined here}}
+  NSLog(ns6); // expected-warning {{more '%' conversions than data arguments}}
 }
 
 // Do not emit warnings when using NSLocalizedString