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