]> granicus.if.org Git - clang/commitdiff
ObjectiveC [QoI] issue warning if an element of an nsarray
authorFariborz Jahanian <fjahanian@apple.com>
Tue, 13 Aug 2013 23:44:55 +0000 (23:44 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Tue, 13 Aug 2013 23:44:55 +0000 (23:44 +0000)
expresison is a concatenated nsstring element.
// rdar://14303083

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

include/clang/Basic/DiagnosticGroups.td
include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaExprObjC.cpp
test/SemaObjC/objc-array-literal.m

index ec9c8eb08c3d35a4d97ad7b3ed4d71ee4e8f3a81..f1ab1e8dd758bef7b24065402c2625f09baacd61 100644 (file)
@@ -552,6 +552,7 @@ def ObjCCocoaAPI : DiagGroup<"objc-cocoa-api", [
   ]>;
 
 def ObjCStringComparison : DiagGroup<"objc-string-compare">;
+def ObjCStringConcatenation : DiagGroup<"objc-string-concatenation">;
 def ObjCLiteralComparison : DiagGroup<"objc-literal-compare", [
     ObjCStringComparison
   ]>;
index 06053501fa636e27f4ecfb203e6632a68ba437e8..5a2e6a9c6bf1894546ab116dba7b95c2e1921a30 100644 (file)
@@ -1898,6 +1898,9 @@ def warn_missing_atsign_prefix : Warning<
 def warn_objc_string_literal_comparison : Warning<
   "direct comparison of a string literal has undefined behavior">, 
   InGroup<ObjCStringComparison>;
+def warn_concatenated_nsarray_literal : Warning<
+  "concatenated nsstring literal for an nsarray expression">,
+  InGroup<ObjCStringConcatenation>;
 def note_objc_literal_comparison_isequal : Note<
   "use 'isEqual:' instead">;
 
index de0daca4b8bca4e5be0e2dc7c92803b830ba7207..e3e91d140a066d918a2fc871a58c0d44ce97b4cc 100644 (file)
@@ -324,7 +324,8 @@ ExprResult Sema::ActOnObjCBoolLiteral(SourceLocation AtLoc,
 /// \brief Check that the given expression is a valid element of an Objective-C
 /// collection literal.
 static ExprResult CheckObjCCollectionLiteralElement(Sema &S, Expr *Element, 
-                                                    QualType T) {  
+                                                    QualType T,
+                                                    bool ArrayLiteral = false) {
   // If the expression is type-dependent, there's nothing for us to do.
   if (Element->isTypeDependent())
     return Element;
@@ -401,14 +402,20 @@ static ExprResult CheckObjCCollectionLiteralElement(Sema &S, Expr *Element,
         Recovered = true;
       }
     }
-    
+  
     if (!Recovered) {
       S.Diag(Element->getLocStart(), diag::err_invalid_collection_element)
         << Element->getType();
       return ExprError();
     }
   }
-  
+  if (ArrayLiteral)
+    if (ObjCStringLiteral *getString = dyn_cast<ObjCStringLiteral>(OrigElement)) {
+      if (getString->getString() && getString->getString()->getNumConcatenated() > 1)
+        S.Diag(Element->getLocStart(), diag::warn_concatenated_nsarray_literal)
+        << Element->getType();
+    }
+
   // Make sure that the element has the type that the container factory 
   // function expects. 
   return S.PerformCopyInitialization(
@@ -710,7 +717,7 @@ ExprResult Sema::BuildObjCArrayLiteral(SourceRange SR, MultiExprArg Elements) {
   for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
     ExprResult Converted = CheckObjCCollectionLiteralElement(*this,
                                                              ElementsBuffer[I],
-                                                             RequiredType);
+                                                             RequiredType, true);
     if (Converted.isInvalid())
       return ExprError();
     
index 9f59316219c78369b934300d1b5120e580be3576..0d18d73c414cd74663e08fb2980c0dd975959423 100644 (file)
@@ -39,3 +39,9 @@ int main() {
   const char *blah;
   NSArray *array2 = @[blah]; // expected-error{{collection element of type 'const char *' is not an Objective-C object}}
 }
+
+// rdar://14303083
+id Test14303083() {
+  id obj = @[ @"A", (@"B" @"C")];
+  return @[ @"A", @"B" @"C"]; // expected-warning {{concatenated nsstring literal for an nsarray expression}}
+}