2008-06-27 02:38:35 +08:00
|
|
|
//===--- 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-08-26 06:03:47 +08:00
|
|
|
#include "clang/Sema/SemaInternal.h"
|
2010-01-10 20:58:08 +08:00
|
|
|
#include "TargetAttributesSema.h"
|
2008-06-27 02:38:35 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2010-08-25 15:42:41 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2011-08-10 01:59:31 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2008-08-11 14:23:49 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
2011-06-16 07:02:42 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2008-06-28 06:18:37 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2010-08-21 02:27:03 +08:00
|
|
|
#include "clang/Sema/DeclSpec.h"
|
2010-08-26 10:13:20 +08:00
|
|
|
#include "clang/Sema/DelayedDiagnostic.h"
|
2009-08-11 03:03:04 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2008-06-27 02:38:35 +08:00
|
|
|
using namespace clang;
|
2010-08-26 10:13:20 +08:00
|
|
|
using namespace sema;
|
2008-06-27 02:38:35 +08:00
|
|
|
|
2011-03-02 20:29:23 +08:00
|
|
|
/// These constants match the enumerated choices of
|
|
|
|
/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
|
2011-08-10 01:59:31 +08:00
|
|
|
enum AttributeDeclKind {
|
2011-03-02 20:29:23 +08:00
|
|
|
ExpectedFunction,
|
|
|
|
ExpectedUnion,
|
|
|
|
ExpectedVariableOrFunction,
|
|
|
|
ExpectedFunctionOrMethod,
|
|
|
|
ExpectedParameter,
|
|
|
|
ExpectedParameterOrMethod,
|
|
|
|
ExpectedFunctionMethodOrBlock,
|
|
|
|
ExpectedClassOrVirtualMethod,
|
|
|
|
ExpectedFunctionMethodOrParameter,
|
|
|
|
ExpectedClass,
|
|
|
|
ExpectedVirtualMethod,
|
|
|
|
ExpectedClassMember,
|
|
|
|
ExpectedVariable,
|
|
|
|
ExpectedMethod,
|
2011-07-29 04:12:35 +08:00
|
|
|
ExpectedVariableFunctionOrLabel,
|
|
|
|
ExpectedFieldOrGlobalVar
|
2011-03-02 20:29:23 +08:00
|
|
|
};
|
|
|
|
|
2008-06-29 08:16:31 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Helper functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
static const FunctionType *getFunctionType(const Decl *D,
|
2009-08-15 04:49:40 +08:00
|
|
|
bool blocksToo = true) {
|
2008-06-27 02:38:35 +08:00
|
|
|
QualType Ty;
|
2011-07-02 07:49:12 +08:00
|
|
|
if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
|
2008-06-27 02:38:35 +08:00
|
|
|
Ty = decl->getType();
|
2011-07-02 07:49:12 +08:00
|
|
|
else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
|
2008-06-27 02:38:35 +08:00
|
|
|
Ty = decl->getType();
|
2011-07-02 07:49:12 +08:00
|
|
|
else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
|
2008-06-27 02:38:35 +08:00
|
|
|
Ty = decl->getUnderlyingType();
|
|
|
|
else
|
|
|
|
return 0;
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2008-06-27 02:38:35 +08:00
|
|
|
if (Ty->isFunctionPointerType())
|
2009-07-30 05:53:49 +08:00
|
|
|
Ty = Ty->getAs<PointerType>()->getPointeeType();
|
2009-05-19 01:39:25 +08:00
|
|
|
else if (blocksToo && Ty->isBlockPointerType())
|
2009-07-30 05:53:49 +08:00
|
|
|
Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
|
2008-10-19 10:04:16 +08:00
|
|
|
|
2009-09-22 07:43:11 +08:00
|
|
|
return Ty->getAs<FunctionType>();
|
2008-06-27 02:38:35 +08:00
|
|
|
}
|
|
|
|
|
2008-09-26 12:12:28 +08:00
|
|
|
// FIXME: We should provide an abstraction around a method or function
|
|
|
|
// to provide the following bits of information.
|
|
|
|
|
2009-12-21 07:11:08 +08:00
|
|
|
/// isFunction - Return true if the given decl has function
|
2009-08-15 04:49:40 +08:00
|
|
|
/// type (function or function-typed variable).
|
2011-07-02 07:49:12 +08:00
|
|
|
static bool isFunction(const Decl *D) {
|
|
|
|
return getFunctionType(D, false) != NULL;
|
2009-08-15 04:49:40 +08:00
|
|
|
}
|
|
|
|
|
2008-10-19 10:04:16 +08:00
|
|
|
/// isFunctionOrMethod - Return true if the given decl has function
|
|
|
|
/// type (function or function-typed variable) or an Objective-C
|
|
|
|
/// method.
|
2011-07-02 07:49:12 +08:00
|
|
|
static bool isFunctionOrMethod(const Decl *D) {
|
|
|
|
return isFunction(D)|| isa<ObjCMethodDecl>(D);
|
2008-10-19 10:04:16 +08:00
|
|
|
}
|
2008-09-26 12:12:28 +08:00
|
|
|
|
2009-05-16 07:15:03 +08:00
|
|
|
/// isFunctionOrMethodOrBlock - Return true if the given decl has function
|
|
|
|
/// type (function or function-typed variable) or an Objective-C
|
|
|
|
/// method or a block.
|
2011-07-02 07:49:12 +08:00
|
|
|
static bool isFunctionOrMethodOrBlock(const Decl *D) {
|
|
|
|
if (isFunctionOrMethod(D))
|
2009-05-16 07:15:03 +08:00
|
|
|
return true;
|
|
|
|
// check for block is more involved.
|
2011-07-02 07:49:12 +08:00
|
|
|
if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
|
2009-05-16 07:15:03 +08:00
|
|
|
QualType Ty = V->getType();
|
|
|
|
return Ty->isBlockPointerType();
|
|
|
|
}
|
2011-07-02 07:49:12 +08:00
|
|
|
return isa<BlockDecl>(D);
|
2009-05-16 07:15:03 +08:00
|
|
|
}
|
|
|
|
|
2011-01-05 20:14:39 +08:00
|
|
|
/// Return true if the given decl has a declarator that should have
|
|
|
|
/// been processed by Sema::GetTypeForDeclarator.
|
2011-07-02 07:49:12 +08:00
|
|
|
static bool hasDeclarator(const Decl *D) {
|
2011-06-16 07:02:42 +08:00
|
|
|
// In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
|
2011-07-02 07:49:12 +08:00
|
|
|
return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
|
|
|
|
isa<ObjCPropertyDecl>(D);
|
2011-01-05 20:14:39 +08:00
|
|
|
}
|
|
|
|
|
2008-10-19 10:04:16 +08:00
|
|
|
/// hasFunctionProto - Return true if the given decl has a argument
|
|
|
|
/// information. This decl should have already passed
|
2009-05-16 07:15:03 +08:00
|
|
|
/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
|
2011-07-02 07:49:12 +08:00
|
|
|
static bool hasFunctionProto(const Decl *D) {
|
|
|
|
if (const FunctionType *FnTy = getFunctionType(D))
|
2009-02-27 07:50:07 +08:00
|
|
|
return isa<FunctionProtoType>(FnTy);
|
2009-05-16 07:15:03 +08:00
|
|
|
else {
|
2011-07-02 07:49:12 +08:00
|
|
|
assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
|
2008-10-19 10:04:16 +08:00
|
|
|
return true;
|
|
|
|
}
|
2008-09-26 12:12:28 +08:00
|
|
|
}
|
|
|
|
|
2008-10-19 10:04:16 +08:00
|
|
|
/// getFunctionOrMethodNumArgs - Return number of function or method
|
|
|
|
/// arguments. It is an error to call this on a K&R function (use
|
|
|
|
/// hasFunctionProto first).
|
2011-07-02 07:49:12 +08:00
|
|
|
static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
|
|
|
|
if (const FunctionType *FnTy = getFunctionType(D))
|
2009-02-27 07:50:07 +08:00
|
|
|
return cast<FunctionProtoType>(FnTy)->getNumArgs();
|
2011-07-02 07:49:12 +08:00
|
|
|
if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
|
2009-05-20 01:08:59 +08:00
|
|
|
return BD->getNumParams();
|
2011-07-02 07:49:12 +08:00
|
|
|
return cast<ObjCMethodDecl>(D)->param_size();
|
2008-09-26 12:12:28 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
|
|
|
|
if (const FunctionType *FnTy = getFunctionType(D))
|
2009-02-27 07:50:07 +08:00
|
|
|
return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
|
2011-07-02 07:49:12 +08:00
|
|
|
if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
|
2009-05-20 01:08:59 +08:00
|
|
|
return BD->getParamDecl(Idx)->getType();
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
|
2008-09-26 12:12:28 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
static QualType getFunctionOrMethodResultType(const Decl *D) {
|
|
|
|
if (const FunctionType *FnTy = getFunctionType(D))
|
2009-05-21 01:41:43 +08:00
|
|
|
return cast<FunctionProtoType>(FnTy)->getResultType();
|
2011-07-02 07:49:12 +08:00
|
|
|
return cast<ObjCMethodDecl>(D)->getResultType();
|
2009-05-21 01:41:43 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
static bool isFunctionOrMethodVariadic(const Decl *D) {
|
|
|
|
if (const FunctionType *FnTy = getFunctionType(D)) {
|
2009-02-27 07:50:07 +08:00
|
|
|
const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
|
2008-09-26 12:12:28 +08:00
|
|
|
return proto->isVariadic();
|
2011-07-02 07:49:12 +08:00
|
|
|
} else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
|
2010-04-30 00:48:58 +08:00
|
|
|
return BD->isVariadic();
|
2009-05-20 01:08:59 +08:00
|
|
|
else {
|
2011-07-02 07:49:12 +08:00
|
|
|
return cast<ObjCMethodDecl>(D)->isVariadic();
|
2008-09-26 12:12:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
static bool isInstanceMethod(const Decl *D) {
|
|
|
|
if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
|
2010-11-16 16:35:43 +08:00
|
|
|
return MethodDecl->isInstance();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:38:35 +08:00
|
|
|
static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
|
2009-09-22 07:43:11 +08:00
|
|
|
const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
|
2008-07-27 06:17:49 +08:00
|
|
|
if (!PT)
|
2008-06-27 02:38:35 +08:00
|
|
|
return false;
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2010-05-18 05:00:27 +08:00
|
|
|
ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
|
|
|
|
if (!Cls)
|
2008-06-27 02:38:35 +08:00
|
|
|
return false;
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2010-05-18 05:00:27 +08:00
|
|
|
IdentifierInfo* ClsName = Cls->getIdentifier();
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2008-06-27 02:38:35 +08:00
|
|
|
// FIXME: Should we walk the chain of classes?
|
|
|
|
return ClsName == &Ctx.Idents.get("NSString") ||
|
|
|
|
ClsName == &Ctx.Idents.get("NSMutableString");
|
|
|
|
}
|
|
|
|
|
2008-09-26 11:32:58 +08:00
|
|
|
static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
|
2009-07-30 05:53:49 +08:00
|
|
|
const PointerType *PT = T->getAs<PointerType>();
|
2008-09-26 11:32:58 +08:00
|
|
|
if (!PT)
|
|
|
|
return false;
|
|
|
|
|
2009-07-30 05:53:49 +08:00
|
|
|
const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
|
2008-09-26 11:32:58 +08:00
|
|
|
if (!RT)
|
|
|
|
return false;
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2008-09-26 11:32:58 +08:00
|
|
|
const RecordDecl *RD = RT->getDecl();
|
2010-05-12 05:36:43 +08:00
|
|
|
if (RD->getTagKind() != TTK_Struct)
|
2008-09-26 11:32:58 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
|
|
|
|
}
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
/// \brief Check if the attribute has exactly as many args as Num. May
|
|
|
|
/// output an error.
|
2011-07-12 07:30:35 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-07-29 04:12:35 +08:00
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
/// \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) {
|
2011-07-29 04:12:35 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-29 01:21:07 +08:00
|
|
|
///
|
|
|
|
/// \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
|
|
|
|
///
|
2011-08-02 12:50:49 +08:00
|
|
|
static bool mayBeSharedVariable(const Decl *D) {
|
2011-07-29 01:21:07 +08:00
|
|
|
if (isa<FieldDecl>(D))
|
|
|
|
return true;
|
2011-08-02 12:50:49 +08:00
|
|
|
if (const VarDecl *vd = dyn_cast<VarDecl>(D))
|
2011-07-29 01:21:07 +08:00
|
|
|
return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
/// \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();
|
|
|
|
}
|
|
|
|
|
2011-07-29 01:21:07 +08:00
|
|
|
///
|
|
|
|
/// \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
|
|
|
|
///
|
2011-08-02 12:50:49 +08:00
|
|
|
static bool checkIsPointer(Sema &S, const Decl *D, const AttributeList &Attr) {
|
|
|
|
if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
|
2011-07-29 01:21:07 +08:00
|
|
|
QualType QT = vd->getType();
|
2011-08-02 12:50:49 +08:00
|
|
|
if (QT->isAnyPointerType())
|
2011-07-29 01:21:07 +08:00
|
|
|
return true;
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_pointer_attribute_wrong_type)
|
|
|
|
<< Attr.getName()->getName() << QT;
|
|
|
|
} else {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
|
|
|
|
<< Attr.getName();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
/// \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.
|
2011-08-19 12:18:11 +08:00
|
|
|
static const RecordType *getRecordType(QualType QT) {
|
|
|
|
if (const RecordType *RT = QT->getAs<RecordType>())
|
2011-08-10 01:59:31 +08:00
|
|
|
return RT;
|
2011-08-19 12:18:11 +08:00
|
|
|
|
|
|
|
// Now check if we point to record type.
|
|
|
|
if (const PointerType *PT = QT->getAs<PointerType>())
|
|
|
|
return PT->getPointeeType()->getAs<RecordType>();
|
|
|
|
|
|
|
|
return 0;
|
2011-08-10 01:59:31 +08:00
|
|
|
}
|
|
|
|
|
2011-08-24 02:46:34 +08:00
|
|
|
/// \brief Thread Safety Analysis: Checks that the passed in RecordType
|
|
|
|
/// resolves to a lockable object. May flag an error.
|
2011-09-03 11:30:59 +08:00
|
|
|
static bool checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
|
|
|
|
const RecordType *RT) {
|
2011-08-24 02:46:34 +08:00
|
|
|
// Flag error if could not get record type for this argument.
|
2011-09-03 11:30:59 +08:00
|
|
|
if (!RT) {
|
2011-08-24 02:46:34 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_class)
|
|
|
|
<< Attr.getName();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Flag error if the type is not lockable.
|
2011-09-03 11:30:59 +08:00
|
|
|
if (!RT->getDecl()->getAttr<LockableAttr>()) {
|
2011-08-24 02:46:34 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_lockable)
|
|
|
|
<< Attr.getName();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
|
|
|
|
/// from Sidx, resolve to a lockable object. May flag an error.
|
2011-08-24 02:46:34 +08:00
|
|
|
/// \param Sidx The attribute argument index to start checking with.
|
|
|
|
/// \param ParamIdxOk Whether an argument can be indexing into a function
|
|
|
|
/// parameter list.
|
|
|
|
static bool checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
|
|
|
|
const AttributeList &Attr,
|
|
|
|
SmallVectorImpl<Expr*> &Args,
|
2011-08-10 01:59:31 +08:00
|
|
|
int Sidx = 0,
|
|
|
|
bool ParamIdxOk = false) {
|
2011-08-24 02:46:34 +08:00
|
|
|
for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
|
2011-08-10 01:59:31 +08:00
|
|
|
Expr *ArgExp = Attr.getArg(Idx);
|
2011-08-24 02:46:34 +08:00
|
|
|
|
2011-09-09 01:42:31 +08:00
|
|
|
if (ArgExp->isTypeDependent()) {
|
|
|
|
// FIXME -- need to processs this again on template instantiation
|
|
|
|
Args.push_back(ArgExp);
|
|
|
|
continue;
|
|
|
|
}
|
2011-08-10 01:59:31 +08:00
|
|
|
|
2011-08-24 02:46:34 +08:00
|
|
|
QualType ArgTy = ArgExp->getType();
|
2011-08-10 01:59:31 +08:00
|
|
|
|
2011-08-24 02:46:34 +08:00
|
|
|
// First see if we can just cast to record type, or point to record type.
|
|
|
|
const RecordType *RT = getRecordType(ArgTy);
|
2011-08-10 01:59:31 +08:00
|
|
|
|
2011-08-24 02:46:34 +08:00
|
|
|
// Now check if we index into a record type function param.
|
|
|
|
if(!RT && ParamIdxOk) {
|
|
|
|
FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
|
2011-08-10 01:59:31 +08:00
|
|
|
IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
|
|
|
|
if(FD && IL) {
|
|
|
|
unsigned int NumParams = FD->getNumParams();
|
|
|
|
llvm::APInt ArgValue = IL->getValue();
|
2011-08-24 02:46:34 +08:00
|
|
|
uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
|
|
|
|
uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
|
|
|
|
if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
|
2011-08-10 01:59:31 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
|
|
|
|
<< Attr.getName() << Idx + 1 << NumParams;
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-24 02:46:34 +08:00
|
|
|
ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
|
|
|
|
RT = getRecordType(ArgTy);
|
2011-08-10 01:59:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-24 02:46:34 +08:00
|
|
|
if (!checkForLockableRecord(S, D, Attr, RT))
|
2011-08-10 01:59:31 +08:00
|
|
|
return false;
|
|
|
|
|
2011-08-24 02:46:34 +08:00
|
|
|
Args.push_back(ArgExp);
|
2011-08-10 01:59:31 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-06-29 08:16:31 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Attribute Implementations
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-08-01 06:40:48 +08:00
|
|
|
// 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).
|
|
|
|
|
2011-07-29 01:21:07 +08:00
|
|
|
static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr,
|
|
|
|
bool pointer = false) {
|
|
|
|
assert(!Attr.isInvalid());
|
|
|
|
|
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// D must be either a member field or global (potentially shared) variable.
|
|
|
|
if (!mayBeSharedVariable(D)) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-08-10 01:59:31 +08:00
|
|
|
<< Attr.getName() << ExpectedFieldOrGlobalVar;
|
2011-07-29 01:21:07 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pointer && !checkIsPointer(S, D, Attr))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (pointer)
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
|
2011-07-29 01:21:07 +08:00
|
|
|
else
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
|
2011-07-29 01:21:07 +08:00
|
|
|
}
|
|
|
|
|
2011-07-29 04:12:35 +08:00
|
|
|
static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr,
|
2011-08-10 01:59:31 +08:00
|
|
|
bool pointer = false) {
|
2011-07-29 04:12:35 +08:00
|
|
|
assert(!Attr.isInvalid());
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 1))
|
2011-07-29 04:12:35 +08:00
|
|
|
return;
|
|
|
|
|
2011-08-24 02:46:34 +08:00
|
|
|
Expr *Arg = Attr.getArg(0);
|
|
|
|
|
2011-07-29 04:12:35 +08:00
|
|
|
// D must be either a member field or global (potentially shared) variable.
|
|
|
|
if (!mayBeSharedVariable(D)) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-08-10 01:59:31 +08:00
|
|
|
<< Attr.getName() << ExpectedFieldOrGlobalVar;
|
2011-07-29 04:12:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pointer && !checkIsPointer(S, D, Attr))
|
|
|
|
return;
|
|
|
|
|
2011-08-24 02:46:34 +08:00
|
|
|
if (Arg->isTypeDependent())
|
2011-09-09 01:42:31 +08:00
|
|
|
// FIXME: handle attributes with dependent types
|
2011-08-24 02:46:34 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// check that the argument is lockable object
|
|
|
|
if (!checkForLockableRecord(S, D, Attr, getRecordType(Arg->getType())))
|
2011-08-10 01:59:31 +08:00
|
|
|
return;
|
|
|
|
|
2011-07-29 04:12:35 +08:00
|
|
|
if (pointer)
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
|
2011-08-24 02:46:34 +08:00
|
|
|
S.Context, Arg));
|
2011-07-29 04:12:35 +08:00
|
|
|
else
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
|
2011-07-29 04:12:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-29 01:21:07 +08:00
|
|
|
static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr,
|
|
|
|
bool scoped = false) {
|
|
|
|
assert(!Attr.isInvalid());
|
|
|
|
|
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
|
|
|
return;
|
|
|
|
|
2011-09-16 08:35:54 +08:00
|
|
|
// FIXME: Lockable structs for C code.
|
2011-07-29 01:21:07 +08:00
|
|
|
if (!isa<CXXRecordDecl>(D)) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
|
|
|
<< Attr.getName() << ExpectedClass;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scoped)
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
|
2011-07-29 01:21:07 +08:00
|
|
|
else
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
|
2011-07-29 01:21:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
|
|
|
|
const AttributeList &Attr) {
|
|
|
|
assert(!Attr.isInvalid());
|
|
|
|
|
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
|
|
|
return;
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
|
2011-07-29 01:21:07 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
|
|
|
<< Attr.getName() << ExpectedFunctionOrMethod;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
|
2011-07-29 01:21:07 +08:00
|
|
|
S.Context));
|
|
|
|
}
|
|
|
|
|
2011-07-29 04:12:35 +08:00
|
|
|
static void handleAcquireOrderAttr(Sema &S, Decl *D, const AttributeList &Attr,
|
|
|
|
bool before) {
|
|
|
|
assert(!Attr.isInvalid());
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
|
2011-07-29 04:12:35 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// D must be either a member field or global (potentially shared) variable.
|
2011-08-10 01:59:31 +08:00
|
|
|
ValueDecl *VD = dyn_cast<ValueDecl>(D);
|
|
|
|
if (!VD || !mayBeSharedVariable(D)) {
|
2011-07-29 04:12:35 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-08-10 01:59:31 +08:00
|
|
|
<< Attr.getName() << ExpectedFieldOrGlobalVar;
|
2011-07-29 04:12:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
// 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::err_attribute_decl_not_lockable)
|
|
|
|
<< Attr.getName();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-24 02:46:34 +08:00
|
|
|
SmallVector<Expr*, 1> Args;
|
2011-08-10 01:59:31 +08:00
|
|
|
// check that all arguments are lockable objects
|
2011-08-24 02:46:34 +08:00
|
|
|
if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args))
|
2011-08-10 01:59:31 +08:00
|
|
|
return;
|
|
|
|
|
2011-08-24 02:46:34 +08:00
|
|
|
unsigned Size = Args.size();
|
|
|
|
assert(Size == Attr.getNumArgs());
|
|
|
|
Expr **StartArg = Size == 0 ? 0 : &Args[0];
|
|
|
|
|
2011-07-29 04:12:35 +08:00
|
|
|
if (before)
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
|
2011-08-24 02:46:34 +08:00
|
|
|
StartArg, Size));
|
2011-07-29 04:12:35 +08:00
|
|
|
else
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
|
2011-08-24 02:46:34 +08:00
|
|
|
StartArg, Size));
|
2011-07-29 04:12:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleLockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
|
2011-08-10 01:59:31 +08:00
|
|
|
bool exclusive = false) {
|
2011-07-29 04:12:35 +08:00
|
|
|
assert(!Attr.isInvalid());
|
|
|
|
|
|
|
|
// zero or more arguments ok
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
// check that the attribute is applied to a function
|
|
|
|
if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
|
2011-07-29 04:12:35 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
|
|
|
<< Attr.getName() << ExpectedFunctionOrMethod;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
// check that all arguments are lockable objects
|
2011-08-24 02:46:34 +08:00
|
|
|
SmallVector<Expr*, 1> Args;
|
|
|
|
if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true))
|
2011-08-10 01:59:31 +08:00
|
|
|
return;
|
|
|
|
|
2011-08-24 02:46:34 +08:00
|
|
|
unsigned Size = Args.size();
|
|
|
|
assert(Size == Attr.getNumArgs());
|
|
|
|
Expr **StartArg = Size == 0 ? 0 : &Args[0];
|
|
|
|
|
2011-07-29 04:12:35 +08:00
|
|
|
if (exclusive)
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
|
2011-08-24 02:46:34 +08:00
|
|
|
S.Context, StartArg,
|
|
|
|
Size));
|
2011-07-29 04:12:35 +08:00
|
|
|
else
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
|
2011-08-24 02:46:34 +08:00
|
|
|
S.Context, StartArg,
|
|
|
|
Size));
|
2011-07-29 04:12:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleTrylockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
|
2011-08-10 01:59:31 +08:00
|
|
|
bool exclusive = false) {
|
2011-07-29 04:12:35 +08:00
|
|
|
assert(!Attr.isInvalid());
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
|
2011-07-29 04:12:35 +08:00
|
|
|
return;
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
|
|
|
|
if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
|
2011-07-29 04:12:35 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
|
|
|
<< Attr.getName() << ExpectedFunctionOrMethod;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
if (!isIntOrBool(Attr.getArg(0))) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
|
|
|
|
<< Attr.getName();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-24 02:46:34 +08:00
|
|
|
SmallVector<Expr*, 2> Args;
|
2011-08-10 01:59:31 +08:00
|
|
|
// check that all arguments are lockable objects
|
2011-08-24 02:46:34 +08:00
|
|
|
if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1))
|
2011-08-10 01:59:31 +08:00
|
|
|
return;
|
|
|
|
|
2011-08-24 02:46:34 +08:00
|
|
|
unsigned Size = Args.size();
|
|
|
|
Expr **StartArg = Size == 0 ? 0 : &Args[0];
|
|
|
|
|
2011-07-29 04:12:35 +08:00
|
|
|
if (exclusive)
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
|
2011-08-24 02:46:34 +08:00
|
|
|
S.Context,
|
2011-09-16 01:50:19 +08:00
|
|
|
Attr.getArg(0),
|
2011-08-24 02:46:34 +08:00
|
|
|
StartArg, Size));
|
2011-07-29 04:12:35 +08:00
|
|
|
else
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
|
2011-09-16 01:50:19 +08:00
|
|
|
S.Context,
|
|
|
|
Attr.getArg(0),
|
|
|
|
StartArg, Size));
|
2011-07-29 04:12:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleLocksRequiredAttr(Sema &S, Decl *D, const AttributeList &Attr,
|
2011-08-10 01:59:31 +08:00
|
|
|
bool exclusive = false) {
|
2011-07-29 04:12:35 +08:00
|
|
|
assert(!Attr.isInvalid());
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
|
2011-07-29 04:12:35 +08:00
|
|
|
return;
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
|
2011-07-29 04:12:35 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
|
|
|
<< Attr.getName() << ExpectedFunctionOrMethod;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
// check that all arguments are lockable objects
|
2011-08-24 02:46:34 +08:00
|
|
|
SmallVector<Expr*, 1> Args;
|
|
|
|
if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args))
|
2011-08-10 01:59:31 +08:00
|
|
|
return;
|
|
|
|
|
2011-08-24 02:46:34 +08:00
|
|
|
unsigned Size = Args.size();
|
|
|
|
assert(Size == Attr.getNumArgs());
|
|
|
|
Expr **StartArg = Size == 0 ? 0 : &Args[0];
|
|
|
|
|
2011-07-29 04:12:35 +08:00
|
|
|
if (exclusive)
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
|
2011-08-24 02:46:34 +08:00
|
|
|
S.Context, StartArg,
|
|
|
|
Size));
|
2011-07-29 04:12:35 +08:00
|
|
|
else
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
|
2011-08-24 02:46:34 +08:00
|
|
|
S.Context, StartArg,
|
|
|
|
Size));
|
2011-07-29 04:12:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleUnlockFunAttr(Sema &S, Decl *D,
|
2011-08-10 01:59:31 +08:00
|
|
|
const AttributeList &Attr) {
|
2011-07-29 04:12:35 +08:00
|
|
|
assert(!Attr.isInvalid());
|
|
|
|
|
|
|
|
// zero or more arguments ok
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
|
2011-07-29 04:12:35 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
|
|
|
<< Attr.getName() << ExpectedFunctionOrMethod;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
// check that all arguments are lockable objects
|
2011-08-24 02:46:34 +08:00
|
|
|
SmallVector<Expr*, 1> Args;
|
|
|
|
if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true))
|
2011-08-10 01:59:31 +08:00
|
|
|
return;
|
|
|
|
|
2011-08-24 02:46:34 +08:00
|
|
|
unsigned Size = Args.size();
|
|
|
|
assert(Size == Attr.getNumArgs());
|
|
|
|
Expr **StartArg = Size == 0 ? 0 : &Args[0];
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
|
2011-08-24 02:46:34 +08:00
|
|
|
StartArg, Size));
|
2011-07-29 04:12:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleLockReturnedAttr(Sema &S, Decl *D,
|
2011-08-10 01:59:31 +08:00
|
|
|
const AttributeList &Attr) {
|
2011-07-29 04:12:35 +08:00
|
|
|
assert(!Attr.isInvalid());
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 1))
|
2011-07-29 04:12:35 +08:00
|
|
|
return;
|
2011-08-24 02:46:34 +08:00
|
|
|
Expr *Arg = Attr.getArg(0);
|
2011-07-29 04:12:35 +08:00
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
|
2011-07-29 04:12:35 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
|
|
|
<< Attr.getName() << ExpectedFunctionOrMethod;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-24 02:46:34 +08:00
|
|
|
if (Arg->isTypeDependent())
|
2011-08-10 01:59:31 +08:00
|
|
|
return;
|
|
|
|
|
2011-08-24 02:46:34 +08:00
|
|
|
// check that the argument is lockable object
|
|
|
|
if (!checkForLockableRecord(S, D, Attr, getRecordType(Arg->getType())))
|
|
|
|
return;
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context, Arg));
|
2011-07-29 04:12:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleLocksExcludedAttr(Sema &S, Decl *D,
|
2011-08-10 01:59:31 +08:00
|
|
|
const AttributeList &Attr) {
|
2011-07-29 04:12:35 +08:00
|
|
|
assert(!Attr.isInvalid());
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
|
2011-07-29 04:12:35 +08:00
|
|
|
return;
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
|
2011-07-29 04:12:35 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
|
|
|
<< Attr.getName() << ExpectedFunctionOrMethod;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-10 01:59:31 +08:00
|
|
|
// check that all arguments are lockable objects
|
2011-08-24 02:46:34 +08:00
|
|
|
SmallVector<Expr*, 1> Args;
|
|
|
|
if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args))
|
2011-08-10 01:59:31 +08:00
|
|
|
return;
|
|
|
|
|
2011-08-24 02:46:34 +08:00
|
|
|
unsigned Size = Args.size();
|
|
|
|
assert(Size == Attr.getNumArgs());
|
|
|
|
Expr **StartArg = Size == 0 ? 0 : &Args[0];
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
|
2011-08-24 02:46:34 +08:00
|
|
|
StartArg, Size));
|
2011-07-29 04:12:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
|
|
|
|
const AttributeList &Attr) {
|
2011-07-02 07:49:12 +08:00
|
|
|
TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
|
2008-06-29 07:36:30 +08:00
|
|
|
if (tDecl == 0) {
|
2008-06-29 08:43:07 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
|
2008-06-29 07:36:30 +08:00
|
|
|
return;
|
2008-06-27 02:38:35 +08:00
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2008-06-27 02:38:35 +08:00
|
|
|
QualType curType = tDecl->getUnderlyingType();
|
2009-06-18 05:51:59 +08:00
|
|
|
|
|
|
|
Expr *sizeExpr;
|
|
|
|
|
|
|
|
// Special case where the argument is a template id.
|
|
|
|
if (Attr.getParameterName()) {
|
2009-11-25 03:00:30 +08:00
|
|
|
CXXScopeSpec SS;
|
|
|
|
UnqualifiedId id;
|
|
|
|
id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
|
2011-06-16 00:02:29 +08:00
|
|
|
|
|
|
|
ExprResult Size = S.ActOnIdExpression(scope, SS, id, false, false);
|
|
|
|
if (Size.isInvalid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
sizeExpr = Size.get();
|
2009-06-18 05:51:59 +08:00
|
|
|
} else {
|
|
|
|
// check the attribute arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 1))
|
2009-06-18 05:51:59 +08:00
|
|
|
return;
|
2011-07-12 07:30:35 +08:00
|
|
|
|
2010-11-24 04:45:58 +08:00
|
|
|
sizeExpr = Attr.getArg(0);
|
2008-06-27 02:38:35 +08:00
|
|
|
}
|
2009-06-18 05:51:59 +08:00
|
|
|
|
|
|
|
// Instantiate/Install the vector type, and let Sema build the type for us.
|
|
|
|
// This will run the reguired checks.
|
2010-08-24 07:25:46 +08:00
|
|
|
QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
|
2009-06-18 05:51:59 +08:00
|
|
|
if (!T.isNull()) {
|
2009-10-24 16:00:42 +08:00
|
|
|
// FIXME: preserve the old source info.
|
2009-12-07 10:54:59 +08:00
|
|
|
tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2009-06-18 05:51:59 +08:00
|
|
|
// Remember this typedef decl, we will need it later for diagnostics.
|
|
|
|
S.ExtVectorDecls.push_back(tDecl);
|
2008-06-27 02:38:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2008-06-27 02:38:35 +08:00
|
|
|
// check the attribute arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (TagDecl *TD = dyn_cast<TagDecl>(D))
|
2011-09-14 00:05:58 +08:00
|
|
|
TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
|
2011-07-02 07:49:12 +08:00
|
|
|
else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
|
2008-06-27 02:38:35 +08:00
|
|
|
// If the alignment is less than or equal to 8 bits, the packed attribute
|
|
|
|
// has no effect.
|
|
|
|
if (!FD->getType()->isIncompleteType() &&
|
2008-06-29 08:43:07 +08:00
|
|
|
S.Context.getTypeAlign(FD->getType()) <= 8)
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
|
2008-11-24 05:45:46 +08:00
|
|
|
<< Attr.getName() << FD->getType();
|
2008-06-27 02:38:35 +08:00
|
|
|
else
|
2011-09-14 00:05:58 +08:00
|
|
|
FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
|
2008-06-27 02:38:35 +08:00
|
|
|
} else
|
2008-11-19 16:23:25 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
|
2008-06-27 02:38:35 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2011-07-02 07:49:12 +08:00
|
|
|
if (TagDecl *TD = dyn_cast<TagDecl>(D))
|
2011-09-14 00:05:58 +08:00
|
|
|
TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
|
2011-04-27 01:54:40 +08:00
|
|
|
else
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
|
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
|
2008-07-16 06:26:48 +08:00
|
|
|
// check the attribute arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
2008-07-16 06:26:48 +08:00
|
|
|
return;
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2010-02-18 11:08:58 +08:00
|
|
|
// The IBAction attributes only apply to instance methods.
|
2011-07-02 07:49:12 +08:00
|
|
|
if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
|
2010-02-18 11:08:58 +08:00
|
|
|
if (MD->isInstanceMethod()) {
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
|
2010-02-18 11:08:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-04 14:54:16 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
|
2010-02-18 11:08:58 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
|
2010-02-18 11:08:58 +08:00
|
|
|
// check the attribute arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
2010-02-18 11:08:58 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// The IBOutlet attributes only apply to instance variables of
|
2010-02-17 10:37:45 +08:00
|
|
|
// Objective-C classes.
|
2011-07-02 07:49:12 +08:00
|
|
|
if (isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D)) {
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
|
2010-02-18 11:08:58 +08:00
|
|
|
return;
|
2010-02-17 10:37:45 +08:00
|
|
|
}
|
2010-02-18 11:08:58 +08:00
|
|
|
|
2011-02-04 14:54:16 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
|
2008-07-16 06:26:48 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleIBOutletCollection(Sema &S, Decl *D,
|
|
|
|
const AttributeList &Attr) {
|
2010-05-20 01:38:06 +08:00
|
|
|
|
|
|
|
// The iboutletcollection attribute can have zero or one arguments.
|
2010-08-18 04:23:12 +08:00
|
|
|
if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
|
2010-05-20 01:38:06 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The IBOutletCollection attributes only apply to instance variables of
|
|
|
|
// Objective-C classes.
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!(isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
|
2011-02-04 14:54:16 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
|
2010-05-20 01:38:06 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-07-02 07:49:12 +08:00
|
|
|
if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
|
2010-08-18 05:39:27 +08:00
|
|
|
if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
|
|
|
|
<< VD->getType() << 0;
|
|
|
|
return;
|
|
|
|
}
|
2011-07-02 07:49:12 +08:00
|
|
|
if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
|
2010-08-18 05:39:27 +08:00
|
|
|
if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
|
|
|
|
<< PD->getType() << 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-18 04:23:12 +08:00
|
|
|
IdentifierInfo *II = Attr.getParameterName();
|
|
|
|
if (!II)
|
|
|
|
II = &S.Context.Idents.get("id");
|
2010-08-18 05:39:27 +08:00
|
|
|
|
2010-08-24 13:47:05 +08:00
|
|
|
ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
|
2011-07-02 07:49:12 +08:00
|
|
|
S.getScopeForContext(D->getDeclContext()->getParent()));
|
2010-08-18 04:23:12 +08:00
|
|
|
if (!TypeRep) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
|
|
|
|
return;
|
|
|
|
}
|
2010-08-24 13:47:05 +08:00
|
|
|
QualType QT = TypeRep.get();
|
2010-08-18 04:23:12 +08:00
|
|
|
// 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->isObjCClassType() &&
|
|
|
|
!QT->isObjCObjectType()) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
|
|
|
|
return;
|
|
|
|
}
|
2011-09-14 02:41:59 +08:00
|
|
|
D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
|
|
|
|
QT, Attr.getParameterLoc()));
|
2010-05-20 01:38:06 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:16 +08:00
|
|
|
static void possibleTransparentUnionPointerType(QualType &T) {
|
2011-06-28 05:12:03 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2009-07-25 03:02:52 +08:00
|
|
|
// GCC ignores the nonnull attribute on K&R style function prototypes, so we
|
|
|
|
// ignore it as well
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedFunction;
|
2008-07-22 05:53:04 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2010-11-16 16:35:43 +08:00
|
|
|
// In C++ the implicit 'this' function parameter also counts, and they are
|
|
|
|
// counted from one.
|
2011-07-02 07:49:12 +08:00
|
|
|
bool HasImplicitThisParam = isInstanceMethod(D);
|
|
|
|
unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
|
2008-07-22 05:53:04 +08:00
|
|
|
|
|
|
|
// The nonnull attribute only applies to pointers.
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<unsigned, 10> NonNullArgs;
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2008-07-22 05:53:04 +08:00
|
|
|
for (AttributeList::arg_iterator I=Attr.arg_begin(),
|
|
|
|
E=Attr.arg_end(); I!=E; ++I) {
|
2009-07-25 03:02:52 +08:00
|
|
|
|
|
|
|
|
2008-07-22 05:53:04 +08:00
|
|
|
// The argument must be an integer constant expression.
|
2010-11-24 04:45:58 +08:00
|
|
|
Expr *Ex = *I;
|
2008-07-22 05:53:04 +08:00
|
|
|
llvm::APSInt ArgNum(32);
|
2010-05-19 07:01:22 +08:00
|
|
|
if (Ex->isTypeDependent() || Ex->isValueDependent() ||
|
|
|
|
!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
|
|
|
|
<< "nonnull" << Ex->getSourceRange();
|
2008-07-22 05:53:04 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2008-07-22 05:53:04 +08:00
|
|
|
unsigned x = (unsigned) ArgNum.getZExtValue();
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2008-07-22 05:53:04 +08:00
|
|
|
if (x < 1 || x > NumArgs) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
|
2008-11-19 15:22:31 +08:00
|
|
|
<< "nonnull" << I.getArgNum() << Ex->getSourceRange();
|
2008-07-22 05:53:04 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2008-07-22 06:09:15 +08:00
|
|
|
--x;
|
2010-11-16 16:35:43 +08:00
|
|
|
if (HasImplicitThisParam) {
|
|
|
|
if (x == 0) {
|
|
|
|
S.Diag(Attr.getLoc(),
|
|
|
|
diag::err_attribute_invalid_implicit_this_argument)
|
|
|
|
<< "nonnull" << Ex->getSourceRange();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
--x;
|
|
|
|
}
|
2008-07-22 05:53:04 +08:00
|
|
|
|
|
|
|
// Is the function argument a pointer type?
|
2011-07-02 07:49:12 +08:00
|
|
|
QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
|
2011-07-02 07:49:16 +08:00
|
|
|
possibleTransparentUnionPointerType(T);
|
2011-06-28 05:12:03 +08:00
|
|
|
|
2009-07-16 07:23:54 +08:00
|
|
|
if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
|
2008-07-22 05:53:04 +08:00
|
|
|
// FIXME: Should also highlight argument in decl.
|
2010-08-13 02:48:43 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
|
2008-11-19 13:08:23 +08:00
|
|
|
<< "nonnull" << Ex->getSourceRange();
|
2008-09-02 03:57:52 +08:00
|
|
|
continue;
|
2008-07-22 05:53:04 +08:00
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2008-07-22 05:53:04 +08:00
|
|
|
NonNullArgs.push_back(x);
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
|
|
|
// If no arguments were specified to __attribute__((nonnull)) then all pointer
|
|
|
|
// arguments have a nonnull attribute.
|
2008-09-02 03:57:52 +08:00
|
|
|
if (NonNullArgs.empty()) {
|
2011-07-02 07:49:12 +08:00
|
|
|
for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
|
|
|
|
QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
|
2011-07-02 07:49:16 +08:00
|
|
|
possibleTransparentUnionPointerType(T);
|
2009-07-16 07:23:54 +08:00
|
|
|
if (T->isAnyPointerType() || T->isBlockPointerType())
|
2008-10-19 10:04:16 +08:00
|
|
|
NonNullArgs.push_back(I);
|
2008-11-18 14:52:58 +08:00
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2010-10-22 02:49:36 +08:00
|
|
|
// No pointer arguments?
|
2010-09-28 03:05:51 +08:00
|
|
|
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);
|
2008-09-02 03:57:52 +08:00
|
|
|
return;
|
2010-09-28 03:05:51 +08:00
|
|
|
}
|
2008-07-22 05:53:04 +08:00
|
|
|
}
|
2008-09-02 03:57:52 +08:00
|
|
|
|
|
|
|
unsigned* start = &NonNullArgs[0];
|
|
|
|
unsigned size = NonNullArgs.size();
|
2010-07-31 09:52:11 +08:00
|
|
|
llvm::array_pod_sort(start, start + size);
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
|
2010-08-19 07:23:40 +08:00
|
|
|
size));
|
2008-07-22 05:53:04 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
|
2010-07-31 09:52:11 +08:00
|
|
|
// 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
|
2010-08-12 16:54:03 +08:00
|
|
|
// after being held. free() should be __attribute((ownership_takes)), whereas
|
|
|
|
// a list append function may well be __attribute((ownership_holds)).
|
2010-07-31 09:52:11 +08:00
|
|
|
|
|
|
|
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.
|
2010-08-19 07:23:40 +08:00
|
|
|
OwnershipAttr::OwnershipKind K;
|
2010-08-12 16:54:03 +08:00
|
|
|
switch (AL.getKind()) {
|
|
|
|
case AttributeList::AT_ownership_takes:
|
2010-08-19 07:23:40 +08:00
|
|
|
K = OwnershipAttr::Takes;
|
2010-07-31 09:52:11 +08:00
|
|
|
if (AL.getNumArgs() < 1) {
|
|
|
|
S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
|
|
|
|
return;
|
|
|
|
}
|
2010-08-12 16:54:03 +08:00
|
|
|
break;
|
|
|
|
case AttributeList::AT_ownership_holds:
|
2010-08-19 07:23:40 +08:00
|
|
|
K = OwnershipAttr::Holds;
|
2010-07-31 09:52:11 +08:00
|
|
|
if (AL.getNumArgs() < 1) {
|
|
|
|
S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
|
|
|
|
return;
|
|
|
|
}
|
2010-08-12 16:54:03 +08:00
|
|
|
break;
|
|
|
|
case AttributeList::AT_ownership_returns:
|
2010-08-19 07:23:40 +08:00
|
|
|
K = OwnershipAttr::Returns;
|
2010-07-31 09:52:11 +08:00
|
|
|
if (AL.getNumArgs() > 1) {
|
|
|
|
S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
|
|
|
|
<< AL.getNumArgs() + 1;
|
|
|
|
return;
|
|
|
|
}
|
2010-08-12 16:54:03 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// This should never happen given how we are called.
|
|
|
|
llvm_unreachable("Unknown ownership attribute");
|
2010-07-31 09:52:11 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isFunction(D) || !hasFunctionProto(D)) {
|
2011-03-02 20:29:23 +08:00
|
|
|
S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
|
|
|
|
<< AL.getName() << ExpectedFunction;
|
2010-07-31 09:52:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-16 16:35:43 +08:00
|
|
|
// In C++ the implicit 'this' function parameter also counts, and they are
|
|
|
|
// counted from one.
|
2011-07-02 07:49:12 +08:00
|
|
|
bool HasImplicitThisParam = isInstanceMethod(D);
|
|
|
|
unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
|
2010-07-31 09:52:11 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Module = AL.getParameterName()->getName();
|
2010-07-31 09:52:11 +08:00
|
|
|
|
|
|
|
// Normalize the argument, __foo__ becomes foo.
|
|
|
|
if (Module.startswith("__") && Module.endswith("__"))
|
|
|
|
Module = Module.substr(2, Module.size() - 4);
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<unsigned, 10> OwnershipArgs;
|
2010-07-31 09:52:11 +08:00
|
|
|
|
2010-08-12 16:54:03 +08:00
|
|
|
for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
|
|
|
|
++I) {
|
2010-07-31 09:52:11 +08:00
|
|
|
|
2010-11-24 04:45:58 +08:00
|
|
|
Expr *IdxExpr = *I;
|
2010-07-31 09:52:11 +08:00
|
|
|
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;
|
2010-11-16 16:35:43 +08:00
|
|
|
if (HasImplicitThisParam) {
|
|
|
|
if (x == 0) {
|
|
|
|
S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
|
|
|
|
<< "ownership" << IdxExpr->getSourceRange();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
--x;
|
|
|
|
}
|
|
|
|
|
2010-07-31 09:52:11 +08:00
|
|
|
switch (K) {
|
2010-08-19 07:23:40 +08:00
|
|
|
case OwnershipAttr::Takes:
|
|
|
|
case OwnershipAttr::Holds: {
|
2010-07-31 09:52:11 +08:00
|
|
|
// Is the function argument a pointer type?
|
2011-07-02 07:49:12 +08:00
|
|
|
QualType T = getFunctionOrMethodArgType(D, x);
|
2010-07-31 09:52:11 +08:00
|
|
|
if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
|
|
|
|
// FIXME: Should also highlight argument in decl.
|
|
|
|
S.Diag(AL.getLoc(), diag::err_ownership_type)
|
2010-08-19 07:23:40 +08:00
|
|
|
<< ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
|
2010-07-31 09:52:11 +08:00
|
|
|
<< "pointer"
|
|
|
|
<< IdxExpr->getSourceRange();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2010-08-19 07:23:40 +08:00
|
|
|
case OwnershipAttr::Returns: {
|
2010-07-31 09:52:11 +08:00
|
|
|
if (AL.getNumArgs() > 1) {
|
|
|
|
// Is the function argument an integer type?
|
2010-11-24 04:45:58 +08:00
|
|
|
Expr *IdxExpr = AL.getArg(0);
|
2010-07-31 09:52:11 +08:00
|
|
|
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;
|
|
|
|
}
|
2010-08-12 16:54:03 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown ownership attribute");
|
2010-07-31 09:52:11 +08:00
|
|
|
} // switch
|
|
|
|
|
|
|
|
// Check we don't have a conflict with another ownership attribute.
|
2010-08-19 07:23:40 +08:00
|
|
|
for (specific_attr_iterator<OwnershipAttr>
|
2011-07-02 07:49:12 +08:00
|
|
|
i = D->specific_attr_begin<OwnershipAttr>(),
|
|
|
|
e = D->specific_attr_end<OwnershipAttr>();
|
2010-08-19 07:23:40 +08:00
|
|
|
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_*";
|
2010-07-31 09:52:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OwnershipArgs.push_back(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned* start = OwnershipArgs.data();
|
|
|
|
unsigned size = OwnershipArgs.size();
|
|
|
|
llvm::array_pod_sort(start, start + size);
|
2010-08-19 07:23:40 +08:00
|
|
|
|
|
|
|
if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
|
|
|
|
S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
|
|
|
|
return;
|
2010-07-31 09:52:11 +08:00
|
|
|
}
|
2010-08-19 07:23:40 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
|
2010-08-19 07:23:40 +08:00
|
|
|
start, size));
|
2010-07-31 09:52:11 +08:00
|
|
|
}
|
|
|
|
|
2011-02-09 06:35:49 +08:00
|
|
|
/// 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!");
|
2010-02-24 06:00:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2010-02-24 06:00:30 +08:00
|
|
|
// Check the attribute arguments.
|
|
|
|
if (Attr.getNumArgs() > 1) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
|
2011-02-09 06:35:49 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedVariableOrFunction;
|
2011-02-09 06:35:49 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
NamedDecl *nd = cast<NamedDecl>(D);
|
2011-02-09 06:35:49 +08:00
|
|
|
|
2010-02-24 06:00:30 +08:00
|
|
|
// 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
|
2011-07-02 07:49:12 +08:00
|
|
|
const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
|
2010-08-31 08:36:30 +08:00
|
|
|
if (!Ctx->isFileContext()) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
|
2011-02-09 06:35:49 +08:00
|
|
|
nd->getNameAsString();
|
2010-08-31 08:36:30 +08:00
|
|
|
return;
|
2010-02-24 06:00:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
2011-02-09 06:35:49 +08:00
|
|
|
if (!hasEffectivelyInternalLinkage(nd)) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
|
2010-02-24 06:00:30 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// GCC rejects
|
|
|
|
// static ((alias ("y"), weakref)).
|
|
|
|
// Should we? How to check that weakref is before or after alias?
|
|
|
|
|
|
|
|
if (Attr.getNumArgs() == 1) {
|
2010-11-24 04:45:58 +08:00
|
|
|
Expr *Arg = Attr.getArg(0);
|
2010-02-24 06:00:30 +08:00
|
|
|
Arg = Arg->IgnoreParenCasts();
|
|
|
|
StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
|
|
|
|
|
2011-07-27 13:40:30 +08:00
|
|
|
if (!Str || !Str->isAscii()) {
|
2010-02-24 06:00:30 +08:00
|
|
|
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?
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
|
2010-12-02 06:13:54 +08:00
|
|
|
Str->getString()));
|
2010-02-24 06:00:30 +08:00
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
|
2010-02-24 06:00:30 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2008-06-27 02:38:35 +08:00
|
|
|
// check the attribute arguments.
|
2008-06-29 07:36:30 +08:00
|
|
|
if (Attr.getNumArgs() != 1) {
|
2008-11-19 16:23:25 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2010-11-24 04:45:58 +08:00
|
|
|
Expr *Arg = Attr.getArg(0);
|
2008-06-27 02:38:35 +08:00
|
|
|
Arg = Arg->IgnoreParenCasts();
|
|
|
|
StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-07-27 13:40:30 +08:00
|
|
|
if (!Str || !Str->isAscii()) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
|
2008-11-19 16:23:25 +08:00
|
|
|
<< "alias" << 1;
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-09-02 08:18:52 +08:00
|
|
|
if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
|
2010-12-07 23:23:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:38:35 +08:00
|
|
|
// FIXME: check if target symbol exists in current file
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
|
2010-12-02 06:13:54 +08:00
|
|
|
Str->getString()));
|
2008-06-27 02:38:35 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2010-09-30 02:20:25 +08:00
|
|
|
// Check the attribute arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
2010-09-30 02:20:25 +08:00
|
|
|
return;
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<FunctionDecl>(D)) {
|
2010-09-30 02:20:25 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedFunction;
|
2010-09-30 02:20:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
|
2010-09-30 02:20:25 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleAlwaysInlineAttr(Sema &S, Decl *D,
|
|
|
|
const AttributeList &Attr) {
|
2010-09-30 02:20:25 +08:00
|
|
|
// Check the attribute arguments.
|
2011-04-15 13:49:29 +08:00
|
|
|
if (Attr.hasParameterOrArguments()) {
|
2008-11-19 16:23:25 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
|
2008-10-28 08:17:57 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-02-20 03:16:48 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<FunctionDecl>(D)) {
|
2009-02-20 03:16:48 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedFunction;
|
2009-02-20 03:16:48 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
|
2008-10-28 08:17:57 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2010-09-30 02:20:25 +08:00
|
|
|
// Check the attribute arguments.
|
2011-04-15 13:49:29 +08:00
|
|
|
if (Attr.hasParameterOrArguments()) {
|
2009-08-10 04:07:29 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
QualType RetTy = FD->getResultType();
|
2009-08-15 08:51:46 +08:00
|
|
|
if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
|
2009-08-15 08:51:46 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-08-10 06:36:29 +08:00
|
|
|
}
|
|
|
|
|
2009-08-15 08:51:46 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
|
2009-08-10 04:07:29 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2010-11-17 08:03:07 +08:00
|
|
|
// check the attribute arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
2010-11-17 08:03:07 +08:00
|
|
|
return;
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
|
2010-11-17 08:03:07 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2011-07-12 07:33:05 +08:00
|
|
|
assert(!Attr.isInvalid());
|
2011-07-02 07:49:12 +08:00
|
|
|
if (isa<VarDecl>(D))
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
|
2010-12-03 14:58:14 +08:00
|
|
|
else
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedVariable;
|
2010-12-02 10:45:55 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2011-07-12 07:33:05 +08:00
|
|
|
assert(!Attr.isInvalid());
|
2011-07-02 07:49:12 +08:00
|
|
|
if (isa<VarDecl>(D))
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
|
2010-12-03 14:58:14 +08:00
|
|
|
else
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedVariable;
|
2010-12-02 10:45:55 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
|
2011-07-02 07:49:12 +08:00
|
|
|
if (hasDeclarator(D)) return;
|
2011-01-05 20:14:39 +08:00
|
|
|
|
|
|
|
if (S.CheckNoReturnAttr(attr)) return;
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<ObjCMethodDecl>(D)) {
|
2011-01-05 20:14:39 +08:00
|
|
|
S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< attr.getName() << ExpectedFunctionOrMethod;
|
2011-01-05 20:14:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
|
2011-01-05 20:14:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
|
2011-04-15 13:49:29 +08:00
|
|
|
if (attr.hasParameterOrArguments()) {
|
2011-01-05 20:14:39 +08:00
|
|
|
Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
|
|
|
|
attr.setInvalid();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2010-08-19 08:51:58 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
|
|
|
|
const AttributeList &Attr) {
|
2010-08-19 08:51:58 +08:00
|
|
|
|
|
|
|
// The checking path for 'noreturn' and 'analyzer_noreturn' are different
|
|
|
|
// because 'analyzer_noreturn' does not impact the type.
|
|
|
|
|
2011-07-12 07:30:35 +08:00
|
|
|
if(!checkAttributeNumArgs(S, Attr, 0))
|
|
|
|
return;
|
2010-08-19 08:51:58 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
|
|
|
|
ValueDecl *VD = dyn_cast<ValueDecl>(D);
|
2009-12-15 11:11:10 +08:00
|
|
|
if (VD == 0 || (!VD->getType()->isBlockPointerType()
|
|
|
|
&& !VD->getType()->isFunctionPointerType())) {
|
2010-04-30 21:10:51 +08:00
|
|
|
S.Diag(Attr.getLoc(),
|
|
|
|
Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
|
2010-08-19 08:51:58 +08:00
|
|
|
: diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedFunctionMethodOrBlock;
|
2010-08-19 08:51:58 +08:00
|
|
|
return;
|
2009-04-30 03:03:13 +08:00
|
|
|
}
|
2008-06-27 02:38:35 +08:00
|
|
|
}
|
2010-08-19 08:51:58 +08:00
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
|
2008-06-27 02:38:35 +08:00
|
|
|
}
|
|
|
|
|
2010-08-10 05:53:52 +08:00
|
|
|
// PS3 PPU-specific.
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2010-08-10 05:53:52 +08:00
|
|
|
/*
|
|
|
|
Returning a Vector Class in Registers
|
|
|
|
|
2010-12-02 06:13:54 +08:00
|
|
|
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.
|
2010-08-10 05:53:52 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
*/
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<RecordDecl>(D)) {
|
2010-08-10 05:53:52 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedClass;
|
2010-08-10 05:53:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (D->getAttr<VecReturnAttr>()) {
|
2010-08-10 05:53:52 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
RecordDecl *record = cast<RecordDecl>(D);
|
2010-09-18 09:12:07 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-12-02 06:13:54 +08:00
|
|
|
for (RecordDecl::field_iterator iter = record->field_begin();
|
|
|
|
iter != record->field_end(); iter++) {
|
2010-09-18 09:12:07 +08:00
|
|
|
if ((count == 1) || !iter->getType()->isVectorType()) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
|
2010-08-10 05:53:52 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
|
2009-11-21 16:43:09 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedFunctionMethodOrParameter;
|
2009-11-21 16:43:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// FIXME: Actually store the attribute on the declaration
|
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2008-07-25 12:39:19 +08:00
|
|
|
// check the attribute arguments.
|
2011-04-15 13:49:29 +08:00
|
|
|
if (Attr.hasParameterOrArguments()) {
|
2008-11-19 16:23:25 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
|
2008-07-25 12:39:19 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
|
|
|
|
!isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedVariableFunctionOrLabel;
|
2008-07-25 12:39:19 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
|
2008-07-25 12:39:19 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2009-02-14 03:23:53 +08:00
|
|
|
// check the attribute arguments.
|
2011-04-15 13:49:29 +08:00
|
|
|
if (Attr.hasParameterOrArguments()) {
|
2009-02-14 03:23:53 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
|
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
2009-02-14 06:48:56 +08:00
|
|
|
if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
|
2009-02-14 03:23:53 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
|
|
|
|
return;
|
|
|
|
}
|
2011-07-02 07:49:12 +08:00
|
|
|
} else if (!isFunctionOrMethod(D)) {
|
2009-02-14 03:23:53 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedVariableOrFunction;
|
2009-02-14 03:23:53 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
|
2009-02-14 03:23:53 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2008-08-01 06:40:48 +08:00
|
|
|
// check the attribute arguments.
|
2011-03-02 20:15:05 +08:00
|
|
|
if (Attr.getNumArgs() > 1) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
|
2008-08-01 06:40:48 +08:00
|
|
|
return;
|
2009-07-25 03:02:52 +08:00
|
|
|
}
|
2008-08-01 06:40:48 +08:00
|
|
|
|
|
|
|
int priority = 65535; // FIXME: Do not hardcode such constants.
|
|
|
|
if (Attr.getNumArgs() > 0) {
|
2010-11-24 04:45:58 +08:00
|
|
|
Expr *E = Attr.getArg(0);
|
2008-08-01 06:40:48 +08:00
|
|
|
llvm::APSInt Idx(32);
|
2010-05-19 07:01:22 +08:00
|
|
|
if (E->isTypeDependent() || E->isValueDependent() ||
|
|
|
|
!E->isIntegerConstantExpr(Idx, S.Context)) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
|
2008-11-19 16:23:25 +08:00
|
|
|
<< "constructor" << 1 << E->getSourceRange();
|
2008-08-01 06:40:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
priority = Idx.getZExtValue();
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<FunctionDecl>(D)) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedFunction;
|
2008-08-01 06:40:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
|
2010-12-02 06:13:54 +08:00
|
|
|
priority));
|
2008-08-01 06:40:48 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2008-08-01 06:40:48 +08:00
|
|
|
// check the attribute arguments.
|
2011-03-02 20:15:05 +08:00
|
|
|
if (Attr.getNumArgs() > 1) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
|
2008-08-01 06:40:48 +08:00
|
|
|
return;
|
2009-07-25 03:02:52 +08:00
|
|
|
}
|
2008-08-01 06:40:48 +08:00
|
|
|
|
|
|
|
int priority = 65535; // FIXME: Do not hardcode such constants.
|
|
|
|
if (Attr.getNumArgs() > 0) {
|
2010-11-24 04:45:58 +08:00
|
|
|
Expr *E = Attr.getArg(0);
|
2008-08-01 06:40:48 +08:00
|
|
|
llvm::APSInt Idx(32);
|
2010-05-19 07:01:22 +08:00
|
|
|
if (E->isTypeDependent() || E->isValueDependent() ||
|
|
|
|
!E->isIntegerConstantExpr(Idx, S.Context)) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
|
2008-11-19 16:23:25 +08:00
|
|
|
<< "destructor" << 1 << E->getSourceRange();
|
2008-08-01 06:40:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
priority = Idx.getZExtValue();
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<FunctionDecl>(D)) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedFunction;
|
2008-08-01 06:40:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
|
2010-12-02 06:13:54 +08:00
|
|
|
priority));
|
2008-08-01 06:40:48 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2011-02-24 13:42:24 +08:00
|
|
|
unsigned NumArgs = Attr.getNumArgs();
|
|
|
|
if (NumArgs > 1) {
|
2011-03-02 20:15:05 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
|
2010-10-07 05:18:44 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-02-24 13:42:24 +08:00
|
|
|
|
2010-10-07 05:18:44 +08:00
|
|
|
// Handle the case where deprecated attribute has a text message.
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Str;
|
2011-02-24 13:42:24 +08:00
|
|
|
if (NumArgs == 1) {
|
|
|
|
StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
|
2010-10-07 05:18:44 +08:00
|
|
|
if (!SE) {
|
2011-02-24 13:42:24 +08:00
|
|
|
S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
|
|
|
|
<< "deprecated";
|
2010-10-07 05:18:44 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-02-24 13:42:24 +08:00
|
|
|
Str = SE->getString();
|
2008-06-27 02:38:35 +08:00
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getRange(), S.Context, Str));
|
2008-06-27 02:38:35 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2011-02-24 13:42:24 +08:00
|
|
|
unsigned NumArgs = Attr.getNumArgs();
|
|
|
|
if (NumArgs > 1) {
|
2011-03-02 20:15:05 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
|
2008-12-17 09:07:27 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-02-24 13:42:24 +08:00
|
|
|
|
2010-10-07 07:12:32 +08:00
|
|
|
// Handle the case where unavailable attribute has a text message.
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Str;
|
2011-02-24 13:42:24 +08:00
|
|
|
if (NumArgs == 1) {
|
|
|
|
StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
|
2010-10-07 07:12:32 +08:00
|
|
|
if (!SE) {
|
2011-02-24 13:42:24 +08:00
|
|
|
S.Diag(Attr.getArg(0)->getLocStart(),
|
2010-10-07 07:12:32 +08:00
|
|
|
diag::err_attribute_not_string) << "unavailable";
|
|
|
|
return;
|
|
|
|
}
|
2011-02-24 13:42:24 +08:00
|
|
|
Str = SE->getString();
|
2010-10-07 07:12:32 +08:00
|
|
|
}
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) UnavailableAttr(Attr.getRange(), S.Context, Str));
|
2008-12-17 09:07:27 +08:00
|
|
|
}
|
|
|
|
|
2011-07-07 03:24:05 +08:00
|
|
|
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(
|
2011-09-14 00:05:58 +08:00
|
|
|
Attr.getRange(), S.Context));
|
2011-07-07 03:24:05 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleAvailabilityAttr(Sema &S, Decl *D,
|
|
|
|
const AttributeList &Attr) {
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
IdentifierInfo *Platform = Attr.getParameterName();
|
|
|
|
SourceLocation PlatformLoc = Attr.getParameterLoc();
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef PlatformName
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
= AvailabilityAttr::getPrettyPlatformName(Platform->getName());
|
|
|
|
if (PlatformName.empty()) {
|
|
|
|
S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
|
|
|
|
<< Platform;
|
|
|
|
|
|
|
|
PlatformName = Platform->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
|
|
|
|
AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
|
|
|
|
AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
|
2011-03-26 11:35:55 +08:00
|
|
|
bool IsUnavailable = Attr.getUnavailableLoc().isValid();
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
|
2011-08-11 00:09:55 +08:00
|
|
|
// Ensure that Introduced <= Deprecated <= Obsoleted (although not all
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
// of these steps are needed).
|
|
|
|
if (Introduced.isValid() && Deprecated.isValid() &&
|
2011-08-10 23:31:35 +08:00
|
|
|
!(Introduced.Version <= Deprecated.Version)) {
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
|
|
|
|
<< 1 << PlatformName << Deprecated.Version.getAsString()
|
|
|
|
<< 0 << Introduced.Version.getAsString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Introduced.isValid() && Obsoleted.isValid() &&
|
2011-08-10 23:31:35 +08:00
|
|
|
!(Introduced.Version <= Obsoleted.Version)) {
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
|
|
|
|
<< 2 << PlatformName << Obsoleted.Version.getAsString()
|
|
|
|
<< 0 << Introduced.Version.getAsString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Deprecated.isValid() && Obsoleted.isValid() &&
|
2011-08-10 23:31:35 +08:00
|
|
|
!(Deprecated.Version <= Obsoleted.Version)) {
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
|
|
|
|
<< 2 << PlatformName << Obsoleted.Version.getAsString()
|
|
|
|
<< 1 << Deprecated.Version.getAsString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getRange(), S.Context,
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
Platform,
|
|
|
|
Introduced.Version,
|
|
|
|
Deprecated.Version,
|
2011-03-26 11:35:55 +08:00
|
|
|
Obsoleted.Version,
|
|
|
|
IsUnavailable));
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2008-06-27 02:38:35 +08:00
|
|
|
// check the attribute arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if(!checkAttributeNumArgs(S, Attr, 1))
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2010-11-24 04:45:58 +08:00
|
|
|
Expr *Arg = Attr.getArg(0);
|
2008-06-27 02:38:35 +08:00
|
|
|
Arg = Arg->IgnoreParenCasts();
|
|
|
|
StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-07-27 13:40:30 +08:00
|
|
|
if (!Str || !Str->isAscii()) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
|
2008-11-19 16:23:25 +08:00
|
|
|
<< "visibility" << 1;
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef TypeStr = Str->getString();
|
2010-08-19 07:23:40 +08:00
|
|
|
VisibilityAttr::VisibilityType type;
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2010-01-24 02:16:35 +08:00
|
|
|
if (TypeStr == "default")
|
2010-08-19 07:23:40 +08:00
|
|
|
type = VisibilityAttr::Default;
|
2010-01-24 02:16:35 +08:00
|
|
|
else if (TypeStr == "hidden")
|
2010-08-19 07:23:40 +08:00
|
|
|
type = VisibilityAttr::Hidden;
|
2010-01-24 02:16:35 +08:00
|
|
|
else if (TypeStr == "internal")
|
2010-08-19 07:23:40 +08:00
|
|
|
type = VisibilityAttr::Hidden; // FIXME
|
2010-01-24 02:16:35 +08:00
|
|
|
else if (TypeStr == "protected")
|
2010-08-19 07:23:40 +08:00
|
|
|
type = VisibilityAttr::Protected;
|
2008-06-27 02:38:35 +08:00
|
|
|
else {
|
2008-11-24 05:45:46 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) VisibilityAttr(Attr.getRange(), S.Context, type));
|
2008-06-27 02:38:35 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
|
|
|
|
const AttributeList &Attr) {
|
2011-03-02 19:33:24 +08:00
|
|
|
ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
|
|
|
|
if (!method) {
|
2011-07-02 07:49:12 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< ExpectedMethod;
|
2011-03-02 19:33:24 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
|
|
|
|
if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
|
2011-03-02 19:33:24 +08:00
|
|
|
<< "objc_method_family" << 1;
|
|
|
|
} else {
|
2011-07-02 07:49:12 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
|
2011-03-02 19:33:24 +08:00
|
|
|
}
|
2011-07-02 07:49:12 +08:00
|
|
|
Attr.setInvalid();
|
2011-03-02 19:33:24 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef param = Attr.getParameterName()->getName();
|
2011-03-02 19:33:24 +08:00
|
|
|
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.
|
2011-07-02 07:49:12 +08:00
|
|
|
S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
|
2011-03-02 19:33:24 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
|
2011-06-16 07:02:42 +08:00
|
|
|
S.Context, family));
|
2011-03-02 19:33:24 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleObjCExceptionAttr(Sema &S, Decl *D,
|
|
|
|
const AttributeList &Attr) {
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
2009-02-14 16:09:34 +08:00
|
|
|
return;
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2009-02-14 16:09:34 +08:00
|
|
|
ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
|
|
|
|
if (OCI == 0) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
|
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
|
2009-02-14 16:09:34 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
|
2009-01-14 07:34:40 +08:00
|
|
|
if (Attr.getNumArgs() != 0) {
|
2010-05-29 02:25:28 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
|
2009-01-14 07:34:40 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-04-15 22:24:37 +08:00
|
|
|
if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
|
2009-01-14 07:34:40 +08:00
|
|
|
QualType T = TD->getUnderlyingType();
|
|
|
|
if (!T->isPointerType() ||
|
2009-07-30 05:53:49 +08:00
|
|
|
!T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
|
2009-01-14 07:34:40 +08:00
|
|
|
S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
|
2009-01-14 07:34:40 +08:00
|
|
|
}
|
|
|
|
|
2009-07-25 03:02:52 +08:00
|
|
|
static void
|
2011-07-02 08:01:44 +08:00
|
|
|
handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
Initial implementation of function overloading in C.
This commit adds a new attribute, "overloadable", that enables C++
function overloading in C. The attribute can only be added to function
declarations, e.g.,
int *f(int) __attribute__((overloadable));
If the "overloadable" attribute exists on a function with a given
name, *all* functions with that name (and in that scope) must have the
"overloadable" attribute. Sets of overloaded functions with the
"overloadable" attribute then follow the normal C++ rules for
overloaded functions, e.g., overloads must have different
parameter-type-lists from each other.
When calling an overloaded function in C, we follow the same
overloading rules as C++, with three extensions to the set of standard
conversions:
- A value of a given struct or union type T can be converted to the
type T. This is just the identity conversion. (In C++, this would
go through a copy constructor).
- A value of pointer type T* can be converted to a value of type U*
if T and U are compatible types. This conversion has Conversion
rank (it's considered a pointer conversion in C).
- A value of type T can be converted to a value of type U if T and U
are compatible (and are not both pointer types). This conversion
has Conversion rank (it's considered to be a new kind of
conversion unique to C, a "compatible" conversion).
Known defects (and, therefore, next steps):
1) The standard-conversion handling does not understand conversions
involving _Complex or vector extensions, so it is likely to get
these wrong. We need to add these conversions.
2) All overloadable functions with the same name will have the same
linkage name, which means we'll get a collision in the linker (if
not sooner). We'll need to mangle the names of these functions.
llvm-svn: 64336
2009-02-12 07:02:49 +08:00
|
|
|
if (Attr.getNumArgs() != 0) {
|
2010-05-29 02:25:28 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
|
Initial implementation of function overloading in C.
This commit adds a new attribute, "overloadable", that enables C++
function overloading in C. The attribute can only be added to function
declarations, e.g.,
int *f(int) __attribute__((overloadable));
If the "overloadable" attribute exists on a function with a given
name, *all* functions with that name (and in that scope) must have the
"overloadable" attribute. Sets of overloaded functions with the
"overloadable" attribute then follow the normal C++ rules for
overloaded functions, e.g., overloads must have different
parameter-type-lists from each other.
When calling an overloaded function in C, we follow the same
overloading rules as C++, with three extensions to the set of standard
conversions:
- A value of a given struct or union type T can be converted to the
type T. This is just the identity conversion. (In C++, this would
go through a copy constructor).
- A value of pointer type T* can be converted to a value of type U*
if T and U are compatible types. This conversion has Conversion
rank (it's considered a pointer conversion in C).
- A value of type T can be converted to a value of type U if T and U
are compatible (and are not both pointer types). This conversion
has Conversion rank (it's considered to be a new kind of
conversion unique to C, a "compatible" conversion).
Known defects (and, therefore, next steps):
1) The standard-conversion handling does not understand conversions
involving _Complex or vector extensions, so it is likely to get
these wrong. We need to add these conversions.
2) All overloadable functions with the same name will have the same
linkage name, which means we'll get a collision in the linker (if
not sooner). We'll need to mangle the names of these functions.
llvm-svn: 64336
2009-02-12 07:02:49 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isa<FunctionDecl>(D)) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
|
Initial implementation of function overloading in C.
This commit adds a new attribute, "overloadable", that enables C++
function overloading in C. The attribute can only be added to function
declarations, e.g.,
int *f(int) __attribute__((overloadable));
If the "overloadable" attribute exists on a function with a given
name, *all* functions with that name (and in that scope) must have the
"overloadable" attribute. Sets of overloaded functions with the
"overloadable" attribute then follow the normal C++ rules for
overloaded functions, e.g., overloads must have different
parameter-type-lists from each other.
When calling an overloaded function in C, we follow the same
overloading rules as C++, with three extensions to the set of standard
conversions:
- A value of a given struct or union type T can be converted to the
type T. This is just the identity conversion. (In C++, this would
go through a copy constructor).
- A value of pointer type T* can be converted to a value of type U*
if T and U are compatible types. This conversion has Conversion
rank (it's considered a pointer conversion in C).
- A value of type T can be converted to a value of type U if T and U
are compatible (and are not both pointer types). This conversion
has Conversion rank (it's considered to be a new kind of
conversion unique to C, a "compatible" conversion).
Known defects (and, therefore, next steps):
1) The standard-conversion handling does not understand conversions
involving _Complex or vector extensions, so it is likely to get
these wrong. We need to add these conversions.
2) All overloadable functions with the same name will have the same
linkage name, which means we'll get a collision in the linker (if
not sooner). We'll need to mangle the names of these functions.
llvm-svn: 64336
2009-02-12 07:02:49 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2009-07-25 03:02:52 +08:00
|
|
|
if (!Attr.getParameterName()) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
|
2008-11-19 16:23:25 +08:00
|
|
|
<< "blocks" << 1;
|
2008-09-19 00:44:58 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2008-09-19 00:44:58 +08:00
|
|
|
if (Attr.getNumArgs() != 0) {
|
2008-11-19 16:23:25 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
|
2008-09-19 00:44:58 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2010-08-19 07:23:40 +08:00
|
|
|
BlocksAttr::BlockType type;
|
2008-11-20 12:42:34 +08:00
|
|
|
if (Attr.getParameterName()->isStr("byref"))
|
2008-09-19 00:44:58 +08:00
|
|
|
type = BlocksAttr::ByRef;
|
|
|
|
else {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
|
2008-11-19 16:23:25 +08:00
|
|
|
<< "blocks" << Attr.getParameterName();
|
2008-09-19 00:44:58 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
|
2008-09-19 00:44:58 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2008-10-06 02:05:59 +08:00
|
|
|
// check the attribute arguments.
|
|
|
|
if (Attr.getNumArgs() > 2) {
|
2011-03-02 20:15:05 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
|
2008-10-06 02:05:59 +08:00
|
|
|
return;
|
2009-07-25 03:02:52 +08:00
|
|
|
}
|
|
|
|
|
2011-09-09 15:56:05 +08:00
|
|
|
unsigned sentinel = 0;
|
2008-10-06 02:05:59 +08:00
|
|
|
if (Attr.getNumArgs() > 0) {
|
2010-11-24 04:45:58 +08:00
|
|
|
Expr *E = Attr.getArg(0);
|
2008-10-06 02:05:59 +08:00
|
|
|
llvm::APSInt Idx(32);
|
2010-05-19 07:01:22 +08:00
|
|
|
if (E->isTypeDependent() || E->isValueDependent() ||
|
|
|
|
!E->isIntegerConstantExpr(Idx, S.Context)) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
|
2008-11-19 16:23:25 +08:00
|
|
|
<< "sentinel" << 1 << E->getSourceRange();
|
2008-10-06 02:05:59 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-09-09 15:56:05 +08:00
|
|
|
if (Idx.isSigned() && Idx.isNegative()) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
|
|
|
|
<< E->getSourceRange();
|
2008-10-06 02:05:59 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-09-09 15:56:05 +08:00
|
|
|
|
|
|
|
sentinel = Idx.getZExtValue();
|
2008-10-06 02:05:59 +08:00
|
|
|
}
|
|
|
|
|
2011-09-09 15:56:05 +08:00
|
|
|
unsigned nullPos = 0;
|
2008-10-06 02:05:59 +08:00
|
|
|
if (Attr.getNumArgs() > 1) {
|
2010-11-24 04:45:58 +08:00
|
|
|
Expr *E = Attr.getArg(1);
|
2008-10-06 02:05:59 +08:00
|
|
|
llvm::APSInt Idx(32);
|
2010-05-19 07:01:22 +08:00
|
|
|
if (E->isTypeDependent() || E->isValueDependent() ||
|
|
|
|
!E->isIntegerConstantExpr(Idx, S.Context)) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
|
2008-11-19 16:23:25 +08:00
|
|
|
<< "sentinel" << 2 << E->getSourceRange();
|
2008-10-06 02:05:59 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
nullPos = Idx.getZExtValue();
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-09-09 15:56:05 +08:00
|
|
|
if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
|
2008-10-06 02:05:59 +08:00
|
|
|
// FIXME: This error message could be improved, it would be nice
|
|
|
|
// to say what the bounds actually are.
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
|
|
|
|
<< E->getSourceRange();
|
2008-10-06 02:05:59 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
2011-09-09 15:56:05 +08:00
|
|
|
const FunctionType *FT = FD->getType()->castAs<FunctionType>();
|
2009-03-18 07:03:47 +08:00
|
|
|
if (isa<FunctionNoProtoType>(FT)) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
|
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2009-03-18 07:03:47 +08:00
|
|
|
if (!cast<FunctionProtoType>(FT)->isVariadic()) {
|
2009-05-16 05:18:04 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
|
2008-10-06 02:05:59 +08:00
|
|
|
return;
|
2009-07-25 03:02:52 +08:00
|
|
|
}
|
2011-07-02 07:49:12 +08:00
|
|
|
} else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
|
2008-10-06 02:05:59 +08:00
|
|
|
if (!MD->isVariadic()) {
|
2009-05-16 05:18:04 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
|
2008-10-06 02:05:59 +08:00
|
|
|
return;
|
2009-05-15 04:53:39 +08:00
|
|
|
}
|
2011-07-02 07:49:12 +08:00
|
|
|
} else if (isa<BlockDecl>(D)) {
|
2009-07-25 03:02:52 +08:00
|
|
|
// Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
|
|
|
|
// caller.
|
2009-05-15 04:53:39 +08:00
|
|
|
;
|
2011-07-02 07:49:12 +08:00
|
|
|
} else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
|
2009-05-15 04:53:39 +08:00
|
|
|
QualType Ty = V->getType();
|
2009-05-16 04:33:25 +08:00
|
|
|
if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
|
2011-07-02 07:49:12 +08:00
|
|
|
const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
|
2010-12-02 06:13:54 +08:00
|
|
|
: Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
|
2009-05-15 04:53:39 +08:00
|
|
|
if (!cast<FunctionProtoType>(FT)->isVariadic()) {
|
2009-05-16 05:18:04 +08:00
|
|
|
int m = Ty->isFunctionPointerType() ? 0 : 1;
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
|
2009-05-15 04:53:39 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-08-05 05:02:39 +08:00
|
|
|
} else {
|
2009-05-15 04:53:39 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedFunctionMethodOrBlock;
|
2009-05-15 04:53:39 +08:00
|
|
|
return;
|
|
|
|
}
|
2008-10-06 02:05:59 +08:00
|
|
|
} else {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedFunctionMethodOrBlock;
|
2008-10-06 02:05:59 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
|
2010-12-02 06:13:54 +08:00
|
|
|
nullPos));
|
2008-10-06 02:05:59 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
|
2009-02-14 15:37:35 +08:00
|
|
|
// check the attribute arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
2009-02-14 15:37:35 +08:00
|
|
|
return;
|
|
|
|
|
2010-03-31 02:22:15 +08:00
|
|
|
if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
|
2009-02-14 15:37:35 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedFunctionOrMethod;
|
2009-02-14 15:37:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2010-03-31 02:22:15 +08:00
|
|
|
if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
|
|
|
|
<< Attr.getName() << 0;
|
2009-12-23 07:59:52 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-03-31 02:22:15 +08:00
|
|
|
if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
|
|
|
|
if (MD->getResultType()->isVoidType()) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
|
|
|
|
<< Attr.getName() << 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
|
2009-02-14 15:37:35 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2008-06-27 02:38:35 +08:00
|
|
|
// check the attribute arguments.
|
2011-07-02 07:49:12 +08:00
|
|
|
if (Attr.hasParameterOrArguments()) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-03-06 14:39:57 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
|
|
|
<< Attr.getName() << ExpectedVariableOrFunction;
|
2009-07-16 09:12:24 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
NamedDecl *nd = cast<NamedDecl>(D);
|
2011-02-09 06:35:49 +08:00
|
|
|
|
|
|
|
// 'weak' only applies to declarations with external linkage.
|
|
|
|
if (hasEffectivelyInternalLinkage(nd)) {
|
2011-07-02 07:49:12 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
|
2009-03-06 14:39:57 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
|
2008-06-27 02:38:35 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2009-03-06 14:39:57 +08:00
|
|
|
// check the attribute arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
2009-03-06 14:39:57 +08:00
|
|
|
return;
|
2011-07-12 07:30:35 +08:00
|
|
|
|
2009-03-06 14:39:57 +08:00
|
|
|
|
|
|
|
// weak_import only applies to variable & function declarations.
|
|
|
|
bool isDef = false;
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
if (!D->canBeWeakImported(isDef)) {
|
|
|
|
if (isDef)
|
|
|
|
S.Diag(Attr.getLoc(),
|
|
|
|
diag::warn_attribute_weak_import_invalid_on_definition)
|
|
|
|
<< "weak_import" << 2 /*variable and function*/;
|
2011-03-23 21:27:51 +08:00
|
|
|
else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
|
2011-09-02 08:18:52 +08:00
|
|
|
(S.Context.getTargetInfo().getTriple().isOSDarwin() &&
|
2011-03-23 21:27:51 +08:00
|
|
|
isa<ObjCInterfaceDecl>(D))) {
|
|
|
|
// Nothing to warn about here.
|
|
|
|
} else
|
2010-04-14 04:22:35 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedVariableOrFunction;
|
2009-03-06 14:39:57 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
|
2009-03-06 14:39:57 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleReqdWorkGroupSize(Sema &S, Decl *D,
|
|
|
|
const AttributeList &Attr) {
|
2009-06-26 14:32:41 +08:00
|
|
|
// Attribute has 3 arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 3))
|
2009-06-26 14:32:41 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned WGSize[3];
|
|
|
|
for (unsigned i = 0; i < 3; ++i) {
|
2010-11-24 04:45:58 +08:00
|
|
|
Expr *E = Attr.getArg(i);
|
2009-06-26 14:32:41 +08:00
|
|
|
llvm::APSInt ArgNum(32);
|
2010-05-19 07:01:22 +08:00
|
|
|
if (E->isTypeDependent() || E->isValueDependent() ||
|
|
|
|
!E->isIntegerConstantExpr(ArgNum, S.Context)) {
|
2009-06-26 14:32:41 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
|
|
|
|
<< "reqd_work_group_size" << E->getSourceRange();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
WGSize[i] = (unsigned) ArgNum.getZExtValue();
|
|
|
|
}
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
|
2010-08-19 07:23:40 +08:00
|
|
|
WGSize[0], WGSize[1],
|
2009-06-26 14:32:41 +08:00
|
|
|
WGSize[2]));
|
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2009-02-13 01:28:23 +08:00
|
|
|
// Attribute has no arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 1))
|
2009-02-13 01:28:23 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Make sure that there is a string literal as the sections's single
|
|
|
|
// argument.
|
2010-11-24 04:45:58 +08:00
|
|
|
Expr *ArgExpr = Attr.getArg(0);
|
2009-08-11 03:03:04 +08:00
|
|
|
StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
|
2009-02-13 01:28:23 +08:00
|
|
|
if (!SE) {
|
2009-08-11 03:03:04 +08:00
|
|
|
S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
|
2009-02-13 01:28:23 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-11 03:03:04 +08:00
|
|
|
// If the target wants to validate the section specifier, make it happen.
|
2011-09-02 08:18:52 +08:00
|
|
|
std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
|
2010-01-13 04:58:53 +08:00
|
|
|
if (!Error.empty()) {
|
|
|
|
S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
|
|
|
|
<< Error;
|
2009-08-11 03:03:04 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-01-13 04:58:53 +08:00
|
|
|
// This attribute cannot be applied to local variables.
|
|
|
|
if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
|
|
|
|
S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) SectionAttr(Attr.getRange(), S.Context,
|
2010-12-02 06:13:54 +08:00
|
|
|
SE->getString()));
|
2009-02-13 01:28:23 +08:00
|
|
|
}
|
|
|
|
|
2008-06-27 02:38:35 +08:00
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2008-06-27 02:38:35 +08:00
|
|
|
// check the attribute arguments.
|
2011-04-15 13:49:29 +08:00
|
|
|
if (Attr.hasParameterOrArguments()) {
|
2008-11-19 16:23:25 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-06-15 13:45:11 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
|
2011-06-15 13:45:11 +08:00
|
|
|
if (Existing->getLocation().isInvalid())
|
2011-09-14 00:05:53 +08:00
|
|
|
Existing->setRange(Attr.getRange());
|
2011-06-15 13:45:11 +08:00
|
|
|
} else {
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
|
2011-06-15 13:45:11 +08:00
|
|
|
}
|
2008-06-27 02:38:35 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2008-10-06 07:32:53 +08:00
|
|
|
// check the attribute arguments.
|
2011-04-15 13:49:29 +08:00
|
|
|
if (Attr.hasParameterOrArguments()) {
|
2008-11-19 16:23:25 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
|
2008-10-06 07:32:53 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
|
2011-06-15 13:45:11 +08:00
|
|
|
if (Existing->getLocation().isInvalid())
|
2011-09-14 00:05:53 +08:00
|
|
|
Existing->setRange(Attr.getRange());
|
2011-06-15 13:45:11 +08:00
|
|
|
} else {
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
|
2011-06-15 13:45:11 +08:00
|
|
|
}
|
2008-10-06 07:32:53 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2008-10-06 07:32:53 +08:00
|
|
|
// check the attribute arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
2008-10-06 07:32:53 +08:00
|
|
|
return;
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
|
2008-10-06 07:32:53 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2009-07-25 03:02:52 +08:00
|
|
|
if (!Attr.getParameterName()) {
|
2009-01-31 09:16:18 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
|
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2009-01-31 09:16:18 +08:00
|
|
|
if (Attr.getNumArgs() != 0) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
|
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
VarDecl *VD = dyn_cast<VarDecl>(D);
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2009-01-31 09:16:18 +08:00
|
|
|
if (!VD || !VD->hasLocalStorage()) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
|
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2009-01-31 09:16:18 +08:00
|
|
|
// Look up the function
|
2010-04-16 06:33:43 +08:00
|
|
|
// FIXME: Lookup probably isn't looking in the right place
|
2009-10-10 05:13:30 +08:00
|
|
|
NamedDecl *CleanupDecl
|
2010-12-07 01:51:50 +08:00
|
|
|
= S.LookupSingleName(S.TUScope, Attr.getParameterName(),
|
|
|
|
Attr.getParameterLoc(), Sema::LookupOrdinaryName);
|
2009-01-31 09:16:18 +08:00
|
|
|
if (!CleanupDecl) {
|
2010-12-07 01:51:50 +08:00
|
|
|
S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
|
2009-01-31 09:16:18 +08:00
|
|
|
Attr.getParameterName();
|
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2009-01-31 09:16:18 +08:00
|
|
|
FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
|
|
|
|
if (!FD) {
|
2010-12-07 01:51:50 +08:00
|
|
|
S.Diag(Attr.getParameterLoc(),
|
|
|
|
diag::err_attribute_cleanup_arg_not_function)
|
|
|
|
<< Attr.getParameterName();
|
2009-01-31 09:16:18 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD->getNumParams() != 1) {
|
2010-12-07 01:51:50 +08:00
|
|
|
S.Diag(Attr.getParameterLoc(),
|
|
|
|
diag::err_attribute_cleanup_func_must_take_one_arg)
|
|
|
|
<< Attr.getParameterName();
|
2009-01-31 09:16:18 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2009-02-08 07:16:50 +08:00
|
|
|
// We're currently more strict than GCC about what function types we accept.
|
|
|
|
// If this ever proves to be a problem it should be easy to fix.
|
|
|
|
QualType Ty = S.Context.getPointerType(VD->getType());
|
|
|
|
QualType ParamTy = FD->getParamDecl(0)->getType();
|
2011-01-28 10:26:04 +08:00
|
|
|
if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
|
|
|
|
ParamTy, Ty) != Sema::Compatible) {
|
2010-12-07 01:51:50 +08:00
|
|
|
S.Diag(Attr.getParameterLoc(),
|
2009-02-08 07:16:50 +08:00
|
|
|
diag::err_attribute_cleanup_func_arg_incompatible_type) <<
|
|
|
|
Attr.getParameterName() << ParamTy << Ty;
|
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
|
2010-12-07 01:51:53 +08:00
|
|
|
S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
|
2009-01-31 09:16:18 +08:00
|
|
|
}
|
|
|
|
|
2009-07-25 03:02:52 +08:00
|
|
|
/// Handle __attribute__((format_arg((idx)))) attribute based on
|
|
|
|
/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 1))
|
2009-05-21 01:41:43 +08:00
|
|
|
return;
|
2011-07-12 07:30:35 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
|
2009-05-21 01:41:43 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedFunction;
|
2009-05-21 01:41:43 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-11-16 16:35:43 +08:00
|
|
|
|
|
|
|
// In C++ the implicit 'this' function parameter also counts, and they are
|
|
|
|
// counted from one.
|
2011-07-02 07:49:12 +08:00
|
|
|
bool HasImplicitThisParam = isInstanceMethod(D);
|
|
|
|
unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
|
2009-05-21 01:41:43 +08:00
|
|
|
unsigned FirstIdx = 1;
|
2010-11-16 16:35:43 +08:00
|
|
|
|
2009-05-21 01:41:43 +08:00
|
|
|
// checks for the 2nd argument
|
2010-11-24 04:45:58 +08:00
|
|
|
Expr *IdxExpr = Attr.getArg(0);
|
2009-05-21 01:41:43 +08:00
|
|
|
llvm::APSInt Idx(32);
|
2010-05-19 07:01:22 +08:00
|
|
|
if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
|
|
|
|
!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
|
2009-05-21 01:41:43 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
|
|
|
|
<< "format" << 2 << IdxExpr->getSourceRange();
|
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2009-05-21 01:41:43 +08:00
|
|
|
if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
|
|
|
|
<< "format" << 2 << IdxExpr->getSourceRange();
|
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2009-05-21 01:41:43 +08:00
|
|
|
unsigned ArgIdx = Idx.getZExtValue() - 1;
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2010-11-16 16:35:43 +08:00
|
|
|
if (HasImplicitThisParam) {
|
|
|
|
if (ArgIdx == 0) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
|
|
|
|
<< "format_arg" << IdxExpr->getSourceRange();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ArgIdx--;
|
|
|
|
}
|
|
|
|
|
2009-05-21 01:41:43 +08:00
|
|
|
// make sure the format string is really a string
|
2011-07-02 07:49:12 +08:00
|
|
|
QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2009-05-21 01:41:43 +08:00
|
|
|
bool not_nsstring_type = !isNSStringType(Ty, S.Context);
|
|
|
|
if (not_nsstring_type &&
|
|
|
|
!isCFStringType(Ty, S.Context) &&
|
|
|
|
(!Ty->isPointerType() ||
|
2009-07-30 05:53:49 +08:00
|
|
|
!Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
|
2009-05-21 01:41:43 +08:00
|
|
|
// FIXME: Should highlight the actual expression that has the wrong type.
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
|
2009-07-25 03:02:52 +08:00
|
|
|
<< (not_nsstring_type ? "a string type" : "an NSString")
|
2009-05-21 01:41:43 +08:00
|
|
|
<< IdxExpr->getSourceRange();
|
|
|
|
return;
|
2009-07-25 03:02:52 +08:00
|
|
|
}
|
2011-07-02 07:49:12 +08:00
|
|
|
Ty = getFunctionOrMethodResultType(D);
|
2009-05-21 01:41:43 +08:00
|
|
|
if (!isNSStringType(Ty, S.Context) &&
|
|
|
|
!isCFStringType(Ty, S.Context) &&
|
|
|
|
(!Ty->isPointerType() ||
|
2009-07-30 05:53:49 +08:00
|
|
|
!Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
|
2009-05-21 01:41:43 +08:00
|
|
|
// FIXME: Should highlight the actual expression that has the wrong type.
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
|
2009-07-25 03:02:52 +08:00
|
|
|
<< (not_nsstring_type ? "string type" : "NSString")
|
2009-05-21 01:41:43 +08:00
|
|
|
<< IdxExpr->getSourceRange();
|
|
|
|
return;
|
2009-07-25 03:02:52 +08:00
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
|
2010-11-16 16:35:43 +08:00
|
|
|
Idx.getZExtValue()));
|
2009-05-21 01:41:43 +08:00
|
|
|
}
|
|
|
|
|
2009-10-18 10:09:17 +08:00
|
|
|
enum FormatAttrKind {
|
|
|
|
CFStringFormat,
|
|
|
|
NSStringFormat,
|
|
|
|
StrftimeFormat,
|
|
|
|
SupportedFormat,
|
2010-03-23 05:08:50 +08:00
|
|
|
IgnoredFormat,
|
2009-10-18 10:09:17 +08:00
|
|
|
InvalidFormat
|
|
|
|
};
|
|
|
|
|
|
|
|
/// getFormatAttrKind - Map from format attribute names to supported format
|
|
|
|
/// types.
|
2011-07-23 18:55:15 +08:00
|
|
|
static FormatAttrKind getFormatAttrKind(StringRef Format) {
|
2009-10-18 10:09:17 +08:00
|
|
|
// Check for formats that get handled specially.
|
|
|
|
if (Format == "NSString")
|
|
|
|
return NSStringFormat;
|
|
|
|
if (Format == "CFString")
|
|
|
|
return CFStringFormat;
|
|
|
|
if (Format == "strftime")
|
|
|
|
return StrftimeFormat;
|
|
|
|
|
|
|
|
// Otherwise, check for supported formats.
|
|
|
|
if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
|
|
|
|
Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
|
|
|
|
Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
|
2011-02-19 01:05:55 +08:00
|
|
|
Format == "zcmn_err" ||
|
|
|
|
Format == "kprintf") // OpenBSD.
|
2009-10-18 10:09:17 +08:00
|
|
|
return SupportedFormat;
|
|
|
|
|
2010-03-23 22:44:19 +08:00
|
|
|
if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
|
|
|
|
Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
|
2010-03-23 05:08:50 +08:00
|
|
|
return IgnoredFormat;
|
|
|
|
|
2009-10-18 10:09:17 +08:00
|
|
|
return InvalidFormat;
|
|
|
|
}
|
|
|
|
|
2010-06-19 05:44:06 +08:00
|
|
|
/// Handle __attribute__((init_priority(priority))) attributes based on
|
|
|
|
/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleInitPriorityAttr(Sema &S, Decl *D,
|
|
|
|
const AttributeList &Attr) {
|
2010-06-19 05:44:06 +08:00
|
|
|
if (!S.getLangOptions().CPlusPlus) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
|
2010-06-19 07:14:53 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
|
|
|
|
Attr.setInvalid();
|
|
|
|
return;
|
|
|
|
}
|
2011-07-02 07:49:12 +08:00
|
|
|
QualType T = dyn_cast<VarDecl>(D)->getType();
|
2010-06-19 07:14:53 +08:00
|
|
|
if (S.Context.getAsArrayType(T))
|
|
|
|
T = S.Context.getBaseElementType(T);
|
|
|
|
if (!T->getAs<RecordType>()) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
|
|
|
|
Attr.setInvalid();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-19 05:44:06 +08:00
|
|
|
if (Attr.getNumArgs() != 1) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
|
|
|
|
Attr.setInvalid();
|
|
|
|
return;
|
|
|
|
}
|
2010-11-24 04:45:58 +08:00
|
|
|
Expr *priorityExpr = Attr.getArg(0);
|
2010-06-19 07:14:53 +08:00
|
|
|
|
2010-06-19 05:44:06 +08:00
|
|
|
llvm::APSInt priority(32);
|
|
|
|
if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
|
|
|
|
!priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
|
|
|
|
<< "init_priority" << priorityExpr->getSourceRange();
|
|
|
|
Attr.setInvalid();
|
|
|
|
return;
|
|
|
|
}
|
2010-06-22 02:45:05 +08:00
|
|
|
unsigned prioritynum = priority.getZExtValue();
|
2010-06-19 05:44:06 +08:00
|
|
|
if (prioritynum < 101 || prioritynum > 65535) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
|
|
|
|
<< priorityExpr->getSourceRange();
|
|
|
|
Attr.setInvalid();
|
|
|
|
return;
|
|
|
|
}
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
|
2010-12-02 06:13:54 +08:00
|
|
|
prioritynum));
|
2010-06-19 05:44:06 +08:00
|
|
|
}
|
|
|
|
|
2009-07-25 03:02:52 +08:00
|
|
|
/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
|
|
|
|
/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2008-06-27 02:38:35 +08:00
|
|
|
|
2008-06-29 07:36:30 +08:00
|
|
|
if (!Attr.getParameterName()) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
|
2008-11-19 16:23:25 +08:00
|
|
|
<< "format" << 1;
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-06-29 07:36:30 +08:00
|
|
|
if (Attr.getNumArgs() != 2) {
|
2008-11-19 16:23:25 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedFunction;
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-16 16:35:43 +08:00
|
|
|
// In C++ the implicit 'this' function parameter also counts, and they are
|
|
|
|
// counted from one.
|
2011-07-02 07:49:12 +08:00
|
|
|
bool HasImplicitThisParam = isInstanceMethod(D);
|
|
|
|
unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
|
2008-06-27 02:38:35 +08:00
|
|
|
unsigned FirstIdx = 1;
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Format = Attr.getParameterName()->getName();
|
2008-06-27 02:38:35 +08:00
|
|
|
|
|
|
|
// Normalize the argument, __foo__ becomes foo.
|
2009-10-18 10:09:17 +08:00
|
|
|
if (Format.startswith("__") && Format.endswith("__"))
|
|
|
|
Format = Format.substr(2, Format.size() - 4);
|
|
|
|
|
|
|
|
// Check for supported formats.
|
|
|
|
FormatAttrKind Kind = getFormatAttrKind(Format);
|
2010-03-23 05:08:50 +08:00
|
|
|
|
|
|
|
if (Kind == IgnoredFormat)
|
|
|
|
return;
|
|
|
|
|
2009-10-18 10:09:17 +08:00
|
|
|
if (Kind == InvalidFormat) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
|
2009-10-19 05:17:35 +08:00
|
|
|
<< "format" << Attr.getParameterName()->getName();
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// checks for the 2nd argument
|
2010-11-24 04:45:58 +08:00
|
|
|
Expr *IdxExpr = Attr.getArg(0);
|
2008-06-29 08:43:07 +08:00
|
|
|
llvm::APSInt Idx(32);
|
2010-05-19 07:01:22 +08:00
|
|
|
if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
|
|
|
|
!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
|
2008-11-19 16:23:25 +08:00
|
|
|
<< "format" << 2 << IdxExpr->getSourceRange();
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
|
2008-11-19 16:23:25 +08:00
|
|
|
<< "format" << 2 << IdxExpr->getSourceRange();
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Do we need to bounds check?
|
|
|
|
unsigned ArgIdx = Idx.getZExtValue() - 1;
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2009-11-18 02:02:24 +08:00
|
|
|
if (HasImplicitThisParam) {
|
|
|
|
if (ArgIdx == 0) {
|
2010-11-16 16:35:43 +08:00
|
|
|
S.Diag(Attr.getLoc(),
|
|
|
|
diag::err_format_attribute_implicit_this_format_string)
|
|
|
|
<< IdxExpr->getSourceRange();
|
2009-11-18 02:02:24 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ArgIdx--;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-27 02:38:35 +08:00
|
|
|
// make sure the format string is really a string
|
2011-07-02 07:49:12 +08:00
|
|
|
QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
|
2008-06-27 02:38:35 +08:00
|
|
|
|
2009-10-18 10:09:17 +08:00
|
|
|
if (Kind == CFStringFormat) {
|
2008-09-26 11:32:58 +08:00
|
|
|
if (!isCFStringType(Ty, S.Context)) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
|
|
|
|
<< "a CFString" << IdxExpr->getSourceRange();
|
2008-09-26 11:32:58 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-10-18 10:09:17 +08:00
|
|
|
} else if (Kind == NSStringFormat) {
|
2009-05-16 15:39:55 +08:00
|
|
|
// FIXME: do we need to check if the type is NSString*? What are the
|
|
|
|
// semantics?
|
2008-06-29 08:43:07 +08:00
|
|
|
if (!isNSStringType(Ty, S.Context)) {
|
2009-05-16 15:39:55 +08:00
|
|
|
// FIXME: Should highlight the actual expression that has the wrong type.
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
|
|
|
|
<< "an NSString" << IdxExpr->getSourceRange();
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
2009-07-25 03:02:52 +08:00
|
|
|
}
|
2008-06-27 02:38:35 +08:00
|
|
|
} else if (!Ty->isPointerType() ||
|
2009-07-30 05:53:49 +08:00
|
|
|
!Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
|
2009-05-16 15:39:55 +08:00
|
|
|
// FIXME: Should highlight the actual expression that has the wrong type.
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
|
|
|
|
<< "a string type" << IdxExpr->getSourceRange();
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the 3rd argument
|
2010-11-24 04:45:58 +08:00
|
|
|
Expr *FirstArgExpr = Attr.getArg(1);
|
2008-06-29 08:43:07 +08:00
|
|
|
llvm::APSInt FirstArg(32);
|
2010-05-19 07:01:22 +08:00
|
|
|
if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
|
|
|
|
!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
|
2008-11-19 16:23:25 +08:00
|
|
|
<< "format" << 3 << FirstArgExpr->getSourceRange();
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the function is variadic if the 3rd argument non-zero
|
|
|
|
if (FirstArg != 0) {
|
2011-07-02 07:49:12 +08:00
|
|
|
if (isFunctionOrMethodVariadic(D)) {
|
2008-06-27 02:38:35 +08:00
|
|
|
++NumArgs; // +1 for ...
|
|
|
|
} else {
|
2011-07-02 07:49:12 +08:00
|
|
|
S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-19 16:23:25 +08:00
|
|
|
// strftime requires FirstArg to be 0 because it doesn't read from any
|
|
|
|
// variable the input is just the current time + the format string.
|
2009-10-18 10:09:17 +08:00
|
|
|
if (Kind == StrftimeFormat) {
|
2008-06-27 02:38:35 +08:00
|
|
|
if (FirstArg != 0) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
|
|
|
|
<< FirstArgExpr->getSourceRange();
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// if 0 it disables parameter checking (to use with e.g. va_list)
|
|
|
|
} else if (FirstArg != 0 && FirstArg != NumArgs) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
|
2008-11-19 16:23:25 +08:00
|
|
|
<< "format" << 3 << FirstArgExpr->getSourceRange();
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-15 13:45:11 +08:00
|
|
|
// Check whether we already have an equivalent format attribute.
|
|
|
|
for (specific_attr_iterator<FormatAttr>
|
2011-07-02 07:49:12 +08:00
|
|
|
i = D->specific_attr_begin<FormatAttr>(),
|
|
|
|
e = D->specific_attr_end<FormatAttr>();
|
2011-06-15 13:45:11 +08:00
|
|
|
i != e ; ++i) {
|
|
|
|
FormatAttr *f = *i;
|
|
|
|
if (f->getType() == Format &&
|
|
|
|
f->getFormatIdx() == (int)Idx.getZExtValue() &&
|
|
|
|
f->getFirstArg() == (int)FirstArg.getZExtValue()) {
|
|
|
|
// If we don't have a valid location for this attribute, adopt the
|
|
|
|
// location.
|
|
|
|
if (f->getLocation().isInvalid())
|
2011-09-14 00:05:53 +08:00
|
|
|
f->setRange(Attr.getRange());
|
2011-06-15 13:45:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) FormatAttr(Attr.getRange(), S.Context, Format,
|
2010-08-19 07:23:40 +08:00
|
|
|
Idx.getZExtValue(),
|
2009-10-18 10:09:17 +08:00
|
|
|
FirstArg.getZExtValue()));
|
2008-06-27 02:38:35 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleTransparentUnionAttr(Sema &S, Decl *D,
|
|
|
|
const AttributeList &Attr) {
|
2008-06-27 02:38:35 +08:00
|
|
|
// check the attribute arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
2011-07-12 07:30:35 +08:00
|
|
|
|
2008-06-27 02:38:35 +08:00
|
|
|
|
2009-04-30 06:16:16 +08:00
|
|
|
// Try to find the underlying union declaration.
|
|
|
|
RecordDecl *RD = 0;
|
2011-07-02 07:49:12 +08:00
|
|
|
TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
|
2009-04-30 06:16:16 +08:00
|
|
|
if (TD && TD->getUnderlyingType()->isUnionType())
|
|
|
|
RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
|
|
|
|
else
|
2011-07-02 07:49:12 +08:00
|
|
|
RD = dyn_cast<RecordDecl>(D);
|
2009-04-30 06:16:16 +08:00
|
|
|
|
|
|
|
if (!RD || !RD->isUnion()) {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedUnion;
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-30 06:16:16 +08:00
|
|
|
if (!RD->isDefinition()) {
|
2009-07-25 03:02:52 +08:00
|
|
|
S.Diag(Attr.getLoc(),
|
2009-04-30 06:16:16 +08:00
|
|
|
diag::warn_transparent_union_attribute_not_definition);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-30 10:36:12 +08:00
|
|
|
RecordDecl::field_iterator Field = RD->field_begin(),
|
|
|
|
FieldEnd = RD->field_end();
|
2009-04-30 06:16:16 +08:00
|
|
|
if (Field == FieldEnd) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
|
|
|
|
return;
|
|
|
|
}
|
2008-09-02 13:19:23 +08:00
|
|
|
|
2009-04-30 06:16:16 +08:00
|
|
|
FieldDecl *FirstField = *Field;
|
|
|
|
QualType FirstType = FirstField->getType();
|
2010-07-01 01:24:13 +08:00
|
|
|
if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
|
2009-07-25 03:02:52 +08:00
|
|
|
S.Diag(FirstField->getLocation(),
|
2010-07-01 01:24:13 +08:00
|
|
|
diag::warn_transparent_union_attribute_floating)
|
|
|
|
<< FirstType->isVectorType() << FirstType;
|
2009-04-30 06:16:16 +08:00
|
|
|
return;
|
|
|
|
}
|
2008-09-02 13:19:23 +08:00
|
|
|
|
2009-04-30 06:16:16 +08:00
|
|
|
uint64_t FirstSize = S.Context.getTypeSize(FirstType);
|
|
|
|
uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
|
|
|
|
for (; Field != FieldEnd; ++Field) {
|
|
|
|
QualType FieldType = Field->getType();
|
|
|
|
if (S.Context.getTypeSize(FieldType) != FirstSize ||
|
|
|
|
S.Context.getTypeAlign(FieldType) != FirstAlign) {
|
|
|
|
// Warn if we drop the attribute.
|
|
|
|
bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
|
2009-07-25 03:02:52 +08:00
|
|
|
unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
|
2009-04-30 06:16:16 +08:00
|
|
|
: S.Context.getTypeAlign(FieldType);
|
2009-07-25 03:02:52 +08:00
|
|
|
S.Diag(Field->getLocation(),
|
2009-04-30 06:16:16 +08:00
|
|
|
diag::warn_transparent_union_attribute_field_size_align)
|
|
|
|
<< isSize << Field->getDeclName() << FieldBits;
|
|
|
|
unsigned FirstBits = isSize? FirstSize : FirstAlign;
|
2009-07-25 03:02:52 +08:00
|
|
|
S.Diag(FirstField->getLocation(),
|
2009-04-30 06:16:16 +08:00
|
|
|
diag::note_transparent_union_first_field_size_align)
|
|
|
|
<< isSize << FirstBits;
|
2008-09-02 13:19:23 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2008-06-27 02:38:35 +08:00
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
|
2008-06-27 02:38:35 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2008-06-27 02:38:35 +08:00
|
|
|
// check the attribute arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 1))
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
2011-07-12 07:30:35 +08:00
|
|
|
|
2010-11-24 04:45:58 +08:00
|
|
|
Expr *ArgExpr = Attr.getArg(0);
|
2009-08-11 03:03:04 +08:00
|
|
|
StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2008-06-27 02:38:35 +08:00
|
|
|
// Make sure that there is a string literal as the annotation's single
|
|
|
|
// argument.
|
|
|
|
if (!SE) {
|
2009-08-11 03:03:04 +08:00
|
|
|
S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-09-10 06:41:49 +08:00
|
|
|
|
|
|
|
// Don't duplicate annotations that are already set.
|
|
|
|
for (specific_attr_iterator<AnnotateAttr>
|
|
|
|
i = D->specific_attr_begin<AnnotateAttr>(),
|
|
|
|
e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
|
|
|
|
if ((*i)->getAnnotation() == SE->getString())
|
|
|
|
return;
|
|
|
|
}
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
|
2010-12-02 06:13:54 +08:00
|
|
|
SE->getString()));
|
2008-06-27 02:38:35 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2008-06-27 02:38:35 +08:00
|
|
|
// check the attribute arguments.
|
2008-06-29 07:36:30 +08:00
|
|
|
if (Attr.getNumArgs() > 1) {
|
2008-11-19 16:23:25 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-11-21 16:43:09 +08:00
|
|
|
|
|
|
|
//FIXME: The C++0x version of this attribute has more limited applicabilty
|
|
|
|
// than GNU's, and should error out when it is used to specify a
|
|
|
|
// weaker alignment, rather than being silently ignored.
|
2008-06-27 02:38:35 +08:00
|
|
|
|
2008-06-29 07:36:30 +08:00
|
|
|
if (Attr.getNumArgs() == 0) {
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, true, 0));
|
2010-06-25 11:22:07 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0));
|
2010-06-25 11:22:07 +08:00
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E) {
|
2010-06-25 11:22:07 +08:00
|
|
|
if (E->isTypeDependent() || E->isValueDependent()) {
|
|
|
|
// Save dependent expressions in the AST to be instantiated.
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E));
|
2008-06-27 02:38:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
SourceLocation AttrLoc = AttrRange.getBegin();
|
2010-08-19 07:23:40 +08:00
|
|
|
// FIXME: Cache the number on the Attr object?
|
2008-06-29 07:50:44 +08:00
|
|
|
llvm::APSInt Alignment(32);
|
2010-06-25 11:22:07 +08:00
|
|
|
if (!E->isIntegerConstantExpr(Alignment, Context)) {
|
|
|
|
Diag(AttrLoc, diag::err_attribute_argument_not_int)
|
|
|
|
<< "aligned" << E->getSourceRange();
|
2008-06-29 07:50:44 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-02-17 07:37:57 +08:00
|
|
|
if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
|
2010-06-25 11:22:07 +08:00
|
|
|
Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
|
|
|
|
<< E->getSourceRange();
|
2009-02-17 07:37:57 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E));
|
2010-08-19 07:23:40 +08:00
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS) {
|
2010-08-19 07:23:40 +08:00
|
|
|
// FIXME: Cache the number on the Attr object if non-dependent?
|
|
|
|
// FIXME: Perform checking of type validity
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS));
|
2010-08-19 07:23:40 +08:00
|
|
|
return;
|
2008-06-27 02:38:35 +08:00
|
|
|
}
|
2008-06-28 06:18:37 +08:00
|
|
|
|
2011-07-02 07:49:16 +08:00
|
|
|
/// handleModeAttr - This attribute modifies the width of a decl with primitive
|
2009-07-25 03:02:52 +08:00
|
|
|
/// type.
|
2008-06-28 06:18:37 +08:00
|
|
|
///
|
2009-07-25 03:02:52 +08:00
|
|
|
/// Despite what would be logical, the mode attribute is a decl attribute, not a
|
|
|
|
/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
|
|
|
|
/// HImode, not an intermediate pointer.
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2008-06-28 06:18:37 +08:00
|
|
|
// This attribute isn't documented, but glibc uses it. It changes
|
|
|
|
// the width of an int or unsigned int to the specified size.
|
|
|
|
|
|
|
|
// Check that there aren't any arguments
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
2008-06-28 06:18:37 +08:00
|
|
|
return;
|
2011-07-12 07:30:35 +08:00
|
|
|
|
2008-06-28 06:18:37 +08:00
|
|
|
|
|
|
|
IdentifierInfo *Name = Attr.getParameterName();
|
|
|
|
if (!Name) {
|
2008-06-29 08:28:59 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
|
2008-06-28 06:18:37 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-10-18 10:09:24 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Str = Attr.getParameterName()->getName();
|
2008-06-28 06:18:37 +08:00
|
|
|
|
|
|
|
// Normalize the attribute name, __foo__ becomes foo.
|
2009-10-18 10:09:24 +08:00
|
|
|
if (Str.startswith("__") && Str.endswith("__"))
|
|
|
|
Str = Str.substr(2, Str.size() - 4);
|
2008-06-28 06:18:37 +08:00
|
|
|
|
|
|
|
unsigned DestWidth = 0;
|
|
|
|
bool IntegerMode = true;
|
2009-03-03 14:41:03 +08:00
|
|
|
bool ComplexMode = false;
|
2009-10-18 10:09:24 +08:00
|
|
|
switch (Str.size()) {
|
2008-06-28 06:18:37 +08:00
|
|
|
case 2:
|
2009-03-03 14:41:03 +08:00
|
|
|
switch (Str[0]) {
|
|
|
|
case 'Q': DestWidth = 8; break;
|
|
|
|
case 'H': DestWidth = 16; break;
|
|
|
|
case 'S': DestWidth = 32; break;
|
|
|
|
case 'D': DestWidth = 64; break;
|
|
|
|
case 'X': DestWidth = 96; break;
|
|
|
|
case 'T': DestWidth = 128; break;
|
|
|
|
}
|
|
|
|
if (Str[1] == 'F') {
|
|
|
|
IntegerMode = false;
|
|
|
|
} else if (Str[1] == 'C') {
|
|
|
|
IntegerMode = false;
|
|
|
|
ComplexMode = true;
|
|
|
|
} else if (Str[1] != 'I') {
|
|
|
|
DestWidth = 0;
|
|
|
|
}
|
2008-06-28 06:18:37 +08:00
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
// FIXME: glibc uses 'word' to define register_t; this is narrower than a
|
|
|
|
// pointer on PIC16 and other embedded platforms.
|
2009-10-18 10:09:24 +08:00
|
|
|
if (Str == "word")
|
2011-09-02 08:18:52 +08:00
|
|
|
DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
|
2009-10-18 10:09:24 +08:00
|
|
|
else if (Str == "byte")
|
2011-09-02 08:18:52 +08:00
|
|
|
DestWidth = S.Context.getTargetInfo().getCharWidth();
|
2008-06-28 06:18:37 +08:00
|
|
|
break;
|
|
|
|
case 7:
|
2009-10-18 10:09:24 +08:00
|
|
|
if (Str == "pointer")
|
2011-09-02 08:18:52 +08:00
|
|
|
DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
|
2008-06-28 06:18:37 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
QualType OldTy;
|
2011-04-15 22:24:37 +08:00
|
|
|
if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
|
2008-06-28 06:18:37 +08:00
|
|
|
OldTy = TD->getUnderlyingType();
|
|
|
|
else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
|
|
|
|
OldTy = VD->getType();
|
|
|
|
else {
|
2008-11-19 13:08:23 +08:00
|
|
|
S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
|
2011-09-14 00:05:58 +08:00
|
|
|
<< "mode" << Attr.getRange();
|
2008-06-28 06:18:37 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-03-03 14:41:03 +08:00
|
|
|
|
2009-09-22 07:43:11 +08:00
|
|
|
if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
|
2009-03-03 14:41:03 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
|
|
|
|
else if (IntegerMode) {
|
2010-06-16 08:17:44 +08:00
|
|
|
if (!OldTy->isIntegralOrEnumerationType())
|
2009-03-03 14:41:03 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
|
|
|
|
} else if (ComplexMode) {
|
|
|
|
if (!OldTy->isComplexType())
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
|
|
|
|
} else {
|
|
|
|
if (!OldTy->isFloatingType())
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
|
|
|
|
}
|
|
|
|
|
2009-05-16 15:39:55 +08:00
|
|
|
// FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
|
|
|
|
// and friends, at least with glibc.
|
|
|
|
// FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
|
|
|
|
// width on unusual platforms.
|
2009-02-13 10:31:07 +08:00
|
|
|
// FIXME: Make sure floating-point mappings are accurate
|
|
|
|
// FIXME: Support XF and TF types
|
2008-06-28 06:18:37 +08:00
|
|
|
QualType NewTy;
|
|
|
|
switch (DestWidth) {
|
|
|
|
case 0:
|
2008-11-19 16:23:25 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
|
2008-06-28 06:18:37 +08:00
|
|
|
return;
|
|
|
|
default:
|
2008-11-19 16:23:25 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
|
2008-06-28 06:18:37 +08:00
|
|
|
return;
|
|
|
|
case 8:
|
2009-03-03 14:41:03 +08:00
|
|
|
if (!IntegerMode) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
|
|
|
|
return;
|
|
|
|
}
|
2008-06-28 06:18:37 +08:00
|
|
|
if (OldTy->isSignedIntegerType())
|
2008-06-29 08:28:59 +08:00
|
|
|
NewTy = S.Context.SignedCharTy;
|
2008-06-28 06:18:37 +08:00
|
|
|
else
|
2008-06-29 08:28:59 +08:00
|
|
|
NewTy = S.Context.UnsignedCharTy;
|
2008-06-28 06:18:37 +08:00
|
|
|
break;
|
|
|
|
case 16:
|
2009-03-03 14:41:03 +08:00
|
|
|
if (!IntegerMode) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
|
|
|
|
return;
|
|
|
|
}
|
2008-06-28 06:18:37 +08:00
|
|
|
if (OldTy->isSignedIntegerType())
|
2008-06-29 08:28:59 +08:00
|
|
|
NewTy = S.Context.ShortTy;
|
2008-06-28 06:18:37 +08:00
|
|
|
else
|
2008-06-29 08:28:59 +08:00
|
|
|
NewTy = S.Context.UnsignedShortTy;
|
2008-06-28 06:18:37 +08:00
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
if (!IntegerMode)
|
2008-06-29 08:28:59 +08:00
|
|
|
NewTy = S.Context.FloatTy;
|
2008-06-28 06:18:37 +08:00
|
|
|
else if (OldTy->isSignedIntegerType())
|
2008-06-29 08:28:59 +08:00
|
|
|
NewTy = S.Context.IntTy;
|
2008-06-28 06:18:37 +08:00
|
|
|
else
|
2008-06-29 08:28:59 +08:00
|
|
|
NewTy = S.Context.UnsignedIntTy;
|
2008-06-28 06:18:37 +08:00
|
|
|
break;
|
|
|
|
case 64:
|
|
|
|
if (!IntegerMode)
|
2008-06-29 08:28:59 +08:00
|
|
|
NewTy = S.Context.DoubleTy;
|
2008-06-28 06:18:37 +08:00
|
|
|
else if (OldTy->isSignedIntegerType())
|
2011-09-02 08:18:52 +08:00
|
|
|
if (S.Context.getTargetInfo().getLongWidth() == 64)
|
2010-01-26 14:39:24 +08:00
|
|
|
NewTy = S.Context.LongTy;
|
|
|
|
else
|
|
|
|
NewTy = S.Context.LongLongTy;
|
2008-06-28 06:18:37 +08:00
|
|
|
else
|
2011-09-02 08:18:52 +08:00
|
|
|
if (S.Context.getTargetInfo().getLongWidth() == 64)
|
2010-01-26 14:39:24 +08:00
|
|
|
NewTy = S.Context.UnsignedLongTy;
|
|
|
|
else
|
|
|
|
NewTy = S.Context.UnsignedLongLongTy;
|
2008-06-28 06:18:37 +08:00
|
|
|
break;
|
2009-03-03 14:41:03 +08:00
|
|
|
case 96:
|
|
|
|
NewTy = S.Context.LongDoubleTy;
|
|
|
|
break;
|
2009-02-13 10:31:07 +08:00
|
|
|
case 128:
|
|
|
|
if (!IntegerMode) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
|
|
|
|
return;
|
|
|
|
}
|
2009-12-29 15:07:36 +08:00
|
|
|
if (OldTy->isSignedIntegerType())
|
|
|
|
NewTy = S.Context.Int128Ty;
|
|
|
|
else
|
|
|
|
NewTy = S.Context.UnsignedInt128Ty;
|
2009-03-03 14:41:03 +08:00
|
|
|
break;
|
2008-06-28 06:18:37 +08:00
|
|
|
}
|
|
|
|
|
2009-03-03 14:41:03 +08:00
|
|
|
if (ComplexMode) {
|
|
|
|
NewTy = S.Context.getComplexType(NewTy);
|
2008-06-28 06:18:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Install the new type.
|
2011-04-15 22:24:37 +08:00
|
|
|
if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
|
2009-10-24 16:00:42 +08:00
|
|
|
// FIXME: preserve existing source info.
|
2009-12-07 10:54:59 +08:00
|
|
|
TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
|
2009-10-24 16:00:42 +08:00
|
|
|
} else
|
2008-06-28 06:18:37 +08:00
|
|
|
cast<ValueDecl>(D)->setType(NewTy);
|
|
|
|
}
|
2008-06-29 08:23:49 +08:00
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2009-02-13 14:46:13 +08:00
|
|
|
// check the attribute arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
2009-02-13 14:46:13 +08:00
|
|
|
return;
|
2009-02-13 16:11:52 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isFunctionOrMethod(D)) {
|
2009-02-13 14:46:13 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedFunction;
|
2009-02-13 14:46:13 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
|
2009-02-13 14:46:13 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2009-02-20 03:16:48 +08:00
|
|
|
// check the attribute arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
2009-02-20 03:16:48 +08:00
|
|
|
return;
|
2011-07-12 07:30:35 +08:00
|
|
|
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<FunctionDecl>(D)) {
|
2009-02-20 03:16:48 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedFunction;
|
2009-02-20 03:16:48 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
|
2009-02-20 03:16:48 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
|
|
|
|
const AttributeList &Attr) {
|
2010-06-22 08:03:40 +08:00
|
|
|
// check the attribute arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
2010-06-22 08:03:40 +08:00
|
|
|
return;
|
2011-07-12 07:30:35 +08:00
|
|
|
|
2010-06-22 08:03:40 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<FunctionDecl>(D)) {
|
2010-06-22 08:03:40 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedFunction;
|
2010-06-22 08:03:40 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
|
2010-12-02 06:13:54 +08:00
|
|
|
S.Context));
|
2010-06-22 08:03:40 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2010-12-01 11:15:31 +08:00
|
|
|
if (S.LangOpts.CUDA) {
|
|
|
|
// check the attribute arguments.
|
2011-04-15 13:49:29 +08:00
|
|
|
if (Attr.hasParameterOrArguments()) {
|
2010-12-01 11:15:31 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<VarDecl>(D)) {
|
2010-12-01 11:15:31 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedVariable;
|
2010-12-01 11:15:31 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
|
2010-12-01 11:15:31 +08:00
|
|
|
} else {
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2010-12-01 11:15:31 +08:00
|
|
|
if (S.LangOpts.CUDA) {
|
|
|
|
// check the attribute arguments.
|
|
|
|
if (Attr.getNumArgs() != 0) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
|
2010-12-01 11:15:31 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedVariableOrFunction;
|
2010-12-01 11:15:31 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
|
2010-12-01 11:15:31 +08:00
|
|
|
} else {
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2010-12-01 11:15:31 +08:00
|
|
|
if (S.LangOpts.CUDA) {
|
|
|
|
// check the attribute arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
2010-12-01 11:15:31 +08:00
|
|
|
return;
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<FunctionDecl>(D)) {
|
2010-12-01 11:15:31 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedFunction;
|
2010-12-01 11:15:31 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
FunctionDecl *FD = cast<FunctionDecl>(D);
|
2010-12-13 07:02:57 +08:00
|
|
|
if (!FD->getResultType()->isVoidType()) {
|
2010-12-15 06:11:44 +08:00
|
|
|
TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
|
2010-12-13 07:02:57 +08:00
|
|
|
if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
|
|
|
|
S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
|
|
|
|
<< FD->getType()
|
|
|
|
<< FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
|
|
|
|
"void");
|
|
|
|
} else {
|
|
|
|
S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
|
|
|
|
<< FD->getType();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
|
2010-12-01 11:15:31 +08:00
|
|
|
} else {
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2010-12-01 11:15:31 +08:00
|
|
|
if (S.LangOpts.CUDA) {
|
|
|
|
// check the attribute arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
2010-12-01 11:15:31 +08:00
|
|
|
return;
|
2011-07-12 07:30:35 +08:00
|
|
|
|
2010-12-01 11:15:31 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<FunctionDecl>(D)) {
|
2010-12-01 11:15:31 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedFunction;
|
2010-12-01 11:15:31 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
|
2010-12-01 11:15:31 +08:00
|
|
|
} else {
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2010-12-01 11:15:31 +08:00
|
|
|
if (S.LangOpts.CUDA) {
|
|
|
|
// check the attribute arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
2010-12-01 11:15:31 +08:00
|
|
|
return;
|
2011-07-12 07:30:35 +08:00
|
|
|
|
2010-12-01 11:15:31 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<VarDecl>(D)) {
|
2010-12-01 11:15:31 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedVariable;
|
2010-12-01 11:15:31 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
|
2010-12-01 11:15:31 +08:00
|
|
|
} else {
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2009-04-15 00:30:50 +08:00
|
|
|
// check the attribute arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 0))
|
2009-04-15 00:30:50 +08:00
|
|
|
return;
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
|
2009-04-15 01:02:11 +08:00
|
|
|
if (Fn == 0) {
|
2009-04-15 00:30:50 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedFunction;
|
2009-04-15 00:30:50 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2009-10-28 05:01:01 +08:00
|
|
|
if (!Fn->isInlineSpecified()) {
|
2009-04-21 03:12:28 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
|
2009-04-15 01:02:11 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
|
2009-04-15 00:30:50 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2011-07-02 07:49:12 +08:00
|
|
|
if (hasDeclarator(D)) return;
|
2011-01-05 20:14:39 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
// Diagnostic is emitted elsewhere: here we store the (valid) Attr
|
2010-04-30 21:10:51 +08:00
|
|
|
// in the Decl node for syntactic reasoning, e.g., pretty-printing.
|
2011-01-05 20:14:39 +08:00
|
|
|
CallingConv CC;
|
2011-07-02 07:49:12 +08:00
|
|
|
if (S.CheckCallingConvAttr(Attr, CC))
|
2011-01-05 20:14:39 +08:00
|
|
|
return;
|
2010-04-30 21:10:51 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<ObjCMethodDecl>(D)) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
|
|
|
<< Attr.getName() << ExpectedFunctionOrMethod;
|
2011-01-05 20:14:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
switch (Attr.getKind()) {
|
2010-04-30 21:10:51 +08:00
|
|
|
case AttributeList::AT_fastcall:
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
|
2010-04-30 21:10:51 +08:00
|
|
|
return;
|
|
|
|
case AttributeList::AT_stdcall:
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
|
2010-04-30 21:10:51 +08:00
|
|
|
return;
|
2010-05-19 00:57:00 +08:00
|
|
|
case AttributeList::AT_thiscall:
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
|
2010-08-31 07:30:49 +08:00
|
|
|
return;
|
2010-04-30 21:10:51 +08:00
|
|
|
case AttributeList::AT_cdecl:
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
|
2010-04-30 21:10:51 +08:00
|
|
|
return;
|
2010-09-03 09:29:35 +08:00
|
|
|
case AttributeList::AT_pascal:
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
|
2010-09-03 09:29:35 +08:00
|
|
|
return;
|
2011-04-15 04:06:49 +08:00
|
|
|
case AttributeList::AT_pcs: {
|
2011-07-02 07:49:12 +08:00
|
|
|
Expr *Arg = Attr.getArg(0);
|
2011-04-15 04:06:49 +08:00
|
|
|
StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
|
2011-07-27 13:40:30 +08:00
|
|
|
if (!Str || !Str->isAscii()) {
|
2011-07-02 07:49:12 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
|
2011-04-15 04:06:49 +08:00
|
|
|
<< "pcs" << 1;
|
2011-07-02 07:49:12 +08:00
|
|
|
Attr.setInvalid();
|
2011-04-15 04:06:49 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef StrRef = Str->getString();
|
2011-04-15 04:06:49 +08:00
|
|
|
PcsAttr::PCSType PCS;
|
|
|
|
if (StrRef == "aapcs")
|
|
|
|
PCS = PcsAttr::AAPCS;
|
|
|
|
else if (StrRef == "aapcs-vfp")
|
|
|
|
PCS = PcsAttr::AAPCS_VFP;
|
|
|
|
else {
|
2011-07-02 07:49:12 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
|
|
|
|
Attr.setInvalid();
|
2011-04-15 04:06:49 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
|
2011-04-15 04:06:49 +08:00
|
|
|
}
|
2010-04-30 21:10:51 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("unexpected attribute kind");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
|
2011-07-12 07:33:05 +08:00
|
|
|
assert(!Attr.isInvalid());
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
|
2011-02-14 09:42:53 +08:00
|
|
|
}
|
|
|
|
|
2011-01-05 20:14:39 +08:00
|
|
|
bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
|
|
|
|
if (attr.isInvalid())
|
|
|
|
return true;
|
|
|
|
|
2011-04-15 13:49:29 +08:00
|
|
|
if ((attr.getNumArgs() != 0 &&
|
|
|
|
!(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
|
|
|
|
attr.getParameterName()) {
|
2011-01-05 20:14:39 +08:00
|
|
|
Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
|
|
|
|
attr.setInvalid();
|
|
|
|
return true;
|
2009-03-28 02:38:55 +08:00
|
|
|
}
|
2009-03-28 05:06:47 +08:00
|
|
|
|
2011-04-15 04:06:49 +08:00
|
|
|
// TODO: diagnose uses of these conventions on the wrong target. Or, better
|
|
|
|
// move to TargetAttributesSema one day.
|
2011-01-05 20:14:39 +08:00
|
|
|
switch (attr.getKind()) {
|
|
|
|
case AttributeList::AT_cdecl: CC = CC_C; break;
|
|
|
|
case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
|
|
|
|
case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
|
|
|
|
case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
|
|
|
|
case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
|
2011-04-15 04:06:49 +08:00
|
|
|
case AttributeList::AT_pcs: {
|
|
|
|
Expr *Arg = attr.getArg(0);
|
|
|
|
StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
|
2011-07-27 13:40:30 +08:00
|
|
|
if (!Str || !Str->isAscii()) {
|
2011-04-15 04:06:49 +08:00
|
|
|
Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
|
|
|
|
<< "pcs" << 1;
|
|
|
|
attr.setInvalid();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef StrRef = Str->getString();
|
2011-04-15 04:06:49 +08:00
|
|
|
if (StrRef == "aapcs") {
|
|
|
|
CC = CC_AAPCS;
|
|
|
|
break;
|
|
|
|
} else if (StrRef == "aapcs-vfp") {
|
|
|
|
CC = CC_AAPCS_VFP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// FALLS THROUGH
|
|
|
|
}
|
2011-01-05 20:14:39 +08:00
|
|
|
default: llvm_unreachable("unexpected attribute kind"); return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2011-07-02 07:49:12 +08:00
|
|
|
if (hasDeclarator(D)) return;
|
2011-01-05 20:14:39 +08:00
|
|
|
|
|
|
|
unsigned numParams;
|
2011-07-02 07:49:12 +08:00
|
|
|
if (S.CheckRegparmAttr(Attr, numParams))
|
2011-01-05 20:14:39 +08:00
|
|
|
return;
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<ObjCMethodDecl>(D)) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
|
|
|
<< Attr.getName() << ExpectedFunctionOrMethod;
|
2009-03-28 02:38:55 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-03-28 05:06:47 +08:00
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
|
2011-01-05 20:14:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks a regparm attribute, returning true if it is ill-formed and
|
|
|
|
/// otherwise setting numParams to the appropriate value.
|
2011-07-02 07:49:12 +08:00
|
|
|
bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
|
|
|
|
if (Attr.isInvalid())
|
2011-01-05 20:14:39 +08:00
|
|
|
return true;
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (Attr.getNumArgs() != 1) {
|
|
|
|
Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
|
|
|
|
Attr.setInvalid();
|
2011-01-05 20:14:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
Expr *NumParamsExpr = Attr.getArg(0);
|
2009-03-28 05:06:47 +08:00
|
|
|
llvm::APSInt NumParams(32);
|
2010-05-19 07:01:22 +08:00
|
|
|
if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
|
2011-01-05 20:14:39 +08:00
|
|
|
!NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
|
2011-07-02 07:49:12 +08:00
|
|
|
Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
|
2009-03-28 05:06:47 +08:00
|
|
|
<< "regparm" << NumParamsExpr->getSourceRange();
|
2011-07-02 07:49:12 +08:00
|
|
|
Attr.setInvalid();
|
2011-01-05 20:14:39 +08:00
|
|
|
return true;
|
2009-03-28 05:06:47 +08:00
|
|
|
}
|
|
|
|
|
2011-09-02 08:18:52 +08:00
|
|
|
if (Context.getTargetInfo().getRegParmMax() == 0) {
|
2011-07-02 07:49:12 +08:00
|
|
|
Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
|
2009-03-28 05:06:47 +08:00
|
|
|
<< NumParamsExpr->getSourceRange();
|
2011-07-02 07:49:12 +08:00
|
|
|
Attr.setInvalid();
|
2011-01-05 20:14:39 +08:00
|
|
|
return true;
|
2009-03-28 05:06:47 +08:00
|
|
|
}
|
|
|
|
|
2011-01-05 20:14:39 +08:00
|
|
|
numParams = NumParams.getZExtValue();
|
2011-09-02 08:18:52 +08:00
|
|
|
if (numParams > Context.getTargetInfo().getRegParmMax()) {
|
2011-07-02 07:49:12 +08:00
|
|
|
Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
|
2011-09-02 08:18:52 +08:00
|
|
|
<< Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
|
2011-07-02 07:49:12 +08:00
|
|
|
Attr.setInvalid();
|
2011-01-05 20:14:39 +08:00
|
|
|
return true;
|
2009-03-28 05:06:47 +08:00
|
|
|
}
|
|
|
|
|
2011-01-05 20:14:39 +08:00
|
|
|
return false;
|
2009-03-28 02:38:55 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
|
2010-12-13 07:03:07 +08:00
|
|
|
if (S.LangOpts.CUDA) {
|
|
|
|
// check the attribute arguments.
|
|
|
|
if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
|
2011-03-02 20:15:05 +08:00
|
|
|
// FIXME: 0 is not okay.
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
|
2010-12-13 07:03:07 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isFunctionOrMethod(D)) {
|
2010-12-13 07:03:07 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
2011-03-02 20:29:23 +08:00
|
|
|
<< Attr.getName() << ExpectedFunctionOrMethod;
|
2010-12-13 07:03:07 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr *MaxThreadsExpr = Attr.getArg(0);
|
|
|
|
llvm::APSInt MaxThreads(32);
|
|
|
|
if (MaxThreadsExpr->isTypeDependent() ||
|
|
|
|
MaxThreadsExpr->isValueDependent() ||
|
|
|
|
!MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
|
|
|
|
<< "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::APSInt MinBlocks(32);
|
|
|
|
if (Attr.getNumArgs() > 1) {
|
|
|
|
Expr *MinBlocksExpr = Attr.getArg(1);
|
|
|
|
if (MinBlocksExpr->isTypeDependent() ||
|
|
|
|
MinBlocksExpr->isValueDependent() ||
|
|
|
|
!MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
|
|
|
|
<< "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
|
2010-12-13 07:03:07 +08:00
|
|
|
MaxThreads.getZExtValue(),
|
|
|
|
MinBlocks.getZExtValue()));
|
|
|
|
} else {
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-09 10:44:38 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Checker-specific attribute handlers.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-01-25 11:31:58 +08:00
|
|
|
static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
|
|
|
|
return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type);
|
|
|
|
}
|
|
|
|
static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
|
|
|
|
return type->isPointerType() || isValidSubjectOfNSAttribute(S, type);
|
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2011-07-02 07:49:12 +08:00
|
|
|
ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
|
2011-01-25 11:31:58 +08:00
|
|
|
if (!param) {
|
2011-07-02 07:49:12 +08:00
|
|
|
S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
|
2011-09-14 00:05:58 +08:00
|
|
|
<< Attr.getRange() << Attr.getName() << ExpectedParameter;
|
2011-01-25 11:31:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool typeOK, cf;
|
2011-07-02 07:49:12 +08:00
|
|
|
if (Attr.getKind() == AttributeList::AT_ns_consumed) {
|
2011-01-25 11:31:58 +08:00
|
|
|
typeOK = isValidSubjectOfNSAttribute(S, param->getType());
|
|
|
|
cf = false;
|
|
|
|
} else {
|
|
|
|
typeOK = isValidSubjectOfCFAttribute(S, param->getType());
|
|
|
|
cf = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!typeOK) {
|
2011-07-02 07:49:12 +08:00
|
|
|
S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
|
2011-09-14 00:05:58 +08:00
|
|
|
<< Attr.getRange() << Attr.getName() << cf;
|
2011-01-25 11:31:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cf)
|
2011-09-14 00:05:58 +08:00
|
|
|
param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
|
2011-01-25 11:31:58 +08:00
|
|
|
else
|
2011-09-14 00:05:58 +08:00
|
|
|
param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
|
2011-01-25 11:31:58 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
|
|
|
|
const AttributeList &Attr) {
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<ObjCMethodDecl>(D)) {
|
|
|
|
S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
|
2011-09-14 00:05:58 +08:00
|
|
|
<< Attr.getRange() << Attr.getName() << ExpectedMethod;
|
2011-01-25 11:31:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
|
2011-01-25 11:31:58 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
|
|
|
|
const AttributeList &Attr) {
|
2009-05-09 10:44:38 +08:00
|
|
|
|
2011-01-25 11:31:58 +08:00
|
|
|
QualType returnType;
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
|
2011-01-25 11:31:58 +08:00
|
|
|
returnType = MD->getResultType();
|
2011-07-02 07:49:12 +08:00
|
|
|
else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
|
2011-06-25 08:17:46 +08:00
|
|
|
returnType = PD->getType();
|
2011-07-02 07:49:12 +08:00
|
|
|
else if (S.getLangOptions().ObjCAutoRefCount && hasDeclarator(D) &&
|
|
|
|
(Attr.getKind() == AttributeList::AT_ns_returns_retained))
|
2011-06-16 07:02:42 +08:00
|
|
|
return; // ignore: was handled as a type attribute
|
2011-07-02 07:49:12 +08:00
|
|
|
else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
|
2011-01-25 11:31:58 +08:00
|
|
|
returnType = FD->getResultType();
|
2009-05-14 05:07:32 +08:00
|
|
|
else {
|
2011-07-02 07:49:12 +08:00
|
|
|
S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
|
2011-09-14 00:05:58 +08:00
|
|
|
<< Attr.getRange() << Attr.getName()
|
2011-03-02 20:29:23 +08:00
|
|
|
<< ExpectedFunctionOrMethod;
|
2009-05-09 10:44:38 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-01-25 11:31:58 +08:00
|
|
|
bool typeOK;
|
|
|
|
bool cf;
|
2011-07-02 07:49:12 +08:00
|
|
|
switch (Attr.getKind()) {
|
2011-01-25 11:31:58 +08:00
|
|
|
default: llvm_unreachable("invalid ownership attribute"); return;
|
|
|
|
case AttributeList::AT_ns_returns_autoreleased:
|
|
|
|
case AttributeList::AT_ns_returns_retained:
|
|
|
|
case AttributeList::AT_ns_returns_not_retained:
|
|
|
|
typeOK = isValidSubjectOfNSAttribute(S, returnType);
|
|
|
|
cf = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AttributeList::AT_cf_returns_retained:
|
|
|
|
case AttributeList::AT_cf_returns_not_retained:
|
|
|
|
typeOK = isValidSubjectOfCFAttribute(S, returnType);
|
|
|
|
cf = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!typeOK) {
|
2011-07-02 07:49:12 +08:00
|
|
|
S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
|
2011-09-14 00:05:58 +08:00
|
|
|
<< Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
|
2009-07-25 03:02:52 +08:00
|
|
|
return;
|
2009-05-14 05:07:32 +08:00
|
|
|
}
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
switch (Attr.getKind()) {
|
2009-05-09 10:44:38 +08:00
|
|
|
default:
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("invalid ownership attribute");
|
2011-01-25 11:31:58 +08:00
|
|
|
case AttributeList::AT_ns_returns_autoreleased:
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
|
2011-01-25 11:31:58 +08:00
|
|
|
S.Context));
|
|
|
|
return;
|
2010-02-18 08:05:45 +08:00
|
|
|
case AttributeList::AT_cf_returns_not_retained:
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
|
2010-12-02 06:13:54 +08:00
|
|
|
S.Context));
|
2010-02-18 08:05:45 +08:00
|
|
|
return;
|
|
|
|
case AttributeList::AT_ns_returns_not_retained:
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
|
2010-12-02 06:13:54 +08:00
|
|
|
S.Context));
|
2010-02-18 08:05:45 +08:00
|
|
|
return;
|
2009-05-09 10:44:38 +08:00
|
|
|
case AttributeList::AT_cf_returns_retained:
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
|
2010-12-02 06:13:54 +08:00
|
|
|
S.Context));
|
2009-05-09 10:44:38 +08:00
|
|
|
return;
|
|
|
|
case AttributeList::AT_ns_returns_retained:
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
|
2010-12-02 06:13:54 +08:00
|
|
|
S.Context));
|
2009-05-09 10:44:38 +08:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-07-22 16:53:00 +08:00
|
|
|
static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
|
|
|
|
const AttributeList &attr) {
|
|
|
|
SourceLocation loc = attr.getLoc();
|
|
|
|
|
|
|
|
ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
|
|
|
|
|
|
|
|
if (!isa<ObjCMethodDecl>(method)) {
|
|
|
|
S.Diag(method->getLocStart(), diag::err_attribute_wrong_decl_type)
|
|
|
|
<< SourceRange(loc, loc) << attr.getName() << 13 /* methods */;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the method returns a normal pointer.
|
|
|
|
QualType resultType = method->getResultType();
|
|
|
|
if (!resultType->isPointerType() || resultType->isObjCRetainableType()) {
|
|
|
|
S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
|
|
|
|
<< SourceRange(loc)
|
|
|
|
<< attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
|
|
|
|
|
|
|
|
// Drop the attribute.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
method->addAttr(
|
2011-09-14 00:05:58 +08:00
|
|
|
::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
|
2011-07-22 16:53:00 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleObjCOwnershipAttr(Sema &S, Decl *D,
|
|
|
|
const AttributeList &Attr) {
|
2011-07-02 07:49:12 +08:00
|
|
|
if (hasDeclarator(D)) return;
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
|
2011-09-14 00:05:58 +08:00
|
|
|
<< Attr.getRange() << Attr.getName() << 12 /* variable */;
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
|
|
|
|
const AttributeList &Attr) {
|
2011-07-02 07:49:12 +08:00
|
|
|
if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
|
|
|
|
S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
|
2011-09-14 00:05:58 +08:00
|
|
|
<< Attr.getRange() << Attr.getName() << 12 /* variable */;
|
2011-06-16 07:02:42 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
ValueDecl *vd = cast<ValueDecl>(D);
|
2011-06-16 07:02:42 +08:00
|
|
|
QualType type = vd->getType();
|
|
|
|
|
|
|
|
if (!type->isDependentType() &&
|
|
|
|
!type->isObjCLifetimeType()) {
|
2011-07-02 07:49:12 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
|
2011-06-16 07:02:42 +08:00
|
|
|
<< type;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
|
|
|
|
|
|
|
|
// If we have no lifetime yet, check the lifetime we're presumably
|
|
|
|
// going to infer.
|
|
|
|
if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
|
|
|
|
lifetime = type->getObjCARCImplicitLifetime();
|
|
|
|
|
|
|
|
switch (lifetime) {
|
|
|
|
case Qualifiers::OCL_None:
|
|
|
|
assert(type->isDependentType() &&
|
|
|
|
"didn't infer lifetime for non-dependent type?");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Weak: // meaningful
|
|
|
|
case Qualifiers::OCL_Strong: // meaningful
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_ExplicitNone:
|
|
|
|
case Qualifiers::OCL_Autoreleasing:
|
2011-07-02 07:49:12 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
|
2011-06-16 07:02:42 +08:00
|
|
|
<< (lifetime == Qualifiers::OCL_Autoreleasing);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-07-02 07:49:12 +08:00
|
|
|
D->addAttr(::new (S.Context)
|
2011-09-14 00:05:58 +08:00
|
|
|
ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
|
|
|
|
2010-02-17 02:27:26 +08:00
|
|
|
static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
|
|
|
|
return Attr.getKind() == AttributeList::AT_dllimport ||
|
2010-12-19 14:50:37 +08:00
|
|
|
Attr.getKind() == AttributeList::AT_dllexport ||
|
|
|
|
Attr.getKind() == AttributeList::AT_uuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Microsoft specific attribute handlers.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
|
2011-09-18 01:15:52 +08:00
|
|
|
if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
|
2010-12-19 14:50:37 +08:00
|
|
|
// check the attribute arguments.
|
2011-07-12 07:30:35 +08:00
|
|
|
if (!checkAttributeNumArgs(S, Attr, 1))
|
2010-12-19 14:50:37 +08:00
|
|
|
return;
|
2011-07-12 07:30:35 +08:00
|
|
|
|
2010-12-19 14:50:37 +08:00
|
|
|
Expr *Arg = Attr.getArg(0);
|
|
|
|
StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
|
2011-07-27 13:40:30 +08:00
|
|
|
if (!Str || !Str->isAscii()) {
|
2010-12-20 09:41:49 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
|
|
|
|
<< "uuid" << 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef StrRef = Str->getString();
|
2010-12-20 09:41:49 +08:00
|
|
|
|
|
|
|
bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
|
|
|
|
StrRef.back() == '}';
|
|
|
|
|
|
|
|
// Validate GUID length.
|
|
|
|
if (IsCurly && StrRef.size() != 38) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!IsCurly && StrRef.size() != 36) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
|
|
|
|
// "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef::iterator I = StrRef.begin();
|
2011-01-24 05:07:30 +08:00
|
|
|
if (IsCurly) // Skip the optional '{'
|
|
|
|
++I;
|
|
|
|
|
|
|
|
for (int i = 0; i < 36; ++i) {
|
2010-12-20 09:41:49 +08:00
|
|
|
if (i == 8 || i == 13 || i == 18 || i == 23) {
|
|
|
|
if (*I != '-') {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (!isxdigit(*I)) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
I++;
|
|
|
|
}
|
2010-12-19 14:50:37 +08:00
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
|
2010-12-19 14:50:37 +08:00
|
|
|
Str->getString()));
|
2010-12-20 09:41:49 +08:00
|
|
|
} else
|
2010-12-19 14:50:37 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
|
2010-02-17 02:27:26 +08:00
|
|
|
}
|
|
|
|
|
2008-06-29 08:23:49 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Top Level Sema Entry Points
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
|
|
|
|
const AttributeList &Attr) {
|
2011-01-21 10:08:45 +08:00
|
|
|
switch (Attr.getKind()) {
|
2011-07-02 08:01:44 +08:00
|
|
|
case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
|
2011-01-21 10:08:45 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-04-30 21:10:51 +08:00
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
|
|
|
|
const AttributeList &Attr) {
|
2008-06-29 08:43:07 +08:00
|
|
|
switch (Attr.getKind()) {
|
2011-07-02 08:01:44 +08:00
|
|
|
case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
|
|
|
|
case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
|
2010-05-20 01:38:06 +08:00
|
|
|
case AttributeList::AT_IBOutletCollection:
|
2011-07-02 08:01:44 +08:00
|
|
|
handleIBOutletCollection(S, D, Attr); break;
|
2008-06-29 08:43:07 +08:00
|
|
|
case AttributeList::AT_address_space:
|
2011-03-19 06:38:29 +08:00
|
|
|
case AttributeList::AT_opencl_image_access:
|
2009-02-19 01:52:36 +08:00
|
|
|
case AttributeList::AT_objc_gc:
|
2009-12-05 05:51:28 +08:00
|
|
|
case AttributeList::AT_vector_size:
|
2010-11-16 08:32:24 +08:00
|
|
|
case AttributeList::AT_neon_vector_type:
|
|
|
|
case AttributeList::AT_neon_polyvector_type:
|
2009-07-25 03:02:52 +08:00
|
|
|
// Ignore these, these are type attributes, handled by
|
|
|
|
// ProcessTypeAttributes.
|
2008-06-29 08:43:07 +08:00
|
|
|
break;
|
2011-01-21 10:08:45 +08:00
|
|
|
case AttributeList::AT_device:
|
|
|
|
case AttributeList::AT_host:
|
|
|
|
case AttributeList::AT_overloadable:
|
|
|
|
// Ignore, this is a non-inheritable attribute, handled
|
|
|
|
// by ProcessNonInheritableDeclAttr.
|
|
|
|
break;
|
2011-07-02 08:01:44 +08:00
|
|
|
case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
|
2009-07-25 03:02:52 +08:00
|
|
|
case AttributeList::AT_always_inline:
|
2011-07-02 08:01:44 +08:00
|
|
|
handleAlwaysInlineAttr (S, D, Attr); break;
|
2009-04-10 08:01:14 +08:00
|
|
|
case AttributeList::AT_analyzer_noreturn:
|
2011-07-02 08:01:44 +08:00
|
|
|
handleAnalyzerNoReturnAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
|
2009-11-21 16:43:09 +08:00
|
|
|
case AttributeList::AT_carries_dependency:
|
2011-07-02 08:01:44 +08:00
|
|
|
handleDependencyAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
|
2008-08-01 06:40:48 +08:00
|
|
|
case AttributeList::AT_ext_vector_type:
|
2011-07-02 08:01:44 +08:00
|
|
|
handleExtVectorTypeAttr(S, scope, D, Attr);
|
2008-08-01 06:40:48 +08:00
|
|
|
break;
|
2011-07-02 08:01:44 +08:00
|
|
|
case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
|
2010-12-13 07:03:07 +08:00
|
|
|
case AttributeList::AT_launch_bounds:
|
2011-07-02 08:01:44 +08:00
|
|
|
handleLaunchBoundsAttr(S, D, Attr);
|
2010-12-13 07:03:07 +08:00
|
|
|
break;
|
2011-07-02 08:01:44 +08:00
|
|
|
case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
|
2010-07-31 09:52:11 +08:00
|
|
|
case AttributeList::AT_ownership_returns:
|
|
|
|
case AttributeList::AT_ownership_takes:
|
|
|
|
case AttributeList::AT_ownership_holds:
|
2011-07-02 08:01:44 +08:00
|
|
|
handleOwnershipAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
|
2009-05-09 10:44:38 +08:00
|
|
|
|
2011-06-24 08:08:59 +08:00
|
|
|
case AttributeList::AT_objc_ownership:
|
2011-07-02 08:01:44 +08:00
|
|
|
handleObjCOwnershipAttr(S, D, Attr); break;
|
2011-06-16 07:02:42 +08:00
|
|
|
case AttributeList::AT_objc_precise_lifetime:
|
2011-07-02 08:01:44 +08:00
|
|
|
handleObjCPreciseLifetimeAttr(S, D, Attr); break;
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2011-07-22 16:53:00 +08:00
|
|
|
case AttributeList::AT_objc_returns_inner_pointer:
|
|
|
|
handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
|
|
|
|
|
2009-05-09 10:44:38 +08:00
|
|
|
// Checker-specific.
|
2011-01-25 11:31:58 +08:00
|
|
|
case AttributeList::AT_cf_consumed:
|
2011-07-02 08:01:44 +08:00
|
|
|
case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
|
2011-01-25 11:31:58 +08:00
|
|
|
case AttributeList::AT_ns_consumes_self:
|
2011-07-02 08:01:44 +08:00
|
|
|
handleNSConsumesSelfAttr(S, D, Attr); break;
|
2011-01-25 11:31:58 +08:00
|
|
|
|
|
|
|
case AttributeList::AT_ns_returns_autoreleased:
|
2010-02-18 08:05:45 +08:00
|
|
|
case AttributeList::AT_ns_returns_not_retained:
|
|
|
|
case AttributeList::AT_cf_returns_not_retained:
|
2009-05-09 10:44:38 +08:00
|
|
|
case AttributeList::AT_ns_returns_retained:
|
|
|
|
case AttributeList::AT_cf_returns_retained:
|
2011-07-02 08:01:44 +08:00
|
|
|
handleNSReturnsRetainedAttr(S, D, Attr); break;
|
2009-05-09 10:44:38 +08:00
|
|
|
|
2009-06-26 14:32:41 +08:00
|
|
|
case AttributeList::AT_reqd_wg_size:
|
2011-07-02 08:01:44 +08:00
|
|
|
handleReqdWorkGroupSize(S, D, Attr); break;
|
2009-06-26 14:32:41 +08:00
|
|
|
|
2010-06-19 05:44:06 +08:00
|
|
|
case AttributeList::AT_init_priority:
|
2011-07-02 08:01:44 +08:00
|
|
|
handleInitPriorityAttr(S, D, Attr); break;
|
2010-06-19 05:44:06 +08:00
|
|
|
|
2011-07-02 08:01:44 +08:00
|
|
|
case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_MsStruct: handleMsStructAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
|
2011-07-07 03:24:05 +08:00
|
|
|
case AttributeList::AT_arc_weakref_unavailable:
|
|
|
|
handleArcWeakrefUnavailableAttr (S, D, Attr);
|
|
|
|
break;
|
2011-07-02 08:01:44 +08:00
|
|
|
case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
|
2009-02-14 15:37:35 +08:00
|
|
|
break;
|
2011-07-02 08:01:44 +08:00
|
|
|
case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
|
2008-06-29 08:43:07 +08:00
|
|
|
case AttributeList::AT_transparent_union:
|
2011-07-02 08:01:44 +08:00
|
|
|
handleTransparentUnionAttr(S, D, Attr);
|
2008-06-29 08:43:07 +08:00
|
|
|
break;
|
2009-02-14 16:09:34 +08:00
|
|
|
case AttributeList::AT_objc_exception:
|
2011-07-02 08:01:44 +08:00
|
|
|
handleObjCExceptionAttr(S, D, Attr);
|
2009-02-14 16:09:34 +08:00
|
|
|
break;
|
2011-03-02 19:33:24 +08:00
|
|
|
case AttributeList::AT_objc_method_family:
|
2011-07-02 08:01:44 +08:00
|
|
|
handleObjCMethodFamilyAttr(S, D, Attr);
|
2011-03-02 19:33:24 +08:00
|
|
|
break;
|
2011-07-02 08:01:44 +08:00
|
|
|
case AttributeList::AT_nsobject: handleObjCNSObject (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
|
|
|
|
case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
|
2009-07-25 03:02:52 +08:00
|
|
|
case AttributeList::IgnoredAttribute:
|
2009-02-13 16:16:43 +08:00
|
|
|
// Just ignore
|
|
|
|
break;
|
2010-06-22 08:03:40 +08:00
|
|
|
case AttributeList::AT_no_instrument_function: // Interacts with -pg.
|
2011-07-02 08:01:44 +08:00
|
|
|
handleNoInstrumentFunctionAttr(S, D, Attr);
|
2010-06-22 08:03:40 +08:00
|
|
|
break;
|
2010-02-06 05:31:56 +08:00
|
|
|
case AttributeList::AT_stdcall:
|
|
|
|
case AttributeList::AT_cdecl:
|
|
|
|
case AttributeList::AT_fastcall:
|
2010-05-19 00:57:00 +08:00
|
|
|
case AttributeList::AT_thiscall:
|
2010-09-03 09:29:35 +08:00
|
|
|
case AttributeList::AT_pascal:
|
2011-04-15 04:06:49 +08:00
|
|
|
case AttributeList::AT_pcs:
|
2011-07-02 08:01:44 +08:00
|
|
|
handleCallConvAttr(S, D, Attr);
|
2010-02-06 05:31:56 +08:00
|
|
|
break;
|
2011-02-14 09:42:53 +08:00
|
|
|
case AttributeList::AT_opencl_kernel_function:
|
2011-07-02 08:01:44 +08:00
|
|
|
handleOpenCLKernelAttr(S, D, Attr);
|
2011-02-14 09:42:53 +08:00
|
|
|
break;
|
2010-12-19 14:50:37 +08:00
|
|
|
case AttributeList::AT_uuid:
|
2011-07-02 08:01:44 +08:00
|
|
|
handleUuidAttr(S, D, Attr);
|
2010-12-19 14:50:37 +08:00
|
|
|
break;
|
2011-07-29 01:21:07 +08:00
|
|
|
|
|
|
|
// Thread safety attributes:
|
|
|
|
case AttributeList::AT_guarded_var:
|
|
|
|
handleGuardedVarAttr(S, D, Attr);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_pt_guarded_var:
|
|
|
|
handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_scoped_lockable:
|
|
|
|
handleLockableAttr(S, D, Attr, /*scoped = */true);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_no_thread_safety_analysis:
|
|
|
|
handleNoThreadSafetyAttr(S, D, Attr);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_lockable:
|
|
|
|
handleLockableAttr(S, D, Attr);
|
|
|
|
break;
|
2011-07-29 04:12:35 +08:00
|
|
|
case AttributeList::AT_guarded_by:
|
|
|
|
handleGuardedByAttr(S, D, Attr);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_pt_guarded_by:
|
|
|
|
handleGuardedByAttr(S, D, Attr, /*pointer = */true);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_exclusive_lock_function:
|
|
|
|
handleLockFunAttr(S, D, Attr, /*exclusive = */true);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_exclusive_locks_required:
|
|
|
|
handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_exclusive_trylock_function:
|
|
|
|
handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_lock_returned:
|
|
|
|
handleLockReturnedAttr(S, D, Attr);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_locks_excluded:
|
|
|
|
handleLocksExcludedAttr(S, D, Attr);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_shared_lock_function:
|
|
|
|
handleLockFunAttr(S, D, Attr);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_shared_locks_required:
|
|
|
|
handleLocksRequiredAttr(S, D, Attr);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_shared_trylock_function:
|
|
|
|
handleTrylockFunAttr(S, D, Attr);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_unlock_function:
|
|
|
|
handleUnlockFunAttr(S, D, Attr);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_acquired_before:
|
|
|
|
handleAcquireOrderAttr(S, D, Attr, /*before = */true);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_acquired_after:
|
|
|
|
handleAcquireOrderAttr(S, D, Attr, /*before = */false);
|
|
|
|
break;
|
2011-07-29 01:21:07 +08:00
|
|
|
|
2008-06-29 08:43:07 +08:00
|
|
|
default:
|
2010-01-10 20:58:08 +08:00
|
|
|
// Ask target about the attribute.
|
|
|
|
const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
|
|
|
|
if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
|
2010-07-08 17:42:26 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
|
|
|
|
<< Attr.getName();
|
2008-06-29 08:43:07 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-21 10:08:45 +08:00
|
|
|
/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
|
|
|
|
/// the attribute applies to decls. If the attribute is a type attribute, just
|
|
|
|
/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
|
|
|
|
/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
|
2011-07-02 08:01:44 +08:00
|
|
|
static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
|
|
|
|
const AttributeList &Attr,
|
2011-01-21 10:08:45 +08:00
|
|
|
bool NonInheritable, bool Inheritable) {
|
|
|
|
if (Attr.isInvalid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
|
|
|
|
// FIXME: Try to deal with other __declspec attributes!
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (NonInheritable)
|
2011-07-02 08:01:44 +08:00
|
|
|
ProcessNonInheritableDeclAttr(S, scope, D, Attr);
|
2011-01-21 10:08:45 +08:00
|
|
|
|
|
|
|
if (Inheritable)
|
2011-07-02 08:01:44 +08:00
|
|
|
ProcessInheritableDeclAttr(S, scope, D, Attr);
|
2011-01-21 10:08:45 +08:00
|
|
|
}
|
|
|
|
|
2008-06-29 08:43:07 +08:00
|
|
|
/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
|
|
|
|
/// attribute list to the specified decl, ignoring any type attributes.
|
2010-12-02 06:13:54 +08:00
|
|
|
void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
|
2011-01-21 10:08:45 +08:00
|
|
|
const AttributeList *AttrList,
|
|
|
|
bool NonInheritable, bool Inheritable) {
|
2010-02-24 06:00:30 +08:00
|
|
|
for (const AttributeList* l = AttrList; l; l = l->getNext()) {
|
2011-07-02 08:01:44 +08:00
|
|
|
ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
|
2010-02-24 06:00:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GCC accepts
|
|
|
|
// static int a9 __attribute__((weakref));
|
|
|
|
// but that looks really pointless. We reject it.
|
2011-01-21 10:08:45 +08:00
|
|
|
if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
|
2010-02-24 06:00:30 +08:00
|
|
|
Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
|
2010-07-31 09:52:11 +08:00
|
|
|
dyn_cast<NamedDecl>(D)->getNameAsString();
|
2010-02-24 06:00:30 +08:00
|
|
|
return;
|
2008-06-29 08:43:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-30 11:15:39 +08:00
|
|
|
/// DeclClonePragmaWeak - clone existing decl (maybe definition),
|
|
|
|
/// #pragma weak needs a non-definition decl and source may not have one
|
2011-09-07 12:05:06 +08:00
|
|
|
NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
|
|
|
|
SourceLocation Loc) {
|
2009-07-31 10:52:19 +08:00
|
|
|
assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
|
2009-07-30 11:15:39 +08:00
|
|
|
NamedDecl *NewD = 0;
|
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
|
2011-09-07 12:05:06 +08:00
|
|
|
FunctionDecl *NewFD;
|
|
|
|
// FIXME: Missing call to CheckFunctionDeclaration().
|
|
|
|
// FIXME: Mangling?
|
|
|
|
// FIXME: Is the qualifier info correct?
|
|
|
|
// FIXME: Is the DeclContext correct?
|
|
|
|
NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
|
|
|
|
Loc, Loc, DeclarationName(II),
|
|
|
|
FD->getType(), FD->getTypeSourceInfo(),
|
|
|
|
SC_None, SC_None,
|
|
|
|
false/*isInlineSpecified*/,
|
|
|
|
FD->hasPrototype(),
|
|
|
|
false/*isConstexprSpecified*/);
|
|
|
|
NewD = NewFD;
|
|
|
|
|
|
|
|
if (FD->getQualifier())
|
2011-02-25 10:25:35 +08:00
|
|
|
NewFD->setQualifierInfo(FD->getQualifierLoc());
|
2011-09-07 12:05:06 +08:00
|
|
|
|
|
|
|
// Fake up parameter variables; they are declared as if this were
|
|
|
|
// a typedef.
|
|
|
|
QualType FDTy = FD->getType();
|
|
|
|
if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
|
|
|
|
SmallVector<ParmVarDecl*, 16> Params;
|
|
|
|
for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
|
|
|
|
AE = FT->arg_type_end(); AI != AE; ++AI) {
|
|
|
|
ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
|
|
|
|
Param->setScopeInfo(0, Params.size());
|
|
|
|
Params.push_back(Param);
|
|
|
|
}
|
2011-09-22 02:16:56 +08:00
|
|
|
NewFD->setParams(Params);
|
2010-03-15 18:12:16 +08:00
|
|
|
}
|
2009-07-30 11:15:39 +08:00
|
|
|
} else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
|
|
|
|
NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
|
2011-03-08 16:55:46 +08:00
|
|
|
VD->getInnerLocStart(), VD->getLocation(), II,
|
2009-12-07 10:54:59 +08:00
|
|
|
VD->getType(), VD->getTypeSourceInfo(),
|
2010-04-20 06:54:31 +08:00
|
|
|
VD->getStorageClass(),
|
|
|
|
VD->getStorageClassAsWritten());
|
2010-03-15 18:12:16 +08:00
|
|
|
if (VD->getQualifier()) {
|
|
|
|
VarDecl *NewVD = cast<VarDecl>(NewD);
|
2011-02-25 10:25:35 +08:00
|
|
|
NewVD->setQualifierInfo(VD->getQualifierLoc());
|
2010-03-15 18:12:16 +08:00
|
|
|
}
|
2009-07-30 11:15:39 +08:00
|
|
|
}
|
|
|
|
return NewD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
|
|
|
|
/// applied to it, possibly with an alias.
|
2009-07-31 10:52:19 +08:00
|
|
|
void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
|
2009-09-09 02:10:11 +08:00
|
|
|
if (W.getUsed()) return; // only do this once
|
|
|
|
W.setUsed(true);
|
|
|
|
if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
|
|
|
|
IdentifierInfo *NDId = ND->getIdentifier();
|
2011-09-07 12:05:06 +08:00
|
|
|
NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
|
2010-08-19 07:23:40 +08:00
|
|
|
NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
|
|
|
|
NDId->getName()));
|
|
|
|
NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
|
2009-09-09 02:10:11 +08:00
|
|
|
WeakTopLevelDecl.push_back(NewD);
|
|
|
|
// FIXME: "hideous" code from Sema::LazilyCreateBuiltin
|
|
|
|
// to insert Decl at TU scope, sorry.
|
|
|
|
DeclContext *SavedContext = CurContext;
|
|
|
|
CurContext = Context.getTranslationUnitDecl();
|
|
|
|
PushOnScopeChains(NewD, S);
|
|
|
|
CurContext = SavedContext;
|
|
|
|
} else { // just add weak to existing
|
2010-08-19 07:23:40 +08:00
|
|
|
ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
|
2009-07-30 11:15:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-29 08:23:49 +08:00
|
|
|
/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
|
|
|
|
/// it, apply them to D. This is a bit tricky because PD can have attributes
|
|
|
|
/// specified in many different places, and we need to find and apply them all.
|
2011-01-21 10:08:45 +08:00
|
|
|
void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
|
|
|
|
bool NonInheritable, bool Inheritable) {
|
2010-10-27 08:59:00 +08:00
|
|
|
// It's valid to "forward-declare" #pragma weak, in which case we
|
|
|
|
// have to do this.
|
2011-07-29 02:09:57 +08:00
|
|
|
if (Inheritable) {
|
|
|
|
LoadExternalWeakUndeclaredIdentifiers();
|
|
|
|
if (!WeakUndeclaredIdentifiers.empty()) {
|
|
|
|
if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
|
|
|
|
if (IdentifierInfo *Id = ND->getIdentifier()) {
|
|
|
|
llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
|
|
|
|
= WeakUndeclaredIdentifiers.find(Id);
|
|
|
|
if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
|
|
|
|
WeakInfo W = I->second;
|
|
|
|
DeclApplyPragmaWeak(S, ND, W);
|
|
|
|
WeakUndeclaredIdentifiers[Id] = W;
|
|
|
|
}
|
2010-10-27 08:59:00 +08:00
|
|
|
}
|
2009-07-30 11:15:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-29 08:23:49 +08:00
|
|
|
// Apply decl attributes from the DeclSpec if present.
|
2010-12-24 10:08:15 +08:00
|
|
|
if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
|
2011-01-21 10:08:45 +08:00
|
|
|
ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2008-06-29 08:23:49 +08:00
|
|
|
// Walk the declarator structure, applying decl attributes that were in a type
|
|
|
|
// position to the decl itself. This handles cases like:
|
|
|
|
// int *__attr__(x)** D;
|
|
|
|
// when X is a decl attribute.
|
|
|
|
for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
|
|
|
|
if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
|
2011-01-21 10:08:45 +08:00
|
|
|
ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
|
2009-07-25 03:02:52 +08:00
|
|
|
|
2008-06-29 08:23:49 +08:00
|
|
|
// Finally, apply any attributes on the decl itself.
|
|
|
|
if (const AttributeList *Attrs = PD.getAttributes())
|
2011-01-21 10:08:45 +08:00
|
|
|
ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
|
2008-06-29 08:23:49 +08:00
|
|
|
}
|
2009-11-04 10:18:39 +08:00
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
/// Is the given declaration allowed to use a forbidden type?
|
|
|
|
static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
|
|
|
|
// Private ivars are always okay. Unfortunately, people don't
|
|
|
|
// always properly make their ivars private, even in system headers.
|
|
|
|
// Plus we need to make fields okay, too.
|
|
|
|
if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Require it to be declared in a system header.
|
|
|
|
return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle a delayed forbidden-type diagnostic.
|
|
|
|
static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
|
|
|
|
Decl *decl) {
|
|
|
|
if (decl && isForbiddenTypeAllowed(S, decl)) {
|
|
|
|
decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
|
|
|
|
"this system declaration uses an unsupported type"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
|
|
|
|
<< diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
|
|
|
|
diag.Triggered = true;
|
|
|
|
}
|
|
|
|
|
2011-02-14 15:13:47 +08:00
|
|
|
// This duplicates a vector push_back but hides the need to know the
|
|
|
|
// size of the type.
|
|
|
|
void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
|
|
|
|
assert(StackSize <= StackCapacity);
|
|
|
|
|
|
|
|
// Grow the stack if necessary.
|
|
|
|
if (StackSize == StackCapacity) {
|
|
|
|
unsigned newCapacity = 2 * StackCapacity + 2;
|
|
|
|
char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
|
|
|
|
const char *oldBuffer = (const char*) Stack;
|
|
|
|
|
|
|
|
if (StackCapacity)
|
|
|
|
memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
|
|
|
|
|
|
|
|
delete[] oldBuffer;
|
|
|
|
Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
|
|
|
|
StackCapacity = newCapacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(StackSize < StackCapacity);
|
|
|
|
new (&Stack[StackSize++]) DelayedDiagnostic(diag);
|
2009-11-04 10:18:39 +08:00
|
|
|
}
|
|
|
|
|
2011-02-14 15:13:47 +08:00
|
|
|
void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
|
|
|
|
Decl *decl) {
|
|
|
|
DelayedDiagnostics &DD = S.DelayedDiagnostics;
|
2009-11-04 10:18:39 +08:00
|
|
|
|
2011-02-14 15:13:47 +08:00
|
|
|
// Check the invariants.
|
|
|
|
assert(DD.StackSize >= state.SavedStackSize);
|
|
|
|
assert(state.SavedStackSize >= DD.ActiveStackBase);
|
|
|
|
assert(DD.ParsingDepth > 0);
|
2009-11-04 10:18:39 +08:00
|
|
|
|
2011-02-14 15:13:47 +08:00
|
|
|
// Drop the parsing depth.
|
|
|
|
DD.ParsingDepth--;
|
2009-11-04 10:18:39 +08:00
|
|
|
|
2011-02-14 15:13:47 +08:00
|
|
|
// If there are no active diagnostics, we're done.
|
|
|
|
if (DD.StackSize == DD.ActiveStackBase)
|
|
|
|
return;
|
2010-03-16 13:22:47 +08:00
|
|
|
|
2010-01-27 11:50:35 +08:00
|
|
|
// We only want to actually emit delayed diagnostics when we
|
|
|
|
// successfully parsed a decl.
|
2011-06-25 03:59:27 +08:00
|
|
|
if (decl && !decl->isInvalidDecl()) {
|
2011-02-14 15:13:47 +08:00
|
|
|
// We emit all the active diagnostics, not just those starting
|
|
|
|
// from the saved state. The idea is this: we get one push for a
|
2010-01-27 11:50:35 +08:00
|
|
|
// decl spec and another for each declarator; in a decl group like:
|
|
|
|
// deprecated_typedef foo, *bar, baz();
|
|
|
|
// only the declarator pops will be passed decls. This is correct;
|
|
|
|
// we really do need to consider delayed diagnostics from the decl spec
|
|
|
|
// for each of the different declarations.
|
2011-02-14 15:13:47 +08:00
|
|
|
for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
|
|
|
|
DelayedDiagnostic &diag = DD.Stack[i];
|
|
|
|
if (diag.Triggered)
|
2010-01-27 11:50:35 +08:00
|
|
|
continue;
|
|
|
|
|
2011-02-14 15:13:47 +08:00
|
|
|
switch (diag.Kind) {
|
2010-01-27 11:50:35 +08:00
|
|
|
case DelayedDiagnostic::Deprecation:
|
2011-02-14 15:13:47 +08:00
|
|
|
S.HandleDelayedDeprecationCheck(diag, decl);
|
2010-01-27 11:50:35 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DelayedDiagnostic::Access:
|
2011-02-14 15:13:47 +08:00
|
|
|
S.HandleDelayedAccessCheck(diag, decl);
|
2010-01-27 11:50:35 +08:00
|
|
|
break;
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
case DelayedDiagnostic::ForbiddenType:
|
|
|
|
handleDelayedForbiddenType(S, diag, decl);
|
|
|
|
break;
|
2009-11-04 10:18:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-16 13:22:47 +08:00
|
|
|
// Destroy all the delayed diagnostics we're about to pop off.
|
2011-02-14 15:13:47 +08:00
|
|
|
for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
|
2011-03-23 23:13:44 +08:00
|
|
|
DD.Stack[i].Destroy();
|
2010-03-16 13:22:47 +08:00
|
|
|
|
2011-02-14 15:13:47 +08:00
|
|
|
DD.StackSize = state.SavedStackSize;
|
2010-01-27 11:50:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isDeclDeprecated(Decl *D) {
|
|
|
|
do {
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
if (D->isDeprecated())
|
2010-01-27 11:50:35 +08:00
|
|
|
return true;
|
|
|
|
} while ((D = cast_or_null<Decl>(D->getDeclContext())));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-26 10:13:20 +08:00
|
|
|
void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
|
2010-01-27 11:50:35 +08:00
|
|
|
Decl *Ctx) {
|
|
|
|
if (isDeclDeprecated(Ctx))
|
|
|
|
return;
|
|
|
|
|
|
|
|
DD.Triggered = true;
|
2010-10-09 23:49:00 +08:00
|
|
|
if (!DD.getDeprecationMessage().empty())
|
2010-10-07 05:18:44 +08:00
|
|
|
Diag(DD.Loc, diag::warn_deprecated_message)
|
2010-10-09 23:49:00 +08:00
|
|
|
<< DD.getDeprecationDecl()->getDeclName()
|
|
|
|
<< DD.getDeprecationMessage();
|
2010-10-07 05:18:44 +08:00
|
|
|
else
|
|
|
|
Diag(DD.Loc, diag::warn_deprecated)
|
2010-10-09 23:49:00 +08:00
|
|
|
<< DD.getDeprecationDecl()->getDeclName();
|
2009-11-04 10:18:39 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
|
2010-12-21 08:44:01 +08:00
|
|
|
SourceLocation Loc,
|
2011-04-24 01:27:19 +08:00
|
|
|
const ObjCInterfaceDecl *UnknownObjCClass) {
|
2009-11-04 10:18:39 +08:00
|
|
|
// Delay if we're currently parsing a declaration.
|
2011-02-14 15:13:47 +08:00
|
|
|
if (DelayedDiagnostics.shouldDelayDiagnostics()) {
|
|
|
|
DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message));
|
2009-11-04 10:18:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, don't warn if our current context is deprecated.
|
|
|
|
if (isDeclDeprecated(cast<Decl>(CurContext)))
|
|
|
|
return;
|
2010-10-09 23:49:00 +08:00
|
|
|
if (!Message.empty())
|
2010-10-07 05:18:44 +08:00
|
|
|
Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
|
|
|
|
<< Message;
|
2010-12-21 08:44:01 +08:00
|
|
|
else {
|
2011-01-03 03:53:12 +08:00
|
|
|
if (!UnknownObjCClass)
|
2010-12-21 08:44:01 +08:00
|
|
|
Diag(Loc, diag::warn_deprecated) << D->getDeclName();
|
2011-04-24 01:27:19 +08:00
|
|
|
else {
|
2010-12-21 08:44:01 +08:00
|
|
|
Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
|
2011-04-24 01:27:19 +08:00
|
|
|
Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
|
|
|
|
}
|
2010-12-21 08:44:01 +08:00
|
|
|
}
|
2009-11-04 10:18:39 +08:00
|
|
|
}
|