Newer
Older
//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements decl-related attribute processing.
//
//===----------------------------------------------------------------------===//
#include "Sema.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/Expr.h"
#include "clang/Basic/TargetInfo.h"
#include <llvm/ADT/StringExtras.h>
using namespace clang;
//===----------------------------------------------------------------------===//
// Helper functions
//===----------------------------------------------------------------------===//
static const FunctionType *getFunctionType(Decl *d) {
QualType Ty;
if (ValueDecl *decl = dyn_cast<ValueDecl>(d))
Ty = decl->getType();
else if (FieldDecl *decl = dyn_cast<FieldDecl>(d))
Ty = decl->getType();
else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Ty = decl->getUnderlyingType();
else
return 0;
if (Ty->isFunctionPointerType())
Ty = Ty->getAsPointerType()->getPointeeType();
// FIXME: We should provide an abstraction around a method or function
// to provide the following bits of information.
/// isFunctionOrMethod - Return true if the given decl has function
/// type (function or function-typed variable) or an Objective-C
/// method.
static bool isFunctionOrMethod(Decl *d) {
return getFunctionType(d) || isa<ObjCMethodDecl>(d);
}
/// hasFunctionProto - Return true if the given decl has a argument
/// information. This decl should have already passed
/// isFunctionOrMethod.
static bool hasFunctionProto(Decl *d) {
if (const FunctionType *FnTy = getFunctionType(d)) {
return isa<FunctionProtoType>(FnTy);
} else {
assert(isa<ObjCMethodDecl>(d));
return true;
}
}
/// getFunctionOrMethodNumArgs - Return number of function or method
/// arguments. It is an error to call this on a K&R function (use
/// hasFunctionProto first).
static unsigned getFunctionOrMethodNumArgs(Decl *d) {
if (const FunctionType *FnTy = getFunctionType(d))
return cast<FunctionProtoType>(FnTy)->getNumArgs();
return cast<ObjCMethodDecl>(d)->param_size();
}
static QualType getFunctionOrMethodArgType(Decl *d, unsigned Idx) {
if (const FunctionType *FnTy = getFunctionType(d))
return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
}
static bool isFunctionOrMethodVariadic(Decl *d) {
if (const FunctionType *FnTy = getFunctionType(d)) {
const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
return proto->isVariadic();
} else {
return cast<ObjCMethodDecl>(d)->isVariadic();
}
}
static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
const PointerType *PT = T->getAsPointerType();
if (!PT)
return false;
const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
if (!ClsT)
return false;
IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
// FIXME: Should we walk the chain of classes?
return ClsName == &Ctx.Idents.get("NSString") ||
ClsName == &Ctx.Idents.get("NSMutableString");
}
static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
const PointerType *PT = T->getAsPointerType();
if (!PT)
return false;
const RecordType *RT = PT->getPointeeType()->getAsRecordType();
if (!RT)
return false;
const RecordDecl *RD = RT->getDecl();
if (RD->getTagKind() != TagDecl::TK_struct)
return false;
return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
}
//===----------------------------------------------------------------------===//
// Attribute Implementations
//===----------------------------------------------------------------------===//
// FIXME: All this manual attribute parsing code is gross. At the
// least add some helper functions to check most argument patterns (#
// and types of args).
static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
Sema &S) {
TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
if (tDecl == 0) {
S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
return;
}
QualType curType = tDecl->getUnderlyingType();
// check the attribute arguments.
if (Attr.getNumArgs() != 1) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
llvm::APSInt vecSize(32);
if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
<< "ext_vector_type" << sizeExpr->getSourceRange();
return;
}
// unlike gcc's vector_size attribute, we do not allow vectors to be defined
// in conjunction with complex types (pointers, arrays, functions, etc.).
if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << curType;
return;
}
// unlike gcc's vector_size attribute, the size is specified as the
// number of elements, not the number of bytes.
unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
if (vectorSize == 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
<< sizeExpr->getSourceRange();
return;
}
// Instantiate/Install the vector type, the number of elements is > 0.
tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
// Remember this typedef decl, we will need it later for diagnostics.
S.ExtVectorDecls.push_back(tDecl);
/// HandleVectorSizeAttribute - this attribute is only applicable to
/// integral and float scalars, although arrays, pointers, and function
/// return values are allowed in conjunction with this construct. Aggregates
/// with this attribute are invalid, even if they are of the same size as a
/// corresponding scalar.
/// The raw attribute should contain precisely 1 argument, the vector size
/// for the variable, measured in bytes. If curType and rawAttr are well
/// formed, this routine will return a new vector type.
static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
QualType CurType;
if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
CurType = VD->getType();
else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
CurType = TD->getUnderlyingType();
else {
S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
<< "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
return;
}
// Check the attribute arugments.
if (Attr.getNumArgs() != 1) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
llvm::APSInt vecSize(32);
if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
<< "vector_size" << sizeExpr->getSourceRange();
}
// navigate to the base type - we need to provide for vector pointers,
// vector arrays, and functions returning vectors.
if (CurType->isPointerType() || CurType->isArrayType() ||
CurType->isFunctionType()) {
assert(0 && "HandleVector(): Complex type construction unimplemented");
/* FIXME: rebuild the type from the inside out, vectorizing the inner type.
do {
if (PointerType *PT = dyn_cast<PointerType>(canonType))
canonType = PT->getPointeeType().getTypePtr();
else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
canonType = AT->getElementType().getTypePtr();
else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
canonType = FT->getResultType().getTypePtr();
} while (canonType->isPointerType() || canonType->isArrayType() ||
canonType->isFunctionType());
*/
}
// the base type must be integer or float.
if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
// vecSize is specified in bytes - convert to bits.
unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
// the vector size needs to be an integral multiple of the type size.
if (vectorSize % typeSize) {
S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
<< sizeExpr->getSourceRange();
}
if (vectorSize == 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
<< sizeExpr->getSourceRange();
// Success! Instantiate the vector type, the number of elements is > 0, and
// not required to be a power of 2, unlike GCC.
CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
VD->setType(CurType);
else
cast<TypedefDecl>(D)->setUnderlyingType(CurType);
static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() > 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
return;
}
if (TagDecl *TD = dyn_cast<TagDecl>(d))
Chris Lattner
committed
TD->addAttr(::new (S.Context) PackedAttr(1));
else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
// If the alignment is less than or equal to 8 bits, the packed attribute
// has no effect.
if (!FD->getType()->isIncompleteType() &&
S.Context.getTypeAlign(FD->getType()) <= 8)
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
<< Attr.getName() << FD->getType();
Chris Lattner
committed
FD->addAttr(::new (S.Context) PackedAttr(1));
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Ted Kremenek
committed
static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() > 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek
committed
return;
}
// The IBOutlet attribute only applies to instance variables of Objective-C
// classes.
if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
Chris Lattner
committed
d->addAttr(::new (S.Context) IBOutletAttr());
Ted Kremenek
committed
else
S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek
committed
}
static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
// GCC ignores the nonnull attribute on K&R style function
// prototypes, so we ignore it as well
if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
return;
}
unsigned NumArgs = getFunctionOrMethodNumArgs(d);
// The nonnull attribute only applies to pointers.
llvm::SmallVector<unsigned, 10> NonNullArgs;
for (AttributeList::arg_iterator I=Attr.arg_begin(),
E=Attr.arg_end(); I!=E; ++I) {
// The argument must be an integer constant expression.
Expr *Ex = static_cast<Expr *>(*I);
llvm::APSInt ArgNum(32);
if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
<< "nonnull" << Ex->getSourceRange();
return;
}
unsigned x = (unsigned) ArgNum.getZExtValue();
if (x < 1 || x > NumArgs) {
S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner
committed
<< "nonnull" << I.getArgNum() << Ex->getSourceRange();
return;
}
// Is the function argument a pointer type?
QualType T = getFunctionOrMethodArgType(d, x);
if (!T->isPointerType() && !T->isBlockPointerType()) {
// FIXME: Should also highlight argument in decl.
S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
<< "nonnull" << Ex->getSourceRange();
}
NonNullArgs.push_back(x);
}
// If no arguments were specified to __attribute__((nonnull)) then all
// pointer arguments have a nonnull attribute.
if (NonNullArgs.empty()) {
for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
QualType T = getFunctionOrMethodArgType(d, I);
if (T->isPointerType() || T->isBlockPointerType())
if (NonNullArgs.empty()) {
S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
return;
}
}
unsigned* start = &NonNullArgs[0];
unsigned size = NonNullArgs.size();
std::sort(start, start + size);
Chris Lattner
committed
d->addAttr(::new (S.Context) NonNullAttr(start, size));
}
static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() != 1) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
return;
}
Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Arg = Arg->IgnoreParenCasts();
StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
if (Str == 0 || Str->isWide()) {
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
return;
}
const char *Alias = Str->getStrData();
unsigned AliasLen = Str->getByteLength();
// FIXME: check if target symbol exists in current file
Chris Lattner
committed
d->addAttr(::new (S.Context) AliasAttr(std::string(Alias, AliasLen)));
static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
if (!isFunctionOrMethod(d)) {
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
<< "always_inline" << 0 /*function*/;
return;
}
Chris Lattner
committed
d->addAttr(::new (S.Context) AlwaysInlineAttr());
static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
return;
}
Chris Lattner
committed
d->addAttr(::new (S.Context) NoReturnAttr());
static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Chris Lattner
committed
d->addAttr(::new (S.Context) UnusedAttr());
Daniel Dunbar
committed
static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
return;
}
if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbar
committed
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
return;
}
} else if (!isFunctionOrMethod(d)) {
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Daniel Dunbar
committed
return;
}
Chris Lattner
committed
d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbar
committed
}
static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
<< "0 or 1";
return;
}
int priority = 65535; // FIXME: Do not hardcode such constants.
if (Attr.getNumArgs() > 0) {
Expr *E = static_cast<Expr *>(Attr.getArg(0));
llvm::APSInt Idx(32);
if (!E->isIntegerConstantExpr(Idx, S.Context)) {
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
<< "constructor" << 1 << E->getSourceRange();
return;
}
priority = Idx.getZExtValue();
}
FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
if (!Fn) {
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
return;
}
Chris Lattner
committed
d->addAttr(::new (S.Context) ConstructorAttr(priority));
}
static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
<< "0 or 1";
return;
}
int priority = 65535; // FIXME: Do not hardcode such constants.
if (Attr.getNumArgs() > 0) {
Expr *E = static_cast<Expr *>(Attr.getArg(0));
llvm::APSInt Idx(32);
if (!E->isIntegerConstantExpr(Idx, S.Context)) {
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
<< "destructor" << 1 << E->getSourceRange();
return;
}
priority = Idx.getZExtValue();
}
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
return;
}
Chris Lattner
committed
d->addAttr(::new (S.Context) DestructorAttr(priority));
static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
return;
}
Chris Lattner
committed
d->addAttr(::new (S.Context) DeprecatedAttr());
static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
return;
}
Chris Lattner
committed
d->addAttr(::new (S.Context) UnavailableAttr());
static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() != 1) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
return;
}
Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Arg = Arg->IgnoreParenCasts();
StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
if (Str == 0 || Str->isWide()) {
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
return;
}
const char *TypeStr = Str->getStrData();
unsigned TypeLen = Str->getByteLength();
VisibilityAttr::VisibilityTypes type;
if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
type = VisibilityAttr::DefaultVisibility;
else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
type = VisibilityAttr::HiddenVisibility;
else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
type = VisibilityAttr::HiddenVisibility; // FIXME
else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
type = VisibilityAttr::ProtectedVisibility;
else {
S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
return;
}
Chris Lattner
committed
d->addAttr(::new (S.Context) VisibilityAttr(type));
static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
Sema &S) {
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
return;
}
ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
if (OCI == 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
return;
}
Chris Lattner
committed
D->addAttr(::new (S.Context) ObjCExceptionAttr());
}
static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian
committed
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
return;
}
if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian
committed
QualType T = TD->getUnderlyingType();
if (!T->isPointerType() ||
!T->getAsPointerType()->getPointeeType()->isRecordType()) {
S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
return;
}
}
Chris Lattner
committed
D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanian
committed
}
static void
HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
return;
}
if (!isa<FunctionDecl>(D)) {
S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
return;
}
Chris Lattner
committed
D->addAttr(::new (S.Context) OverloadableAttr());
static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
if (!Attr.getParameterName()) {
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
return;
}
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
return;
}
BlocksAttr::BlocksAttrTypes type;
if (Attr.getParameterName()->isStr("byref"))
type = BlocksAttr::ByRef;
else {
S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
<< "blocks" << Attr.getParameterName();
return;
}
Chris Lattner
committed
d->addAttr(::new (S.Context) BlocksAttr(type));
}
Anders Carlsson
committed
static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() > 2) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
<< "0, 1 or 2";
Anders Carlsson
committed
return;
}
int sentinel = 0;
if (Attr.getNumArgs() > 0) {
Expr *E = static_cast<Expr *>(Attr.getArg(0));
llvm::APSInt Idx(32);
if (!E->isIntegerConstantExpr(Idx, S.Context)) {
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
<< "sentinel" << 1 << E->getSourceRange();
Anders Carlsson
committed
return;
}
sentinel = Idx.getZExtValue();
if (sentinel < 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
<< E->getSourceRange();
Anders Carlsson
committed
return;
}
}
int nullPos = 0;
if (Attr.getNumArgs() > 1) {
Expr *E = static_cast<Expr *>(Attr.getArg(1));
llvm::APSInt Idx(32);
if (!E->isIntegerConstantExpr(Idx, S.Context)) {
S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
<< "sentinel" << 2 << E->getSourceRange();
Anders Carlsson
committed
return;
}
nullPos = Idx.getZExtValue();
if (nullPos > 1 || nullPos < 0) {
// FIXME: This error message could be improved, it would be nice
// to say what the bounds actually are.
S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
<< E->getSourceRange();
Anders Carlsson
committed
return;
}
}
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Chris Lattner
committed
const FunctionType *FT = FD->getType()->getAsFunctionType();
assert(FT && "FunctionDecl has non-function type?");
if (isa<FunctionNoProtoType>(FT)) {
S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
return;
}
if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Anders Carlsson
committed
S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
return;
}
} else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
if (!MD->isVariadic()) {
S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
return;
}
} else {
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Anders Carlsson
committed
return;
}
// FIXME: Actually create the attribute.
}
static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
return;
}
// TODO: could also be applied to methods?
FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
if (!Fn) {
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
<< "warn_unused_result" << 0 /*function*/;
return;
}
Chris Lattner
committed
Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
}
static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
// TODO: could also be applied to methods?
if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
<< "weak" << 2 /*variable and function*/;
return;
}
Chris Lattner
committed
D->addAttr(::new (S.Context) WeakAttr());
static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
return;
}
// weak_import only applies to variable & function declarations.
bool isDef = false;
if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
isDef = (!VD->hasExternalStorage() || VD->getInit());
} else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
isDef = FD->getBody();
} else if (isa<ObjCPropertyDecl>(D)) {
// We ignore weak import on properties
} else {
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
<< "weak_import" << 2 /*variable and function*/;
return;
}
// Merge should handle any subsequent violations.
if (isDef) {
S.Diag(Attr.getLoc(),
diag::warn_attribute_weak_import_invalid_on_definition)
<< "weak_import" << 2 /*variable and function*/;
return;
}
D->addAttr(::new (S.Context) WeakImportAttr());
}
static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
// Attribute can be applied only to functions or variables.
Chris Lattner
committed
D->addAttr(::new (S.Context) DLLImportAttr());
return;
}
if (!FD) {
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
return;
}
// Currently, the dllimport attribute is ignored for inlined functions.
// Warning is emitted.
if (FD->isInline()) {
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
return;
}
// The attribute is also overridden by a subsequent declaration as dllexport.
// Warning is emitted.
for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
nextAttr = nextAttr->getNext()) {
if (nextAttr->getKind() == AttributeList::AT_dllexport) {
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
return;
}
}
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
return;
}
Chris Lattner
committed
D->addAttr(::new (S.Context) DLLImportAttr());
static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
// Attribute can be applied only to functions or variables.
Chris Lattner
committed
D->addAttr(::new (S.Context) DLLExportAttr());
return;
}
if (!FD) {
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
return;
}
// Currently, the dllexport attribute is ignored for inlined functions,
// unless the -fkeep-inline-functions flag has been used. Warning is emitted;
if (FD->isInline()) {
// FIXME: ... unless the -fkeep-inline-functions flag has been used.
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
return;
}
Chris Lattner
committed
D->addAttr(::new (S.Context) DLLExportAttr());
static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
// Attribute has no arguments.
if (Attr.getNumArgs() != 1) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
return;
}
// Make sure that there is a string literal as the sections's single
// argument.
StringLiteral *SE =
dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0)));
if (!SE) {
// FIXME
S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
return;
}
Chris Lattner
committed
D->addAttr(::new (S.Context) SectionAttr(std::string(SE->getStrData(),
SE->getByteLength())));
static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
// Attribute has no arguments.
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
// Attribute can be applied only to functions.
if (!isa<FunctionDecl>(d)) {
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
return;
}
// stdcall and fastcall attributes are mutually incompatible.
if (d->getAttr<FastCallAttr>()) {
S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
<< "stdcall" << "fastcall";
return;
}
Chris Lattner
committed
d->addAttr(::new (S.Context) StdCallAttr());
static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
// Attribute has no arguments.
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
if (!isa<FunctionDecl>(d)) {
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
return;
}
// stdcall and fastcall attributes are mutually incompatible.
if (d->getAttr<StdCallAttr>()) {
S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
<< "fastcall" << "stdcall";
return;
}
Chris Lattner
committed
d->addAttr(::new (S.Context) FastCallAttr());
static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
return;
}
Chris Lattner
committed
d->addAttr(::new (S.Context) NoThrowAttr());
static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
return;
}
Chris Lattner
committed
d->addAttr(::new (S.Context) ConstAttr());
}
static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
return;
}
Chris Lattner
committed
d->addAttr(::new (S.Context) PureAttr());
static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
// Match gcc which ignores cleanup attrs when compiling C++.
if (S.getLangOptions().CPlusPlus)
return;
if (!Attr.getParameterName()) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
return;
}
if (Attr.getNumArgs() != 0) {
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
return;
}
VarDecl *VD = dyn_cast<VarDecl>(d);
if (!VD || !VD->hasLocalStorage()) {
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
return;
}
// Look up the function
NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
Sema::LookupOrdinaryName);
S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Attr.getParameterName();
return;
}
FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
if (!FD) {
S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Attr.getParameterName();
return;
}
if (FD->getNumParams() != 1) {
S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Attr.getParameterName();
return;
}
// We're currently more strict than GCC about what function types we accept.