blob: fd79a10bf4b73c9de401a310aed9f12a6199ec89 [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- 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 Dunbaracc5f3e2008-08-11 06:23:49 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/Expr.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000018#include "clang/Basic/Diagnostic.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000019#include "clang/Basic/TargetInfo.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Ted Kremenek6e1eb872008-07-22 16:56:21 +000021#include <llvm/ADT/StringExtras.h>
Chris Lattner6b6b5372008-06-26 18:38:35 +000022using namespace clang;
23
Chris Lattnere5c5ee12008-06-29 00:16:31 +000024//===----------------------------------------------------------------------===//
25// Helper functions
26//===----------------------------------------------------------------------===//
27
Chris Lattner6b6b5372008-06-26 18:38:35 +000028static const FunctionTypeProto *getFunctionProto(Decl *d) {
29 QualType Ty;
Chris Lattner6b6b5372008-06-26 18:38:35 +000030 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 Dunbar35682492008-09-26 04:12:28 +000048// 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.
53static bool isFunctionOrMethod(Decl *d) {
54 return getFunctionProto(d) || isa<ObjCMethodDecl>(d);
55
56}
57
58static 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
66static 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
74static 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 Lattner6b6b5372008-06-26 18:38:35 +000082static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Chris Lattnerb77792e2008-07-26 22:17:49 +000083 const PointerType *PT = T->getAsPointerType();
84 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +000085 return false;
86
Chris Lattnerb77792e2008-07-26 22:17:49 +000087 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6b6b5372008-06-26 18:38:35 +000088 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 Dunbar085e8f72008-09-26 03:32:58 +000098static 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 Lattnere5c5ee12008-06-29 00:16:31 +0000114//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000115// Attribute Implementations
116//===----------------------------------------------------------------------===//
117
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000118// 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 Lattner803d0802008-06-29 00:43:07 +0000122static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
123 Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000124 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
125 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000126 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000127 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000128 }
129
Chris Lattner6b6b5372008-06-26 18:38:35 +0000130 QualType curType = tDecl->getUnderlyingType();
131 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000132 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000133 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
134 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000135 return;
136 }
Chris Lattner545dd342008-06-28 23:36:30 +0000137 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000138 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000139 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
140 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
141 "ext_vector_type", sizeExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000142 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 Lattnerb77792e2008-07-26 22:17:49 +0000146 if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000147 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000148 curType.getAsString());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000149 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 Lattner803d0802008-06-29 00:43:07 +0000156 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size,
157 sizeExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000158 return;
159 }
160 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner803d0802008-06-29 00:43:07 +0000161 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000162 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner803d0802008-06-29 00:43:07 +0000163 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000164}
165
Chris Lattner065c5a82008-06-28 23:48:25 +0000166
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 Lattner803d0802008-06-29 00:43:07 +0000175static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner065c5a82008-06-28 23:48:25 +0000176 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 Lattner803d0802008-06-29 00:43:07 +0000182 S.Diag(D->getLocation(), diag::err_attr_wrong_decl,
183 std::string("vector_size"),
184 SourceRange(Attr.getLoc(), Attr.getLoc()));
Chris Lattner065c5a82008-06-28 23:48:25 +0000185 return;
186 }
187
188 // Check the attribute arugments.
Chris Lattner545dd342008-06-28 23:36:30 +0000189 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000190 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
191 std::string("1"));
Chris Lattner065c5a82008-06-28 23:48:25 +0000192 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000193 }
Chris Lattner545dd342008-06-28 23:36:30 +0000194 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000195 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000196 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
197 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
198 "vector_size", sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000199 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000200 }
201 // navigate to the base type - we need to provide for vector pointers,
202 // vector arrays, and functions returning vectors.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000203 if (CurType->isPointerType() || CurType->isArrayType() ||
204 CurType->isFunctionType()) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000205 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 Lattnerb77792e2008-07-26 22:17:49 +0000219 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000220 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000221 CurType.getAsString());
Chris Lattner065c5a82008-06-28 23:48:25 +0000222 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000223 }
Chris Lattner803d0802008-06-29 00:43:07 +0000224 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000225 // 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 Lattner803d0802008-06-29 00:43:07 +0000230 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size,
231 sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000232 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000233 }
234 if (vectorSize == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000235 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size,
236 sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000237 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000238 }
Chris Lattner065c5a82008-06-28 23:48:25 +0000239
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 Lattner803d0802008-06-29 00:43:07 +0000242 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner065c5a82008-06-28 23:48:25 +0000243
244 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
245 VD->setType(CurType);
246 else
247 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000248}
249
Chris Lattner803d0802008-06-29 00:43:07 +0000250static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000251 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000252 if (Attr.getNumArgs() > 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000253 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
254 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000255 return;
256 }
257
258 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Chris Lattner49e2d342008-06-28 23:50:44 +0000259 TD->addAttr(new PackedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000260 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 Lattner803d0802008-06-29 00:43:07 +0000264 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 Lattner6b6b5372008-06-26 18:38:35 +0000268 else
Chris Lattner49e2d342008-06-28 23:50:44 +0000269 FD->addAttr(new PackedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000270 } else
Chris Lattner803d0802008-06-29 00:43:07 +0000271 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored,
272 Attr.getName()->getName());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000273}
274
Ted Kremenek96329d42008-07-15 22:26:48 +0000275static 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 Kremenekeb2b2a32008-07-21 21:53:04 +0000291static 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 Kremenekeb2b2a32008-07-21 21:53:04 +0000296 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 Kremenek6e1eb872008-07-22 16:56:21 +0000324 "nonnull", llvm::utostr_32(I.getArgNum()), Ex->getSourceRange());
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000325 return;
326 }
Ted Kremenek465172f2008-07-21 22:09:15 +0000327
328 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000329
330 // Is the function argument a pointer type?
Chris Lattnerb77792e2008-07-26 22:17:49 +0000331 if (!proto->getArgType(x)->isPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000332 // FIXME: Should also highlight argument in decl.
333 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only,
334 "nonnull", Ex->getSourceRange());
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000335 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000336 }
337
338 NonNullArgs.push_back(x);
339 }
340
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000341 // 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 Kremenekeb2b2a32008-07-21 21:53:04 +0000355 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000356
357 unsigned* start = &NonNullArgs[0];
358 unsigned size = NonNullArgs.size();
359 std::sort(start, start + size);
360 d->addAttr(new NonNullAttr(start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000361}
362
Chris Lattner803d0802008-06-29 00:43:07 +0000363static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000364 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000365 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000366 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
367 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000368 return;
369 }
370
Chris Lattner545dd342008-06-28 23:36:30 +0000371 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000372 Arg = Arg->IgnoreParenCasts();
373 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
374
375 if (Str == 0 || Str->isWide()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000376 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
377 "alias", std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000378 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 Lattner803d0802008-06-29 00:43:07 +0000389static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000390 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000391 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000392 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
393 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000394 return;
395 }
396
Daniel Dunbar35682492008-09-26 04:12:28 +0000397 if (!isa<FunctionDecl>(d) && !isa<ObjCMethodDecl>(d)) {
Chris Lattner803d0802008-06-29 00:43:07 +0000398 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
399 "noreturn", "function");
Chris Lattner6b6b5372008-06-26 18:38:35 +0000400 return;
401 }
402
403 d->addAttr(new NoReturnAttr());
404}
405
Ted Kremenek73798892008-07-25 04:39:19 +0000406static 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 Kremenek356b63a2008-08-07 01:02:05 +0000414 if (!isa<VarDecl>(d) && !getFunctionProto(d)) {
Ted Kremenek73798892008-07-25 04:39:19 +0000415 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
Ted Kremenek356b63a2008-08-07 01:02:05 +0000416 "unused", "variable and function");
Ted Kremenek73798892008-07-25 04:39:19 +0000417 return;
418 }
419
420 d->addAttr(new UnusedAttr());
421}
422
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000423static 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
452static 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 Carlsson6782fc62008-08-22 22:10:48 +0000471 if (!isa<FunctionDecl>(d)) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000472 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 Lattner803d0802008-06-29 00:43:07 +0000480static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000481 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000482 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000483 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
484 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000485 return;
486 }
487
488 d->addAttr(new DeprecatedAttr());
489}
490
Chris Lattner803d0802008-06-29 00:43:07 +0000491static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000492 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000493 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000494 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
495 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000496 return;
497 }
498
Chris Lattner545dd342008-06-28 23:36:30 +0000499 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000500 Arg = Arg->IgnoreParenCasts();
501 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
502
503 if (Str == 0 || Str->isWide()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000504 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
505 "visibility", std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000506 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 Lattner803d0802008-06-29 00:43:07 +0000522 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
523 "visibility", TypeStr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000524 return;
525 }
526
527 d->addAttr(new VisibilityAttr(type));
528}
529
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000530static void HandleObjCGCAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson6e14a8f2008-08-24 16:33:25 +0000531 if (!Attr.getParameterName()) {
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000532 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 Carlsson6e14a8f2008-08-24 16:33:25 +0000550 else if (TypeLen == 6 && !memcmp(TypeStr, "strong", 6))
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000551 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 Naroff9eae5762008-09-18 16:44:58 +0000561static 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 Carlsson77091822008-10-05 18:05:59 +0000589static 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 Lattner803d0802008-06-29 00:43:07 +0000654static void HandleWeakAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000655 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000656 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000657 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
658 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000659 return;
660 }
661
662 d->addAttr(new WeakAttr());
663}
664
Chris Lattner803d0802008-06-29 00:43:07 +0000665static void HandleDLLImportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000666 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000667 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000668 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
669 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000670 return;
671 }
672
673 d->addAttr(new DLLImportAttr());
674}
675
Chris Lattner803d0802008-06-29 00:43:07 +0000676static void HandleDLLExportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000677 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000678 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000679 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
680 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000681 return;
682 }
683
684 d->addAttr(new DLLExportAttr());
685}
686
Chris Lattner803d0802008-06-29 00:43:07 +0000687static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000688 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000689 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000690 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
691 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000692 return;
693 }
694
695 d->addAttr(new StdCallAttr());
696}
697
Chris Lattner803d0802008-06-29 00:43:07 +0000698static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000699 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000700 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000701 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
702 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000703 return;
704 }
705
706 d->addAttr(new FastCallAttr());
707}
708
Chris Lattner803d0802008-06-29 00:43:07 +0000709static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000710 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000711 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000712 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
713 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000714 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 Lattner803d0802008-06-29 00:43:07 +0000722static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000723
Chris Lattner545dd342008-06-28 23:36:30 +0000724 if (!Attr.getParameterName()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000725 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000726 "format", std::string("1"));
727 return;
728 }
729
Chris Lattner545dd342008-06-28 23:36:30 +0000730 if (Attr.getNumArgs() != 2) {
Chris Lattner803d0802008-06-29 00:43:07 +0000731 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
732 std::string("3"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000733 return;
734 }
735
736 // GCC ignores the format attribute on K&R style function
737 // prototypes, so we ignore it as well
Daniel Dunbar35682492008-09-26 04:12:28 +0000738 if (!isFunctionOrMethod(d)) {
Chris Lattner803d0802008-06-29 00:43:07 +0000739 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
740 "format", "function");
Chris Lattner6b6b5372008-06-26 18:38:35 +0000741 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 Dunbar35682492008-09-26 04:12:28 +0000747 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000748 unsigned FirstIdx = 1;
749
Chris Lattner545dd342008-06-28 23:36:30 +0000750 const char *Format = Attr.getParameterName()->getName();
751 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000752
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 Dunbar085e8f72008-09-26 03:32:58 +0000763 bool is_CFString = false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000764
765 switch (FormatLen) {
766 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +0000767 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 Lattner6b6b5372008-06-26 18:38:35 +0000770 case 8:
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000771 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
772 (is_NSString = !memcmp(Format, "NSString", 8)) ||
773 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000774 break;
775 }
776
777 if (!Supported) {
Chris Lattner803d0802008-06-29 00:43:07 +0000778 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
Chris Lattner545dd342008-06-28 23:36:30 +0000779 "format", Attr.getParameterName()->getName());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000780 return;
781 }
782
783 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +0000784 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +0000785 llvm::APSInt Idx(32);
786 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
787 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000788 "format", std::string("2"), IdxExpr->getSourceRange());
789 return;
790 }
791
792 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner803d0802008-06-29 00:43:07 +0000793 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000794 "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 Dunbar35682492008-09-26 04:12:28 +0000802 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000803
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000804 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 Lattner6b6b5372008-06-26 18:38:35 +0000811 // FIXME: do we need to check if the type is NSString*? What are
812 // the semantics?
Chris Lattner803d0802008-06-29 00:43:07 +0000813 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000814 // FIXME: Should highlight the actual expression that has the
815 // wrong type.
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000816 S.Diag(Attr.getLoc(), diag::err_format_attribute_not,
817 "an NSString", IdxExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000818 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 Dunbar085e8f72008-09-26 03:32:58 +0000824 S.Diag(Attr.getLoc(), diag::err_format_attribute_not,
825 "a string type", IdxExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000826 return;
827 }
828
829 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +0000830 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +0000831 llvm::APSInt FirstArg(32);
832 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
833 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000834 "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 Dunbar35682492008-09-26 04:12:28 +0000840 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000841 ++NumArgs; // +1 for ...
842 } else {
Chris Lattner803d0802008-06-29 00:43:07 +0000843 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000844 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 Lattner803d0802008-06-29 00:43:07 +0000852 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000853 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 Lattner803d0802008-06-29 00:43:07 +0000858 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000859 "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 Lattner0b2f4da2008-06-29 00:28:59 +0000867static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
868 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000869 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000870 if (Attr.getNumArgs() != 0) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000871 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000872 std::string("0"));
873 return;
874 }
875
Eli Friedmanbc887452008-09-02 05:19:23 +0000876 // FIXME: This shouldn't be restricted to typedefs
877 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
878 if (!TD || !TD->getUnderlyingType()->isUnionType()) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000879 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000880 "transparent_union", "union");
881 return;
882 }
883
Eli Friedmanbc887452008-09-02 05:19:23 +0000884 RecordDecl* RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000885
Eli Friedmanbc887452008-09-02 05:19:23 +0000886 // 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 Lattner6b6b5372008-06-26 18:38:35 +0000904}
905
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000906static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000907 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000908 if (Attr.getNumArgs() != 1) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000909 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
910 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000911 return;
912 }
Chris Lattner545dd342008-06-28 23:36:30 +0000913 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000914 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 Lattner0b2f4da2008-06-29 00:28:59 +0000919 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000920 return;
921 }
922 d->addAttr(new AnnotateAttr(std::string(SE->getStrData(),
923 SE->getByteLength())));
924}
925
Chris Lattner803d0802008-06-29 00:43:07 +0000926static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000927 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000928 if (Attr.getNumArgs() > 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000929 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
930 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000931 return;
932 }
933
934 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +0000935 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000936 // 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 Lattner6b6b5372008-06-26 18:38:35 +0000940 }
Chris Lattner49e2d342008-06-28 23:50:44 +0000941
942 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
943 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000944 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
945 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
946 "aligned", alignmentExpr->getSourceRange());
Chris Lattner49e2d342008-06-28 23:50:44 +0000947 return;
948 }
949 d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000950}
Chris Lattnerfbf13472008-06-27 22:18:37 +0000951
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000952/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner065c5a82008-06-28 23:48:25 +0000953/// primitive type.
Chris Lattnerfbf13472008-06-27 22:18:37 +0000954///
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 Lattner0b2f4da2008-06-29 00:28:59 +0000959static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +0000960 // 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 Lattner0b2f4da2008-06-29 00:28:59 +0000965 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
966 std::string("0"));
Chris Lattnerfbf13472008-06-27 22:18:37 +0000967 return;
968 }
969
970 IdentifierInfo *Name = Attr.getParameterName();
971 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000972 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000973 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 Lattner0b2f4da2008-06-29 00:28:59 +00001003 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001004 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001005 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001006 break;
1007 case 7:
1008 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001009 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001010 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 Lattner0b2f4da2008-06-29 00:28:59 +00001019 S.Diag(D->getLocation(), diag::err_attr_wrong_decl, "mode",
1020 SourceRange(Attr.getLoc(), Attr.getLoc()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00001021 return;
1022 }
1023
1024 // FIXME: Need proper fixed-width types
1025 QualType NewTy;
1026 switch (DestWidth) {
1027 case 0:
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001028 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode, Name->getName());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001029 return;
1030 default:
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001031 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode, Name->getName());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001032 return;
1033 case 8:
1034 assert(IntegerMode);
1035 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001036 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001037 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001038 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001039 break;
1040 case 16:
1041 assert(IntegerMode);
1042 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001043 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001044 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001045 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001046 break;
1047 case 32:
1048 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001049 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001050 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001051 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001052 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001053 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001054 break;
1055 case 64:
1056 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001057 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001058 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001059 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001060 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001061 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001062 break;
1063 }
1064
1065 if (!OldTy->getAsBuiltinType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001066 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001067 else if (!(IntegerMode && OldTy->isIntegerType()) &&
1068 !(!IntegerMode && OldTy->isFloatingType())) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001069 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001070 }
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 Lattner0744e5f2008-06-29 00:23:49 +00001078
1079//===----------------------------------------------------------------------===//
1080// Top Level Sema Entry Points
1081//===----------------------------------------------------------------------===//
1082
Chris Lattner803d0802008-06-29 00:43:07 +00001083/// 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.
1086static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
1087 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001088 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001089 case AttributeList::AT_address_space:
1090 // Ignore this, this is a type attribute, handled by ProcessTypeAttributes.
1091 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001092 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 Lattner803d0802008-06-29 00:43:07 +00001100 case AttributeList::AT_ext_vector_type:
1101 HandleExtVectorTypeAttr(D, Attr, S);
1102 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001103 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1104 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001105 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001106 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 Lattner803d0802008-06-29 00:43:07 +00001113 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
1114 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001115 case AttributeList::AT_transparent_union:
1116 HandleTransparentUnionAttr(D, Attr, S);
1117 break;
Anders Carlssonaa0d25b2008-08-23 23:22:21 +00001118 case AttributeList::AT_objc_gc: HandleObjCGCAttr (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001119 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001120 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001121 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.
1133void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1134 while (AttrList) {
1135 ProcessDeclAttribute(D, *AttrList, *this);
1136 AttrList = AttrList->getNext();
1137 }
1138}
1139
1140
Chris Lattner0744e5f2008-06-29 00:23:49 +00001141/// 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.
1144void 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 Lattner803d0802008-06-29 00:43:07 +00001148
Chris Lattner0744e5f2008-06-29 00:23:49 +00001149 // 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