28 #include "llvm/ADT/SmallString.h"
29 #include "llvm/Support/ConvertUTF.h"
31 using namespace clang;
33 using llvm::makeArrayRef;
44 if (Strings.size() != 1) {
49 for (
Expr *E : Strings) {
50 S = cast<StringLiteral>(E);
54 Diag(S->getBeginLoc(), diag::err_cfstring_literal_not_string_constant)
55 << S->getSourceRange();
60 StrBuf += S->getString();
63 StrLocs.append(S->tokloc_begin(), S->tokloc_end());
69 assert(CAT &&
"String literal not of constant array type!");
74 false, StrTy, &StrLocs[0],
78 return BuildObjCStringLiteral(AtLocs[0], S);
83 if (CheckObjCString(S))
93 }
else if (getLangOpts().NoConstantCFStrings) {
95 std::string StringClass(getLangOpts().ObjCConstantStringClass);
97 if (StringClass.empty())
98 NSIdent = &Context.
Idents.
get(
"NSConstantString");
100 NSIdent = &Context.
Idents.
get(StringClass);
102 NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLoc,
111 Diag(S->getBeginLoc(), diag::err_no_nsconstant_string_class)
112 << NSIdent << S->getSourceRange();
117 NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLoc,
152 S.
Diag(Loc, diag::err_undeclared_boxing_method) << Sel << Class->getName();
159 S.
Diag(Loc, diag::err_objc_literal_method_sig)
172 switch (LiteralKind) {
190 llvm_unreachable(
"LiteralKind can't be converted into a ClassKind");
202 S.
Diag(Loc, diag::err_undeclared_objc_literal_class)
203 << II->
getName() << LiteralKind;
205 }
else if (!
Decl->hasDefinition() && !S.
getLangOpts().DebuggerObjCLiteral) {
206 S.
Diag(Loc, diag::err_undeclared_objc_literal_class)
207 <<
Decl->getName() << LiteralKind;
247 S.
NSAPIObj->getNSNumberFactoryMethodKind(NumberType);
251 S.
Diag(Loc, diag::err_invalid_nsnumber_type)
284 if (!Method && S.
getLangOpts().DebuggerObjCLiteral) {
322 switch (Char->getKind()) {
325 NumberType = Context.
CharTy;
354 ExprResult ConvertedNumber = PerformCopyInitialization(Entity,
359 Number = ConvertedNumber.
get();
362 return MaybeBindToTemporary(
372 Inner = ActOnCXXBoolLiteral(ValueLoc,
Value? tok::kw_true : tok::kw_false);
376 Inner = ActOnIntegerConstant(ValueLoc,
Value? 1 : 0);
377 Inner = ImpCastExprToType(Inner.get(), Context.
BoolTy,
378 CK_IntegralToBoolean);
381 return BuildObjCNumericLiteral(AtLoc, Inner.get());
388 bool ArrayLiteral =
false) {
394 if (Result.isInvalid())
396 Element = Result.get();
411 Expr *OrigElement = Element;
415 if (Result.isInvalid())
417 Element = Result.get();
422 bool Recovered =
false;
425 if (isa<IntegerLiteral>(OrigElement) ||
426 isa<CharacterLiteral>(OrigElement) ||
427 isa<FloatingLiteral>(OrigElement) ||
428 isa<ObjCBoolLiteralExpr>(OrigElement) ||
429 isa<CXXBoolLiteralExpr>(OrigElement)) {
430 if (S.
NSAPIObj->getNSNumberFactoryMethodKind(OrigElement->
getType())) {
431 int Which = isa<CharacterLiteral>(OrigElement) ? 1
432 : (isa<CXXBoolLiteralExpr>(OrigElement) ||
433 isa<ObjCBoolLiteralExpr>(OrigElement)) ? 2
442 if (Result.isInvalid())
445 Element = Result.get();
450 else if (
StringLiteral *String = dyn_cast<StringLiteral>(OrigElement)) {
451 if (String->isAscii()) {
457 if (Result.isInvalid())
460 Element = Result.get();
473 dyn_cast<ObjCStringLiteral>(OrigElement)) {
475 unsigned numConcat = SL->getNumConcatenated();
478 bool hasMacro =
false;
479 for (
unsigned i = 0; i < numConcat ; ++i)
480 if (SL->getStrTokenLoc(i).isMacroID()) {
486 diag::warn_concatenated_nsarray_literal)
509 ExprResult RValue = DefaultFunctionArrayLvalueConversion(ValueExpr);
514 ValueExpr = RValue.
get();
532 if (
auto *CE = dyn_cast<ImplicitCastExpr>(ValueExpr))
533 if (CE->getCastKind() == CK_ArrayToPointerDecay)
535 dyn_cast<StringLiteral>(CE->getSubExpr()->IgnoreParens())) {
536 assert((SL->isAscii() || SL->isUTF8()) &&
537 "unexpected character encoding");
538 StringRef Str = SL->getString();
539 const llvm::UTF8 *StrBegin = Str.bytes_begin();
540 const llvm::UTF8 *StrEnd = Str.bytes_end();
542 if (llvm::isLegalUTF8String(&StrBegin, StrEnd)) {
546 NSStringPointer, NSStringPointer);
547 return new (Context)
ObjCBoxedExpr(CE, BoxedType,
nullptr, SR);
550 Diag(SL->getBeginLoc(), diag::warn_objc_boxing_invalid_utf8_string)
551 << NSStringPointer << SL->getSourceRange();
554 if (!StringWithUTF8StringMethod) {
559 BoxingMethod = NSStringDecl->lookupClassMethod(stringWithUTF8String);
560 if (!BoxingMethod && getLangOpts().DebuggerObjCLiteral) {
565 NSStringPointer, ReturnTInfo, NSStringDecl,
585 stringWithUTF8String, BoxingMethod))
588 StringWithUTF8StringMethod = BoxingMethod;
591 BoxingMethod = StringWithUTF8StringMethod;
592 BoxedType = NSStringPointer;
609 dyn_cast<CharacterLiteral>(ValueExpr->
IgnoreParens())) {
612 switch (Char->getKind()) {
615 ValueType = Context.
CharTy;
635 BoxedType = NSNumberPointer;
637 if (!ET->getDecl()->isComplete()) {
638 Diag(Loc, diag::err_objc_incomplete_boxed_expression_type)
644 ET->getDecl()->getIntegerType());
645 BoxedType = NSNumberPointer;
664 if (!ValueWithBytesObjCTypeMethod) {
672 BoxingMethod = NSValueDecl->lookupClassMethod(ValueWithBytesObjCType);
673 if (!BoxingMethod && getLangOpts().DebuggerObjCLiteral) {
678 NSValuePointer, ReturnTInfo, NSValueDecl,
696 Params.push_back(
bytes);
706 Params.push_back(
type);
713 ValueWithBytesObjCType, BoxingMethod))
716 ValueWithBytesObjCTypeMethod = BoxingMethod;
720 Diag(Loc, diag::err_objc_non_trivially_copyable_boxed_expression_type)
725 BoxingMethod = ValueWithBytesObjCTypeMethod;
726 BoxedType = NSValuePointer;
730 Diag(Loc, diag::err_objc_illegal_boxed_expression_type)
735 DiagnoseUseOfDecl(BoxingMethod, Loc);
740 ConvertedValueExpr = PerformCopyInitialization(IE, ValueExpr->
getExprLoc(),
747 ConvertedValueExpr = PerformCopyInitialization(IE,
SourceLocation(),
753 ValueExpr = ConvertedValueExpr.
get();
758 return MaybeBindToTemporary(BoxedExpr);
772 "base or index cannot have dependent type here");
776 ExprResult Result = CheckPlaceholderExpr(IndexExpr);
777 if (Result.isInvalid())
779 IndexExpr = Result.get();
782 Result = DefaultLvalueConversion(BaseExpr);
783 if (Result.isInvalid())
785 BaseExpr = Result.get();
790 getterMethod, setterMethod, RB);
806 if (!ArrayWithObjectsMethod) {
810 if (!Method && getLangOpts().DebuggerObjCLiteral) {
827 Params.push_back(objects);
835 Params.push_back(cnt);
850 diag::note_objc_literal_method_param)
857 if (!Method->
parameters()[1]->getType()->isIntegerType()) {
861 diag::note_objc_literal_method_param)
869 ArrayWithObjectsMethod = Method;
872 QualType ObjectsType = ArrayWithObjectsMethod->parameters()[0]->getType();
877 Expr **ElementsBuffer = Elements.data();
878 for (
unsigned I = 0, N = Elements.size(); I != N; ++I) {
885 ElementsBuffer[I] = Converted.
get();
892 return MaybeBindToTemporary(
894 ArrayWithObjectsMethod, SR));
908 struct APSIntCompare {
910 return llvm::APSInt::compareValues(LHS, RHS) < 0;
914 llvm::DenseMap<StringRef, SourceLocation> StringKeys;
915 std::map<llvm::APSInt, SourceLocation, APSIntCompare> IntegralKeys;
917 auto checkOneKey = [&](
auto &Map,
const auto &Key,
SourceLocation Loc) {
918 auto Pair = Map.insert({Key, Loc});
920 S.
Diag(Loc, diag::warn_nsdictionary_duplicate_key);
921 S.
Diag(Pair.first->second, diag::note_nsdictionary_duplicate_key_here);
925 for (
unsigned Idx = 0,
End =
Literal->getNumElements(); Idx !=
End; ++Idx) {
926 Expr *Key =
Literal->getKeyValueElement(Idx).Key->IgnoreParenImpCasts();
928 if (
auto *StrLit = dyn_cast<ObjCStringLiteral>(Key)) {
929 StringRef Bytes = StrLit->getString()->getBytes();
931 checkOneKey(StringKeys, Bytes, Loc);
934 if (
auto *BE = dyn_cast<ObjCBoxedExpr>(Key)) {
935 Expr *Boxed = BE->getSubExpr();
940 checkOneKey(StringKeys, Str->getBytes(), Loc);
947 checkOneKey(IntegralKeys, Result.Val.getInt(), Loc);
957 if (!NSDictionaryDecl) {
960 if (!NSDictionaryDecl) {
968 if (!DictionaryWithObjectsMethod) {
969 Selector Sel = NSAPIObj->getNSDictionarySelector(
971 ObjCMethodDecl *Method = NSDictionaryDecl->lookupClassMethod(Sel);
972 if (!Method && getLangOpts().DebuggerObjCLiteral) {
989 Params.push_back(objects);
997 Params.push_back(keys);
1005 Params.push_back(cnt);
1021 diag::note_objc_literal_method_param)
1035 if (QIDNSCopying.isNull()) {
1049 if (!QIDNSCopying.isNull())
1058 diag::note_objc_literal_method_param)
1066 QualType CountType = Method->parameters()[2]->getType();
1070 Diag(Method->parameters()[2]->getLocation(),
1071 diag::note_objc_literal_method_param)
1078 DictionaryWithObjectsMethod = Method;
1081 QualType ValuesT = DictionaryWithObjectsMethod->parameters()[0]->getType();
1083 QualType KeysT = DictionaryWithObjectsMethod->parameters()[1]->getType();
1088 bool HasPackExpansions =
false;
1099 if (
Value.isInvalid())
1102 Element.Key = Key.
get();
1103 Element.Value =
Value.get();
1105 if (Element.EllipsisLoc.isInvalid())
1108 if (!Element.Key->containsUnexpandedParameterPack() &&
1109 !Element.Value->containsUnexpandedParameterPack()) {
1110 Diag(Element.EllipsisLoc,
1111 diag::err_pack_expansion_without_parameter_packs)
1113 Element.Value->getEndLoc());
1117 HasPackExpansions =
true;
1125 DictionaryWithObjectsMethod, SR);
1127 return MaybeBindToTemporary(
Literal);
1141 diag::err_incomplete_type_objc_at_encode,
1148 if (!NotEncodedT.
isNull())
1149 Diag(AtLoc, diag::warn_incomplete_encoded_type)
1150 << EncodedType << NotEncodedT;
1182 bool Warned =
false;
1185 if (MatchingMethodDecl == Method ||
1193 S.
Diag(AtLoc, diag::warn_multiple_selectors)
1199 S.
Diag(MatchingMethodDecl->
getLocation(), diag::note_method_declared_at)
1210 bool WarnMultipleSelectors) {
1211 if (!WarnMultipleSelectors ||
1214 bool Warned =
false;
1220 Method, InstMethList))
1226 Method, ClsMethList) || Warned)
1242 assert(Method->
getSelector() == Sel &&
"Method with wrong selector in method list");
1245 DirectMethod = Method;
1250 return DirectMethod;
1265 S, Sel, Iter->second.first, onlyDirect, anyDirect);
1267 S, Sel, Iter->second.second, onlyDirect, anyDirect);
1269 return DirectInstance ? DirectInstance : DirectClass;
1298 bool WarnMultipleSelectors) {
1306 Selector MatchedSel = OM->getSelector();
1309 Diag(SelLoc, diag::warn_undeclared_selector_with_typo)
1310 << Sel << MatchedSel
1314 Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
1317 WarnMultipleSelectors);
1319 bool onlyDirect =
true;
1320 bool anyDirect =
false;
1325 Diag(AtLoc, diag::err_direct_selector_expression)
1329 }
else if (anyDirect) {
1334 if (LikelyTargetMethod && LikelyTargetMethod->
isDirectMethod()) {
1335 Diag(AtLoc, diag::warn_potentially_direct_selector_expression) << Sel;
1337 diag::note_direct_method_declared_at)
1339 }
else if (!LikelyTargetMethod) {
1342 Diag(AtLoc, diag::warn_strict_potentially_direct_selector_expression)
1345 diag::note_direct_method_declared_at)
1365 Diag(AtLoc, diag::err_arc_illegal_selector) <<
1394 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
1398 Diag(ProtoLoc, diag::err_objc_non_runtime_protocol_in_protocol_expr)
1401 Diag(ProtoLoc, diag::err_atprotocol_protocol) << PDecl;
1457 bool isClassMessage,
1458 bool isSuperMessage) {
1459 assert(Method &&
"Must have a method");
1494 if (isSuperMessage) {
1497 return transferNullability(
1516 return transferNullability(ReceiverType);
1522 bool isClassMessage,
1523 bool isSuperMessage) {
1531 if (isClassMessage) {
1543 cast<ImplicitParamDecl>(
1545 ->getDeclContext());
1552 NewResultType, NewResultType);
1553 return NewResultType;
1565 unsigned receiverNullabilityIdx = 0;
1570 receiverNullabilityIdx = 1 +
static_cast<unsigned>(*nullability);
1573 unsigned resultNullabilityIdx = 0;
1578 resultNullabilityIdx = 1 +
static_cast<unsigned>(*nullability);
1583 static const uint8_t None = 0;
1584 static const uint8_t
NonNull = 1;
1587 static const uint8_t nullabilityMap[4][4] = {
1595 unsigned newResultNullabilityIdx
1596 = nullabilityMap[receiverNullabilityIdx][resultNullabilityIdx];
1597 if (newResultNullabilityIdx == resultNullabilityIdx)
1603 if (
auto attributed = dyn_cast<AttributedType>(resultType.
getTypePtr())) {
1604 resultType = attributed->getModifiedType();
1611 if (newResultNullabilityIdx > 0) {
1616 resultType, resultType);
1635 dyn_cast<ObjCCategoryImplDecl>(impl)) {
1636 iface = catImpl->getCategoryDecl();
1638 iface = impl->getClassInterface();
1648 for (
unsigned i = 0, e = overrides.size(); i != e; ++i) {
1672 loc = overridden->getLocation();
1673 Diag(loc, diag::note_related_result_type_explicit)
1719 if (!SelectorLocs.empty() && SelectorLocs.front().isValid())
1720 SelLoc = SelectorLocs.front();
1726 for (
unsigned i = 0, e = Args.size(); i != e; i++) {
1727 if (Args[i]->isTypeDependent())
1739 Args[i] = result.
get();
1744 DiagID = diag::err_arc_method_not_found;
1746 DiagID = isClassMessage ? diag::warn_class_method_not_found
1747 : diag::warn_inst_method_not_found;
1752 DiagID = diag::err_method_not_found_with_typo;
1754 DiagID = isClassMessage ? diag::warn_class_method_not_found_with_typo
1755 : diag::warn_instance_method_not_found_with_typo;
1757 SourceRange SelectorRange(SelectorLocs.front(), SelectorLocs.back());
1759 Diag(SelLoc, DiagID)
1760 << Sel<< isClassMessage << MatchedSel
1763 Diag(SelLoc, DiagID) << Sel<< isClassMessage << MatchedSel;
1766 Diag(SelLoc, DiagID)
1767 << Sel << isClassMessage <<
SourceRange(SelectorLocs.front(),
1768 SelectorLocs.back());
1772 Diag(ThisClass->getLocation(), diag::note_receiver_class_declared);
1774 if (ThisClass->lookupClassMethod(Sel))
1775 Diag(RecRange.
getBegin(), diag::note_receiver_expr_here)
1777 ThisClass->getNameAsString());
1794 isClassMessage, isSuperMessage);
1803 if (Args.size() < NumNamedArgs) {
1804 Diag(SelLoc, diag::err_typecheck_call_too_few_args)
1805 << 2 << NumNamedArgs << static_cast<unsigned>(Args.size());
1813 bool IsError =
false;
1814 for (
unsigned i = 0; i < NumNamedArgs; i++) {
1816 if (Args[i]->isTypeDependent())
1819 Expr *argExpr = Args[i];
1822 assert(argExpr &&
"CheckMessageArgumentTypes(): missing expression");
1824 if (param->
hasAttr<NoEscapeAttr>() &&
1826 if (
auto *BE = dyn_cast<BlockExpr>(
1828 BE->getBlockDecl()->setDoesNotEscape();
1833 !param->
hasAttr<CFConsumedAttr>())
1844 Args[i] = argE.
get();
1862 diag::err_call_incomplete_argument, argExpr))
1877 Args[i]->getType()->isBlockPointerType() &&
1881 Args[i] =
arg.get();
1888 for (
unsigned i = NumNamedArgs, e = Args.size(); i < e; ++i) {
1889 if (Args[i]->isTypeDependent())
1895 Args[i] = Arg.
get();
1899 if (Args.size() != NumNamedArgs) {
1900 Diag(Args[NumNamedArgs]->getBeginLoc(),
1901 diag::err_typecheck_call_too_many_args)
1902 << 2 << NumNamedArgs << static_cast<unsigned>(Args.size())
1905 Args.back()->getEndLoc());
1912 IsError |= CheckObjCMethodCall(
1913 Method, SelLoc, makeArrayRef(Args.data(), Args.size()));
1926 if (!method)
return false;
1929 if (
DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(receiver))
1941 if (
ObjCMethodDecl *method = iface->lookupMethod(sel, isInstance))
1946 if (
ObjCMethodDecl *method = iface->lookupPrivateMethod(sel, isInstance))
1951 for (
const auto *I : objType->
quals())
1965 for (
const auto *PROTO : OPT->
quals()) {
1966 if ((MD = PROTO->lookupMethod(Sel, Instance))) {
1986 Diag(MemberLoc, diag::err_invalid_property_name)
1996 diag::err_property_not_found_forward_class,
1997 MemberName, BaseRange))
2015 for (
const auto *I : OPT->
quals())
2025 SuperLoc, SuperType);
2082 if (!(PDecl->getPropertyAttributes() &
2085 diag::warn_property_access_suggest)
2086 << MemberName <<
QualType(OPT, 0) << PDecl->getName()
2091 if (Getter || Setter) {
2113 Corrected.isKeyword() ? nullptr : Corrected.getFoundDecl();
2114 if (ChosenDecl && isa<ObjCPropertyDecl>(ChosenDecl))
2115 if (cast<ObjCPropertyDecl>(ChosenDecl)->isClassProperty()) {
2118 Diag(MemberLoc, diag::err_class_property_found) << MemberName
2126 << MemberName <<
QualType(OPT, 0));
2128 TypoResult, MemberLoc,
2129 SuperLoc, SuperType, Super);
2134 IFace->lookupInstanceVariable(
Member, ClassDeclared)) {
2139 diag::err_property_not_as_forward_class,
2140 MemberName, BaseExpr))
2144 diag::err_ivar_access_using_property_syntax_suggest)
2145 << MemberName <<
QualType(OPT, 0) << Ivar->getDeclName()
2150 Diag(MemberLoc, diag::err_property_not_found)
2172 if (receiverNamePtr->
isStr(
"super")) {
2174 if (
auto classDecl = CurMethod->getClassInterface()) {
2175 SuperType =
QualType(classDecl->getSuperClassType(), 0);
2176 if (CurMethod->isInstanceMethod()) {
2177 if (SuperType.
isNull()) {
2179 Diag(receiverNameLoc, diag::err_root_class_cannot_use_super)
2180 << CurMethod->getClassInterface()->getIdentifier();
2190 receiverNameLoc, T,
true);
2201 Diag(receiverNameLoc, diag::err_expected_either) << tok::identifier
2211 GetterSel = PD->getGetterName();
2212 SetterSel = PD->getSetterName();
2247 if (Getter || Setter) {
2256 propertyNameLoc, receiverNameLoc, IFace);
2258 return ExprError(
Diag(propertyNameLoc, diag::err_property_not_found)
2272 bool ValidateCandidate(
const TypoCorrection &candidate)
override {
2277 std::unique_ptr<CorrectionCandidateCallback> clone()
override {
2278 return std::make_unique<ObjCInterfaceOrSuperCCC>(*
this);
2288 bool HasTrailingDot,
2290 ReceiverType =
nullptr;
2295 if (IsSuper && S->isInObjcMethodScope())
2301 switch (Result.getResultKind()) {
2326 Result.suppressDiagnostics();
2357 Result.getLookupNameInfo(), Result.getLookupKind(), S,
nullptr, CCC,
2359 if (Corrected.isKeyword()) {
2363 PDiag(diag::err_unknown_receiver_suggest) << Name);
2370 PDiag(diag::err_unknown_receiver_suggest) << Name);
2392 Diag(SuperLoc, diag::err_invalid_receiver_to_message_super);
2398 Diag(SuperLoc, diag::err_no_super_class_message)
2403 QualType SuperTy(Class->getSuperClassType(), 0);
2406 Diag(SuperLoc, diag::err_root_class_cannot_use_super)
2407 << Class->getIdentifier();
2422 LBracLoc, SelectorLocs, RBracLoc, Args);
2429 SuperLoc, Sel,
nullptr,
2430 LBracLoc, SelectorLocs, RBracLoc, Args);
2434 bool isSuperReceiver,
2440 if (!ReceiverType.
isNull())
2445 Sel, Method, Loc, Loc, Loc, Args,
2459 if (refactor(Msg,*S.
NSAPIObj, ECommit)) {
2460 auto Builder = S.
Diag(MsgLoc, DiagID)
2468 switch (Edit.
Kind) {
2475 Builder.AddFixItHint(
2496 bool IsClassObjectCall) {
2502 const auto *SE = dyn_cast<ObjCSelectorExpr>(Args[0]->IgnoreParens());
2506 if (!IsClassObjectCall) {
2508 if (!OPT || !OPT->getInterfaceDecl())
2511 OPT->getInterfaceDecl()->lookupInstanceMethod(SE->getSelector());
2514 OPT->getInterfaceDecl()->lookupPrivateMethod(SE->getSelector());
2519 ImpliedMethod = IT->getDecl()->lookupClassMethod(SE->getSelector());
2522 IT->getDecl()->lookupPrivateClassMethod(SE->getSelector());
2527 if (
Ret->isRecordType() ||
Ret->isVectorType() ||
Ret->isExtVectorType()) {
2528 S.
Diag(Loc, diag::warn_objc_unsafe_perform_selector)
2530 << (!
Ret->isRecordType()
2532 :
Ret->isUnionType() ? 1 : 0);
2534 diag::note_objc_unsafe_perform_selector_method_declared_here)
2545 Expr **Args,
unsigned NumArgs) {
2547 bool Format =
false;
2561 if (!Format || NumArgs <= Idx)
2564 Expr *FormatExpr = Args[Idx];
2569 S.
Diag(FormatExpr->
getExprLoc(), diag::warn_objc_cdirective_format_string)
2619 Diag(Loc, diag::err_missing_open_square_message_send)
2624 if (!SelectorLocs.empty() && SelectorLocs.front().isValid())
2625 SelectorSlotLocs = SelectorLocs;
2627 SelectorSlotLocs = Loc;
2633 unsigned NumArgs = ArgsIn.size();
2634 Expr **Args = ArgsIn.data();
2635 assert(SuperLoc.
isInvalid() &&
"Message to super with dependent type");
2638 SelectorLocs,
nullptr, makeArrayRef(Args, NumArgs), RBracLoc,
2645 if (!ClassType || !(Class = ClassType->
getInterface())) {
2646 Diag(Loc, diag::err_invalid_receiver_class_message)
2650 assert(Class &&
"We don't know which class we're messaging?");
2661 ? diag::err_arc_receiver_forward_class
2662 : diag::warn_receiver_forward_class),
2672 Method = Class->lookupClassMethod(Sel);
2676 Method = Class->lookupPrivateClassMethod(Sel);
2679 nullptr,
false,
false, Class))
2687 unsigned NumArgs = ArgsIn.size();
2688 Expr **Args = ArgsIn.data();
2691 Method,
true, SuperLoc.
isValid(), LBracLoc,
2697 diag::err_illegal_message_expr_incomplete_type))
2701 Diag(SuperLoc, diag::err_messaging_super_with_direct_method)
2716 Diag(Loc, diag::warn_direct_initialize_call);
2724 Diag(Loc, diag::warn_direct_super_initialize_call);
2727 Diag(CurMeth->getLocation(), diag::note_method_declared_at)
2728 << CurMeth->getDeclName();
2740 ReceiverType, Sel, SelectorLocs,
2741 Method, makeArrayRef(Args, NumArgs),
2742 RBracLoc, isImplicit);
2745 ReceiverTypeInfo, Sel, SelectorLocs,
2746 Method, makeArrayRef(Args, NumArgs),
2747 RBracLoc, isImplicit);
2753 ReceiverType,
true);
2769 if (ReceiverType.
isNull())
2772 if (!ReceiverTypeInfo)
2777 nullptr, LBracLoc, SelectorLocs, RBracLoc,
2789 Sel, Method, Loc, Loc, Loc, Args,
2796 const auto *Protocol = dyn_cast<ObjCProtocolDecl>(M->
getDeclContext());
2800 if (
const auto *RootClass = dyn_cast_or_null<ObjCInterfaceDecl>(
2804 if (
P->getCanonicalDecl() == Protocol->getCanonicalDecl())
2849 assert((Receiver || SuperLoc.
isValid()) &&
"If the Receiver is null, the "
2850 "SuperLoc must be valid so we can "
2858 if (!SelectorLocs.empty() && SelectorLocs.front().isValid())
2859 SelectorSlotLocs = SelectorLocs;
2861 SelectorSlotLocs = Loc;
2865 Diag(Loc, diag::err_missing_open_square_message_send)
2879 if (Result.isInvalid())
return ExprError();
2880 Receiver = Result.get();
2886 unsigned NumArgs = ArgsIn.size();
2887 Expr **Args = ArgsIn.data();
2888 assert(SuperLoc.
isInvalid() &&
"Message to super with dependent type");
2891 SelectorLocs,
nullptr, makeArrayRef(Args, NumArgs),
2892 RBracLoc, isImplicit);
2898 if (Result.isInvalid())
2900 Receiver = Result.get();
2901 ReceiverType = Receiver->
getType();
2914 Diag(Loc, diag::warn_bad_receiver_type) << ReceiverType << RecRange;
2917 CK_CPointerToObjCPointerCast).
get();
2922 CastKind Kind = IsNull ? CK_NullToPointer : CK_IntegralToPointer;
2926 ReceiverType = Receiver->
getType();
2930 diag::err_incomplete_receiver_type))
2935 Receiver = result.
get();
2936 ReceiverType = Receiver->
getType();
2957 if (!Methods.empty()) {
2960 Method = Methods[0];
2964 Method = BestMethod;
2968 receiverIsIdLike, Methods))
2986 Diag(SelLoc, diag::warn_instance_method_on_class_found)
2999 Method = ClassDecl->lookupClassMethod(Sel);
3002 Method = ClassDecl->lookupPrivateClassMethod(Sel);
3017 if (!Methods.empty()) {
3020 Method = Methods[0];
3026 if (
ID->getSuperClass())
3027 Diag(SelLoc, diag::warn_root_inst_method_not_found)
3035 Method = BestMethod;
3057 ClassDecl = OCIType->getInterfaceDecl();
3066 ? diag::err_arc_receiver_forward_instance
3067 : diag::warn_receiver_forward_instance,
3072 forwardClass = OCIType->getInterfaceDecl();
3074 diag::note_receiver_is_id);
3089 Diag(SelLoc, diag::err_arc_may_not_respond)
3090 << OCIType->getPointeeType() << Sel << RecRange
3091 <<
SourceRange(SelectorLocs.front(), SelectorLocs.back());
3095 if (!Method && (!Receiver || !
isSelfExpr(Receiver))) {
3099 if (OCIType->qual_empty()) {
3104 if (!Methods.empty()) {
3107 Method = Methods[0];
3112 Method = BestMethod;
3119 if (Method && !forwardClass)
3120 Diag(SelLoc, diag::warn_maynot_respond)
3121 << OCIType->getInterfaceDecl()->getIdentifier()
3130 Diag(Loc, diag::err_bad_receiver_type) << ReceiverType << RecRange;
3143 diag::err_messaging_unqualified_id_with_direct_method);
3155 diag::err_messaging_class_with_direct_method);
3168 Diag(SuperLoc, diag::err_messaging_super_with_direct_method);
3179 }
else if (ReceiverType->
isObjCIdType() && !isImplicit) {
3180 Diag(Receiver->
getExprLoc(), diag::warn_messaging_unqualified_id);
3183 if (DIFunctionScopeInfo &&
3186 bool isDesignatedInitChain =
false;
3193 if (!
ID->declaresOrInheritsDesignatedInitializers() ||
3194 ID->isDesignatedInitializer(Sel)) {
3195 isDesignatedInitChain =
true;
3201 if (!isDesignatedInitChain) {
3205 assert(isDesignated && InitMethod);
3208 diag::warn_objc_designated_init_non_designated_init_call :
3209 diag::warn_objc_designated_init_non_super_designated_init_call);
3211 diag::note_objc_designated_init_marked_here);
3215 if (DIFunctionScopeInfo &&
3219 Diag(SelLoc, diag::warn_objc_secondary_init_super_init_call);
3226 unsigned NumArgs = ArgsIn.size();
3227 Expr **Args = ArgsIn.data();
3234 Method, ClassMessage, SuperLoc.
isValid(),
3235 LBracLoc, RBracLoc, RecRange, ReturnType, VK))
3240 diag::err_illegal_message_expr_incomplete_type))
3269 Diag(SelLoc, diag::err_arc_illegal_explicit_message)
3274 if (Method && NumArgs >= 1) {
3275 if (
const auto *SelExp =
3276 dyn_cast<ObjCSelectorExpr>(Args[0]->IgnoreParens())) {
3277 Selector ArgSel = SelExp->getSelector();
3280 SelExp->getSourceRange());
3284 SelExp->getSourceRange());
3287 switch (SelFamily) {
3294 if (!SelMethod->
hasAttr<NSReturnsNotRetainedAttr>()) {
3297 diag::err_arc_perform_selector_retains);
3304 if (SelMethod->
hasAttr<NSReturnsRetainedAttr>()) {
3307 diag::err_arc_perform_selector_retains);
3316 Diag(SelLoc, diag::warn_arc_perform_selector_leaks);
3317 Diag(Args[0]->getExprLoc(), diag::note_used_here);
3331 ReceiverType, Sel, SelectorLocs, Method,
3332 makeArrayRef(Args, NumArgs), RBracLoc,
3336 Receiver, Sel, SelectorLocs, Method,
3337 makeArrayRef(Args, NumArgs), RBracLoc,
3343 bool IsClassObjectCall = ClassMessage;
3350 if (OPT->getObjectType()->isObjCClass()) {
3352 IsClassObjectCall =
true;
3360 ReceiverType, IsClassObjectCall);
3365 if (Result->getMethodFamily() ==
OMF_init &&
3373 Result->setDelegateInitCall(
true);
3384 if (!isImplicit && Method) {
3397 CheckObjCCircularContainer(Result);
3427 if (isa<ParenListExpr>(Receiver)) {
3429 if (Result.isInvalid())
return ExprError();
3430 Receiver = Result.get();
3442 nullptr, LBracLoc, SelectorLocs,
3476 bool isIndirect =
false;
3480 type = ref->getPointeeType();
3487 type = ptr->getPointeeType();
3494 }
else if (
const ArrayType *array =
type->getAsArrayTypeUnsafe()) {
3495 type =
QualType(array->getElementType()->getBaseElementTypeUnsafe(), 0);
3503 if (
type->isObjCARCBridgableType())
3508 if (
type->isObjCARCBridgableType())
3529 ACCResult merge(ACCResult left, ACCResult right) {
3530 if (left == right)
return left;
3531 if (left == ACC_bottom)
return right;
3532 if (right == ACC_bottom)
return left;
3538 class ARCCastChecker :
public StmtVisitor<ARCCastChecker, ACCResult> {
3548 return type->isCARCBridgableType();
3554 : Context(Context), SourceClass(source), TargetClass(target),
3555 Diagnose(diagnose) {}
3558 ACCResult Visit(
Expr *e) {
3562 ACCResult VisitStmt(
Stmt *
s) {
3567 ACCResult VisitExpr(
Expr *e) {
3583 ACCResult VisitCastExpr(
CastExpr *e) {
3585 case CK_NullToPointer:
3589 case CK_LValueToRValue:
3591 case CK_CPointerToObjCPointerCast:
3592 case CK_BlockPointerToObjCPointerCast:
3593 case CK_AnyPointerToBlockPointerCast:
3608 return Visit(e->
getRHS());
3614 if (left == ACC_invalid)
return ACC_invalid;
3625 ACCResult VisitStmtExpr(
StmtExpr *e) {
3636 !
var->hasDefinition(Context) &&
3637 var->getType().isConstQualified()) {
3644 return ACC_plusZero;
3652 ACCResult VisitCallExpr(
CallExpr *e) {
3654 if (ACCResult result = checkCallToFunction(fn))
3657 return super::VisitCallExpr(e);
3669 if (fn->
hasAttr<CFReturnsNotRetainedAttr>())
3670 return ACC_plusZero;
3675 if (fn->
hasAttr<CFReturnsRetainedAttr>())
3676 return Diagnose ? ACC_plusOne
3681 if (builtinID == Builtin::BI__builtin___CFStringMakeConstantString)
3685 if (!fn->
hasAttr<CFAuditedTransferAttr>())
3690 return Diagnose ? ACC_plusOne
3693 return ACC_plusZero;
3706 return checkCallToMethod(method);
3710 if (!method)
return ACC_invalid;
3719 if (method->
hasAttr<CFReturnsNotRetainedAttr>())
3720 return ACC_plusZero;
3724 if (method->
hasAttr<CFReturnsRetainedAttr>())
3736 return ACC_plusZero;
3750 template <
typename DiagBuilderT>
3754 Expr *realCast,
const char *bridgeKeyword,
const char *CFBridgeName) {
3770 NCE->getAngleBrackets().getEnd());
3774 char PrevChar = *
SM.getCharacterData(
range.getBegin().getLocWithOffset(-1));
3778 BridgeCall += CFBridgeName;
3785 castedE = CCE->getSubExpr();
3792 char PrevChar = *
SM.getCharacterData(
range.getBegin().getLocWithOffset(-1));
3796 BridgeCall += CFBridgeName;
3798 if (isa<ParenExpr>(castedE)) {
3817 castCode += bridgeKeyword;
3821 NCE->getAngleBrackets().getEnd());
3826 castCode += bridgeKeyword;
3831 if (isa<ParenExpr>(castedE)) {
3845 template <
typename T>
3852 for (
auto *Redecl : RT->getDecl()->getMostRecentDecl()->redecls()) {
3853 if (
auto *
attr = Redecl->getAttr<T>())
3864 TDNDecl = TD->getDecl();
3865 if (ObjCBridgeRelatedAttr *ObjCBAttr =
3866 getObjCBridgeAttr<ObjCBridgeRelatedAttr>(TD))
3883 UnavailableAttr::IR_ARCForbiddenConversion))
3896 unsigned srcKind = 0;
3920 S.
Diag(loc, diag::err_arc_cast_requires_bridge)
3929 ACCResult CreateRule =
3931 assert(CreateRule != ACC_bottom &&
"This cast should already be accepted.");
3932 if (CreateRule != ACC_plusOne)
3935 ? S.
Diag(noteLoc, diag::note_arc_bridge)
3936 : S.
Diag(noteLoc, diag::note_arc_cstyle_bridge);
3939 castType,
castExpr, realCast,
"__bridge ",
3942 if (CreateRule != ACC_plusZero)
3945 ? S.
Diag(noteLoc, diag::note_arc_cstyle_bridge_transfer)
3948 diag::note_arc_bridge_transfer)
3949 << castExprType << br;
3952 castType,
castExpr, realCast,
"__bridge_transfer ",
3953 br ?
"CFBridgingRelease" :
nullptr);
3962 S.
Diag(loc, diag::err_arc_cast_requires_bridge)
3970 ACCResult CreateRule =
3972 assert(CreateRule != ACC_bottom &&
"This cast should already be accepted.");
3973 if (CreateRule != ACC_plusOne)
3976 ? S.
Diag(noteLoc, diag::note_arc_bridge)
3977 : S.
Diag(noteLoc, diag::note_arc_cstyle_bridge);
3979 castType,
castExpr, realCast,
"__bridge ",
3982 if (CreateRule != ACC_plusZero)
3985 ? S.
Diag(noteLoc, diag::note_arc_cstyle_bridge_retained)
3988 diag::note_arc_bridge_retained)
3992 castType,
castExpr, realCast,
"__bridge_retained ",
3993 br ?
"CFBridgingRetain" :
nullptr);
3999 S.
Diag(loc, diag::err_arc_mismatched_cast)
4001 << srcKind << castExprType << castType
4002 << castRange <<
castExpr->getSourceRange();
4005 template <
typename TB>
4007 bool &HadTheAttribute,
bool warn) {
4009 HadTheAttribute =
false;
4012 if (TB *ObjCBAttr = getObjCBridgeAttr<TB>(TD)) {
4014 HadTheAttribute =
true;
4015 if (Parm->isStr(
"id"))
4028 = InterfacePointerType->getObjectType()->getInterface();
4029 if ((CastClass == ExprClass) ||
4033 S.
Diag(
castExpr->getBeginLoc(), diag::warn_objc_invalid_bridge)
4038 castType, ExprClass)))
4045 S.
Diag(
castExpr->getBeginLoc(), diag::warn_objc_invalid_bridge)
4046 << T <<
Target->getName() << castType;
4048 S.
Diag(
Target->getBeginLoc(), diag::note_declared_at);
4055 diag::err_objc_cf_bridged_not_interface)
4068 template <
typename TB>
4070 bool &HadTheAttribute,
bool warn) {
4072 HadTheAttribute =
false;
4075 if (TB *ObjCBAttr = getObjCBridgeAttr<TB>(TD)) {
4077 HadTheAttribute =
true;
4078 if (Parm->isStr(
"id"))
4090 castExpr->getType()->getAsObjCInterfacePointerType()) {
4092 = InterfacePointerType->getObjectType()->getInterface();
4093 if ((CastClass == ExprClass) ||
4098 diag::warn_objc_invalid_bridge_to_cf)
4099 <<
castExpr->getType()->getPointeeType() << T;
4103 }
else if (
castExpr->getType()->isObjCIdType() ||
4113 diag::warn_objc_invalid_bridge_to_cf)
4114 <<
castExpr->getType() << castType;
4116 S.
Diag(
Target->getBeginLoc(), diag::note_declared_at);
4123 diag::err_objc_ns_bridged_invalid_cfobject)
4124 <<
castExpr->getType() << castType;
4127 S.
Diag(
Target->getBeginLoc(), diag::note_declared_at);
4144 bool HasObjCBridgeAttr;
4145 bool ObjCBridgeAttrWillNotWarn =
4146 CheckObjCBridgeNSCast<ObjCBridgeAttr>(*
this, castType,
castExpr, HasObjCBridgeAttr,
4148 if (ObjCBridgeAttrWillNotWarn && HasObjCBridgeAttr)
4150 bool HasObjCBridgeMutableAttr;
4151 bool ObjCBridgeMutableAttrWillNotWarn =
4152 CheckObjCBridgeNSCast<ObjCBridgeMutableAttr>(*
this, castType,
castExpr,
4153 HasObjCBridgeMutableAttr,
false);
4154 if (ObjCBridgeMutableAttrWillNotWarn && HasObjCBridgeMutableAttr)
4157 if (HasObjCBridgeAttr)
4158 CheckObjCBridgeNSCast<ObjCBridgeAttr>(*
this, castType,
castExpr, HasObjCBridgeAttr,
4160 else if (HasObjCBridgeMutableAttr)
4161 CheckObjCBridgeNSCast<ObjCBridgeMutableAttr>(*
this, castType,
castExpr,
4162 HasObjCBridgeMutableAttr,
true);
4165 bool HasObjCBridgeAttr;
4166 bool ObjCBridgeAttrWillNotWarn =
4167 CheckObjCBridgeCFCast<ObjCBridgeAttr>(*
this, castType,
castExpr, HasObjCBridgeAttr,
4169 if (ObjCBridgeAttrWillNotWarn && HasObjCBridgeAttr)
4171 bool HasObjCBridgeMutableAttr;
4172 bool ObjCBridgeMutableAttrWillNotWarn =
4173 CheckObjCBridgeCFCast<ObjCBridgeMutableAttr>(*
this, castType,
castExpr,
4174 HasObjCBridgeMutableAttr,
false);
4175 if (ObjCBridgeMutableAttrWillNotWarn && HasObjCBridgeMutableAttr)
4178 if (HasObjCBridgeAttr)
4179 CheckObjCBridgeCFCast<ObjCBridgeAttr>(*
this, castType,
castExpr, HasObjCBridgeAttr,
4181 else if (HasObjCBridgeMutableAttr)
4182 CheckObjCBridgeCFCast<ObjCBridgeMutableAttr>(*
this, castType,
castExpr,
4183 HasObjCBridgeMutableAttr,
true);
4190 if (PRE->isExplicitProperty()) {
4192 SrcType = PDecl->getType();
4194 else if (PRE->isImplicitProperty()) {
4196 SrcType = Getter->getReturnType();
4219 : CK_CPointerToObjCPointerCast;
4231 bool CfToNs,
bool Diagnose) {
4232 QualType T = CfToNs ? SrcType : DestType;
4248 Diag(Loc, diag::err_objc_bridged_related_invalid_class) << RCId
4249 << SrcType << DestType;
4256 RelatedClass = cast<ObjCInterfaceDecl>(
Target);
4259 Diag(Loc, diag::err_objc_bridged_related_invalid_class_name) << RCId
4260 << SrcType << DestType;
4263 Diag(
Target->getBeginLoc(), diag::note_declared_at);
4269 if (CfToNs && CMId) {
4274 Diag(Loc, diag::err_objc_bridged_related_known_method)
4275 << SrcType << DestType << Sel <<
false;
4283 if (!CfToNs && IMId) {
4285 InstanceMethod = RelatedClass->
lookupMethod(Sel,
true);
4286 if (!InstanceMethod) {
4288 Diag(Loc, diag::err_objc_bridged_related_known_method)
4289 << SrcType << DestType << Sel <<
true;
4301 Expr *&SrcExpr,
bool Diagnose) {
4306 if (!CfToNs && !NsToCf)
4314 ClassMethod, InstanceMethod, TDNDecl,
4324 ExpressionString +=
" ";
4325 ExpressionString += ClassMethod->getSelector().getAsString();
4329 Diag(Loc, diag::err_objc_bridged_related_known_method)
4330 << SrcType << DestType << ClassMethod->getSelector() <<
false
4339 Expr *args[] = { SrcExpr };
4341 ClassMethod->getLocation(),
4342 ClassMethod->getSelector(), ClassMethod,
4344 SrcExpr = msg.
get();
4351 if (InstanceMethod) {
4356 if (InstanceMethod->isPropertyAccessor())
4358 InstanceMethod->findPropertyDecl()) {
4360 ExpressionString =
".";
4361 ExpressionString += PDecl->getNameAsString();
4362 Diag(Loc, diag::err_objc_bridged_related_known_method)
4363 << SrcType << DestType << InstanceMethod->getSelector() <<
true
4366 if (ExpressionString.empty()) {
4368 ExpressionString =
" ";
4369 ExpressionString += InstanceMethod->getSelector().getAsString();
4370 ExpressionString +=
"]";
4372 Diag(Loc, diag::err_objc_bridged_related_known_method)
4373 << SrcType << DestType << InstanceMethod->getSelector() <<
true
4382 InstanceMethod->getLocation(),
4383 InstanceMethod->getSelector(),
4384 InstanceMethod, None);
4385 SrcExpr = msg.
get();
4396 bool Diagnose,
bool DiagnoseCFAudited,
4408 if (exprACTC == castACTC) {
4413 castType != castExprType) {
4419 if (
const ParenType *PT = dyn_cast<ParenType>(DT))
4420 QDT = PT->desugar();
4421 else if (
const TypeOfType *TP = dyn_cast<TypeOfType>(DT))
4422 QDT = TP->desugar();
4423 else if (
const AttributedType *AT = dyn_cast<AttributedType>(DT))
4424 QDT = AT->desugar();
4425 if (QDT != castType &&
4430 Diag(loc, diag::err_arc_nolifetime_behavior);
4464 switch (ARCCastChecker(
Context, exprACTC, castACTC,
false).Visit(
castExpr)) {
4477 CK_ARCConsumeObject,
castExpr,
nullptr,
4503 (Opc == BO_NE || Opc == BO_EQ))) {
4525 castType =
cast->getTypeAsWritten();
4528 castRange =
cast->getTypeInfoAsWritten()->getTypeLoc().getSourceRange();
4529 castType =
cast->getTypeAsWritten();
4532 llvm_unreachable(
"Unexpected ImplicitCastExpr");
4550 if (
ParenExpr *pe = dyn_cast<ParenExpr>(e)) {
4553 }
else if (
UnaryOperator *uo = dyn_cast<UnaryOperator>(e)) {
4554 assert(uo->getOpcode() == UO_Extension);
4558 uo->getOperatorLoc(),
false,
4561 assert(!gse->isResultDependent());
4563 unsigned n = gse->getNumAssocs();
4566 subExprs.reserve(n);
4567 subTypes.reserve(n);
4569 subTypes.push_back(assoc.getTypeSourceInfo());
4570 Expr *sub = assoc.getAssociationExpr();
4571 if (assoc.isSelected())
4573 subExprs.push_back(sub);
4577 Context, gse->getGenericLoc(), gse->getControllingExpr(), subTypes,
4578 subExprs, gse->getDefaultLoc(), gse->getRParenLoc(),
4579 gse->containsUnexpandedParameterPack(), gse->getResultIndex());
4581 assert(isa<ImplicitCastExpr>(e) &&
"bad form of unbridged cast!");
4582 return cast<ImplicitCastExpr>(e)->getSubExpr();
4592 if (isa<ObjCObjectPointerType>(canCastType) &&
4598 return !ObjI->isArcWeakrefUnavailable();
4605 Expr *curExpr = e, *prevExpr =
nullptr;
4610 if (
auto *pe = dyn_cast<ParenExpr>(curExpr)) {
4612 curExpr = pe->getSubExpr();
4616 if (
auto *ce = dyn_cast<CastExpr>(curExpr)) {
4617 if (
auto *ice = dyn_cast<ImplicitCastExpr>(ce))
4618 if (ice->getCastKind() == CK_ARCReclaimReturnedObject) {
4620 return ice->getSubExpr();
4621 if (
auto *pe = dyn_cast<ParenExpr>(prevExpr))
4622 pe->setSubExpr(ice->getSubExpr());
4624 cast<CastExpr>(prevExpr)->setSubExpr(ice->getSubExpr());
4629 curExpr = ce->getSubExpr();
4647 SubExpr = SubResult.
get();
4654 bool MustConsume =
false;
4661 : CK_CPointerToObjCPointerCast);
4668 Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind)
4675 Diag(BridgeKeywordLoc, diag::note_arc_bridge)
4677 Diag(BridgeKeywordLoc, diag::note_arc_bridge_transfer)
4680 br ?
"CFBridgingRelease "
4681 :
"__bridge_transfer ");
4711 Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind)
4719 Diag(BridgeKeywordLoc, diag::note_arc_bridge)
4721 Diag(BridgeKeywordLoc, diag::note_arc_bridge_retained)
4724 br ?
"CFBridgingRetain " :
"__bridge_retained");
4731 Diag(LParenLoc, diag::err_arc_bridge_cast_incompatible)
4732 << FromType << T <<
Kind