blob: e6dac4901edd38a0d40de394c8f4e2f7746f5fac [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"
Chris Lattnerfbf13472008-06-27 22:18:37 +000016#include "clang/Basic/TargetInfo.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000017using namespace clang;
18
Chris Lattnere5c5ee12008-06-29 00:16:31 +000019//===----------------------------------------------------------------------===//
20// Helper functions
21//===----------------------------------------------------------------------===//
22
Chris Lattner6b6b5372008-06-26 18:38:35 +000023static const FunctionTypeProto *getFunctionProto(Decl *d) {
24 QualType Ty;
Chris Lattner6b6b5372008-06-26 18:38:35 +000025 if (ValueDecl *decl = dyn_cast<ValueDecl>(d))
26 Ty = decl->getType();
27 else if (FieldDecl *decl = dyn_cast<FieldDecl>(d))
28 Ty = decl->getType();
29 else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
30 Ty = decl->getUnderlyingType();
31 else
32 return 0;
33
34 if (Ty->isFunctionPointerType())
35 Ty = Ty->getAsPointerType()->getPointeeType();
36
37 if (const FunctionType *FnTy = Ty->getAsFunctionType())
38 return dyn_cast<FunctionTypeProto>(FnTy->getAsFunctionType());
39
40 return 0;
41}
42
43static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
44 if (!T->isPointerType())
45 return false;
46
47 T = T->getAsPointerType()->getPointeeType().getCanonicalType();
48 ObjCInterfaceType* ClsT = dyn_cast<ObjCInterfaceType>(T.getTypePtr());
49
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 Lattnere5c5ee12008-06-29 00:16:31 +000060//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +000061// Attribute Implementations
62//===----------------------------------------------------------------------===//
63
Chris Lattner803d0802008-06-29 00:43:07 +000064static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
65 Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +000066 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
67 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +000068 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +000069 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +000070 }
71
Chris Lattner6b6b5372008-06-26 18:38:35 +000072 QualType curType = tDecl->getUnderlyingType();
73 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +000074 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +000075 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
76 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +000077 return;
78 }
Chris Lattner545dd342008-06-28 23:36:30 +000079 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +000080 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +000081 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
82 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
83 "ext_vector_type", sizeExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +000084 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.).
88 Type *canonType = curType.getCanonicalType().getTypePtr();
89 if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) {
Chris Lattner803d0802008-06-29 00:43:07 +000090 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type,
91 curType.getCanonicalType().getAsString());
Chris Lattner6b6b5372008-06-26 18:38:35 +000092 return;
93 }
94 // unlike gcc's vector_size attribute, the size is specified as the
95 // number of elements, not the number of bytes.
96 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
97
98 if (vectorSize == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +000099 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size,
100 sizeExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000101 return;
102 }
103 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner803d0802008-06-29 00:43:07 +0000104 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000105 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner803d0802008-06-29 00:43:07 +0000106 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000107}
108
Chris Lattner065c5a82008-06-28 23:48:25 +0000109
110/// HandleVectorSizeAttribute - this attribute is only applicable to
111/// integral and float scalars, although arrays, pointers, and function
112/// return values are allowed in conjunction with this construct. Aggregates
113/// with this attribute are invalid, even if they are of the same size as a
114/// corresponding scalar.
115/// The raw attribute should contain precisely 1 argument, the vector size
116/// for the variable, measured in bytes. If curType and rawAttr are well
117/// formed, this routine will return a new vector type.
Chris Lattner803d0802008-06-29 00:43:07 +0000118static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner065c5a82008-06-28 23:48:25 +0000119 QualType CurType;
120 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
121 CurType = VD->getType();
122 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
123 CurType = TD->getUnderlyingType();
124 else {
Chris Lattner803d0802008-06-29 00:43:07 +0000125 S.Diag(D->getLocation(), diag::err_attr_wrong_decl,
126 std::string("vector_size"),
127 SourceRange(Attr.getLoc(), Attr.getLoc()));
Chris Lattner065c5a82008-06-28 23:48:25 +0000128 return;
129 }
130
131 // Check the attribute arugments.
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 Lattner065c5a82008-06-28 23:48:25 +0000135 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000136 }
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 "vector_size", sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000142 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000143 }
144 // navigate to the base type - we need to provide for vector pointers,
145 // vector arrays, and functions returning vectors.
Chris Lattner065c5a82008-06-28 23:48:25 +0000146 Type *canonType = CurType.getCanonicalType().getTypePtr();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000147
148 if (canonType->isPointerType() || canonType->isArrayType() ||
149 canonType->isFunctionType()) {
150 assert(0 && "HandleVector(): Complex type construction unimplemented");
151 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
152 do {
153 if (PointerType *PT = dyn_cast<PointerType>(canonType))
154 canonType = PT->getPointeeType().getTypePtr();
155 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
156 canonType = AT->getElementType().getTypePtr();
157 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
158 canonType = FT->getResultType().getTypePtr();
159 } while (canonType->isPointerType() || canonType->isArrayType() ||
160 canonType->isFunctionType());
161 */
162 }
163 // the base type must be integer or float.
164 if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) {
Chris Lattner803d0802008-06-29 00:43:07 +0000165 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type,
166 CurType.getCanonicalType().getAsString());
Chris Lattner065c5a82008-06-28 23:48:25 +0000167 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000168 }
Chris Lattner803d0802008-06-29 00:43:07 +0000169 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000170 // vecSize is specified in bytes - convert to bits.
171 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
172
173 // the vector size needs to be an integral multiple of the type size.
174 if (vectorSize % typeSize) {
Chris Lattner803d0802008-06-29 00:43:07 +0000175 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size,
176 sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000177 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000178 }
179 if (vectorSize == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000180 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size,
181 sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000182 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000183 }
Chris Lattner065c5a82008-06-28 23:48:25 +0000184
185 // Success! Instantiate the vector type, the number of elements is > 0, and
186 // not required to be a power of 2, unlike GCC.
Chris Lattner803d0802008-06-29 00:43:07 +0000187 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner065c5a82008-06-28 23:48:25 +0000188
189 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
190 VD->setType(CurType);
191 else
192 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000193}
194
Chris Lattner803d0802008-06-29 00:43:07 +0000195static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000196 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000197 if (Attr.getNumArgs() > 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000198 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
199 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000200 return;
201 }
202
203 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Chris Lattner49e2d342008-06-28 23:50:44 +0000204 TD->addAttr(new PackedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000205 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
206 // If the alignment is less than or equal to 8 bits, the packed attribute
207 // has no effect.
208 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000209 S.Context.getTypeAlign(FD->getType()) <= 8)
210 S.Diag(Attr.getLoc(),
211 diag::warn_attribute_ignored_for_field_of_type,
212 Attr.getName()->getName(), FD->getType().getAsString());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000213 else
Chris Lattner49e2d342008-06-28 23:50:44 +0000214 FD->addAttr(new PackedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000215 } else
Chris Lattner803d0802008-06-29 00:43:07 +0000216 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored,
217 Attr.getName()->getName());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000218}
219
Chris Lattner803d0802008-06-29 00:43:07 +0000220static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000221 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000222 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000223 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
224 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000225 return;
226 }
227
Chris Lattner545dd342008-06-28 23:36:30 +0000228 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000229 Arg = Arg->IgnoreParenCasts();
230 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
231
232 if (Str == 0 || Str->isWide()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000233 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
234 "alias", std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000235 return;
236 }
237
238 const char *Alias = Str->getStrData();
239 unsigned AliasLen = Str->getByteLength();
240
241 // FIXME: check if target symbol exists in current file
242
243 d->addAttr(new AliasAttr(std::string(Alias, AliasLen)));
244}
245
Chris Lattner803d0802008-06-29 00:43:07 +0000246static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000247 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000248 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000249 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
250 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000251 return;
252 }
253
254 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000255 if (!Fn) {
Chris Lattner803d0802008-06-29 00:43:07 +0000256 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
257 "noreturn", "function");
Chris Lattner6b6b5372008-06-26 18:38:35 +0000258 return;
259 }
260
261 d->addAttr(new NoReturnAttr());
262}
263
Chris Lattner803d0802008-06-29 00:43:07 +0000264static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000265 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000266 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000267 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
268 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000269 return;
270 }
271
272 d->addAttr(new DeprecatedAttr());
273}
274
Chris Lattner803d0802008-06-29 00:43:07 +0000275static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000276 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000277 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000278 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
279 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000280 return;
281 }
282
Chris Lattner545dd342008-06-28 23:36:30 +0000283 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000284 Arg = Arg->IgnoreParenCasts();
285 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
286
287 if (Str == 0 || Str->isWide()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000288 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
289 "visibility", std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000290 return;
291 }
292
293 const char *TypeStr = Str->getStrData();
294 unsigned TypeLen = Str->getByteLength();
295 VisibilityAttr::VisibilityTypes type;
296
297 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
298 type = VisibilityAttr::DefaultVisibility;
299 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
300 type = VisibilityAttr::HiddenVisibility;
301 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
302 type = VisibilityAttr::HiddenVisibility; // FIXME
303 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
304 type = VisibilityAttr::ProtectedVisibility;
305 else {
Chris Lattner803d0802008-06-29 00:43:07 +0000306 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
307 "visibility", TypeStr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000308 return;
309 }
310
311 d->addAttr(new VisibilityAttr(type));
312}
313
Chris Lattner803d0802008-06-29 00:43:07 +0000314static void HandleWeakAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000315 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000316 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000317 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
318 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000319 return;
320 }
321
322 d->addAttr(new WeakAttr());
323}
324
Chris Lattner803d0802008-06-29 00:43:07 +0000325static void HandleDLLImportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000326 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000327 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000328 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
329 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000330 return;
331 }
332
333 d->addAttr(new DLLImportAttr());
334}
335
Chris Lattner803d0802008-06-29 00:43:07 +0000336static void HandleDLLExportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000337 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000338 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000339 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
340 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000341 return;
342 }
343
344 d->addAttr(new DLLExportAttr());
345}
346
Chris Lattner803d0802008-06-29 00:43:07 +0000347static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000348 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000349 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000350 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
351 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000352 return;
353 }
354
355 d->addAttr(new StdCallAttr());
356}
357
Chris Lattner803d0802008-06-29 00:43:07 +0000358static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000359 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000360 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000361 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
362 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000363 return;
364 }
365
366 d->addAttr(new FastCallAttr());
367}
368
Chris Lattner803d0802008-06-29 00:43:07 +0000369static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000370 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000371 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000372 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
373 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000374 return;
375 }
376
377 d->addAttr(new NoThrowAttr());
378}
379
380/// Handle __attribute__((format(type,idx,firstarg))) attributes
381/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +0000382static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000383
Chris Lattner545dd342008-06-28 23:36:30 +0000384 if (!Attr.getParameterName()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000385 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000386 "format", std::string("1"));
387 return;
388 }
389
Chris Lattner545dd342008-06-28 23:36:30 +0000390 if (Attr.getNumArgs() != 2) {
Chris Lattner803d0802008-06-29 00:43:07 +0000391 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
392 std::string("3"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000393 return;
394 }
395
396 // GCC ignores the format attribute on K&R style function
397 // prototypes, so we ignore it as well
398 const FunctionTypeProto *proto = getFunctionProto(d);
399
400 if (!proto) {
Chris Lattner803d0802008-06-29 00:43:07 +0000401 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
402 "format", "function");
Chris Lattner6b6b5372008-06-26 18:38:35 +0000403 return;
404 }
405
406 // FIXME: in C++ the implicit 'this' function parameter also counts.
407 // this is needed in order to be compatible with GCC
408 // the index must start in 1 and the limit is numargs+1
409 unsigned NumArgs = proto->getNumArgs();
410 unsigned FirstIdx = 1;
411
Chris Lattner545dd342008-06-28 23:36:30 +0000412 const char *Format = Attr.getParameterName()->getName();
413 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000414
415 // Normalize the argument, __foo__ becomes foo.
416 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
417 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
418 Format += 2;
419 FormatLen -= 4;
420 }
421
422 bool Supported = false;
423 bool is_NSString = false;
424 bool is_strftime = false;
425
426 switch (FormatLen) {
427 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +0000428 case 5: Supported = !memcmp(Format, "scanf", 5); break;
429 case 6: Supported = !memcmp(Format, "printf", 6); break;
430 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000431 case 8:
432 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
433 (is_NSString = !memcmp(Format, "NSString", 8));
434 break;
435 }
436
437 if (!Supported) {
Chris Lattner803d0802008-06-29 00:43:07 +0000438 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
Chris Lattner545dd342008-06-28 23:36:30 +0000439 "format", Attr.getParameterName()->getName());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000440 return;
441 }
442
443 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +0000444 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +0000445 llvm::APSInt Idx(32);
446 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
447 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000448 "format", std::string("2"), IdxExpr->getSourceRange());
449 return;
450 }
451
452 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner803d0802008-06-29 00:43:07 +0000453 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000454 "format", std::string("2"), IdxExpr->getSourceRange());
455 return;
456 }
457
458 // FIXME: Do we need to bounds check?
459 unsigned ArgIdx = Idx.getZExtValue() - 1;
460
461 // make sure the format string is really a string
462 QualType Ty = proto->getArgType(ArgIdx);
463
464 if (is_NSString) {
465 // FIXME: do we need to check if the type is NSString*? What are
466 // the semantics?
Chris Lattner803d0802008-06-29 00:43:07 +0000467 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000468 // FIXME: Should highlight the actual expression that has the
469 // wrong type.
Chris Lattner803d0802008-06-29 00:43:07 +0000470 S.Diag(Attr.getLoc(), diag::err_format_attribute_not_NSString,
471 IdxExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000472 return;
473 }
474 } else if (!Ty->isPointerType() ||
475 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
476 // FIXME: Should highlight the actual expression that has the
477 // wrong type.
Chris Lattner803d0802008-06-29 00:43:07 +0000478 S.Diag(Attr.getLoc(), diag::err_format_attribute_not_string,
479 IdxExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000480 return;
481 }
482
483 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +0000484 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +0000485 llvm::APSInt FirstArg(32);
486 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
487 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000488 "format", std::string("3"), FirstArgExpr->getSourceRange());
489 return;
490 }
491
492 // check if the function is variadic if the 3rd argument non-zero
493 if (FirstArg != 0) {
494 if (proto->isVariadic()) {
495 ++NumArgs; // +1 for ...
496 } else {
Chris Lattner803d0802008-06-29 00:43:07 +0000497 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000498 return;
499 }
500 }
501
502 // strftime requires FirstArg to be 0 because it doesn't read from any variable
503 // the input is just the current time + the format string
504 if (is_strftime) {
505 if (FirstArg != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000506 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000507 FirstArgExpr->getSourceRange());
508 return;
509 }
510 // if 0 it disables parameter checking (to use with e.g. va_list)
511 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner803d0802008-06-29 00:43:07 +0000512 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000513 "format", std::string("3"), FirstArgExpr->getSourceRange());
514 return;
515 }
516
517 d->addAttr(new FormatAttr(std::string(Format, FormatLen),
518 Idx.getZExtValue(), FirstArg.getZExtValue()));
519}
520
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000521static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
522 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000523 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000524 if (Attr.getNumArgs() != 0) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000525 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000526 std::string("0"));
527 return;
528 }
529
530 TypeDecl *decl = dyn_cast<TypeDecl>(d);
531
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000532 if (!decl || !S.Context.getTypeDeclType(decl)->isUnionType()) {
533 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000534 "transparent_union", "union");
535 return;
536 }
537
538 //QualType QTy = Context.getTypeDeclType(decl);
539 //const RecordType *Ty = QTy->getAsUnionType();
540
541// FIXME
542// Ty->addAttr(new TransparentUnionAttr());
543}
544
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000545static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000546 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000547 if (Attr.getNumArgs() != 1) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000548 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
549 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000550 return;
551 }
Chris Lattner545dd342008-06-28 23:36:30 +0000552 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000553 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
554
555 // Make sure that there is a string literal as the annotation's single
556 // argument.
557 if (!SE) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000558 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000559 return;
560 }
561 d->addAttr(new AnnotateAttr(std::string(SE->getStrData(),
562 SE->getByteLength())));
563}
564
Chris Lattner803d0802008-06-29 00:43:07 +0000565static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000566 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000567 if (Attr.getNumArgs() > 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000568 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
569 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000570 return;
571 }
572
573 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +0000574 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000575 // FIXME: This should be the target specific maximum alignment.
576 // (For now we just use 128 bits which is the maximum on X86.
577 Align = 128;
578 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000579 }
Chris Lattner49e2d342008-06-28 23:50:44 +0000580
581 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
582 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000583 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
584 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
585 "aligned", alignmentExpr->getSourceRange());
Chris Lattner49e2d342008-06-28 23:50:44 +0000586 return;
587 }
588 d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000589}
Chris Lattnerfbf13472008-06-27 22:18:37 +0000590
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000591/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner065c5a82008-06-28 23:48:25 +0000592/// primitive type.
Chris Lattnerfbf13472008-06-27 22:18:37 +0000593///
594/// Despite what would be logical, the mode attribute is a decl attribute,
595/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
596/// 'G' be HImode, not an intermediate pointer.
597///
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000598static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +0000599 // This attribute isn't documented, but glibc uses it. It changes
600 // the width of an int or unsigned int to the specified size.
601
602 // Check that there aren't any arguments
603 if (Attr.getNumArgs() != 0) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000604 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
605 std::string("0"));
Chris Lattnerfbf13472008-06-27 22:18:37 +0000606 return;
607 }
608
609 IdentifierInfo *Name = Attr.getParameterName();
610 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000611 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000612 return;
613 }
614 const char *Str = Name->getName();
615 unsigned Len = Name->getLength();
616
617 // Normalize the attribute name, __foo__ becomes foo.
618 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
619 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
620 Str += 2;
621 Len -= 4;
622 }
623
624 unsigned DestWidth = 0;
625 bool IntegerMode = true;
626 switch (Len) {
627 case 2:
628 if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; }
629 if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; }
630 if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; }
631 if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; }
632 if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; }
633 if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; }
634 if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; }
635 if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; }
636 if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; }
637 break;
638 case 4:
639 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
640 // pointer on PIC16 and other embedded platforms.
641 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000642 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000643 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000644 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +0000645 break;
646 case 7:
647 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000648 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000649 break;
650 }
651
652 QualType OldTy;
653 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
654 OldTy = TD->getUnderlyingType();
655 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
656 OldTy = VD->getType();
657 else {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000658 S.Diag(D->getLocation(), diag::err_attr_wrong_decl, "mode",
659 SourceRange(Attr.getLoc(), Attr.getLoc()));
Chris Lattnerfbf13472008-06-27 22:18:37 +0000660 return;
661 }
662
663 // FIXME: Need proper fixed-width types
664 QualType NewTy;
665 switch (DestWidth) {
666 case 0:
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000667 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode, Name->getName());
Chris Lattnerfbf13472008-06-27 22:18:37 +0000668 return;
669 default:
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000670 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode, Name->getName());
Chris Lattnerfbf13472008-06-27 22:18:37 +0000671 return;
672 case 8:
673 assert(IntegerMode);
674 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000675 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000676 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000677 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000678 break;
679 case 16:
680 assert(IntegerMode);
681 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000682 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000683 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000684 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000685 break;
686 case 32:
687 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000688 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000689 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000690 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000691 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000692 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000693 break;
694 case 64:
695 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000696 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000697 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000698 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000699 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000700 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000701 break;
702 }
703
704 if (!OldTy->getAsBuiltinType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000705 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000706 else if (!(IntegerMode && OldTy->isIntegerType()) &&
707 !(!IntegerMode && OldTy->isFloatingType())) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000708 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000709 }
710
711 // Install the new type.
712 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
713 TD->setUnderlyingType(NewTy);
714 else
715 cast<ValueDecl>(D)->setType(NewTy);
716}
Chris Lattner0744e5f2008-06-29 00:23:49 +0000717
718//===----------------------------------------------------------------------===//
719// Top Level Sema Entry Points
720//===----------------------------------------------------------------------===//
721
Chris Lattner803d0802008-06-29 00:43:07 +0000722/// HandleDeclAttribute - Apply the specific attribute to the specified decl if
723/// the attribute applies to decls. If the attribute is a type attribute, just
724/// silently ignore it.
725static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
726 switch (Attr.getKind()) {
727 case AttributeList::AT_address_space:
728 // Ignore this, this is a type attribute, handled by ProcessTypeAttributes.
729 break;
730 case AttributeList::AT_ext_vector_type:
731 HandleExtVectorTypeAttr(D, Attr, S);
732 break;
733 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
734 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
735 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
736 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
737 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
738 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
739 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
740 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
741 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
742 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
743 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
744 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
745 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
746 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
747 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
748 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
749 case AttributeList::AT_transparent_union:
750 HandleTransparentUnionAttr(D, Attr, S);
751 break;
752 default:
753#if 0
754 // TODO: when we have the full set of attributes, warn about unknown ones.
755 S.Diag(Attr->getLoc(), diag::warn_attribute_ignored,
756 Attr->getName()->getName());
757#endif
758 break;
759 }
760}
761
762/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
763/// attribute list to the specified decl, ignoring any type attributes.
764void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
765 while (AttrList) {
766 ProcessDeclAttribute(D, *AttrList, *this);
767 AttrList = AttrList->getNext();
768 }
769}
770
771
Chris Lattner0744e5f2008-06-29 00:23:49 +0000772/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
773/// it, apply them to D. This is a bit tricky because PD can have attributes
774/// specified in many different places, and we need to find and apply them all.
775void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
776 // Apply decl attributes from the DeclSpec if present.
777 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
778 ProcessDeclAttributeList(D, Attrs);
Chris Lattner803d0802008-06-29 00:43:07 +0000779
Chris Lattner0744e5f2008-06-29 00:23:49 +0000780 // Walk the declarator structure, applying decl attributes that were in a type
781 // position to the decl itself. This handles cases like:
782 // int *__attr__(x)** D;
783 // when X is a decl attribute.
784 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
785 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
786 ProcessDeclAttributeList(D, Attrs);
787
788 // Finally, apply any attributes on the decl itself.
789 if (const AttributeList *Attrs = PD.getAttributes())
790 ProcessDeclAttributeList(D, Attrs);
791}
792