Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1 | //===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements decl-related attribute processing. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | acc5f3e | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
| 17 | #include "clang/AST/Expr.h" |
Daniel Dunbar | e4858a6 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | 12bc692 | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 20 | #include "clang/Parse/DeclSpec.h" |
Ted Kremenek | 6e1eb87 | 2008-07-22 16:56:21 +0000 | [diff] [blame] | 21 | #include <llvm/ADT/StringExtras.h> |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | // Helper functions |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame^] | 28 | static const FunctionType *getFunctionType(Decl *d) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 29 | QualType Ty; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 30 | if (ValueDecl *decl = dyn_cast<ValueDecl>(d)) |
| 31 | Ty = decl->getType(); |
| 32 | else if (FieldDecl *decl = dyn_cast<FieldDecl>(d)) |
| 33 | Ty = decl->getType(); |
| 34 | else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d)) |
| 35 | Ty = decl->getUnderlyingType(); |
| 36 | else |
| 37 | return 0; |
| 38 | |
| 39 | if (Ty->isFunctionPointerType()) |
| 40 | Ty = Ty->getAsPointerType()->getPointeeType(); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame^] | 41 | |
| 42 | return Ty->getAsFunctionType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 45 | // FIXME: We should provide an abstraction around a method or function |
| 46 | // to provide the following bits of information. |
| 47 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame^] | 48 | /// isFunctionOrMethod - Return true if the given decl has function |
| 49 | /// type (function or function-typed variable) or an Objective-C |
| 50 | /// method. |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 51 | static bool isFunctionOrMethod(Decl *d) { |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame^] | 52 | return getFunctionType(d) || isa<ObjCMethodDecl>(d); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame^] | 55 | /// hasFunctionProto - Return true if the given decl has a argument |
| 56 | /// information. This decl should have already passed |
| 57 | /// isFunctionOrMethod. |
| 58 | static bool hasFunctionProto(Decl *d) { |
| 59 | if (const FunctionType *FnTy = getFunctionType(d)) { |
| 60 | return isa<FunctionTypeProto>(FnTy); |
| 61 | } else { |
| 62 | assert(isa<ObjCMethodDecl>(d)); |
| 63 | return true; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 68 | /// arguments. It is an error to call this on a K&R function (use |
| 69 | /// hasFunctionProto first). |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 70 | static unsigned getFunctionOrMethodNumArgs(Decl *d) { |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame^] | 71 | if (const FunctionType *FnTy = getFunctionType(d)) { |
| 72 | const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 73 | return proto->getNumArgs(); |
| 74 | } else { |
| 75 | return cast<ObjCMethodDecl>(d)->getNumParams(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static QualType getFunctionOrMethodArgType(Decl *d, unsigned Idx) { |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame^] | 80 | if (const FunctionType *FnTy = getFunctionType(d)) { |
| 81 | const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 82 | return proto->getArgType(Idx); |
| 83 | } else { |
| 84 | return cast<ObjCMethodDecl>(d)->getParamDecl(Idx)->getType(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | static bool isFunctionOrMethodVariadic(Decl *d) { |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame^] | 89 | if (const FunctionType *FnTy = getFunctionType(d)) { |
| 90 | const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 91 | return proto->isVariadic(); |
| 92 | } else { |
| 93 | return cast<ObjCMethodDecl>(d)->isVariadic(); |
| 94 | } |
| 95 | } |
| 96 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 97 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 98 | const PointerType *PT = T->getAsPointerType(); |
| 99 | if (!PT) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 100 | return false; |
| 101 | |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 102 | const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 103 | if (!ClsT) |
| 104 | return false; |
| 105 | |
| 106 | IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier(); |
| 107 | |
| 108 | // FIXME: Should we walk the chain of classes? |
| 109 | return ClsName == &Ctx.Idents.get("NSString") || |
| 110 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 111 | } |
| 112 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 113 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
| 114 | const PointerType *PT = T->getAsPointerType(); |
| 115 | if (!PT) |
| 116 | return false; |
| 117 | |
| 118 | const RecordType *RT = PT->getPointeeType()->getAsRecordType(); |
| 119 | if (!RT) |
| 120 | return false; |
| 121 | |
| 122 | const RecordDecl *RD = RT->getDecl(); |
| 123 | if (RD->getTagKind() != TagDecl::TK_struct) |
| 124 | return false; |
| 125 | |
| 126 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 127 | } |
| 128 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 129 | //===----------------------------------------------------------------------===// |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 130 | // Attribute Implementations |
| 131 | //===----------------------------------------------------------------------===// |
| 132 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 133 | // FIXME: All this manual attribute parsing code is gross. At the |
| 134 | // least add some helper functions to check most argument patterns (# |
| 135 | // and types of args). |
| 136 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 137 | static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr, |
| 138 | Sema &S) { |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 139 | TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d); |
| 140 | if (tDecl == 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 141 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 142 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 145 | QualType curType = tDecl->getUnderlyingType(); |
| 146 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 147 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 148 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 149 | std::string("1")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 150 | return; |
| 151 | } |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 152 | Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 153 | llvm::APSInt vecSize(32); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 154 | if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) { |
| 155 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int, |
| 156 | "ext_vector_type", sizeExpr->getSourceRange()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 157 | return; |
| 158 | } |
| 159 | // unlike gcc's vector_size attribute, we do not allow vectors to be defined |
| 160 | // in conjunction with complex types (pointers, arrays, functions, etc.). |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 161 | if (!curType->isIntegerType() && !curType->isRealFloatingType()) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 162 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type, |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 163 | curType.getAsString()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 164 | return; |
| 165 | } |
| 166 | // unlike gcc's vector_size attribute, the size is specified as the |
| 167 | // number of elements, not the number of bytes. |
| 168 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); |
| 169 | |
| 170 | if (vectorSize == 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 171 | S.Diag(Attr.getLoc(), diag::err_attribute_zero_size, |
| 172 | sizeExpr->getSourceRange()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 173 | return; |
| 174 | } |
| 175 | // Instantiate/Install the vector type, the number of elements is > 0. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 176 | tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 177 | // Remember this typedef decl, we will need it later for diagnostics. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 178 | S.ExtVectorDecls.push_back(tDecl); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 181 | |
| 182 | /// HandleVectorSizeAttribute - this attribute is only applicable to |
| 183 | /// integral and float scalars, although arrays, pointers, and function |
| 184 | /// return values are allowed in conjunction with this construct. Aggregates |
| 185 | /// with this attribute are invalid, even if they are of the same size as a |
| 186 | /// corresponding scalar. |
| 187 | /// The raw attribute should contain precisely 1 argument, the vector size |
| 188 | /// for the variable, measured in bytes. If curType and rawAttr are well |
| 189 | /// formed, this routine will return a new vector type. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 190 | static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 191 | QualType CurType; |
| 192 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 193 | CurType = VD->getType(); |
| 194 | else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 195 | CurType = TD->getUnderlyingType(); |
| 196 | else { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 197 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl, |
| 198 | std::string("vector_size"), |
| 199 | SourceRange(Attr.getLoc(), Attr.getLoc())); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 200 | return; |
| 201 | } |
| 202 | |
| 203 | // Check the attribute arugments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 204 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 205 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 206 | std::string("1")); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 207 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 208 | } |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 209 | Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 210 | llvm::APSInt vecSize(32); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 211 | if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) { |
| 212 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int, |
| 213 | "vector_size", sizeExpr->getSourceRange()); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 214 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 215 | } |
| 216 | // navigate to the base type - we need to provide for vector pointers, |
| 217 | // vector arrays, and functions returning vectors. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 218 | if (CurType->isPointerType() || CurType->isArrayType() || |
| 219 | CurType->isFunctionType()) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 220 | assert(0 && "HandleVector(): Complex type construction unimplemented"); |
| 221 | /* FIXME: rebuild the type from the inside out, vectorizing the inner type. |
| 222 | do { |
| 223 | if (PointerType *PT = dyn_cast<PointerType>(canonType)) |
| 224 | canonType = PT->getPointeeType().getTypePtr(); |
| 225 | else if (ArrayType *AT = dyn_cast<ArrayType>(canonType)) |
| 226 | canonType = AT->getElementType().getTypePtr(); |
| 227 | else if (FunctionType *FT = dyn_cast<FunctionType>(canonType)) |
| 228 | canonType = FT->getResultType().getTypePtr(); |
| 229 | } while (canonType->isPointerType() || canonType->isArrayType() || |
| 230 | canonType->isFunctionType()); |
| 231 | */ |
| 232 | } |
| 233 | // the base type must be integer or float. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 234 | if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 235 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type, |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 236 | CurType.getAsString()); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 237 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 238 | } |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 239 | unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 240 | // vecSize is specified in bytes - convert to bits. |
| 241 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8); |
| 242 | |
| 243 | // the vector size needs to be an integral multiple of the type size. |
| 244 | if (vectorSize % typeSize) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 245 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size, |
| 246 | sizeExpr->getSourceRange()); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 247 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 248 | } |
| 249 | if (vectorSize == 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 250 | S.Diag(Attr.getLoc(), diag::err_attribute_zero_size, |
| 251 | sizeExpr->getSourceRange()); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 252 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 253 | } |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 254 | |
| 255 | // Success! Instantiate the vector type, the number of elements is > 0, and |
| 256 | // not required to be a power of 2, unlike GCC. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 257 | CurType = S.Context.getVectorType(CurType, vectorSize/typeSize); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 258 | |
| 259 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 260 | VD->setType(CurType); |
| 261 | else |
| 262 | cast<TypedefDecl>(D)->setUnderlyingType(CurType); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 265 | static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 266 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 267 | if (Attr.getNumArgs() > 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 268 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 269 | std::string("0")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 270 | return; |
| 271 | } |
| 272 | |
| 273 | if (TagDecl *TD = dyn_cast<TagDecl>(d)) |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 274 | TD->addAttr(new PackedAttr(1)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 275 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) { |
| 276 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 277 | // has no effect. |
| 278 | if (!FD->getType()->isIncompleteType() && |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 279 | S.Context.getTypeAlign(FD->getType()) <= 8) |
| 280 | S.Diag(Attr.getLoc(), |
| 281 | diag::warn_attribute_ignored_for_field_of_type, |
| 282 | Attr.getName()->getName(), FD->getType().getAsString()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 283 | else |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 284 | FD->addAttr(new PackedAttr(1)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 285 | } else |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 286 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored, |
| 287 | Attr.getName()->getName()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 290 | static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 291 | // check the attribute arguments. |
| 292 | if (Attr.getNumArgs() > 0) { |
| 293 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 294 | std::string("0")); |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | // The IBOutlet attribute only applies to instance variables of Objective-C |
| 299 | // classes. |
| 300 | if (ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(d)) |
| 301 | ID->addAttr(new IBOutletAttr()); |
| 302 | else |
| 303 | S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet_non_ivar); |
| 304 | } |
| 305 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 306 | static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 307 | // GCC ignores the nonnull attribute on K&R style function |
| 308 | // prototypes, so we ignore it as well |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame^] | 309 | if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 310 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
| 311 | "nonnull", "function"); |
| 312 | return; |
| 313 | } |
| 314 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame^] | 315 | unsigned NumArgs = getFunctionOrMethodNumArgs(d); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 316 | |
| 317 | // The nonnull attribute only applies to pointers. |
| 318 | llvm::SmallVector<unsigned, 10> NonNullArgs; |
| 319 | |
| 320 | for (AttributeList::arg_iterator I=Attr.arg_begin(), |
| 321 | E=Attr.arg_end(); I!=E; ++I) { |
| 322 | |
| 323 | |
| 324 | // The argument must be an integer constant expression. |
| 325 | Expr *Ex = static_cast<Expr *>(Attr.getArg(0)); |
| 326 | llvm::APSInt ArgNum(32); |
| 327 | if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 328 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int, |
| 329 | "nonnull", Ex->getSourceRange()); |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
| 334 | |
| 335 | if (x < 1 || x > NumArgs) { |
| 336 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds, |
Ted Kremenek | 6e1eb87 | 2008-07-22 16:56:21 +0000 | [diff] [blame] | 337 | "nonnull", llvm::utostr_32(I.getArgNum()), Ex->getSourceRange()); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 338 | return; |
| 339 | } |
Ted Kremenek | 465172f | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 340 | |
| 341 | --x; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 342 | |
| 343 | // Is the function argument a pointer type? |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame^] | 344 | if (!getFunctionOrMethodArgType(d, x)->isPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 345 | // FIXME: Should also highlight argument in decl. |
| 346 | S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only, |
| 347 | "nonnull", Ex->getSourceRange()); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 348 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | NonNullArgs.push_back(x); |
| 352 | } |
| 353 | |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 354 | // If no arguments were specified to __attribute__((nonnull)) then all |
| 355 | // pointer arguments have a nonnull attribute. |
| 356 | if (NonNullArgs.empty()) { |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame^] | 357 | for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) |
| 358 | if (getFunctionOrMethodArgType(d, I)->isPointerType()) |
| 359 | NonNullArgs.push_back(I); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 360 | |
| 361 | if (NonNullArgs.empty()) { |
| 362 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
| 363 | return; |
| 364 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 365 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 366 | |
| 367 | unsigned* start = &NonNullArgs[0]; |
| 368 | unsigned size = NonNullArgs.size(); |
| 369 | std::sort(start, start + size); |
| 370 | d->addAttr(new NonNullAttr(start, size)); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 373 | static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 374 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 375 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 376 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 377 | std::string("1")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 378 | return; |
| 379 | } |
| 380 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 381 | Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 382 | Arg = Arg->IgnoreParenCasts(); |
| 383 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 384 | |
| 385 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 386 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string, |
| 387 | "alias", std::string("1")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 388 | return; |
| 389 | } |
| 390 | |
| 391 | const char *Alias = Str->getStrData(); |
| 392 | unsigned AliasLen = Str->getByteLength(); |
| 393 | |
| 394 | // FIXME: check if target symbol exists in current file |
| 395 | |
| 396 | d->addAttr(new AliasAttr(std::string(Alias, AliasLen))); |
| 397 | } |
| 398 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 399 | static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 400 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 401 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 402 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 403 | std::string("0")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 404 | return; |
| 405 | } |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame^] | 406 | |
| 407 | if (!isFunctionOrMethod(d)) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 408 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
| 409 | "noreturn", "function"); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 410 | return; |
| 411 | } |
| 412 | |
| 413 | d->addAttr(new NoReturnAttr()); |
| 414 | } |
| 415 | |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 416 | static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 417 | // check the attribute arguments. |
| 418 | if (Attr.getNumArgs() != 0) { |
| 419 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 420 | std::string("0")); |
| 421 | return; |
| 422 | } |
| 423 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame^] | 424 | if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) { |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 425 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
Ted Kremenek | 356b63a | 2008-08-07 01:02:05 +0000 | [diff] [blame] | 426 | "unused", "variable and function"); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 427 | return; |
| 428 | } |
| 429 | |
| 430 | d->addAttr(new UnusedAttr()); |
| 431 | } |
| 432 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 433 | static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 434 | // check the attribute arguments. |
| 435 | if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) { |
| 436 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, "0 or 1"); |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 441 | if (Attr.getNumArgs() > 0) { |
| 442 | Expr *E = static_cast<Expr *>(Attr.getArg(0)); |
| 443 | llvm::APSInt Idx(32); |
| 444 | if (!E->isIntegerConstantExpr(Idx, S.Context)) { |
| 445 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int, |
| 446 | "constructor", "1", E->getSourceRange()); |
| 447 | return; |
| 448 | } |
| 449 | priority = Idx.getZExtValue(); |
| 450 | } |
| 451 | |
| 452 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(d); |
| 453 | if (!Fn) { |
| 454 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
| 455 | "constructor", "function"); |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | d->addAttr(new ConstructorAttr(priority)); |
| 460 | } |
| 461 | |
| 462 | static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 463 | // check the attribute arguments. |
| 464 | if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) { |
| 465 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, "0 or 1"); |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 470 | if (Attr.getNumArgs() > 0) { |
| 471 | Expr *E = static_cast<Expr *>(Attr.getArg(0)); |
| 472 | llvm::APSInt Idx(32); |
| 473 | if (!E->isIntegerConstantExpr(Idx, S.Context)) { |
| 474 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int, |
| 475 | "destructor", "1", E->getSourceRange()); |
| 476 | return; |
| 477 | } |
| 478 | priority = Idx.getZExtValue(); |
| 479 | } |
| 480 | |
Anders Carlsson | 6782fc6 | 2008-08-22 22:10:48 +0000 | [diff] [blame] | 481 | if (!isa<FunctionDecl>(d)) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 482 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
| 483 | "destructor", "function"); |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | d->addAttr(new DestructorAttr(priority)); |
| 488 | } |
| 489 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 490 | static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 491 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 492 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 493 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 494 | std::string("0")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 495 | return; |
| 496 | } |
| 497 | |
| 498 | d->addAttr(new DeprecatedAttr()); |
| 499 | } |
| 500 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 501 | static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 502 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 503 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 504 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 505 | std::string("1")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 506 | return; |
| 507 | } |
| 508 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 509 | Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 510 | Arg = Arg->IgnoreParenCasts(); |
| 511 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 512 | |
| 513 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 514 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string, |
| 515 | "visibility", std::string("1")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 516 | return; |
| 517 | } |
| 518 | |
| 519 | const char *TypeStr = Str->getStrData(); |
| 520 | unsigned TypeLen = Str->getByteLength(); |
| 521 | VisibilityAttr::VisibilityTypes type; |
| 522 | |
| 523 | if (TypeLen == 7 && !memcmp(TypeStr, "default", 7)) |
| 524 | type = VisibilityAttr::DefaultVisibility; |
| 525 | else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6)) |
| 526 | type = VisibilityAttr::HiddenVisibility; |
| 527 | else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8)) |
| 528 | type = VisibilityAttr::HiddenVisibility; // FIXME |
| 529 | else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9)) |
| 530 | type = VisibilityAttr::ProtectedVisibility; |
| 531 | else { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 532 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported, |
| 533 | "visibility", TypeStr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 534 | return; |
| 535 | } |
| 536 | |
| 537 | d->addAttr(new VisibilityAttr(type)); |
| 538 | } |
| 539 | |
Anders Carlsson | aa0d25b | 2008-08-23 23:22:21 +0000 | [diff] [blame] | 540 | static void HandleObjCGCAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Anders Carlsson | 6e14a8f | 2008-08-24 16:33:25 +0000 | [diff] [blame] | 541 | if (!Attr.getParameterName()) { |
Anders Carlsson | aa0d25b | 2008-08-23 23:22:21 +0000 | [diff] [blame] | 542 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string, |
| 543 | "objc_gc", std::string("1")); |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | if (Attr.getNumArgs() != 0) { |
| 548 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 549 | std::string("1")); |
| 550 | return; |
| 551 | } |
| 552 | |
| 553 | const char *TypeStr = Attr.getParameterName()->getName(); |
| 554 | unsigned TypeLen = Attr.getParameterName()->getLength(); |
| 555 | |
| 556 | ObjCGCAttr::GCAttrTypes type; |
| 557 | |
| 558 | if (TypeLen == 4 && !memcmp(TypeStr, "weak", 4)) |
| 559 | type = ObjCGCAttr::Weak; |
Anders Carlsson | 6e14a8f | 2008-08-24 16:33:25 +0000 | [diff] [blame] | 560 | else if (TypeLen == 6 && !memcmp(TypeStr, "strong", 6)) |
Anders Carlsson | aa0d25b | 2008-08-23 23:22:21 +0000 | [diff] [blame] | 561 | type = ObjCGCAttr::Strong; |
| 562 | else { |
| 563 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported, |
| 564 | "objc_gc", TypeStr); |
| 565 | return; |
| 566 | } |
| 567 | |
| 568 | d->addAttr(new ObjCGCAttr(type)); |
| 569 | } |
| 570 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 571 | static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 572 | if (!Attr.getParameterName()) { |
| 573 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string, |
| 574 | "blocks", std::string("1")); |
| 575 | return; |
| 576 | } |
| 577 | |
| 578 | if (Attr.getNumArgs() != 0) { |
| 579 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 580 | std::string("1")); |
| 581 | return; |
| 582 | } |
| 583 | const char *TypeStr = Attr.getParameterName()->getName(); |
| 584 | unsigned TypeLen = Attr.getParameterName()->getLength(); |
| 585 | |
| 586 | BlocksAttr::BlocksAttrTypes type; |
| 587 | |
| 588 | if (TypeLen == 5 && !memcmp(TypeStr, "byref", 5)) |
| 589 | type = BlocksAttr::ByRef; |
| 590 | else { |
| 591 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported, |
| 592 | "blocks", TypeStr); |
| 593 | return; |
| 594 | } |
| 595 | |
| 596 | d->addAttr(new BlocksAttr(type)); |
| 597 | } |
| 598 | |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 599 | static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 600 | // check the attribute arguments. |
| 601 | if (Attr.getNumArgs() > 2) { |
| 602 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, "0, 1 or 2"); |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | int sentinel = 0; |
| 607 | if (Attr.getNumArgs() > 0) { |
| 608 | Expr *E = static_cast<Expr *>(Attr.getArg(0)); |
| 609 | llvm::APSInt Idx(32); |
| 610 | if (!E->isIntegerConstantExpr(Idx, S.Context)) { |
| 611 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int, |
| 612 | "sentinel", "1", E->getSourceRange()); |
| 613 | return; |
| 614 | } |
| 615 | sentinel = Idx.getZExtValue(); |
| 616 | |
| 617 | if (sentinel < 0) { |
| 618 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero, |
| 619 | E->getSourceRange()); |
| 620 | return; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | int nullPos = 0; |
| 625 | if (Attr.getNumArgs() > 1) { |
| 626 | Expr *E = static_cast<Expr *>(Attr.getArg(1)); |
| 627 | llvm::APSInt Idx(32); |
| 628 | if (!E->isIntegerConstantExpr(Idx, S.Context)) { |
| 629 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int, |
| 630 | "sentinel", "2", E->getSourceRange()); |
| 631 | return; |
| 632 | } |
| 633 | nullPos = Idx.getZExtValue(); |
| 634 | |
| 635 | if (nullPos > 1 || nullPos < 0) { |
| 636 | // FIXME: This error message could be improved, it would be nice |
| 637 | // to say what the bounds actually are. |
| 638 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one, |
| 639 | E->getSourceRange()); |
| 640 | return; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) { |
| 645 | QualType FT = FD->getType(); |
| 646 | if (!FT->getAsFunctionTypeProto()->isVariadic()) { |
| 647 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic); |
| 648 | return; |
| 649 | } |
| 650 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) { |
| 651 | if (!MD->isVariadic()) { |
| 652 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic); |
| 653 | return; |
| 654 | } |
| 655 | } else { |
| 656 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
| 657 | "sentinel", "function or method"); |
| 658 | return; |
| 659 | } |
| 660 | |
| 661 | // FIXME: Actually create the attribute. |
| 662 | } |
| 663 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 664 | static void HandleWeakAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 665 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 666 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 667 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 668 | std::string("0")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 669 | return; |
| 670 | } |
| 671 | |
| 672 | d->addAttr(new WeakAttr()); |
| 673 | } |
| 674 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 675 | static void HandleDLLImportAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 676 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 677 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 678 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 679 | std::string("0")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 680 | return; |
| 681 | } |
| 682 | |
| 683 | d->addAttr(new DLLImportAttr()); |
| 684 | } |
| 685 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 686 | static void HandleDLLExportAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 687 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 688 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 689 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 690 | std::string("0")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 691 | return; |
| 692 | } |
| 693 | |
| 694 | d->addAttr(new DLLExportAttr()); |
| 695 | } |
| 696 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 697 | static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 698 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 699 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 700 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 701 | std::string("0")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 702 | return; |
| 703 | } |
| 704 | |
| 705 | d->addAttr(new StdCallAttr()); |
| 706 | } |
| 707 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 708 | static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 709 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 710 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 711 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 712 | std::string("0")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 713 | return; |
| 714 | } |
| 715 | |
| 716 | d->addAttr(new FastCallAttr()); |
| 717 | } |
| 718 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 719 | static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 720 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 721 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 722 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 723 | std::string("0")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 724 | return; |
| 725 | } |
| 726 | |
| 727 | d->addAttr(new NoThrowAttr()); |
| 728 | } |
| 729 | |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 730 | static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 731 | // check the attribute arguments. |
| 732 | if (Attr.getNumArgs() != 0) { |
| 733 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 734 | std::string("0")); |
| 735 | return; |
| 736 | } |
| 737 | |
| 738 | d->addAttr(new ConstAttr()); |
| 739 | } |
| 740 | |
| 741 | static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 742 | // check the attribute arguments. |
| 743 | if (Attr.getNumArgs() != 0) { |
| 744 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 745 | std::string("0")); |
| 746 | return; |
| 747 | } |
| 748 | |
| 749 | d->addAttr(new PureAttr()); |
| 750 | } |
| 751 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 752 | /// Handle __attribute__((format(type,idx,firstarg))) attributes |
| 753 | /// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 754 | static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 755 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 756 | if (!Attr.getParameterName()) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 757 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string, |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 758 | "format", std::string("1")); |
| 759 | return; |
| 760 | } |
| 761 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 762 | if (Attr.getNumArgs() != 2) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 763 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 764 | std::string("3")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 765 | return; |
| 766 | } |
| 767 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame^] | 768 | if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 769 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
| 770 | "format", "function"); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 771 | return; |
| 772 | } |
| 773 | |
| 774 | // FIXME: in C++ the implicit 'this' function parameter also counts. |
| 775 | // this is needed in order to be compatible with GCC |
| 776 | // the index must start in 1 and the limit is numargs+1 |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 777 | unsigned NumArgs = getFunctionOrMethodNumArgs(d); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 778 | unsigned FirstIdx = 1; |
| 779 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 780 | const char *Format = Attr.getParameterName()->getName(); |
| 781 | unsigned FormatLen = Attr.getParameterName()->getLength(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 782 | |
| 783 | // Normalize the argument, __foo__ becomes foo. |
| 784 | if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' && |
| 785 | Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') { |
| 786 | Format += 2; |
| 787 | FormatLen -= 4; |
| 788 | } |
| 789 | |
| 790 | bool Supported = false; |
| 791 | bool is_NSString = false; |
| 792 | bool is_strftime = false; |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 793 | bool is_CFString = false; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 794 | |
| 795 | switch (FormatLen) { |
| 796 | default: break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 797 | case 5: Supported = !memcmp(Format, "scanf", 5); break; |
| 798 | case 6: Supported = !memcmp(Format, "printf", 6); break; |
| 799 | case 7: Supported = !memcmp(Format, "strfmon", 7); break; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 800 | case 8: |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 801 | Supported = (is_strftime = !memcmp(Format, "strftime", 8)) || |
| 802 | (is_NSString = !memcmp(Format, "NSString", 8)) || |
| 803 | (is_CFString = !memcmp(Format, "CFString", 8)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 804 | break; |
| 805 | } |
| 806 | |
| 807 | if (!Supported) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 808 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported, |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 809 | "format", Attr.getParameterName()->getName()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 810 | return; |
| 811 | } |
| 812 | |
| 813 | // checks for the 2nd argument |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 814 | Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 815 | llvm::APSInt Idx(32); |
| 816 | if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
| 817 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int, |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 818 | "format", std::string("2"), IdxExpr->getSourceRange()); |
| 819 | return; |
| 820 | } |
| 821 | |
| 822 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 823 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds, |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 824 | "format", std::string("2"), IdxExpr->getSourceRange()); |
| 825 | return; |
| 826 | } |
| 827 | |
| 828 | // FIXME: Do we need to bounds check? |
| 829 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
| 830 | |
| 831 | // make sure the format string is really a string |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 832 | QualType Ty = getFunctionOrMethodArgType(d, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 833 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 834 | if (is_CFString) { |
| 835 | if (!isCFStringType(Ty, S.Context)) { |
| 836 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not, |
| 837 | "a CFString", IdxExpr->getSourceRange()); |
| 838 | return; |
| 839 | } |
| 840 | } else if (is_NSString) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 841 | // FIXME: do we need to check if the type is NSString*? What are |
| 842 | // the semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 843 | if (!isNSStringType(Ty, S.Context)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 844 | // FIXME: Should highlight the actual expression that has the |
| 845 | // wrong type. |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 846 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not, |
| 847 | "an NSString", IdxExpr->getSourceRange()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 848 | return; |
| 849 | } |
| 850 | } else if (!Ty->isPointerType() || |
| 851 | !Ty->getAsPointerType()->getPointeeType()->isCharType()) { |
| 852 | // FIXME: Should highlight the actual expression that has the |
| 853 | // wrong type. |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 854 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not, |
| 855 | "a string type", IdxExpr->getSourceRange()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 856 | return; |
| 857 | } |
| 858 | |
| 859 | // check the 3rd argument |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 860 | Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1)); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 861 | llvm::APSInt FirstArg(32); |
| 862 | if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
| 863 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int, |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 864 | "format", std::string("3"), FirstArgExpr->getSourceRange()); |
| 865 | return; |
| 866 | } |
| 867 | |
| 868 | // check if the function is variadic if the 3rd argument non-zero |
| 869 | if (FirstArg != 0) { |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 870 | if (isFunctionOrMethodVariadic(d)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 871 | ++NumArgs; // +1 for ... |
| 872 | } else { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 873 | S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 874 | return; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | // strftime requires FirstArg to be 0 because it doesn't read from any variable |
| 879 | // the input is just the current time + the format string |
| 880 | if (is_strftime) { |
| 881 | if (FirstArg != 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 882 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter, |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 883 | FirstArgExpr->getSourceRange()); |
| 884 | return; |
| 885 | } |
| 886 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 887 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 888 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds, |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 889 | "format", std::string("3"), FirstArgExpr->getSourceRange()); |
| 890 | return; |
| 891 | } |
| 892 | |
| 893 | d->addAttr(new FormatAttr(std::string(Format, FormatLen), |
| 894 | Idx.getZExtValue(), FirstArg.getZExtValue())); |
| 895 | } |
| 896 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 897 | static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr, |
| 898 | Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 899 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 900 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 901 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 902 | std::string("0")); |
| 903 | return; |
| 904 | } |
| 905 | |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 906 | // FIXME: This shouldn't be restricted to typedefs |
| 907 | TypedefDecl *TD = dyn_cast<TypedefDecl>(d); |
| 908 | if (!TD || !TD->getUnderlyingType()->isUnionType()) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 909 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 910 | "transparent_union", "union"); |
| 911 | return; |
| 912 | } |
| 913 | |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 914 | RecordDecl* RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 915 | |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 916 | // FIXME: Should we do a check for RD->isDefinition()? |
| 917 | |
| 918 | // FIXME: This isn't supposed to be restricted to pointers, but otherwise |
| 919 | // we might silently generate incorrect code; see following code |
| 920 | for (int i = 0; i < RD->getNumMembers(); i++) { |
| 921 | if (!RD->getMember(i)->getType()->isPointerType()) { |
| 922 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_nonpointer); |
| 923 | return; |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | // FIXME: This is a complete hack; we should be properly propagating |
| 928 | // transparent_union through Sema. That said, this is close enough to |
| 929 | // correctly compile all the common cases of transparent_union without |
| 930 | // errors or warnings |
| 931 | QualType NewTy = S.Context.VoidPtrTy; |
| 932 | NewTy.addConst(); |
| 933 | TD->setUnderlyingType(NewTy); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 936 | static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 937 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 938 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 939 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 940 | std::string("1")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 941 | return; |
| 942 | } |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 943 | Expr *argExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 944 | StringLiteral *SE = dyn_cast<StringLiteral>(argExpr); |
| 945 | |
| 946 | // Make sure that there is a string literal as the annotation's single |
| 947 | // argument. |
| 948 | if (!SE) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 949 | S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 950 | return; |
| 951 | } |
| 952 | d->addAttr(new AnnotateAttr(std::string(SE->getStrData(), |
| 953 | SE->getByteLength()))); |
| 954 | } |
| 955 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 956 | static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 957 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 958 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 959 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 960 | std::string("1")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 961 | return; |
| 962 | } |
| 963 | |
| 964 | unsigned Align = 0; |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 965 | if (Attr.getNumArgs() == 0) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 966 | // FIXME: This should be the target specific maximum alignment. |
| 967 | // (For now we just use 128 bits which is the maximum on X86. |
| 968 | Align = 128; |
| 969 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 970 | } |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 971 | |
| 972 | Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 973 | llvm::APSInt Alignment(32); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 974 | if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) { |
| 975 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int, |
| 976 | "aligned", alignmentExpr->getSourceRange()); |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 977 | return; |
| 978 | } |
| 979 | d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 980 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 981 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 982 | /// HandleModeAttr - This attribute modifies the width of a decl with |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 983 | /// primitive type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 984 | /// |
| 985 | /// Despite what would be logical, the mode attribute is a decl attribute, |
| 986 | /// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make |
| 987 | /// 'G' be HImode, not an intermediate pointer. |
| 988 | /// |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 989 | static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 990 | // This attribute isn't documented, but glibc uses it. It changes |
| 991 | // the width of an int or unsigned int to the specified size. |
| 992 | |
| 993 | // Check that there aren't any arguments |
| 994 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 995 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 996 | std::string("0")); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 997 | return; |
| 998 | } |
| 999 | |
| 1000 | IdentifierInfo *Name = Attr.getParameterName(); |
| 1001 | if (!Name) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1002 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1003 | return; |
| 1004 | } |
| 1005 | const char *Str = Name->getName(); |
| 1006 | unsigned Len = Name->getLength(); |
| 1007 | |
| 1008 | // Normalize the attribute name, __foo__ becomes foo. |
| 1009 | if (Len > 4 && Str[0] == '_' && Str[1] == '_' && |
| 1010 | Str[Len - 2] == '_' && Str[Len - 1] == '_') { |
| 1011 | Str += 2; |
| 1012 | Len -= 4; |
| 1013 | } |
| 1014 | |
| 1015 | unsigned DestWidth = 0; |
| 1016 | bool IntegerMode = true; |
| 1017 | switch (Len) { |
| 1018 | case 2: |
| 1019 | if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; } |
| 1020 | if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; } |
| 1021 | if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; } |
| 1022 | if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; } |
| 1023 | if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; } |
| 1024 | if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; } |
| 1025 | if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; } |
| 1026 | if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; } |
| 1027 | if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; } |
| 1028 | break; |
| 1029 | case 4: |
| 1030 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 1031 | // pointer on PIC16 and other embedded platforms. |
| 1032 | if (!memcmp(Str, "word", 4)) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1033 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1034 | if (!memcmp(Str, "byte", 4)) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1035 | DestWidth = S.Context.Target.getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1036 | break; |
| 1037 | case 7: |
| 1038 | if (!memcmp(Str, "pointer", 7)) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1039 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1040 | break; |
| 1041 | } |
| 1042 | |
| 1043 | QualType OldTy; |
| 1044 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 1045 | OldTy = TD->getUnderlyingType(); |
| 1046 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 1047 | OldTy = VD->getType(); |
| 1048 | else { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1049 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl, "mode", |
| 1050 | SourceRange(Attr.getLoc(), Attr.getLoc())); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1051 | return; |
| 1052 | } |
| 1053 | |
| 1054 | // FIXME: Need proper fixed-width types |
| 1055 | QualType NewTy; |
| 1056 | switch (DestWidth) { |
| 1057 | case 0: |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1058 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode, Name->getName()); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1059 | return; |
| 1060 | default: |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1061 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode, Name->getName()); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1062 | return; |
| 1063 | case 8: |
| 1064 | assert(IntegerMode); |
| 1065 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1066 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1067 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1068 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1069 | break; |
| 1070 | case 16: |
| 1071 | assert(IntegerMode); |
| 1072 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1073 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1074 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1075 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1076 | break; |
| 1077 | case 32: |
| 1078 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1079 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1080 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1081 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1082 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1083 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1084 | break; |
| 1085 | case 64: |
| 1086 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1087 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1088 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1089 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1090 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1091 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1092 | break; |
| 1093 | } |
| 1094 | |
| 1095 | if (!OldTy->getAsBuiltinType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1096 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1097 | else if (!(IntegerMode && OldTy->isIntegerType()) && |
| 1098 | !(!IntegerMode && OldTy->isFloatingType())) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1099 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | // Install the new type. |
| 1103 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 1104 | TD->setUnderlyingType(NewTy); |
| 1105 | else |
| 1106 | cast<ValueDecl>(D)->setType(NewTy); |
| 1107 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1108 | |
| 1109 | //===----------------------------------------------------------------------===// |
| 1110 | // Top Level Sema Entry Points |
| 1111 | //===----------------------------------------------------------------------===// |
| 1112 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1113 | /// HandleDeclAttribute - Apply the specific attribute to the specified decl if |
| 1114 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 1115 | /// silently ignore it. |
| 1116 | static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) { |
| 1117 | switch (Attr.getKind()) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1118 | case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1119 | case AttributeList::AT_address_space: |
| 1120 | // Ignore this, this is a type attribute, handled by ProcessTypeAttributes. |
| 1121 | break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1122 | case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break; |
| 1123 | case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break; |
| 1124 | case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break; |
| 1125 | case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break; |
| 1126 | case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break; |
| 1127 | case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break; |
| 1128 | case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break; |
| 1129 | case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1130 | case AttributeList::AT_ext_vector_type: |
| 1131 | HandleExtVectorTypeAttr(D, Attr, S); |
| 1132 | break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1133 | case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break; |
| 1134 | case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1135 | case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1136 | case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break; |
| 1137 | case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break; |
| 1138 | case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break; |
| 1139 | case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break; |
| 1140 | case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break; |
| 1141 | case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break; |
| 1142 | case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1143 | case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break; |
| 1144 | case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1145 | case AttributeList::AT_transparent_union: |
| 1146 | HandleTransparentUnionAttr(D, Attr, S); |
| 1147 | break; |
Anders Carlsson | aa0d25b | 2008-08-23 23:22:21 +0000 | [diff] [blame] | 1148 | case AttributeList::AT_objc_gc: HandleObjCGCAttr (D, Attr, S); break; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1149 | case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1150 | case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1151 | case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break; |
| 1152 | case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1153 | default: |
| 1154 | #if 0 |
| 1155 | // TODO: when we have the full set of attributes, warn about unknown ones. |
| 1156 | S.Diag(Attr->getLoc(), diag::warn_attribute_ignored, |
| 1157 | Attr->getName()->getName()); |
| 1158 | #endif |
| 1159 | break; |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 1164 | /// attribute list to the specified decl, ignoring any type attributes. |
| 1165 | void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) { |
| 1166 | while (AttrList) { |
| 1167 | ProcessDeclAttribute(D, *AttrList, *this); |
| 1168 | AttrList = AttrList->getNext(); |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1173 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 1174 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 1175 | /// specified in many different places, and we need to find and apply them all. |
| 1176 | void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) { |
| 1177 | // Apply decl attributes from the DeclSpec if present. |
| 1178 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes()) |
| 1179 | ProcessDeclAttributeList(D, Attrs); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1180 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1181 | // Walk the declarator structure, applying decl attributes that were in a type |
| 1182 | // position to the decl itself. This handles cases like: |
| 1183 | // int *__attr__(x)** D; |
| 1184 | // when X is a decl attribute. |
| 1185 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 1186 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
| 1187 | ProcessDeclAttributeList(D, Attrs); |
| 1188 | |
| 1189 | // Finally, apply any attributes on the decl itself. |
| 1190 | if (const AttributeList *Attrs = PD.getAttributes()) |
| 1191 | ProcessDeclAttributeList(D, Attrs); |
| 1192 | } |
| 1193 | |