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