| //===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===// |
| // |
| // The LLVM Compiler Infrastructure |
| // |
| // This file is distributed under the University of Illinois Open Source |
| // License. See LICENSE.TXT for details. |
| // |
| //===----------------------------------------------------------------------===// |
| // |
| // This file implements decl-related attribute processing. |
| // |
| //===----------------------------------------------------------------------===// |
| |
| #include "clang/Sema/SemaInternal.h" |
| #include "TargetAttributesSema.h" |
| #include "clang/AST/ASTContext.h" |
| #include "clang/AST/CXXInheritance.h" |
| #include "clang/AST/DeclCXX.h" |
| #include "clang/AST/DeclTemplate.h" |
| #include "clang/AST/DeclObjC.h" |
| #include "clang/AST/Expr.h" |
| #include "clang/Basic/SourceManager.h" |
| #include "clang/Basic/TargetInfo.h" |
| #include "clang/Sema/DeclSpec.h" |
| #include "clang/Sema/DelayedDiagnostic.h" |
| #include "clang/Sema/Lookup.h" |
| #include "llvm/ADT/StringExtras.h" |
| using namespace clang; |
| using namespace sema; |
| |
| /// These constants match the enumerated choices of |
| /// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type. |
| enum AttributeDeclKind { |
| ExpectedFunction, |
| ExpectedUnion, |
| ExpectedVariableOrFunction, |
| ExpectedFunctionOrMethod, |
| ExpectedParameter, |
| ExpectedFunctionMethodOrBlock, |
| ExpectedFunctionMethodOrParameter, |
| ExpectedClass, |
| ExpectedVariable, |
| ExpectedMethod, |
| ExpectedVariableFunctionOrLabel, |
| ExpectedFieldOrGlobalVar, |
| ExpectedStruct, |
| ExpectedTLSVar |
| }; |
| |
| //===----------------------------------------------------------------------===// |
| // Helper functions |
| //===----------------------------------------------------------------------===// |
| |
| static const FunctionType *getFunctionType(const Decl *D, |
| bool blocksToo = true) { |
| QualType Ty; |
| if (const ValueDecl *decl = dyn_cast<ValueDecl>(D)) |
| Ty = decl->getType(); |
| else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D)) |
| Ty = decl->getType(); |
| else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
| Ty = decl->getUnderlyingType(); |
| else |
| return 0; |
| |
| if (Ty->isFunctionPointerType()) |
| Ty = Ty->getAs<PointerType>()->getPointeeType(); |
| else if (blocksToo && Ty->isBlockPointerType()) |
| Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
| |
| return Ty->getAs<FunctionType>(); |
| } |
| |
| // FIXME: We should provide an abstraction around a method or function |
| // to provide the following bits of information. |
| |
| /// isFunction - Return true if the given decl has function |
| /// type (function or function-typed variable). |
| static bool isFunction(const Decl *D) { |
| return getFunctionType(D, false) != NULL; |
| } |
| |
| /// isFunctionOrMethod - Return true if the given decl has function |
| /// type (function or function-typed variable) or an Objective-C |
| /// method. |
| static bool isFunctionOrMethod(const Decl *D) { |
| return isFunction(D) || isa<ObjCMethodDecl>(D); |
| } |
| |
| /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| /// type (function or function-typed variable) or an Objective-C |
| /// method or a block. |
| static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| if (isFunctionOrMethod(D)) |
| return true; |
| // check for block is more involved. |
| if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
| QualType Ty = V->getType(); |
| return Ty->isBlockPointerType(); |
| } |
| return isa<BlockDecl>(D); |
| } |
| |
| /// Return true if the given decl has a declarator that should have |
| /// been processed by Sema::GetTypeForDeclarator. |
| static bool hasDeclarator(const Decl *D) { |
| // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
| return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| isa<ObjCPropertyDecl>(D); |
| } |
| |
| /// hasFunctionProto - Return true if the given decl has a argument |
| /// information. This decl should have already passed |
| /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
| static bool hasFunctionProto(const Decl *D) { |
| if (const FunctionType *FnTy = getFunctionType(D)) |
| return isa<FunctionProtoType>(FnTy); |
| else { |
| assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
| return true; |
| } |
| } |
| |
| /// getFunctionOrMethodNumArgs - Return number of function or method |
| /// arguments. It is an error to call this on a K&R function (use |
| /// hasFunctionProto first). |
| static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| if (const FunctionType *FnTy = getFunctionType(D)) |
| return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
| if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
| return BD->getNumParams(); |
| return cast<ObjCMethodDecl>(D)->param_size(); |
| } |
| |
| static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| if (const FunctionType *FnTy = getFunctionType(D)) |
| return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
| if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
| return BD->getParamDecl(Idx)->getType(); |
| |
| return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
| } |
| |
| static QualType getFunctionOrMethodResultType(const Decl *D) { |
| if (const FunctionType *FnTy = getFunctionType(D)) |
| return cast<FunctionProtoType>(FnTy)->getResultType(); |
| return cast<ObjCMethodDecl>(D)->getResultType(); |
| } |
| |
| static bool isFunctionOrMethodVariadic(const Decl *D) { |
| if (const FunctionType *FnTy = getFunctionType(D)) { |
| const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
| return proto->isVariadic(); |
| } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
| return BD->isVariadic(); |
| else { |
| return cast<ObjCMethodDecl>(D)->isVariadic(); |
| } |
| } |
| |
| static bool isInstanceMethod(const Decl *D) { |
| if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
| return MethodDecl->isInstance(); |
| return false; |
| } |
| |
| static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
| const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
| if (!PT) |
| return false; |
| |
| ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| if (!Cls) |
| return false; |
| |
| IdentifierInfo* ClsName = Cls->getIdentifier(); |
| |
| // FIXME: Should we walk the chain of classes? |
| return ClsName == &Ctx.Idents.get("NSString") || |
| ClsName == &Ctx.Idents.get("NSMutableString"); |
| } |
| |
| static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
| const PointerType *PT = T->getAs<PointerType>(); |
| if (!PT) |
| return false; |
| |
| const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
| if (!RT) |
| return false; |
| |
| const RecordDecl *RD = RT->getDecl(); |
| if (RD->getTagKind() != TTK_Struct) |
| return false; |
| |
| return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| } |
| |
| /// \brief Check if the attribute has exactly as many args as Num. May |
| /// output an error. |
| static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| unsigned int Num) { |
| if (Attr.getNumArgs() != Num) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num; |
| return false; |
| } |
| |
| return true; |
| } |
| |
| |
| /// \brief Check if the attribute has at least as many args as Num. May |
| /// output an error. |
| static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
| unsigned int Num) { |
| if (Attr.getNumArgs() < Num) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| return false; |
| } |
| |
| return true; |
| } |
| |
| /// \brief Check if IdxExpr is a valid argument index for a function or |
| /// instance method D. May output an error. |
| /// |
| /// \returns true if IdxExpr is a valid index. |
| static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D, |
| StringRef AttrName, |
| SourceLocation AttrLoc, |
| unsigned AttrArgNum, |
| const Expr *IdxExpr, |
| uint64_t &Idx) |
| { |
| assert(isFunctionOrMethod(D) && hasFunctionProto(D)); |
| |
| // In C++ the implicit 'this' function parameter also counts. |
| // Parameters are counted from one. |
| const bool HasImplicitThisParam = isInstanceMethod(D); |
| const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| const unsigned FirstIdx = 1; |
| |
| llvm::APSInt IdxInt; |
| if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
| S.Diag(AttrLoc, diag::err_attribute_argument_n_not_int) |
| << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| return false; |
| } |
| |
| Idx = IdxInt.getLimitedValue(); |
| if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) { |
| S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds) |
| << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| return false; |
| } |
| Idx--; // Convert to zero-based. |
| if (HasImplicitThisParam) { |
| if (Idx == 0) { |
| S.Diag(AttrLoc, |
| diag::err_attribute_invalid_implicit_this_argument) |
| << AttrName << IdxExpr->getSourceRange(); |
| return false; |
| } |
| --Idx; |
| } |
| |
| return true; |
| } |
| |
| /// |
| /// \brief Check if passed in Decl is a field or potentially shared global var |
| /// \return true if the Decl is a field or potentially shared global variable |
| /// |
| static bool mayBeSharedVariable(const Decl *D) { |
| if (isa<FieldDecl>(D)) |
| return true; |
| if (const VarDecl *vd = dyn_cast<VarDecl>(D)) |
| return (vd->hasGlobalStorage() && !(vd->isThreadSpecified())); |
| |
| return false; |
| } |
| |
| /// \brief Check if the passed-in expression is of type int or bool. |
| static bool isIntOrBool(Expr *Exp) { |
| QualType QT = Exp->getType(); |
| return QT->isBooleanType() || QT->isIntegerType(); |
| } |
| |
| |
| // Check to see if the type is a smart pointer of some kind. We assume |
| // it's a smart pointer if it defines both operator-> and operator*. |
| static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
| if (Res1.first == Res1.second) |
| return false; |
| |
| DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
| if (Res2.first == Res2.second) |
| return false; |
| |
| return true; |
| } |
| |
| /// \brief Check if passed in Decl is a pointer type. |
| /// Note that this function may produce an error message. |
| /// \return true if the Decl is a pointer type; false otherwise |
| static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| const AttributeList &Attr) { |
| if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
| QualType QT = vd->getType(); |
| if (QT->isAnyPointerType()) |
| return true; |
| |
| if (const RecordType *RT = QT->getAs<RecordType>()) { |
| // If it's an incomplete type, it could be a smart pointer; skip it. |
| // (We don't want to force template instantiation if we can avoid it, |
| // since that would alter the order in which templates are instantiated.) |
| if (RT->isIncompleteType()) |
| return true; |
| |
| if (threadSafetyCheckIsSmartPointer(S, RT)) |
| return true; |
| } |
| |
| S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
| << Attr.getName()->getName() << QT; |
| } else { |
| S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| << Attr.getName(); |
| } |
| return false; |
| } |
| |
| /// \brief Checks that the passed in QualType either is of RecordType or points |
| /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
| static const RecordType *getRecordType(QualType QT) { |
| if (const RecordType *RT = QT->getAs<RecordType>()) |
| return RT; |
| |
| // Now check if we point to record type. |
| if (const PointerType *PT = QT->getAs<PointerType>()) |
| return PT->getPointeeType()->getAs<RecordType>(); |
| |
| return 0; |
| } |
| |
| |
| static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| CXXBasePath &Path, void *Unused) { |
| const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
| if (RT->getDecl()->getAttr<LockableAttr>()) |
| return true; |
| return false; |
| } |
| |
| |
| /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
| /// resolves to a lockable object. |
| static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| QualType Ty) { |
| const RecordType *RT = getRecordType(Ty); |
| |
| // Warn if could not get record type for this argument. |
| if (!RT) { |
| S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
| << Attr.getName() << Ty.getAsString(); |
| return; |
| } |
| |
| // Don't check for lockable if the class hasn't been defined yet. |
| if (RT->isIncompleteType()) |
| return; |
| |
| // Allow smart pointers to be used as lockable objects. |
| // FIXME -- Check the type that the smart pointer points to. |
| if (threadSafetyCheckIsSmartPointer(S, RT)) |
| return; |
| |
| // Check if the type is lockable. |
| RecordDecl *RD = RT->getDecl(); |
| if (RD->getAttr<LockableAttr>()) |
| return; |
| |
| // Else check if any base classes are lockable. |
| if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| CXXBasePaths BPaths(false, false); |
| if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| return; |
| } |
| |
| S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| << Attr.getName() << Ty.getAsString(); |
| } |
| |
| /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
| /// from Sidx, resolve to a lockable object. |
| /// \param Sidx The attribute argument index to start checking with. |
| /// \param ParamIdxOk Whether an argument can be indexing into a function |
| /// parameter list. |
| static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
| const AttributeList &Attr, |
| SmallVectorImpl<Expr*> &Args, |
| int Sidx = 0, |
| bool ParamIdxOk = false) { |
| for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
| Expr *ArgExp = Attr.getArg(Idx); |
| |
| if (ArgExp->isTypeDependent()) { |
| // FIXME -- need to check this again on template instantiation |
| Args.push_back(ArgExp); |
| continue; |
| } |
| |
| if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
| if (StrLit->getLength() == 0 || |
| StrLit->getString() == StringRef("*")) { |
| // Pass empty strings to the analyzer without warnings. |
| // Treat "*" as the universal lock. |
| Args.push_back(ArgExp); |
| continue; |
| } |
| |
| // We allow constant strings to be used as a placeholder for expressions |
| // that are not valid C++ syntax, but warn that they are ignored. |
| S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| Attr.getName(); |
| Args.push_back(ArgExp); |
| continue; |
| } |
| |
| QualType ArgTy = ArgExp->getType(); |
| |
| // A pointer to member expression of the form &MyClass::mu is treated |
| // specially -- we need to look at the type of the member. |
| if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| if (UOp->getOpcode() == UO_AddrOf) |
| if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| if (DRE->getDecl()->isCXXInstanceMember()) |
| ArgTy = DRE->getDecl()->getType(); |
| |
| // First see if we can just cast to record type, or point to record type. |
| const RecordType *RT = getRecordType(ArgTy); |
| |
| // Now check if we index into a record type function param. |
| if(!RT && ParamIdxOk) { |
| FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| if(FD && IL) { |
| unsigned int NumParams = FD->getNumParams(); |
| llvm::APInt ArgValue = IL->getValue(); |
| uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| << Attr.getName() << Idx + 1 << NumParams; |
| continue; |
| } |
| ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
| } |
| } |
| |
| checkForLockableRecord(S, D, Attr, ArgTy); |
| |
| Args.push_back(ArgExp); |
| } |
| } |
| |
| //===----------------------------------------------------------------------===// |
| // Attribute Implementations |
| //===----------------------------------------------------------------------===// |
| |
| // FIXME: All this manual attribute parsing code is gross. At the |
| // least add some helper functions to check most argument patterns (# |
| // and types of args). |
| |
| enum ThreadAttributeDeclKind { |
| ThreadExpectedFieldOrGlobalVar, |
| ThreadExpectedFunctionOrMethod, |
| ThreadExpectedClassOrStruct |
| }; |
| |
| static bool checkGuardedVarAttrCommon(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| assert(!Attr.isInvalid()); |
| |
| if (!checkAttributeNumArgs(S, Attr, 0)) |
| return false; |
| |
| // D must be either a member field or global (potentially shared) variable. |
| if (!mayBeSharedVariable(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
| return false; |
| } |
| |
| return true; |
| } |
| |
| static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| return; |
| |
| D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context)); |
| } |
| |
| static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| return; |
| |
| if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| return; |
| |
| D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context)); |
| } |
| |
| static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| const AttributeList &Attr, |
| Expr* &Arg) { |
| assert(!Attr.isInvalid()); |
| |
| if (!checkAttributeNumArgs(S, Attr, 1)) |
| return false; |
| |
| // D must be either a member field or global (potentially shared) variable. |
| if (!mayBeSharedVariable(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
| return false; |
| } |
| |
| SmallVector<Expr*, 1> Args; |
| // check that all arguments are lockable objects |
| checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| unsigned Size = Args.size(); |
| if (Size != 1) |
| return false; |
| |
| Arg = Args[0]; |
| |
| return true; |
| } |
| |
| static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| Expr *Arg = 0; |
| if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| return; |
| |
| D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| } |
| |
| static void handlePtGuardedByAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| Expr *Arg = 0; |
| if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| return; |
| |
| if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| return; |
| |
| D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| S.Context, Arg)); |
| } |
| |
| static bool checkLockableAttrCommon(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| assert(!Attr.isInvalid()); |
| |
| if (!checkAttributeNumArgs(S, Attr, 0)) |
| return false; |
| |
| // FIXME: Lockable structs for C code. |
| if (!isa<CXXRecordDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| << Attr.getName() << ThreadExpectedClassOrStruct; |
| return false; |
| } |
| |
| return true; |
| } |
| |
| static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| if (!checkLockableAttrCommon(S, D, Attr)) |
| return; |
| |
| D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); |
| } |
| |
| static void handleScopedLockableAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| if (!checkLockableAttrCommon(S, D, Attr)) |
| return; |
| |
| D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context)); |
| } |
| |
| static void handleNoThreadSafetyAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| assert(!Attr.isInvalid()); |
| |
| if (!checkAttributeNumArgs(S, Attr, 0)) |
| return; |
| |
| if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| << Attr.getName() << ThreadExpectedFunctionOrMethod; |
| return; |
| } |
| |
| D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(), |
| S.Context)); |
| } |
| |
| static void handleNoAddressSafetyAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| assert(!Attr.isInvalid()); |
| |
| if (!checkAttributeNumArgs(S, Attr, 0)) |
| return; |
| |
| if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| << Attr.getName() << ExpectedFunctionOrMethod; |
| return; |
| } |
| |
| D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(), |
| S.Context)); |
| } |
| |
| static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| const AttributeList &Attr, |
| SmallVector<Expr*, 1> &Args) { |
| assert(!Attr.isInvalid()); |
| |
| if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| return false; |
| |
| // D must be either a member field or global (potentially shared) variable. |
| ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| if (!VD || !mayBeSharedVariable(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
| return false; |
| } |
| |
| // Check that this attribute only applies to lockable types. |
| QualType QT = VD->getType(); |
| if (!QT->isDependentType()) { |
| const RecordType *RT = getRecordType(QT); |
| if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
| S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
| << Attr.getName(); |
| return false; |
| } |
| } |
| |
| // Check that all arguments are lockable objects. |
| checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| if (Args.size() == 0) |
| return false; |
| |
| return true; |
| } |
| |
| static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| SmallVector<Expr*, 1> Args; |
| if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| return; |
| |
| Expr **StartArg = &Args[0]; |
| D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context, |
| StartArg, Args.size())); |
| } |
| |
| static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| SmallVector<Expr*, 1> Args; |
| if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| return; |
| |
| Expr **StartArg = &Args[0]; |
| D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| StartArg, Args.size())); |
| } |
| |
| static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| const AttributeList &Attr, |
| SmallVector<Expr*, 1> &Args) { |
| assert(!Attr.isInvalid()); |
| |
| // zero or more arguments ok |
| |
| // check that the attribute is applied to a function |
| if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| << Attr.getName() << ThreadExpectedFunctionOrMethod; |
| return false; |
| } |
| |
| // check that all arguments are lockable objects |
| checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
| |
| return true; |
| } |
| |
| static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| SmallVector<Expr*, 1> Args; |
| if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| return; |
| |
| unsigned Size = Args.size(); |
| Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(), |
| S.Context, |
| StartArg, Size)); |
| } |
| |
| static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| SmallVector<Expr*, 1> Args; |
| if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| return; |
| |
| unsigned Size = Args.size(); |
| Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(), |
| S.Context, |
| StartArg, Size)); |
| } |
| |
| static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| const AttributeList &Attr, |
| SmallVector<Expr*, 2> &Args) { |
| assert(!Attr.isInvalid()); |
| |
| if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| return false; |
| |
| if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| << Attr.getName() << ThreadExpectedFunctionOrMethod; |
| return false; |
| } |
| |
| if (!isIntOrBool(Attr.getArg(0))) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool) |
| << Attr.getName(); |
| return false; |
| } |
| |
| // check that all arguments are lockable objects |
| checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
| |
| return true; |
| } |
| |
| static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| SmallVector<Expr*, 2> Args; |
| if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| return; |
| |
| unsigned Size = Args.size(); |
| Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(), |
| S.Context, |
| Attr.getArg(0), |
| StartArg, Size)); |
| } |
| |
| static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| SmallVector<Expr*, 2> Args; |
| if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| return; |
| |
| unsigned Size = Args.size(); |
| Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(), |
| S.Context, |
| Attr.getArg(0), |
| StartArg, Size)); |
| } |
| |
| static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| const AttributeList &Attr, |
| SmallVector<Expr*, 1> &Args) { |
| assert(!Attr.isInvalid()); |
| |
| if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| return false; |
| |
| if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| << Attr.getName() << ThreadExpectedFunctionOrMethod; |
| return false; |
| } |
| |
| // check that all arguments are lockable objects |
| checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| if (Args.size() == 0) |
| return false; |
| |
| return true; |
| } |
| |
| static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| SmallVector<Expr*, 1> Args; |
| if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| return; |
| |
| Expr **StartArg = &Args[0]; |
| D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(), |
| S.Context, |
| StartArg, |
| Args.size())); |
| } |
| |
| static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| SmallVector<Expr*, 1> Args; |
| if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| return; |
| |
| Expr **StartArg = &Args[0]; |
| D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(), |
| S.Context, |
| StartArg, |
| Args.size())); |
| } |
| |
| static void handleUnlockFunAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| assert(!Attr.isInvalid()); |
| |
| // zero or more arguments ok |
| |
| if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| << Attr.getName() << ThreadExpectedFunctionOrMethod; |
| return; |
| } |
| |
| // check that all arguments are lockable objects |
| SmallVector<Expr*, 1> Args; |
| checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
| unsigned Size = Args.size(); |
| Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| |
| D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context, |
| StartArg, Size)); |
| } |
| |
| static void handleLockReturnedAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| assert(!Attr.isInvalid()); |
| |
| if (!checkAttributeNumArgs(S, Attr, 1)) |
| return; |
| Expr *Arg = Attr.getArg(0); |
| |
| if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| << Attr.getName() << ThreadExpectedFunctionOrMethod; |
| return; |
| } |
| |
| if (Arg->isTypeDependent()) |
| return; |
| |
| // check that the argument is lockable object |
| SmallVector<Expr*, 1> Args; |
| checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| unsigned Size = Args.size(); |
| if (Size == 0) |
| return; |
| |
| D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context, |
| Args[0])); |
| } |
| |
| static void handleLocksExcludedAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| assert(!Attr.isInvalid()); |
| |
| if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| return; |
| |
| if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| << Attr.getName() << ThreadExpectedFunctionOrMethod; |
| return; |
| } |
| |
| // check that all arguments are lockable objects |
| SmallVector<Expr*, 1> Args; |
| checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| unsigned Size = Args.size(); |
| if (Size == 0) |
| return; |
| Expr **StartArg = &Args[0]; |
| |
| D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context, |
| StartArg, Size)); |
| } |
| |
| |
| static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| const AttributeList &Attr) { |
| TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D); |
| if (tDecl == 0) { |
| S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
| return; |
| } |
| |
| QualType curType = tDecl->getUnderlyingType(); |
| |
| Expr *sizeExpr; |
| |
| // Special case where the argument is a template id. |
| if (Attr.getParameterName()) { |
| CXXScopeSpec SS; |
| SourceLocation TemplateKWLoc; |
| UnqualifiedId id; |
| id.setIdentifier(Attr.getParameterName(), Attr.getLoc()); |
| |
| ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id, |
| false, false); |
| if (Size.isInvalid()) |
| return; |
| |
| sizeExpr = Size.get(); |
| } else { |
| // check the attribute arguments. |
| if (!checkAttributeNumArgs(S, Attr, 1)) |
| return; |
| |
| sizeExpr = Attr.getArg(0); |
| } |
| |
| // Instantiate/Install the vector type, and let Sema build the type for us. |
| // This will run the reguired checks. |
| QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc()); |
| if (!T.isNull()) { |
| // FIXME: preserve the old source info. |
| tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T)); |
| |
| // Remember this typedef decl, we will need it later for diagnostics. |
| S.ExtVectorDecls.push_back(tDecl); |
| } |
| } |
| |
| static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| // check the attribute arguments. |
| if (!checkAttributeNumArgs(S, Attr, 0)) |
| return; |
| |
| if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
| TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
| else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| // If the alignment is less than or equal to 8 bits, the packed attribute |
| // has no effect. |
| if (!FD->getType()->isIncompleteType() && |
| S.Context.getTypeAlign(FD->getType()) <= 8) |
| S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
| << Attr.getName() << FD->getType(); |
| else |
| FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
| } else |
| S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| } |
| |
| static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
| TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context)); |
| else |
| S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| } |
| |
| static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
| // check the attribute arguments. |
| if (!checkAttributeNumArgs(S, Attr, 0)) |
| return; |
| |
| // The IBAction attributes only apply to instance methods. |
| if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| if (MD->isInstanceMethod()) { |
| D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context)); |
| return; |
| } |
| |
| S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
| } |
| |
| static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| // The IBOutlet/IBOutletCollection attributes only apply to instance |
| // variables or properties of Objective-C classes. The outlet must also |
| // have an object reference type. |
| if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
| S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
| << Attr.getName() << VD->getType() << 0; |
| return false; |
| } |
| } |
| else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
| S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
| << Attr.getName() << PD->getType() << 1; |
| return false; |
| } |
| } |
| else { |
| S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| return false; |
| } |
| |
| return true; |
| } |
| |
| static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
| // check the attribute arguments. |
| if (!checkAttributeNumArgs(S, Attr, 0)) |
| return; |
| |
| if (!checkIBOutletCommon(S, D, Attr)) |
| return; |
| |
| D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context)); |
| } |
| |
| static void handleIBOutletCollection(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| |
| // The iboutletcollection attribute can have zero or one arguments. |
| if (Attr.getParameterName() && Attr.getNumArgs() > 0) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| return; |
| } |
| |
| if (!checkIBOutletCommon(S, D, Attr)) |
| return; |
| |
| IdentifierInfo *II = Attr.getParameterName(); |
| if (!II) |
| II = &S.Context.Idents.get("NSObject"); |
| |
| ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), |
| S.getScopeForContext(D->getDeclContext()->getParent())); |
| if (!TypeRep) { |
| S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| return; |
| } |
| QualType QT = TypeRep.get(); |
| // Diagnose use of non-object type in iboutletcollection attribute. |
| // FIXME. Gnu attribute extension ignores use of builtin types in |
| // attributes. So, __attribute__((iboutletcollection(char))) will be |
| // treated as __attribute__((iboutletcollection())). |
| if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
| S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| return; |
| } |
| D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context, |
| QT, Attr.getParameterLoc())); |
| } |
| |
| static void possibleTransparentUnionPointerType(QualType &T) { |
| if (const RecordType *UT = T->getAsUnionType()) |
| if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| RecordDecl *UD = UT->getDecl(); |
| for (RecordDecl::field_iterator it = UD->field_begin(), |
| itend = UD->field_end(); it != itend; ++it) { |
| QualType QT = it->getType(); |
| if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| T = QT; |
| return; |
| } |
| } |
| } |
| } |
| |
| static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| if (!isFunctionOrMethod(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| << "alloc_size" << ExpectedFunctionOrMethod; |
| return; |
| } |
| |
| if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| return; |
| |
| // In C++ the implicit 'this' function parameter also counts, and they are |
| // counted from one. |
| bool HasImplicitThisParam = isInstanceMethod(D); |
| unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| |
| SmallVector<unsigned, 8> SizeArgs; |
| |
| for (AttributeList::arg_iterator I = Attr.arg_begin(), |
| E = Attr.arg_end(); I!=E; ++I) { |
| // The argument must be an integer constant expression. |
| Expr *Ex = *I; |
| llvm::APSInt ArgNum; |
| if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| << "alloc_size" << Ex->getSourceRange(); |
| return; |
| } |
| |
| uint64_t x = ArgNum.getZExtValue(); |
| |
| if (x < 1 || x > NumArgs) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| << "alloc_size" << I.getArgNum() << Ex->getSourceRange(); |
| return; |
| } |
| |
| --x; |
| if (HasImplicitThisParam) { |
| if (x == 0) { |
| S.Diag(Attr.getLoc(), |
| diag::err_attribute_invalid_implicit_this_argument) |
| << "alloc_size" << Ex->getSourceRange(); |
| return; |
| } |
| --x; |
| } |
| |
| // check if the function argument is of an integer type |
| QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
| if (!T->isIntegerType()) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| << "alloc_size" << Ex->getSourceRange(); |
| return; |
| } |
| |
| SizeArgs.push_back(x); |
| } |
| |
| // check if the function returns a pointer |
| if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
| << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
| } |
| |
| D->addAttr(::new (S.Context) AllocSizeAttr(Attr.getRange(), S.Context, |
| SizeArgs.data(), SizeArgs.size())); |
| } |
| |
| static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| // ignore it as well |
| if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| << Attr.getName() << ExpectedFunction; |
| return; |
| } |
| |
| // In C++ the implicit 'this' function parameter also counts, and they are |
| // counted from one. |
| bool HasImplicitThisParam = isInstanceMethod(D); |
| unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| |
| // The nonnull attribute only applies to pointers. |
| SmallVector<unsigned, 10> NonNullArgs; |
| |
| for (AttributeList::arg_iterator I=Attr.arg_begin(), |
| E=Attr.arg_end(); I!=E; ++I) { |
| |
| |
| // The argument must be an integer constant expression. |
| Expr *Ex = *I; |
| llvm::APSInt ArgNum(32); |
| if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| << "nonnull" << Ex->getSourceRange(); |
| return; |
| } |
| |
| unsigned x = (unsigned) ArgNum.getZExtValue(); |
| |
| if (x < 1 || x > NumArgs) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| << "nonnull" << I.getArgNum() << Ex->getSourceRange(); |
| return; |
| } |
| |
| --x; |
| if (HasImplicitThisParam) { |
| if (x == 0) { |
| S.Diag(Attr.getLoc(), |
| diag::err_attribute_invalid_implicit_this_argument) |
| << "nonnull" << Ex->getSourceRange(); |
| return; |
| } |
| --x; |
| } |
| |
| // Is the function argument a pointer type? |
| QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
| possibleTransparentUnionPointerType(T); |
| |
| if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| // FIXME: Should also highlight argument in decl. |
| S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
| << "nonnull" << Ex->getSourceRange(); |
| continue; |
| } |
| |
| NonNullArgs.push_back(x); |
| } |
| |
| // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| // arguments have a nonnull attribute. |
| if (NonNullArgs.empty()) { |
| for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) { |
| QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType(); |
| possibleTransparentUnionPointerType(T); |
| if (T->isAnyPointerType() || T->isBlockPointerType()) |
| NonNullArgs.push_back(I); |
| } |
| |
| // No pointer arguments? |
| if (NonNullArgs.empty()) { |
| // Warn the trivial case only if attribute is not coming from a |
| // macro instantiation. |
| if (Attr.getLoc().isFileID()) |
| S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
| return; |
| } |
| } |
| |
| unsigned* start = &NonNullArgs[0]; |
| unsigned size = NonNullArgs.size(); |
| llvm::array_pod_sort(start, start + size); |
| D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start, |
| size)); |
| } |
| |
| static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
| // This attribute must be applied to a function declaration. |
| // The first argument to the attribute must be a string, |
| // the name of the resource, for example "malloc". |
| // The following arguments must be argument indexes, the arguments must be |
| // of integer type for Returns, otherwise of pointer type. |
| // The difference between Holds and Takes is that a pointer may still be used |
| // after being held. free() should be __attribute((ownership_takes)), whereas |
| // a list append function may well be __attribute((ownership_holds)). |
| |
| if (!AL.getParameterName()) { |
| S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string) |
| << AL.getName()->getName() << 1; |
| return; |
| } |
| // Figure out our Kind, and check arguments while we're at it. |
| OwnershipAttr::OwnershipKind K; |
| switch (AL.getKind()) { |
| case AttributeList::AT_ownership_takes: |
| K = OwnershipAttr::Takes; |
| if (AL.getNumArgs() < 1) { |
| S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| return; |
| } |
| break; |
| case AttributeList::AT_ownership_holds: |
| K = OwnershipAttr::Holds; |
| if (AL.getNumArgs() < 1) { |
| S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| return; |
| } |
| break; |
| case AttributeList::AT_ownership_returns: |
| K = OwnershipAttr::Returns; |
| if (AL.getNumArgs() > 1) { |
| S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| << AL.getNumArgs() + 1; |
| return; |
| } |
| break; |
| default: |
| // This should never happen given how we are called. |
| llvm_unreachable("Unknown ownership attribute"); |
| } |
| |
| if (!isFunction(D) || !hasFunctionProto(D)) { |
| S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| << AL.getName() << ExpectedFunction; |
| return; |
| } |
| |
| // In C++ the implicit 'this' function parameter also counts, and they are |
| // counted from one. |
| bool HasImplicitThisParam = isInstanceMethod(D); |
| unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| |
| StringRef Module = AL.getParameterName()->getName(); |
| |
| // Normalize the argument, __foo__ becomes foo. |
| if (Module.startswith("__") && Module.endswith("__")) |
| Module = Module.substr(2, Module.size() - 4); |
| |
| SmallVector<unsigned, 10> OwnershipArgs; |
| |
| for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E; |
| ++I) { |
| |
| Expr *IdxExpr = *I; |
| llvm::APSInt ArgNum(32); |
| if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int) |
| << AL.getName()->getName() << IdxExpr->getSourceRange(); |
| continue; |
| } |
| |
| unsigned x = (unsigned) ArgNum.getZExtValue(); |
| |
| if (x > NumArgs || x < 1) { |
| S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| << AL.getName()->getName() << x << IdxExpr->getSourceRange(); |
| continue; |
| } |
| --x; |
| if (HasImplicitThisParam) { |
| if (x == 0) { |
| S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| << "ownership" << IdxExpr->getSourceRange(); |
| return; |
| } |
| --x; |
| } |
| |
| switch (K) { |
| case OwnershipAttr::Takes: |
| case OwnershipAttr::Holds: { |
| // Is the function argument a pointer type? |
| QualType T = getFunctionOrMethodArgType(D, x); |
| if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| // FIXME: Should also highlight argument in decl. |
| S.Diag(AL.getLoc(), diag::err_ownership_type) |
| << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") |
| << "pointer" |
| << IdxExpr->getSourceRange(); |
| continue; |
| } |
| break; |
| } |
| case OwnershipAttr::Returns: { |
| if (AL.getNumArgs() > 1) { |
| // Is the function argument an integer type? |
| Expr *IdxExpr = AL.getArg(0); |
| llvm::APSInt ArgNum(32); |
| if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| S.Diag(AL.getLoc(), diag::err_ownership_type) |
| << "ownership_returns" << "integer" |
| << IdxExpr->getSourceRange(); |
| return; |
| } |
| } |
| break; |
| } |
| } // switch |
| |
| // Check we don't have a conflict with another ownership attribute. |
| for (specific_attr_iterator<OwnershipAttr> |
| i = D->specific_attr_begin<OwnershipAttr>(), |
| e = D->specific_attr_end<OwnershipAttr>(); |
| i != e; ++i) { |
| if ((*i)->getOwnKind() != K) { |
| for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); |
| I!=E; ++I) { |
| if (x == *I) { |
| S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| << AL.getName()->getName() << "ownership_*"; |
| } |
| } |
| } |
| } |
| OwnershipArgs.push_back(x); |
| } |
| |
| unsigned* start = OwnershipArgs.data(); |
| unsigned size = OwnershipArgs.size(); |
| llvm::array_pod_sort(start, start + size); |
| |
| if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { |
| S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| return; |
| } |
| |
| D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module, |
| start, size)); |
| } |
| |
| /// Whether this declaration has internal linkage for the purposes of |
| /// things that want to complain about things not have internal linkage. |
| static bool hasEffectivelyInternalLinkage(NamedDecl *D) { |
| switch (D->getLinkage()) { |
| case NoLinkage: |
| case InternalLinkage: |
| return true; |
| |
| // Template instantiations that go from external to unique-external |
| // shouldn't get diagnosed. |
| case UniqueExternalLinkage: |
| return true; |
| |
| case ExternalLinkage: |
| return false; |
| } |
| llvm_unreachable("unknown linkage kind!"); |
| } |
| |
| static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| // Check the attribute arguments. |
| if (Attr.getNumArgs() > 1) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| return; |
| } |
| |
| if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| << Attr.getName() << ExpectedVariableOrFunction; |
| return; |
| } |
| |
| NamedDecl *nd = cast<NamedDecl>(D); |
| |
| // gcc rejects |
| // class c { |
| // static int a __attribute__((weakref ("v2"))); |
| // static int b() __attribute__((weakref ("f3"))); |
| // }; |
| // and ignores the attributes of |
| // void f(void) { |
| // static int a __attribute__((weakref ("v2"))); |
| // } |
| // we reject them |
| const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
| if (!Ctx->isFileContext()) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
| nd->getNameAsString(); |
| return; |
| } |
| |
| // The GCC manual says |
| // |
| // At present, a declaration to which `weakref' is attached can only |
| // be `static'. |
| // |
| // It also says |
| // |
| // Without a TARGET, |
| // given as an argument to `weakref' or to `alias', `weakref' is |
| // equivalent to `weak'. |
| // |
| // gcc 4.4.1 will accept |
| // int a7 __attribute__((weakref)); |
| // as |
| // int a7 __attribute__((weak)); |
| // This looks like a bug in gcc. We reject that for now. We should revisit |
| // it if this behaviour is actually used. |
| |
| if (!hasEffectivelyInternalLinkage(nd)) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static); |
| return; |
| } |
| |
| // GCC rejects |
| // static ((alias ("y"), weakref)). |
| // Should we? How to check that weakref is before or after alias? |
| |
| if (Attr.getNumArgs() == 1) { |
| Expr *Arg = Attr.getArg(0); |
| Arg = Arg->IgnoreParenCasts(); |
| StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| |
| if (!Str || !Str->isAscii()) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| << "weakref" << 1; |
| return; |
| } |
| // GCC will accept anything as the argument of weakref. Should we |
| // check for an existing decl? |
| D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
| Str->getString())); |
| } |
| |
| D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context)); |
| } |
| |
| static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| // check the attribute arguments. |
| if (Attr.getNumArgs() != 1) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| return; |
| } |
| |
| Expr *Arg = Attr.getArg(0); |
| Arg = Arg->IgnoreParenCasts(); |
| StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| |
| if (!Str || !Str->isAscii()) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| << "alias" << 1; |
| return; |
| } |
| |
| if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
| S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| return; |
| } |
| |
| // FIXME: check if target symbol exists in current file |
| |
| D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
| Str->getString())); |
| } |
| |
| static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| // Check the attribute arguments. |
| if (!checkAttributeNumArgs(S, Attr, 0)) |
| return; |
| |
| if (!isa<FunctionDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| << Attr.getName() << ExpectedFunction; |
| return; |
| } |
| |
| if (D->hasAttr<HotAttr>()) { |
| S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| << Attr.getName() << "hot"; |
| return; |
| } |
| |
| D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context)); |
| } |
| |
| static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| // Check the attribute arguments. |
| if (!checkAttributeNumArgs(S, Attr, 0)) |
| return; |
| |
| if (!isa<FunctionDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| << Attr.getName() << ExpectedFunction; |
| return; |
| } |
| |
| if (D->hasAttr<ColdAttr>()) { |
| S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| << Attr.getName() << "cold"; |
| return; |
| } |
| |
| D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context)); |
| } |
| |
| static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| // Check the attribute arguments. |
| if (!checkAttributeNumArgs(S, Attr, 0)) |
| return; |
| |
| if (!isa<FunctionDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| << Attr.getName() << ExpectedFunction; |
| return; |
| } |
| |
| D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context)); |
| } |
| |
| static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| // Check the attribute arguments. |
| if (Attr.hasParameterOrArguments()) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| return; |
| } |
| |
| if (!isa<FunctionDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| << Attr.getName() << ExpectedFunction; |
| return; |
| } |
| |
| D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context)); |
| } |
| |
| static void handleTLSModelAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| // Check the attribute arguments. |
| if (Attr.getNumArgs() != 1) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| return; |
| } |
| |
| Expr *Arg = Attr.getArg(0); |
| Arg = Arg->IgnoreParenCasts(); |
| StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| |
| // Check that it is a string. |
| if (!Str) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model"; |
| return; |
| } |
| |
| if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| << Attr.getName() << ExpectedTLSVar; |
| return; |
| } |
| |
| // Check that the value. |
| StringRef Model = Str->getString(); |
| if (Model != "global-dynamic" && Model != "local-dynamic" |
| && Model != "initial-exec" && Model != "local-exec") { |
| S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg); |
| return; |
| } |
| |
| D->addAttr(::new (S.Context) TLSModelAttr(Attr.getRange(), S.Context, |
| Model)); |
| } |
| |
| static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| // Check the attribute arguments. |
| if (Attr.hasParameterOrArguments()) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| return; |
| } |
| |
| if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| QualType RetTy = FD->getResultType(); |
| if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
| D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context)); |
| return; |
| } |
| } |
| |
| S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
| } |
| |
| static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| // check the attribute arguments. |
| if (!checkAttributeNumArgs(S, Attr, 0)) |
| return; |
| |
| D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context)); |
| } |
| |
| static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| assert(!Attr.isInvalid()); |
| if (isa<VarDecl>(D)) |
| D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context)); |
| else |
| S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| << Attr.getName() << ExpectedVariable; |
| } |
| |
| static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| assert(!Attr.isInvalid()); |
| if (isa<VarDecl>(D)) |
| D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context)); |
| else |
| S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| << Attr.getName() << ExpectedVariable; |
| } |
| |
| static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
| if (hasDeclarator(D)) return; |
| |
| if (S.CheckNoReturnAttr(attr)) return; |
| |
| if (!isa<ObjCMethodDecl>(D)) { |
| S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| << attr.getName() << ExpectedFunctionOrMethod; |
| return; |
| } |
| |
| D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context)); |
| } |
| |
| bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
| if (attr.hasParameterOrArguments()) { |
| Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| attr.setInvalid(); |
| return true; |
| } |
| |
| return false; |
| } |
| |
| static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| |
| // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| // because 'analyzer_noreturn' does not impact the type. |
| |
| if(!checkAttributeNumArgs(S, Attr, 0)) |
| return; |
| |
| if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| && !VD->getType()->isFunctionPointerType())) { |
| S.Diag(Attr.getLoc(), |
| Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type |
| : diag::warn_attribute_wrong_decl_type) |
| << Attr.getName() << ExpectedFunctionMethodOrBlock; |
| return; |
| } |
| } |
| |
| D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context)); |
| } |
| |
| // PS3 PPU-specific. |
| static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| /* |
| Returning a Vector Class in Registers |
| |
| According to the PPU ABI specifications, a class with a single member of |
| vector type is returned in memory when used as the return value of a function. |
| This results in inefficient code when implementing vector classes. To return |
| the value in a single vector register, add the vecreturn attribute to the |
| class definition. This attribute is also applicable to struct types. |
| |
| Example: |
| |
| struct Vector |
| { |
| __vector float xyzw; |
| } __attribute__((vecreturn)); |
| |
| Vector Add(Vector lhs, Vector rhs) |
| { |
| Vector result; |
| result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| return result; // This will be returned in a register |
| } |
| */ |
| if (!isa<RecordDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| << Attr.getName() << ExpectedClass; |
| return; |
| } |
| |
| if (D->getAttr<VecReturnAttr>()) { |
| S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| return; |
| } |
| |
| RecordDecl *record = cast<RecordDecl>(D); |
| int count = 0; |
| |
| if (!isa<CXXRecordDecl>(record)) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| return; |
| } |
| |
| if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| return; |
| } |
| |
| for (RecordDecl::field_iterator iter = record->field_begin(); |
| iter != record->field_end(); iter++) { |
| if ((count == 1) || !iter->getType()->isVectorType()) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| return; |
| } |
| count++; |
| } |
| |
| D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context)); |
| } |
| |
| static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| << Attr.getName() << ExpectedFunctionMethodOrParameter; |
| return; |
| } |
| // FIXME: Actually store the attribute on the declaration |
| } |
| |
| static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| // check the attribute arguments. |
| if (Attr.hasParameterOrArguments()) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| return; |
| } |
| |
| if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
| !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| << Attr.getName() << ExpectedVariableFunctionOrLabel; |
| return; |
| } |
| |
| D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context)); |
| } |
| |
| static void handleReturnsTwiceAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| // check the attribute arguments. |
| if (Attr.hasParameterOrArguments()) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| return; |
| } |
| |
| if (!isa<FunctionDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| << Attr.getName() << ExpectedFunction; |
| return; |
| } |
| |
| D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context)); |
| } |
| |
| static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| // check the attribute arguments. |
| if (Attr.hasParameterOrArguments()) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| return; |
| } |
| |
| if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
| S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| return; |
| } |
| } else if (!isFunctionOrMethod(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| << Attr.getName() << ExpectedVariableOrFunction; |
| return; |
| } |
| |
| D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context)); |
| } |
| |
| static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| // check the attribute arguments. |
| if (Attr.getNumArgs() > 1) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
| return; |
| } |
| |
| int priority = 65535; // FIXME: Do not hardcode such constants. |
| if (Attr.getNumArgs() > 0) { |
| Expr *E = Attr.getArg(0); |
| llvm::APSInt Idx(32); |
| if (E->isTypeDependent() || E->isValueDependent() || |
| !E->isIntegerConstantExpr(Idx, S.Context)) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| << "constructor" << 1 << E->getSourceRange(); |
| return; |
| } |
| priority = Idx.getZExtValue(); |
| } |
| |
| if (!isa<FunctionDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| << Attr.getName() << ExpectedFunction; |
| return; |
| } |
| |
| D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context, |
| priority)); |
| } |
| |
| static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| // check the attribute arguments. |
| if (Attr.getNumArgs() > 1) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
| return; |
| } |
| |
| int priority = 65535; // FIXME: Do not hardcode such constants. |
| if (Attr.getNumArgs() > 0) { |
| Expr *E = Attr.getArg(0); |
| llvm::APSInt Idx(32); |
| if (E->isTypeDependent() || E->isValueDependent() || |
| !E->isIntegerConstantExpr(Idx, S.Context)) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| << "destructor" << 1 << E->getSourceRange(); |
| return; |
| } |
| priority = Idx.getZExtValue(); |
| } |
| |
| if (!isa<FunctionDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| << Attr.getName() << ExpectedFunction; |
| return; |
| } |
| |
| D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context, |
| priority)); |
| } |
| |
| template <typename AttrTy> |
| static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr, |
| const char *Name) { |
| unsigned NumArgs = Attr.getNumArgs(); |
| if (NumArgs > 1) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
| return; |
| } |
| |
| // Handle the case where the attribute has a text message. |
| StringRef Str; |
| if (NumArgs == 1) { |
| StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
| if (!SE) { |
| S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string) |
| << Name; |
| return; |
| } |
| Str = SE->getString(); |
| } |
| |
| D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str)); |
| } |
| |
| static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| unsigned NumArgs = Attr.getNumArgs(); |
| if (NumArgs > 0) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| return; |
| } |
| |
| D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr( |
| Attr.getRange(), S.Context)); |
| } |
| |
| static void handleObjCRootClassAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| if (!isa<ObjCInterfaceDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| return; |
| } |
| |
| unsigned NumArgs = Attr.getNumArgs(); |
| if (NumArgs > 0) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| return; |
| } |
| |
| D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context)); |
| } |
| |
| static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| if (!isa<ObjCInterfaceDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis); |
| return; |
| } |
| |
| unsigned NumArgs = Attr.getNumArgs(); |
| if (NumArgs > 0) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| return; |
| } |
| |
| D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr( |
| Attr.getRange(), S.Context)); |
| } |
| |
| static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| IdentifierInfo *Platform, |
| VersionTuple Introduced, |
| VersionTuple Deprecated, |
| VersionTuple Obsoleted) { |
| StringRef PlatformName |
| = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| if (PlatformName.empty()) |
| PlatformName = Platform->getName(); |
| |
| // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| // of these steps are needed). |
| if (!Introduced.empty() && !Deprecated.empty() && |
| !(Introduced <= Deprecated)) { |
| S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| << 1 << PlatformName << Deprecated.getAsString() |
| << 0 << Introduced.getAsString(); |
| return true; |
| } |
| |
| if (!Introduced.empty() && !Obsoleted.empty() && |
| !(Introduced <= Obsoleted)) { |
| S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| << 2 << PlatformName << Obsoleted.getAsString() |
| << 0 << Introduced.getAsString(); |
| return true; |
| } |
| |
| if (!Deprecated.empty() && !Obsoleted.empty() && |
| !(Deprecated <= Obsoleted)) { |
| S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| << 2 << PlatformName << Obsoleted.getAsString() |
| << 1 << Deprecated.getAsString(); |
| return true; |
| } |
| |
| return false; |
| } |
| |
| AvailabilityAttr *Sema::mergeAvailabilityAttr(Decl *D, SourceRange Range, |
| IdentifierInfo *Platform, |
| VersionTuple Introduced, |
| VersionTuple Deprecated, |
| VersionTuple Obsoleted, |
| bool IsUnavailable, |
| StringRef Message) { |
| VersionTuple MergedIntroduced = Introduced; |
| VersionTuple MergedDeprecated = Deprecated; |
| VersionTuple MergedObsoleted = Obsoleted; |
| bool FoundAny = false; |
| |
| if (D->hasAttrs()) { |
| AttrVec &Attrs = D->getAttrs(); |
| for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| if (!OldAA) { |
| ++i; |
| continue; |
| } |
| |
| IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| if (OldPlatform != Platform) { |
| ++i; |
| continue; |
| } |
| |
| FoundAny = true; |
| VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| bool OldIsUnavailable = OldAA->getUnavailable(); |
| StringRef OldMessage = OldAA->getMessage(); |
| |
| if ((!OldIntroduced.empty() && !Introduced.empty() && |
| OldIntroduced != Introduced) || |
| (!OldDeprecated.empty() && !Deprecated.empty() && |
| OldDeprecated != Deprecated) || |
| (!OldObsoleted.empty() && !Obsoleted.empty() && |
| OldObsoleted != Obsoleted) || |
| (OldIsUnavailable != IsUnavailable) || |
| (OldMessage != Message)) { |
| Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| Diag(Range.getBegin(), diag::note_previous_attribute); |
| Attrs.erase(Attrs.begin() + i); |
| --e; |
| continue; |
| } |
| |
| VersionTuple MergedIntroduced2 = MergedIntroduced; |
| VersionTuple MergedDeprecated2 = MergedDeprecated; |
| VersionTuple MergedObsoleted2 = MergedObsoleted; |
| |
| if (MergedIntroduced2.empty()) |
| MergedIntroduced2 = OldIntroduced; |
| if (MergedDeprecated2.empty()) |
| MergedDeprecated2 = OldDeprecated; |
| if (MergedObsoleted2.empty()) |
| MergedObsoleted2 = OldObsoleted; |
| |
| if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| MergedIntroduced2, MergedDeprecated2, |
| MergedObsoleted2)) { |
| Attrs.erase(Attrs.begin() + i); |
| --e; |
| continue; |
| } |
| |
| MergedIntroduced = MergedIntroduced2; |
| MergedDeprecated = MergedDeprecated2; |
| MergedObsoleted = MergedObsoleted2; |
| ++i; |
| } |
| } |
| |
| if (FoundAny && |
| MergedIntroduced == Introduced && |
| MergedDeprecated == Deprecated && |
| MergedObsoleted == Obsoleted) |
| return NULL; |
| |
| if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
| MergedDeprecated, MergedObsoleted)) { |
| return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| Introduced, Deprecated, |
| Obsoleted, IsUnavailable, Message); |
| } |
| return NULL; |
| } |
| |
| static void handleAvailabilityAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| IdentifierInfo *Platform = Attr.getParameterName(); |
| SourceLocation PlatformLoc = Attr.getParameterLoc(); |
| |
| if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty()) |
| S.Diag(PlatformLoc, diag::warn_availability_unknown_platform) |
| << Platform; |
| |
| AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
| bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
| StringRef Str; |
| const StringLiteral *SE = |
| dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr()); |
| if (SE) |
| Str = SE->getString(); |
| |
| AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(D, Attr.getRange(), |
| Platform, |
| Introduced.Version, |
| Deprecated.Version, |
| Obsoleted.Version, |
| IsUnavailable, Str); |
| if (NewAttr) |
| D->addAttr(NewAttr); |
| } |
| |
| VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
| VisibilityAttr::VisibilityType Vis) { |
| if (isa<TypedefNameDecl>(D)) { |
| Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility"; |
| return NULL; |
| } |
| VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>(); |
| if (ExistingAttr) { |
| VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility(); |
| if (ExistingVis == Vis) |
| return NULL; |
| Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility); |
| Diag(Range.getBegin(), diag::note_previous_attribute); |
| D->dropAttr<VisibilityAttr>(); |
| } |
| return ::new (Context) VisibilityAttr(Range, Context, Vis); |
| } |
| |
| static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| // check the attribute arguments. |
| if(!checkAttributeNumArgs(S, Attr, 1)) |
| return; |
| |
| Expr *Arg = Attr.getArg(0); |
| Arg = Arg->IgnoreParenCasts(); |
| StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| |
| if (!Str || !Str->isAscii()) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| << "visibility" << 1; |
| return; |
| } |
| |
| StringRef TypeStr = Str->getString(); |
| VisibilityAttr::VisibilityType type; |
| |
| if (TypeStr == "default") |
| type = VisibilityAttr::Default; |
| else if (TypeStr == "hidden") |
| type = VisibilityAttr::Hidden; |
| else if (TypeStr == "internal") |
| type = VisibilityAttr::Hidden; // FIXME |
| else if (TypeStr == "protected") { |
| // Complain about attempts to use protected visibility on targets |
| // (like Darwin) that don't support it. |
| if (!S.Context.getTargetInfo().hasProtectedVisibility()) { |
| S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| type = VisibilityAttr::Default; |
| } else { |
| type = VisibilityAttr::Protected; |
| } |
| } else { |
| S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
| return; |
| } |
| |
| VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type); |
| if (NewAttr) |
| D->addAttr(NewAttr); |
| } |
| |
| static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| const AttributeList &Attr) { |
| ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| if (!method) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| << ExpectedMethod; |
| return; |
| } |
| |
| if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) { |
| if (!Attr.getParameterName() && Attr.getNumArgs() == 1) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| << "objc_method_family" << 1; |
| } else { |
| S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| } |
| Attr.setInvalid(); |
| return; |
| } |
| |
| StringRef param = Attr.getParameterName()->getName(); |
| ObjCMethodFamilyAttr::FamilyKind family; |
| if (param == "none") |
| family = ObjCMethodFamilyAttr::OMF_None; |
| else if (param == "alloc") |
| family = ObjCMethodFamilyAttr::OMF_alloc; |
| else if (param == "copy") |
| family = ObjCMethodFamilyAttr::OMF_copy; |
| else if (param == "init") |
| family = ObjCMethodFamilyAttr::OMF_init; |
| else if (param == "mutableCopy") |
| family = ObjCMethodFamilyAttr::OMF_mutableCopy; |
| else if (param == "new") |
| family = ObjCMethodFamilyAttr::OMF_new; |
| else { |
| // Just warn and ignore it. This is future-proof against new |
| // families being used in system headers. |
| S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family); |
| return; |
| } |
| |
| if (family == ObjCMethodFamilyAttr::OMF_init && |
| !method->getResultType()->isObjCObjectPointerType()) { |
| S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| << method->getResultType(); |
| // Ignore the attribute. |
| return; |
| } |
| |
| method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
| S.Context, family)); |
| } |
| |
| static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| const AttributeList &Attr) { |
| if (!checkAttributeNumArgs(S, Attr, 0)) |
| return; |
| |
| ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| if (OCI == 0) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| return; |
| } |
| |
| D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context)); |
| } |
| |
| static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
| if (Attr.getNumArgs() != 0) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| return; |
| } |
| if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| QualType T = TD->getUnderlyingType(); |
| if (!T->isCARCBridgableType()) { |
| S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| return; |
| } |
| } |
| else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| QualType T = PD->getType(); |
| if (!T->isCARCBridgableType()) { |
| S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| return; |
| } |
| } |
| else { |
| // It is okay to include this attribute on properties, e.g.: |
| // |
| // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| // |
| // In this case it follows tradition and suppresses an error in the above |
| // case. |
| S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
| } |
| D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context)); |
| } |
| |
| static void |
| handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| if (Attr.getNumArgs() != 0) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| return; |
| } |
| |
| if (!isa<FunctionDecl>(D)) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| return; |
| } |
| |
| D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context)); |
| } |
| |
| static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| if (!Attr.getParameterName()) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| << "blocks" << 1; |
| return; |
| } |
| |
| if (Attr.getNumArgs() != 0) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| return; |
| } |
| |
| BlocksAttr::BlockType type; |
| if (Attr.getParameterName()->isStr("byref")) |
| type = BlocksAttr::ByRef; |
| else { |
| S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| << "blocks" << Attr.getParameterName(); |
| return; |
| } |
| |
| D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type)); |
| } |
| |
| static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| // check the attribute arguments. |
| if (Attr.getNumArgs() > 2) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
| return; |
| } |
| |
| unsigned sentinel = 0; |
| if (Attr.getNumArgs() > 0) { |
| Expr *E = Attr.getArg(0); |
| llvm::APSInt Idx(32); |
| if (E->isTypeDependent() || E->isValueDependent() || |
| !E->isIntegerConstantExpr(Idx, S.Context)) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| << "sentinel" << 1 << E->getSourceRange(); |
| return; |
| } |
| |
| if (Idx.isSigned() && Idx.isNegative()) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| << E->getSourceRange(); |
| return; |
| } |
| |
| sentinel = Idx.getZExtValue(); |
| } |
| |
| unsigned nullPos = 0; |
| if (Attr.getNumArgs() > 1) { |
| Expr *E = Attr.getArg(1); |
| llvm::APSInt Idx(32); |
| if (E->isTypeDependent() || E->isValueDependent() || |
| !E->isIntegerConstantExpr(Idx, S.Context)) { |
| S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| << "sentinel" << 2 << E->getSourceRange(); |
| return; |
| } |
| nullPos = Idx.getZExtValue(); |
| |
| if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
| // FIXME: This error message could be improved, it would be nice |
| // to say what the bounds actually are. |
| S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| << E->getSourceRange(); |
| return; |
| } |
| } |
| |
| if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
| if (isa<FunctionNoProtoType>(FT |