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" |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 16 | #include "clang/Basic/TargetInfo.h" |
Ted Kremenek | 6e1eb87 | 2008-07-22 16:56:21 +0000 | [diff] [blame] | 17 | #include <llvm/ADT/StringExtras.h> |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 20 | //===----------------------------------------------------------------------===// |
| 21 | // Helper functions |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 24 | static const FunctionTypeProto *getFunctionProto(Decl *d) { |
| 25 | QualType Ty; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 26 | if (ValueDecl *decl = dyn_cast<ValueDecl>(d)) |
| 27 | Ty = decl->getType(); |
| 28 | else if (FieldDecl *decl = dyn_cast<FieldDecl>(d)) |
| 29 | Ty = decl->getType(); |
| 30 | else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d)) |
| 31 | Ty = decl->getUnderlyingType(); |
| 32 | else |
| 33 | return 0; |
| 34 | |
| 35 | if (Ty->isFunctionPointerType()) |
| 36 | Ty = Ty->getAsPointerType()->getPointeeType(); |
| 37 | |
| 38 | if (const FunctionType *FnTy = Ty->getAsFunctionType()) |
| 39 | return dyn_cast<FunctionTypeProto>(FnTy->getAsFunctionType()); |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame^] | 45 | const PointerType *PT = T->getAsPointerType(); |
| 46 | if (!PT) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 47 | return false; |
| 48 | |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame^] | 49 | const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 50 | if (!ClsT) |
| 51 | return false; |
| 52 | |
| 53 | IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier(); |
| 54 | |
| 55 | // FIXME: Should we walk the chain of classes? |
| 56 | return ClsName == &Ctx.Idents.get("NSString") || |
| 57 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 58 | } |
| 59 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 60 | //===----------------------------------------------------------------------===// |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 61 | // Attribute Implementations |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 64 | static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr, |
| 65 | Sema &S) { |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 66 | TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d); |
| 67 | if (tDecl == 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 68 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 69 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 72 | QualType curType = tDecl->getUnderlyingType(); |
| 73 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 74 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 75 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 76 | std::string("1")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 77 | return; |
| 78 | } |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 79 | Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 80 | llvm::APSInt vecSize(32); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 81 | if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) { |
| 82 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int, |
| 83 | "ext_vector_type", sizeExpr->getSourceRange()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | // unlike gcc's vector_size attribute, we do not allow vectors to be defined |
| 87 | // in conjunction with complex types (pointers, arrays, functions, etc.). |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame^] | 88 | if (!curType->isIntegerType() && !curType->isRealFloatingType()) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 89 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type, |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame^] | 90 | curType.getAsString()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 91 | return; |
| 92 | } |
| 93 | // unlike gcc's vector_size attribute, the size is specified as the |
| 94 | // number of elements, not the number of bytes. |
| 95 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); |
| 96 | |
| 97 | if (vectorSize == 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 98 | S.Diag(Attr.getLoc(), diag::err_attribute_zero_size, |
| 99 | sizeExpr->getSourceRange()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 100 | return; |
| 101 | } |
| 102 | // Instantiate/Install the vector type, the number of elements is > 0. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 103 | tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 104 | // Remember this typedef decl, we will need it later for diagnostics. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 105 | S.ExtVectorDecls.push_back(tDecl); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 108 | |
| 109 | /// HandleVectorSizeAttribute - this attribute is only applicable to |
| 110 | /// integral and float scalars, although arrays, pointers, and function |
| 111 | /// return values are allowed in conjunction with this construct. Aggregates |
| 112 | /// with this attribute are invalid, even if they are of the same size as a |
| 113 | /// corresponding scalar. |
| 114 | /// The raw attribute should contain precisely 1 argument, the vector size |
| 115 | /// for the variable, measured in bytes. If curType and rawAttr are well |
| 116 | /// formed, this routine will return a new vector type. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 117 | static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 118 | QualType CurType; |
| 119 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 120 | CurType = VD->getType(); |
| 121 | else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 122 | CurType = TD->getUnderlyingType(); |
| 123 | else { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 124 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl, |
| 125 | std::string("vector_size"), |
| 126 | SourceRange(Attr.getLoc(), Attr.getLoc())); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 127 | return; |
| 128 | } |
| 129 | |
| 130 | // Check the attribute arugments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 131 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 132 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 133 | std::string("1")); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 134 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 135 | } |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 136 | Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 137 | llvm::APSInt vecSize(32); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 138 | if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) { |
| 139 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int, |
| 140 | "vector_size", sizeExpr->getSourceRange()); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 141 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 142 | } |
| 143 | // navigate to the base type - we need to provide for vector pointers, |
| 144 | // vector arrays, and functions returning vectors. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame^] | 145 | if (CurType->isPointerType() || CurType->isArrayType() || |
| 146 | CurType->isFunctionType()) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 147 | assert(0 && "HandleVector(): Complex type construction unimplemented"); |
| 148 | /* FIXME: rebuild the type from the inside out, vectorizing the inner type. |
| 149 | do { |
| 150 | if (PointerType *PT = dyn_cast<PointerType>(canonType)) |
| 151 | canonType = PT->getPointeeType().getTypePtr(); |
| 152 | else if (ArrayType *AT = dyn_cast<ArrayType>(canonType)) |
| 153 | canonType = AT->getElementType().getTypePtr(); |
| 154 | else if (FunctionType *FT = dyn_cast<FunctionType>(canonType)) |
| 155 | canonType = FT->getResultType().getTypePtr(); |
| 156 | } while (canonType->isPointerType() || canonType->isArrayType() || |
| 157 | canonType->isFunctionType()); |
| 158 | */ |
| 159 | } |
| 160 | // the base type must be integer or float. |
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 | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 164 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 165 | } |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 166 | unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 167 | // vecSize is specified in bytes - convert to bits. |
| 168 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8); |
| 169 | |
| 170 | // the vector size needs to be an integral multiple of the type size. |
| 171 | if (vectorSize % typeSize) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 172 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size, |
| 173 | sizeExpr->getSourceRange()); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 174 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 175 | } |
| 176 | if (vectorSize == 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 177 | S.Diag(Attr.getLoc(), diag::err_attribute_zero_size, |
| 178 | sizeExpr->getSourceRange()); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 179 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 180 | } |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 181 | |
| 182 | // Success! Instantiate the vector type, the number of elements is > 0, and |
| 183 | // not required to be a power of 2, unlike GCC. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 184 | CurType = S.Context.getVectorType(CurType, vectorSize/typeSize); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 185 | |
| 186 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 187 | VD->setType(CurType); |
| 188 | else |
| 189 | cast<TypedefDecl>(D)->setUnderlyingType(CurType); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 192 | static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 193 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 194 | if (Attr.getNumArgs() > 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 195 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 196 | std::string("0")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 197 | return; |
| 198 | } |
| 199 | |
| 200 | if (TagDecl *TD = dyn_cast<TagDecl>(d)) |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 201 | TD->addAttr(new PackedAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 202 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) { |
| 203 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 204 | // has no effect. |
| 205 | if (!FD->getType()->isIncompleteType() && |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 206 | S.Context.getTypeAlign(FD->getType()) <= 8) |
| 207 | S.Diag(Attr.getLoc(), |
| 208 | diag::warn_attribute_ignored_for_field_of_type, |
| 209 | Attr.getName()->getName(), FD->getType().getAsString()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 210 | else |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 211 | FD->addAttr(new PackedAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 212 | } else |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 213 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored, |
| 214 | Attr.getName()->getName()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 217 | static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 218 | // check the attribute arguments. |
| 219 | if (Attr.getNumArgs() > 0) { |
| 220 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 221 | std::string("0")); |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | // The IBOutlet attribute only applies to instance variables of Objective-C |
| 226 | // classes. |
| 227 | if (ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(d)) |
| 228 | ID->addAttr(new IBOutletAttr()); |
| 229 | else |
| 230 | S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet_non_ivar); |
| 231 | } |
| 232 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 233 | static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 234 | |
| 235 | // GCC ignores the nonnull attribute on K&R style function |
| 236 | // prototypes, so we ignore it as well |
| 237 | const FunctionTypeProto *proto = getFunctionProto(d); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 238 | if (!proto) { |
| 239 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
| 240 | "nonnull", "function"); |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | unsigned NumArgs = proto->getNumArgs(); |
| 245 | |
| 246 | // The nonnull attribute only applies to pointers. |
| 247 | llvm::SmallVector<unsigned, 10> NonNullArgs; |
| 248 | |
| 249 | for (AttributeList::arg_iterator I=Attr.arg_begin(), |
| 250 | E=Attr.arg_end(); I!=E; ++I) { |
| 251 | |
| 252 | |
| 253 | // The argument must be an integer constant expression. |
| 254 | Expr *Ex = static_cast<Expr *>(Attr.getArg(0)); |
| 255 | llvm::APSInt ArgNum(32); |
| 256 | if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 257 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int, |
| 258 | "nonnull", Ex->getSourceRange()); |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
| 263 | |
| 264 | if (x < 1 || x > NumArgs) { |
| 265 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds, |
Ted Kremenek | 6e1eb87 | 2008-07-22 16:56:21 +0000 | [diff] [blame] | 266 | "nonnull", llvm::utostr_32(I.getArgNum()), Ex->getSourceRange()); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 267 | return; |
| 268 | } |
Ted Kremenek | 465172f | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 269 | |
| 270 | --x; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 271 | |
| 272 | // Is the function argument a pointer type? |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame^] | 273 | if (!proto->getArgType(x)->isPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 274 | // FIXME: Should also highlight argument in decl. |
| 275 | S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only, |
| 276 | "nonnull", Ex->getSourceRange()); |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | NonNullArgs.push_back(x); |
| 281 | } |
| 282 | |
| 283 | if (!NonNullArgs.empty()) { |
| 284 | unsigned* start = &NonNullArgs[0]; |
| 285 | unsigned size = NonNullArgs.size(); |
| 286 | std::sort(start, start + size); |
| 287 | d->addAttr(new NonNullAttr(start, size)); |
| 288 | } |
| 289 | else |
| 290 | d->addAttr(new NonNullAttr()); |
| 291 | } |
| 292 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 293 | static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 294 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 295 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 296 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 297 | std::string("1")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 298 | return; |
| 299 | } |
| 300 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 301 | Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 302 | Arg = Arg->IgnoreParenCasts(); |
| 303 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 304 | |
| 305 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 306 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string, |
| 307 | "alias", std::string("1")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 308 | return; |
| 309 | } |
| 310 | |
| 311 | const char *Alias = Str->getStrData(); |
| 312 | unsigned AliasLen = Str->getByteLength(); |
| 313 | |
| 314 | // FIXME: check if target symbol exists in current file |
| 315 | |
| 316 | d->addAttr(new AliasAttr(std::string(Alias, AliasLen))); |
| 317 | } |
| 318 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 319 | static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 320 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 321 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 322 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 323 | std::string("0")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 324 | return; |
| 325 | } |
| 326 | |
| 327 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(d); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 328 | if (!Fn) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 329 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
| 330 | "noreturn", "function"); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 331 | return; |
| 332 | } |
| 333 | |
| 334 | d->addAttr(new NoReturnAttr()); |
| 335 | } |
| 336 | |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 337 | static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 338 | // check the attribute arguments. |
| 339 | if (Attr.getNumArgs() != 0) { |
| 340 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 341 | std::string("0")); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | VarDecl *VD = dyn_cast<VarDecl>(d); |
| 346 | |
| 347 | if (!VD) { |
| 348 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
| 349 | "unused", "variable"); |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | d->addAttr(new UnusedAttr()); |
| 354 | } |
| 355 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 356 | static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 357 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 358 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 359 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 360 | std::string("0")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 361 | return; |
| 362 | } |
| 363 | |
| 364 | d->addAttr(new DeprecatedAttr()); |
| 365 | } |
| 366 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 367 | static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 368 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 369 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 370 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 371 | std::string("1")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 372 | return; |
| 373 | } |
| 374 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 375 | Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 376 | Arg = Arg->IgnoreParenCasts(); |
| 377 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 378 | |
| 379 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 380 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string, |
| 381 | "visibility", std::string("1")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 382 | return; |
| 383 | } |
| 384 | |
| 385 | const char *TypeStr = Str->getStrData(); |
| 386 | unsigned TypeLen = Str->getByteLength(); |
| 387 | VisibilityAttr::VisibilityTypes type; |
| 388 | |
| 389 | if (TypeLen == 7 && !memcmp(TypeStr, "default", 7)) |
| 390 | type = VisibilityAttr::DefaultVisibility; |
| 391 | else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6)) |
| 392 | type = VisibilityAttr::HiddenVisibility; |
| 393 | else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8)) |
| 394 | type = VisibilityAttr::HiddenVisibility; // FIXME |
| 395 | else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9)) |
| 396 | type = VisibilityAttr::ProtectedVisibility; |
| 397 | else { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 398 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported, |
| 399 | "visibility", TypeStr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 400 | return; |
| 401 | } |
| 402 | |
| 403 | d->addAttr(new VisibilityAttr(type)); |
| 404 | } |
| 405 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 406 | static void HandleWeakAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 407 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 408 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 409 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 410 | std::string("0")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 411 | return; |
| 412 | } |
| 413 | |
| 414 | d->addAttr(new WeakAttr()); |
| 415 | } |
| 416 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 417 | static void HandleDLLImportAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 418 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 419 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 420 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 421 | std::string("0")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 422 | return; |
| 423 | } |
| 424 | |
| 425 | d->addAttr(new DLLImportAttr()); |
| 426 | } |
| 427 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 428 | static void HandleDLLExportAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 429 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 430 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 431 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 432 | std::string("0")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 433 | return; |
| 434 | } |
| 435 | |
| 436 | d->addAttr(new DLLExportAttr()); |
| 437 | } |
| 438 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 439 | static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 440 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 441 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 442 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 443 | std::string("0")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 444 | return; |
| 445 | } |
| 446 | |
| 447 | d->addAttr(new StdCallAttr()); |
| 448 | } |
| 449 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 450 | static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 451 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 452 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 453 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 454 | std::string("0")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 455 | return; |
| 456 | } |
| 457 | |
| 458 | d->addAttr(new FastCallAttr()); |
| 459 | } |
| 460 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 461 | static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 462 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 463 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 464 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 465 | std::string("0")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 466 | return; |
| 467 | } |
| 468 | |
| 469 | d->addAttr(new NoThrowAttr()); |
| 470 | } |
| 471 | |
| 472 | /// Handle __attribute__((format(type,idx,firstarg))) attributes |
| 473 | /// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 474 | static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 475 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 476 | if (!Attr.getParameterName()) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 477 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string, |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 478 | "format", std::string("1")); |
| 479 | return; |
| 480 | } |
| 481 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 482 | if (Attr.getNumArgs() != 2) { |
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("3")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 485 | return; |
| 486 | } |
| 487 | |
| 488 | // GCC ignores the format attribute on K&R style function |
| 489 | // prototypes, so we ignore it as well |
| 490 | const FunctionTypeProto *proto = getFunctionProto(d); |
| 491 | |
| 492 | if (!proto) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 493 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
| 494 | "format", "function"); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 495 | return; |
| 496 | } |
| 497 | |
| 498 | // FIXME: in C++ the implicit 'this' function parameter also counts. |
| 499 | // this is needed in order to be compatible with GCC |
| 500 | // the index must start in 1 and the limit is numargs+1 |
| 501 | unsigned NumArgs = proto->getNumArgs(); |
| 502 | unsigned FirstIdx = 1; |
| 503 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 504 | const char *Format = Attr.getParameterName()->getName(); |
| 505 | unsigned FormatLen = Attr.getParameterName()->getLength(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 506 | |
| 507 | // Normalize the argument, __foo__ becomes foo. |
| 508 | if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' && |
| 509 | Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') { |
| 510 | Format += 2; |
| 511 | FormatLen -= 4; |
| 512 | } |
| 513 | |
| 514 | bool Supported = false; |
| 515 | bool is_NSString = false; |
| 516 | bool is_strftime = false; |
| 517 | |
| 518 | switch (FormatLen) { |
| 519 | default: break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 520 | case 5: Supported = !memcmp(Format, "scanf", 5); break; |
| 521 | case 6: Supported = !memcmp(Format, "printf", 6); break; |
| 522 | case 7: Supported = !memcmp(Format, "strfmon", 7); break; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 523 | case 8: |
| 524 | Supported = (is_strftime = !memcmp(Format, "strftime", 8)) || |
| 525 | (is_NSString = !memcmp(Format, "NSString", 8)); |
| 526 | break; |
| 527 | } |
| 528 | |
| 529 | if (!Supported) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 530 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported, |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 531 | "format", Attr.getParameterName()->getName()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 532 | return; |
| 533 | } |
| 534 | |
| 535 | // checks for the 2nd argument |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 536 | Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 537 | llvm::APSInt Idx(32); |
| 538 | if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
| 539 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int, |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 540 | "format", std::string("2"), IdxExpr->getSourceRange()); |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 545 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds, |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 546 | "format", std::string("2"), IdxExpr->getSourceRange()); |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | // FIXME: Do we need to bounds check? |
| 551 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
| 552 | |
| 553 | // make sure the format string is really a string |
| 554 | QualType Ty = proto->getArgType(ArgIdx); |
| 555 | |
| 556 | if (is_NSString) { |
| 557 | // FIXME: do we need to check if the type is NSString*? What are |
| 558 | // the semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 559 | if (!isNSStringType(Ty, S.Context)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 560 | // FIXME: Should highlight the actual expression that has the |
| 561 | // wrong type. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 562 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not_NSString, |
| 563 | IdxExpr->getSourceRange()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 564 | return; |
| 565 | } |
| 566 | } else if (!Ty->isPointerType() || |
| 567 | !Ty->getAsPointerType()->getPointeeType()->isCharType()) { |
| 568 | // FIXME: Should highlight the actual expression that has the |
| 569 | // wrong type. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 570 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not_string, |
| 571 | IdxExpr->getSourceRange()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 572 | return; |
| 573 | } |
| 574 | |
| 575 | // check the 3rd argument |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 576 | Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1)); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 577 | llvm::APSInt FirstArg(32); |
| 578 | if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
| 579 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int, |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 580 | "format", std::string("3"), FirstArgExpr->getSourceRange()); |
| 581 | return; |
| 582 | } |
| 583 | |
| 584 | // check if the function is variadic if the 3rd argument non-zero |
| 585 | if (FirstArg != 0) { |
| 586 | if (proto->isVariadic()) { |
| 587 | ++NumArgs; // +1 for ... |
| 588 | } else { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 589 | S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 590 | return; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | // strftime requires FirstArg to be 0 because it doesn't read from any variable |
| 595 | // the input is just the current time + the format string |
| 596 | if (is_strftime) { |
| 597 | if (FirstArg != 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 598 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter, |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 599 | FirstArgExpr->getSourceRange()); |
| 600 | return; |
| 601 | } |
| 602 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 603 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 604 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds, |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 605 | "format", std::string("3"), FirstArgExpr->getSourceRange()); |
| 606 | return; |
| 607 | } |
| 608 | |
| 609 | d->addAttr(new FormatAttr(std::string(Format, FormatLen), |
| 610 | Idx.getZExtValue(), FirstArg.getZExtValue())); |
| 611 | } |
| 612 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 613 | static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr, |
| 614 | Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 615 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 616 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 617 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 618 | std::string("0")); |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | TypeDecl *decl = dyn_cast<TypeDecl>(d); |
| 623 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 624 | if (!decl || !S.Context.getTypeDeclType(decl)->isUnionType()) { |
| 625 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 626 | "transparent_union", "union"); |
| 627 | return; |
| 628 | } |
| 629 | |
| 630 | //QualType QTy = Context.getTypeDeclType(decl); |
| 631 | //const RecordType *Ty = QTy->getAsUnionType(); |
| 632 | |
| 633 | // FIXME |
| 634 | // Ty->addAttr(new TransparentUnionAttr()); |
| 635 | } |
| 636 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 637 | static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 638 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 639 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 640 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 641 | std::string("1")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 642 | return; |
| 643 | } |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 644 | Expr *argExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 645 | StringLiteral *SE = dyn_cast<StringLiteral>(argExpr); |
| 646 | |
| 647 | // Make sure that there is a string literal as the annotation's single |
| 648 | // argument. |
| 649 | if (!SE) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 650 | S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 651 | return; |
| 652 | } |
| 653 | d->addAttr(new AnnotateAttr(std::string(SE->getStrData(), |
| 654 | SE->getByteLength()))); |
| 655 | } |
| 656 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 657 | static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 658 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 659 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 660 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 661 | std::string("1")); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 662 | return; |
| 663 | } |
| 664 | |
| 665 | unsigned Align = 0; |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 666 | if (Attr.getNumArgs() == 0) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 667 | // FIXME: This should be the target specific maximum alignment. |
| 668 | // (For now we just use 128 bits which is the maximum on X86. |
| 669 | Align = 128; |
| 670 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 671 | } |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 672 | |
| 673 | Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 674 | llvm::APSInt Alignment(32); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 675 | if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) { |
| 676 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int, |
| 677 | "aligned", alignmentExpr->getSourceRange()); |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 678 | return; |
| 679 | } |
| 680 | d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 681 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 682 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 683 | /// HandleModeAttr - This attribute modifies the width of a decl with |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 684 | /// primitive type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 685 | /// |
| 686 | /// Despite what would be logical, the mode attribute is a decl attribute, |
| 687 | /// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make |
| 688 | /// 'G' be HImode, not an intermediate pointer. |
| 689 | /// |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 690 | static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 691 | // This attribute isn't documented, but glibc uses it. It changes |
| 692 | // the width of an int or unsigned int to the specified size. |
| 693 | |
| 694 | // Check that there aren't any arguments |
| 695 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 696 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 697 | std::string("0")); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 698 | return; |
| 699 | } |
| 700 | |
| 701 | IdentifierInfo *Name = Attr.getParameterName(); |
| 702 | if (!Name) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 703 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 704 | return; |
| 705 | } |
| 706 | const char *Str = Name->getName(); |
| 707 | unsigned Len = Name->getLength(); |
| 708 | |
| 709 | // Normalize the attribute name, __foo__ becomes foo. |
| 710 | if (Len > 4 && Str[0] == '_' && Str[1] == '_' && |
| 711 | Str[Len - 2] == '_' && Str[Len - 1] == '_') { |
| 712 | Str += 2; |
| 713 | Len -= 4; |
| 714 | } |
| 715 | |
| 716 | unsigned DestWidth = 0; |
| 717 | bool IntegerMode = true; |
| 718 | switch (Len) { |
| 719 | case 2: |
| 720 | if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; } |
| 721 | if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; } |
| 722 | if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; } |
| 723 | if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; } |
| 724 | if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; } |
| 725 | if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; } |
| 726 | if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; } |
| 727 | if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; } |
| 728 | if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; } |
| 729 | break; |
| 730 | case 4: |
| 731 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 732 | // pointer on PIC16 and other embedded platforms. |
| 733 | if (!memcmp(Str, "word", 4)) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 734 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 735 | if (!memcmp(Str, "byte", 4)) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 736 | DestWidth = S.Context.Target.getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 737 | break; |
| 738 | case 7: |
| 739 | if (!memcmp(Str, "pointer", 7)) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 740 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 741 | break; |
| 742 | } |
| 743 | |
| 744 | QualType OldTy; |
| 745 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 746 | OldTy = TD->getUnderlyingType(); |
| 747 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 748 | OldTy = VD->getType(); |
| 749 | else { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 750 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl, "mode", |
| 751 | SourceRange(Attr.getLoc(), Attr.getLoc())); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 752 | return; |
| 753 | } |
| 754 | |
| 755 | // FIXME: Need proper fixed-width types |
| 756 | QualType NewTy; |
| 757 | switch (DestWidth) { |
| 758 | case 0: |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 759 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode, Name->getName()); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 760 | return; |
| 761 | default: |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 762 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode, Name->getName()); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 763 | return; |
| 764 | case 8: |
| 765 | assert(IntegerMode); |
| 766 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 767 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 768 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 769 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 770 | break; |
| 771 | case 16: |
| 772 | assert(IntegerMode); |
| 773 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 774 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 775 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 776 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 777 | break; |
| 778 | case 32: |
| 779 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 780 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 781 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 782 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 783 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 784 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 785 | break; |
| 786 | case 64: |
| 787 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 788 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 789 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 790 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 791 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 792 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 793 | break; |
| 794 | } |
| 795 | |
| 796 | if (!OldTy->getAsBuiltinType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 797 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 798 | else if (!(IntegerMode && OldTy->isIntegerType()) && |
| 799 | !(!IntegerMode && OldTy->isFloatingType())) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 800 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | // Install the new type. |
| 804 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 805 | TD->setUnderlyingType(NewTy); |
| 806 | else |
| 807 | cast<ValueDecl>(D)->setType(NewTy); |
| 808 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 809 | |
| 810 | //===----------------------------------------------------------------------===// |
| 811 | // Top Level Sema Entry Points |
| 812 | //===----------------------------------------------------------------------===// |
| 813 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 814 | /// HandleDeclAttribute - Apply the specific attribute to the specified decl if |
| 815 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 816 | /// silently ignore it. |
| 817 | static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) { |
| 818 | switch (Attr.getKind()) { |
| 819 | case AttributeList::AT_address_space: |
| 820 | // Ignore this, this is a type attribute, handled by ProcessTypeAttributes. |
| 821 | break; |
| 822 | case AttributeList::AT_ext_vector_type: |
| 823 | HandleExtVectorTypeAttr(D, Attr, S); |
| 824 | break; |
| 825 | case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break; |
| 826 | case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break; |
| 827 | case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break; |
| 828 | case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break; |
| 829 | case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break; |
| 830 | case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break; |
| 831 | case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break; |
| 832 | case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break; |
| 833 | case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break; |
| 834 | case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break; |
| 835 | case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break; |
| 836 | case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break; |
| 837 | case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break; |
| 838 | case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break; |
| 839 | case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break; |
| 840 | case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 841 | case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break; |
| 842 | case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 843 | case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 844 | case AttributeList::AT_transparent_union: |
| 845 | HandleTransparentUnionAttr(D, Attr, S); |
| 846 | break; |
| 847 | default: |
| 848 | #if 0 |
| 849 | // TODO: when we have the full set of attributes, warn about unknown ones. |
| 850 | S.Diag(Attr->getLoc(), diag::warn_attribute_ignored, |
| 851 | Attr->getName()->getName()); |
| 852 | #endif |
| 853 | break; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 858 | /// attribute list to the specified decl, ignoring any type attributes. |
| 859 | void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) { |
| 860 | while (AttrList) { |
| 861 | ProcessDeclAttribute(D, *AttrList, *this); |
| 862 | AttrList = AttrList->getNext(); |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 867 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 868 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 869 | /// specified in many different places, and we need to find and apply them all. |
| 870 | void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) { |
| 871 | // Apply decl attributes from the DeclSpec if present. |
| 872 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes()) |
| 873 | ProcessDeclAttributeList(D, Attrs); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 874 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 875 | // Walk the declarator structure, applying decl attributes that were in a type |
| 876 | // position to the decl itself. This handles cases like: |
| 877 | // int *__attr__(x)** D; |
| 878 | // when X is a decl attribute. |
| 879 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 880 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
| 881 | ProcessDeclAttributeList(D, Attrs); |
| 882 | |
| 883 | // Finally, apply any attributes on the decl itself. |
| 884 | if (const AttributeList *Attrs = PD.getAttributes()) |
| 885 | ProcessDeclAttributeList(D, Attrs); |
| 886 | } |
| 887 | |