SemaDeclAttr.cpp revision d66f22d9f8423579322a6dd16587ed52b0a58834
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/TargetInfo.h"
19#include "clang/Parse/DeclSpec.h"
20#include <llvm/ADT/StringExtras.h>
21using namespace clang;
22
23//===----------------------------------------------------------------------===//
24//  Helper functions
25//===----------------------------------------------------------------------===//
26
27static const FunctionType *getFunctionType(Decl *d, bool blocksToo = true) {
28  QualType Ty;
29  if (ValueDecl *decl = dyn_cast<ValueDecl>(d))
30    Ty = decl->getType();
31  else if (FieldDecl *decl = dyn_cast<FieldDecl>(d))
32    Ty = decl->getType();
33  else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
34    Ty = decl->getUnderlyingType();
35  else
36    return 0;
37
38  if (Ty->isFunctionPointerType())
39    Ty = Ty->getAsPointerType()->getPointeeType();
40  else if (blocksToo && Ty->isBlockPointerType())
41    Ty = Ty->getAsBlockPointerType()->getPointeeType();
42
43  return Ty->getAsFunctionType();
44}
45
46// FIXME: We should provide an abstraction around a method or function
47// to provide the following bits of information.
48
49/// isFunctionOrMethod - Return true if the given decl has function
50/// type (function or function-typed variable) or an Objective-C
51/// method.
52static bool isFunctionOrMethod(Decl *d) {
53  return getFunctionType(d, false) || isa<ObjCMethodDecl>(d);
54}
55
56/// isFunctionOrMethodOrBlock - Return true if the given decl has function
57/// type (function or function-typed variable) or an Objective-C
58/// method or a block.
59static bool isFunctionOrMethodOrBlock(Decl *d) {
60  if (isFunctionOrMethod(d))
61    return true;
62  // check for block is more involved.
63  if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
64    QualType Ty = V->getType();
65    return Ty->isBlockPointerType();
66  }
67  return isa<BlockDecl>(d);
68}
69
70/// hasFunctionProto - Return true if the given decl has a argument
71/// information. This decl should have already passed
72/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
73static bool hasFunctionProto(Decl *d) {
74  if (const FunctionType *FnTy = getFunctionType(d))
75    return isa<FunctionProtoType>(FnTy);
76  else {
77    assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
78    return true;
79  }
80}
81
82/// getFunctionOrMethodNumArgs - Return number of function or method
83/// arguments. It is an error to call this on a K&R function (use
84/// hasFunctionProto first).
85static unsigned getFunctionOrMethodNumArgs(Decl *d) {
86  if (const FunctionType *FnTy = getFunctionType(d))
87    return cast<FunctionProtoType>(FnTy)->getNumArgs();
88  if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
89    return BD->getNumParams();
90  return cast<ObjCMethodDecl>(d)->param_size();
91}
92
93static QualType getFunctionOrMethodArgType(Decl *d, unsigned Idx) {
94  if (const FunctionType *FnTy = getFunctionType(d))
95    return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
96  if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
97    return BD->getParamDecl(Idx)->getType();
98
99  return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
100}
101
102static bool isFunctionOrMethodVariadic(Decl *d) {
103  if (const FunctionType *FnTy = getFunctionType(d)) {
104    const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
105    return proto->isVariadic();
106  } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
107    return BD->IsVariadic();
108  else {
109    return cast<ObjCMethodDecl>(d)->isVariadic();
110  }
111}
112
113static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
114  const PointerType *PT = T->getAsPointerType();
115  if (!PT)
116    return false;
117
118  const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
119  if (!ClsT)
120    return false;
121
122  IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
123
124  // FIXME: Should we walk the chain of classes?
125  return ClsName == &Ctx.Idents.get("NSString") ||
126         ClsName == &Ctx.Idents.get("NSMutableString");
127}
128
129static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
130  const PointerType *PT = T->getAsPointerType();
131  if (!PT)
132    return false;
133
134  const RecordType *RT = PT->getPointeeType()->getAsRecordType();
135  if (!RT)
136    return false;
137
138  const RecordDecl *RD = RT->getDecl();
139  if (RD->getTagKind() != TagDecl::TK_struct)
140    return false;
141
142  return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
143}
144
145//===----------------------------------------------------------------------===//
146// Attribute Implementations
147//===----------------------------------------------------------------------===//
148
149// FIXME: All this manual attribute parsing code is gross. At the
150// least add some helper functions to check most argument patterns (#
151// and types of args).
152
153static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
154                                    Sema &S) {
155  TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
156  if (tDecl == 0) {
157    S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
158    return;
159  }
160
161  QualType curType = tDecl->getUnderlyingType();
162  // check the attribute arguments.
163  if (Attr.getNumArgs() != 1) {
164    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
165    return;
166  }
167  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
168  llvm::APSInt vecSize(32);
169  if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
170    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
171      << "ext_vector_type" << sizeExpr->getSourceRange();
172    return;
173  }
174  // unlike gcc's vector_size attribute, we do not allow vectors to be defined
175  // in conjunction with complex types (pointers, arrays, functions, etc.).
176  if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
177    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << curType;
178    return;
179  }
180  // unlike gcc's vector_size attribute, the size is specified as the
181  // number of elements, not the number of bytes.
182  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
183
184  if (vectorSize == 0) {
185    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
186      << sizeExpr->getSourceRange();
187    return;
188  }
189  // Instantiate/Install the vector type, the number of elements is > 0.
190  tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
191  // Remember this typedef decl, we will need it later for diagnostics.
192  S.ExtVectorDecls.push_back(tDecl);
193}
194
195
196/// HandleVectorSizeAttribute - this attribute is only applicable to
197/// integral and float scalars, although arrays, pointers, and function
198/// return values are allowed in conjunction with this construct. Aggregates
199/// with this attribute are invalid, even if they are of the same size as a
200/// corresponding scalar.
201/// The raw attribute should contain precisely 1 argument, the vector size
202/// for the variable, measured in bytes. If curType and rawAttr are well
203/// formed, this routine will return a new vector type.
204static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
205  QualType CurType;
206  if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
207    CurType = VD->getType();
208  else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
209    CurType = TD->getUnderlyingType();
210  else {
211    S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
212      << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
213    return;
214  }
215
216  // Check the attribute arugments.
217  if (Attr.getNumArgs() != 1) {
218    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
219    return;
220  }
221  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
222  llvm::APSInt vecSize(32);
223  if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
224    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
225      << "vector_size" << sizeExpr->getSourceRange();
226    return;
227  }
228  // navigate to the base type - we need to provide for vector pointers,
229  // vector arrays, and functions returning vectors.
230  if (CurType->isPointerType() || CurType->isArrayType() ||
231      CurType->isFunctionType()) {
232    S.Diag(Attr.getLoc(), diag::err_unsupported_vector_size) << CurType;
233    return;
234    /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
235     do {
236     if (PointerType *PT = dyn_cast<PointerType>(canonType))
237     canonType = PT->getPointeeType().getTypePtr();
238     else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
239     canonType = AT->getElementType().getTypePtr();
240     else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
241     canonType = FT->getResultType().getTypePtr();
242     } while (canonType->isPointerType() || canonType->isArrayType() ||
243     canonType->isFunctionType());
244     */
245  }
246  // the base type must be integer or float, and can't already be a vector.
247  if (CurType->isVectorType() ||
248      (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
249    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
250    return;
251  }
252  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
253  // vecSize is specified in bytes - convert to bits.
254  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
255
256  // the vector size needs to be an integral multiple of the type size.
257  if (vectorSize % typeSize) {
258    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
259      << sizeExpr->getSourceRange();
260    return;
261  }
262  if (vectorSize == 0) {
263    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
264      << sizeExpr->getSourceRange();
265    return;
266  }
267
268  // Success! Instantiate the vector type, the number of elements is > 0, and
269  // not required to be a power of 2, unlike GCC.
270  CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
271
272  if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
273    VD->setType(CurType);
274  else
275    cast<TypedefDecl>(D)->setUnderlyingType(CurType);
276}
277
278static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
279  // check the attribute arguments.
280  if (Attr.getNumArgs() > 0) {
281    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
282    return;
283  }
284
285  if (TagDecl *TD = dyn_cast<TagDecl>(d))
286    TD->addAttr(::new (S.Context) PackedAttr(1));
287  else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
288    // If the alignment is less than or equal to 8 bits, the packed attribute
289    // has no effect.
290    if (!FD->getType()->isIncompleteType() &&
291        S.Context.getTypeAlign(FD->getType()) <= 8)
292      S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
293        << Attr.getName() << FD->getType();
294    else
295      FD->addAttr(::new (S.Context) PackedAttr(1));
296  } else
297    S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
298}
299
300static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
301  // check the attribute arguments.
302  if (Attr.getNumArgs() > 0) {
303    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
304    return;
305  }
306
307  // The IBOutlet attribute only applies to instance variables of Objective-C
308  // classes.
309  if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
310    d->addAttr(::new (S.Context) IBOutletAttr());
311  else
312    S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
313}
314
315static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
316  // GCC ignores the nonnull attribute on K&R style function
317  // prototypes, so we ignore it as well
318  if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
319    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
320      << Attr.getName() << 0 /*function*/;
321    return;
322  }
323
324  unsigned NumArgs = getFunctionOrMethodNumArgs(d);
325
326  // The nonnull attribute only applies to pointers.
327  llvm::SmallVector<unsigned, 10> NonNullArgs;
328
329  for (AttributeList::arg_iterator I=Attr.arg_begin(),
330                                   E=Attr.arg_end(); I!=E; ++I) {
331
332
333    // The argument must be an integer constant expression.
334    Expr *Ex = static_cast<Expr *>(*I);
335    llvm::APSInt ArgNum(32);
336    if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
337      S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
338        << "nonnull" << Ex->getSourceRange();
339      return;
340    }
341
342    unsigned x = (unsigned) ArgNum.getZExtValue();
343
344    if (x < 1 || x > NumArgs) {
345      S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
346       << "nonnull" << I.getArgNum() << Ex->getSourceRange();
347      return;
348    }
349
350    --x;
351
352    // Is the function argument a pointer type?
353    QualType T = getFunctionOrMethodArgType(d, x);
354    if (!T->isPointerType() && !T->isBlockPointerType()) {
355      // FIXME: Should also highlight argument in decl.
356      S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
357        << "nonnull" << Ex->getSourceRange();
358      continue;
359    }
360
361    NonNullArgs.push_back(x);
362  }
363
364  // If no arguments were specified to __attribute__((nonnull)) then all
365  // pointer arguments have a nonnull attribute.
366  if (NonNullArgs.empty()) {
367    for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
368      QualType T = getFunctionOrMethodArgType(d, I);
369      if (T->isPointerType() || T->isBlockPointerType())
370        NonNullArgs.push_back(I);
371    }
372
373    if (NonNullArgs.empty()) {
374      S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
375      return;
376    }
377  }
378
379  unsigned* start = &NonNullArgs[0];
380  unsigned size = NonNullArgs.size();
381  std::sort(start, start + size);
382  d->addAttr(::new (S.Context) NonNullAttr(start, size));
383}
384
385static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
386  // check the attribute arguments.
387  if (Attr.getNumArgs() != 1) {
388    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
389    return;
390  }
391
392  Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
393  Arg = Arg->IgnoreParenCasts();
394  StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
395
396  if (Str == 0 || Str->isWide()) {
397    S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
398      << "alias" << 1;
399    return;
400  }
401
402  const char *Alias = Str->getStrData();
403  unsigned AliasLen = Str->getByteLength();
404
405  // FIXME: check if target symbol exists in current file
406
407  d->addAttr(::new (S.Context) AliasAttr(std::string(Alias, AliasLen)));
408}
409
410static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
411                                   Sema &S) {
412  // check the attribute arguments.
413  if (Attr.getNumArgs() != 0) {
414    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
415    return;
416  }
417
418  if (!isa<FunctionDecl>(d)) {
419    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
420    << Attr.getName() << 0 /*function*/;
421    return;
422  }
423
424  d->addAttr(::new (S.Context) AlwaysInlineAttr());
425}
426
427static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
428                                     Sema &S) {
429  // check the attribute arguments.
430  if (Attr.getNumArgs() != 0) {
431    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
432    return false;
433  }
434
435  if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
436    ValueDecl *VD = dyn_cast<ValueDecl>(d);
437    if (VD == 0 || !VD->getType()->isBlockPointerType()) {
438      S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
439        << Attr.getName() << 0 /*function*/;
440      return false;
441    }
442  }
443
444  return true;
445}
446
447static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
448  if (HandleCommonNoReturnAttr(d, Attr, S))
449    d->addAttr(::new (S.Context) NoReturnAttr());
450}
451
452static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
453                                       Sema &S) {
454  if (HandleCommonNoReturnAttr(d, Attr, S))
455    d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
456}
457
458static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
459  // check the attribute arguments.
460  if (Attr.getNumArgs() != 0) {
461    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
462    return;
463  }
464
465  if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
466    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
467      << Attr.getName() << 2 /*variable and function*/;
468    return;
469  }
470
471  d->addAttr(::new (S.Context) UnusedAttr());
472}
473
474static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
475  // check the attribute arguments.
476  if (Attr.getNumArgs() != 0) {
477    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
478    return;
479  }
480
481  if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
482    if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
483      S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
484      return;
485    }
486  } else if (!isFunctionOrMethod(d)) {
487    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
488      << Attr.getName() << 2 /*variable and function*/;
489    return;
490  }
491
492  d->addAttr(::new (S.Context) UsedAttr());
493}
494
495static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
496  // check the attribute arguments.
497  if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
498    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
499      << "0 or 1";
500    return;
501  }
502
503  int priority = 65535; // FIXME: Do not hardcode such constants.
504  if (Attr.getNumArgs() > 0) {
505    Expr *E = static_cast<Expr *>(Attr.getArg(0));
506    llvm::APSInt Idx(32);
507    if (!E->isIntegerConstantExpr(Idx, S.Context)) {
508      S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
509        << "constructor" << 1 << E->getSourceRange();
510      return;
511    }
512    priority = Idx.getZExtValue();
513  }
514
515  if (!isa<FunctionDecl>(d)) {
516    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
517      << Attr.getName() << 0 /*function*/;
518    return;
519  }
520
521  d->addAttr(::new (S.Context) ConstructorAttr(priority));
522}
523
524static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
525  // check the attribute arguments.
526  if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
527    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
528       << "0 or 1";
529    return;
530  }
531
532  int priority = 65535; // FIXME: Do not hardcode such constants.
533  if (Attr.getNumArgs() > 0) {
534    Expr *E = static_cast<Expr *>(Attr.getArg(0));
535    llvm::APSInt Idx(32);
536    if (!E->isIntegerConstantExpr(Idx, S.Context)) {
537      S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
538        << "destructor" << 1 << E->getSourceRange();
539      return;
540    }
541    priority = Idx.getZExtValue();
542  }
543
544  if (!isa<FunctionDecl>(d)) {
545    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
546      << Attr.getName() << 0 /*function*/;
547    return;
548  }
549
550  d->addAttr(::new (S.Context) DestructorAttr(priority));
551}
552
553static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
554  // check the attribute arguments.
555  if (Attr.getNumArgs() != 0) {
556    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
557    return;
558  }
559
560  d->addAttr(::new (S.Context) DeprecatedAttr());
561}
562
563static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
564  // check the attribute arguments.
565  if (Attr.getNumArgs() != 0) {
566    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
567    return;
568  }
569
570  d->addAttr(::new (S.Context) UnavailableAttr());
571}
572
573static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
574  // check the attribute arguments.
575  if (Attr.getNumArgs() != 1) {
576    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
577    return;
578  }
579
580  Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
581  Arg = Arg->IgnoreParenCasts();
582  StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
583
584  if (Str == 0 || Str->isWide()) {
585    S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
586      << "visibility" << 1;
587    return;
588  }
589
590  const char *TypeStr = Str->getStrData();
591  unsigned TypeLen = Str->getByteLength();
592  VisibilityAttr::VisibilityTypes type;
593
594  if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
595    type = VisibilityAttr::DefaultVisibility;
596  else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
597    type = VisibilityAttr::HiddenVisibility;
598  else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
599    type = VisibilityAttr::HiddenVisibility; // FIXME
600  else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
601    type = VisibilityAttr::ProtectedVisibility;
602  else {
603    S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
604    return;
605  }
606
607  d->addAttr(::new (S.Context) VisibilityAttr(type));
608}
609
610static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
611                                    Sema &S) {
612  if (Attr.getNumArgs() != 0) {
613    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
614    return;
615  }
616
617  ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
618  if (OCI == 0) {
619    S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
620    return;
621  }
622
623  D->addAttr(::new (S.Context) ObjCExceptionAttr());
624}
625
626static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
627  if (Attr.getNumArgs() != 0) {
628    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
629    return;
630  }
631  if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
632    QualType T = TD->getUnderlyingType();
633    if (!T->isPointerType() ||
634        !T->getAsPointerType()->getPointeeType()->isRecordType()) {
635      S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
636      return;
637    }
638  }
639  D->addAttr(::new (S.Context) ObjCNSObjectAttr());
640}
641
642static void
643HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
644  if (Attr.getNumArgs() != 0) {
645    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
646    return;
647  }
648
649  if (!isa<FunctionDecl>(D)) {
650    S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
651    return;
652  }
653
654  D->addAttr(::new (S.Context) OverloadableAttr());
655}
656
657static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
658  if (!Attr.getParameterName()) {
659    S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
660      << "blocks" << 1;
661    return;
662  }
663
664  if (Attr.getNumArgs() != 0) {
665    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
666    return;
667  }
668
669  BlocksAttr::BlocksAttrTypes type;
670  if (Attr.getParameterName()->isStr("byref"))
671    type = BlocksAttr::ByRef;
672  else {
673    S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
674      << "blocks" << Attr.getParameterName();
675    return;
676  }
677
678  d->addAttr(::new (S.Context) BlocksAttr(type));
679}
680
681static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
682  // check the attribute arguments.
683  if (Attr.getNumArgs() > 2) {
684    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
685      << "0, 1 or 2";
686    return;
687  }
688
689  int sentinel = 0;
690  if (Attr.getNumArgs() > 0) {
691    Expr *E = static_cast<Expr *>(Attr.getArg(0));
692    llvm::APSInt Idx(32);
693    if (!E->isIntegerConstantExpr(Idx, S.Context)) {
694      S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
695       << "sentinel" << 1 << E->getSourceRange();
696      return;
697    }
698    sentinel = Idx.getZExtValue();
699
700    if (sentinel < 0) {
701      S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
702        << E->getSourceRange();
703      return;
704    }
705  }
706
707  int nullPos = 0;
708  if (Attr.getNumArgs() > 1) {
709    Expr *E = static_cast<Expr *>(Attr.getArg(1));
710    llvm::APSInt Idx(32);
711    if (!E->isIntegerConstantExpr(Idx, S.Context)) {
712      S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
713        << "sentinel" << 2 << E->getSourceRange();
714      return;
715    }
716    nullPos = Idx.getZExtValue();
717
718    if (nullPos > 1 || nullPos < 0) {
719      // FIXME: This error message could be improved, it would be nice
720      // to say what the bounds actually are.
721      S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
722        << E->getSourceRange();
723      return;
724    }
725  }
726
727  if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
728    const FunctionType *FT = FD->getType()->getAsFunctionType();
729    assert(FT && "FunctionDecl has non-function type?");
730
731    if (isa<FunctionNoProtoType>(FT)) {
732      S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
733      return;
734    }
735
736    if (!cast<FunctionProtoType>(FT)->isVariadic()) {
737      S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
738      return;
739    }
740  } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
741    if (!MD->isVariadic()) {
742      S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
743      return;
744    }
745  } else if (isa<BlockDecl>(d)) {
746    // Note! BlockDecl is typeless. Variadic diagnostics
747    // will be issued by the caller.
748    ;
749  } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
750    QualType Ty = V->getType();
751    if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
752      const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
753        : Ty->getAsBlockPointerType()->getPointeeType()->getAsFunctionType();
754      if (!cast<FunctionProtoType>(FT)->isVariadic()) {
755        int m = Ty->isFunctionPointerType() ? 0 : 1;
756        S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
757        return;
758      }
759    }
760    else {
761      S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
762      << Attr.getName() << 6 /*function, method or block */;
763      return;
764    }
765  } else {
766    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
767      << Attr.getName() << 6 /*function, method or block */;
768    return;
769  }
770  d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
771}
772
773static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
774  // check the attribute arguments.
775  if (Attr.getNumArgs() != 0) {
776    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
777    return;
778  }
779
780  // TODO: could also be applied to methods?
781  FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
782  if (!Fn) {
783    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
784      << Attr.getName() << 0 /*function*/;
785    return;
786  }
787
788  Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
789}
790
791static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
792  // check the attribute arguments.
793  if (Attr.getNumArgs() != 0) {
794    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
795    return;
796  }
797
798  // TODO: could also be applied to methods?
799  if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
800    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
801      << Attr.getName() << 2 /*variable and function*/;
802    return;
803  }
804
805  D->addAttr(::new (S.Context) WeakAttr());
806}
807
808static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
809  // check the attribute arguments.
810  if (Attr.getNumArgs() != 0) {
811    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
812    return;
813  }
814
815  // weak_import only applies to variable & function declarations.
816  bool isDef = false;
817  if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
818    isDef = (!VD->hasExternalStorage() || VD->getInit());
819  } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
820    isDef = FD->getBody(S.Context);
821  } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
822    // We ignore weak import on properties and methods
823    return;
824  } else {
825    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
826    << Attr.getName() << 2 /*variable and function*/;
827    return;
828  }
829
830  // Merge should handle any subsequent violations.
831  if (isDef) {
832    S.Diag(Attr.getLoc(),
833           diag::warn_attribute_weak_import_invalid_on_definition)
834      << "weak_import" << 2 /*variable and function*/;
835    return;
836  }
837
838  D->addAttr(::new (S.Context) WeakImportAttr());
839}
840
841static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
842  // check the attribute arguments.
843  if (Attr.getNumArgs() != 0) {
844    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
845    return;
846  }
847
848  // Attribute can be applied only to functions or variables.
849  if (isa<VarDecl>(D)) {
850    D->addAttr(::new (S.Context) DLLImportAttr());
851    return;
852  }
853
854  FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
855  if (!FD) {
856    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
857      << Attr.getName() << 2 /*variable and function*/;
858    return;
859  }
860
861  // Currently, the dllimport attribute is ignored for inlined functions.
862  // Warning is emitted.
863  if (FD->isInline()) {
864    S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
865    return;
866  }
867
868  // The attribute is also overridden by a subsequent declaration as dllexport.
869  // Warning is emitted.
870  for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
871       nextAttr = nextAttr->getNext()) {
872    if (nextAttr->getKind() == AttributeList::AT_dllexport) {
873      S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
874      return;
875    }
876  }
877
878  if (D->getAttr<DLLExportAttr>()) {
879    S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
880    return;
881  }
882
883  D->addAttr(::new (S.Context) DLLImportAttr());
884}
885
886static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
887  // check the attribute arguments.
888  if (Attr.getNumArgs() != 0) {
889    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
890    return;
891  }
892
893  // Attribute can be applied only to functions or variables.
894  if (isa<VarDecl>(D)) {
895    D->addAttr(::new (S.Context) DLLExportAttr());
896    return;
897  }
898
899  FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
900  if (!FD) {
901    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
902      << Attr.getName() << 2 /*variable and function*/;
903    return;
904  }
905
906  // Currently, the dllexport attribute is ignored for inlined functions,
907  // unless the -fkeep-inline-functions flag has been used. Warning is emitted;
908  if (FD->isInline()) {
909    // FIXME: ... unless the -fkeep-inline-functions flag has been used.
910    S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
911    return;
912  }
913
914  D->addAttr(::new (S.Context) DLLExportAttr());
915}
916
917static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
918  // Attribute has no arguments.
919  if (Attr.getNumArgs() != 1) {
920    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
921    return;
922  }
923
924  // Make sure that there is a string literal as the sections's single
925  // argument.
926  StringLiteral *SE =
927    dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0)));
928  if (!SE) {
929    // FIXME
930    S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
931    return;
932  }
933  D->addAttr(::new (S.Context) SectionAttr(std::string(SE->getStrData(),
934                                                     SE->getByteLength())));
935}
936
937static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
938  // Attribute has no arguments.
939  if (Attr.getNumArgs() != 0) {
940    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
941    return;
942  }
943
944  // Attribute can be applied only to functions.
945  if (!isa<FunctionDecl>(d)) {
946    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
947      << Attr.getName() << 0 /*function*/;
948    return;
949  }
950
951  // stdcall and fastcall attributes are mutually incompatible.
952  if (d->getAttr<FastCallAttr>()) {
953    S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
954      << "stdcall" << "fastcall";
955    return;
956  }
957
958  d->addAttr(::new (S.Context) StdCallAttr());
959}
960
961static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
962  // Attribute has no arguments.
963  if (Attr.getNumArgs() != 0) {
964    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
965    return;
966  }
967
968  if (!isa<FunctionDecl>(d)) {
969    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
970      << Attr.getName() << 0 /*function*/;
971    return;
972  }
973
974  // stdcall and fastcall attributes are mutually incompatible.
975  if (d->getAttr<StdCallAttr>()) {
976    S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
977      << "fastcall" << "stdcall";
978    return;
979  }
980
981  d->addAttr(::new (S.Context) FastCallAttr());
982}
983
984static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
985  // check the attribute arguments.
986  if (Attr.getNumArgs() != 0) {
987    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
988    return;
989  }
990
991  d->addAttr(::new (S.Context) NoThrowAttr());
992}
993
994static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
995  // check the attribute arguments.
996  if (Attr.getNumArgs() != 0) {
997    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
998    return;
999  }
1000
1001  d->addAttr(::new (S.Context) ConstAttr());
1002}
1003
1004static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1005  // check the attribute arguments.
1006  if (Attr.getNumArgs() != 0) {
1007    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1008    return;
1009  }
1010
1011  d->addAttr(::new (S.Context) PureAttr());
1012}
1013
1014static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1015  // Match gcc which ignores cleanup attrs when compiling C++.
1016  if (S.getLangOptions().CPlusPlus)
1017    return;
1018
1019  if (!Attr.getParameterName()) {
1020    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1021    return;
1022  }
1023
1024  if (Attr.getNumArgs() != 0) {
1025    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1026    return;
1027  }
1028
1029  VarDecl *VD = dyn_cast<VarDecl>(d);
1030
1031  if (!VD || !VD->hasLocalStorage()) {
1032    S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1033    return;
1034  }
1035
1036  // Look up the function
1037  NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
1038                                        Sema::LookupOrdinaryName);
1039  if (!CleanupDecl) {
1040    S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
1041      Attr.getParameterName();
1042    return;
1043  }
1044
1045  FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1046  if (!FD) {
1047    S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
1048      Attr.getParameterName();
1049    return;
1050  }
1051
1052  if (FD->getNumParams() != 1) {
1053    S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
1054      Attr.getParameterName();
1055    return;
1056  }
1057
1058  // We're currently more strict than GCC about what function types we accept.
1059  // If this ever proves to be a problem it should be easy to fix.
1060  QualType Ty = S.Context.getPointerType(VD->getType());
1061  QualType ParamTy = FD->getParamDecl(0)->getType();
1062  if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
1063    S.Diag(Attr.getLoc(),
1064           diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1065      Attr.getParameterName() << ParamTy << Ty;
1066    return;
1067  }
1068
1069  d->addAttr(::new (S.Context) CleanupAttr(FD));
1070}
1071
1072/// Handle __attribute__((format(type,idx,firstarg))) attributes
1073/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1074static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1075
1076  if (!Attr.getParameterName()) {
1077    S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1078      << "format" << 1;
1079    return;
1080  }
1081
1082  if (Attr.getNumArgs() != 2) {
1083    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
1084    return;
1085  }
1086
1087  if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
1088    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1089      << Attr.getName() << 0 /*function*/;
1090    return;
1091  }
1092
1093  // FIXME: in C++ the implicit 'this' function parameter also counts.
1094  // this is needed in order to be compatible with GCC
1095  // the index must start in 1 and the limit is numargs+1
1096  unsigned NumArgs  = getFunctionOrMethodNumArgs(d);
1097  unsigned FirstIdx = 1;
1098
1099  const char *Format = Attr.getParameterName()->getName();
1100  unsigned FormatLen = Attr.getParameterName()->getLength();
1101
1102  // Normalize the argument, __foo__ becomes foo.
1103  if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1104      Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1105    Format += 2;
1106    FormatLen -= 4;
1107  }
1108
1109  bool Supported = false;
1110  bool is_NSString = false;
1111  bool is_strftime = false;
1112  bool is_CFString = false;
1113
1114  switch (FormatLen) {
1115  default: break;
1116  case 5: Supported = !memcmp(Format, "scanf", 5); break;
1117  case 6: Supported = !memcmp(Format, "printf", 6); break;
1118  case 7: Supported = !memcmp(Format, "strfmon", 7); break;
1119  case 8:
1120    Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1121                (is_NSString = !memcmp(Format, "NSString", 8)) ||
1122                (is_CFString = !memcmp(Format, "CFString", 8));
1123    break;
1124  }
1125
1126  if (!Supported) {
1127    S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1128      << "format" << Attr.getParameterName()->getName();
1129    return;
1130  }
1131
1132  // checks for the 2nd argument
1133  Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1134  llvm::APSInt Idx(32);
1135  if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1136    S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1137      << "format" << 2 << IdxExpr->getSourceRange();
1138    return;
1139  }
1140
1141  if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1142    S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1143      << "format" << 2 << IdxExpr->getSourceRange();
1144    return;
1145  }
1146
1147  // FIXME: Do we need to bounds check?
1148  unsigned ArgIdx = Idx.getZExtValue() - 1;
1149
1150  // make sure the format string is really a string
1151  QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
1152
1153  if (is_CFString) {
1154    if (!isCFStringType(Ty, S.Context)) {
1155      S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1156        << "a CFString" << IdxExpr->getSourceRange();
1157      return;
1158    }
1159  } else if (is_NSString) {
1160    // FIXME: do we need to check if the type is NSString*?  What are the
1161    // semantics?
1162    if (!isNSStringType(Ty, S.Context)) {
1163      // FIXME: Should highlight the actual expression that has the wrong type.
1164      S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1165        << "an NSString" << IdxExpr->getSourceRange();
1166      return;
1167    }
1168  } else if (!Ty->isPointerType() ||
1169             !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
1170    // FIXME: Should highlight the actual expression that has the wrong type.
1171    S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1172      << "a string type" << IdxExpr->getSourceRange();
1173    return;
1174  }
1175
1176  // check the 3rd argument
1177  Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
1178  llvm::APSInt FirstArg(32);
1179  if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
1180    S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1181      << "format" << 3 << FirstArgExpr->getSourceRange();
1182    return;
1183  }
1184
1185  // check if the function is variadic if the 3rd argument non-zero
1186  if (FirstArg != 0) {
1187    if (isFunctionOrMethodVariadic(d)) {
1188      ++NumArgs; // +1 for ...
1189    } else {
1190      S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
1191      return;
1192    }
1193  }
1194
1195  // strftime requires FirstArg to be 0 because it doesn't read from any
1196  // variable the input is just the current time + the format string.
1197  if (is_strftime) {
1198    if (FirstArg != 0) {
1199      S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1200        << FirstArgExpr->getSourceRange();
1201      return;
1202    }
1203  // if 0 it disables parameter checking (to use with e.g. va_list)
1204  } else if (FirstArg != 0 && FirstArg != NumArgs) {
1205    S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1206      << "format" << 3 << FirstArgExpr->getSourceRange();
1207    return;
1208  }
1209
1210  d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen),
1211                            Idx.getZExtValue(), FirstArg.getZExtValue()));
1212}
1213
1214static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1215                                       Sema &S) {
1216  // check the attribute arguments.
1217  if (Attr.getNumArgs() != 0) {
1218    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1219    return;
1220  }
1221
1222  // Try to find the underlying union declaration.
1223  RecordDecl *RD = 0;
1224  TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
1225  if (TD && TD->getUnderlyingType()->isUnionType())
1226    RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1227  else
1228    RD = dyn_cast<RecordDecl>(d);
1229
1230  if (!RD || !RD->isUnion()) {
1231    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1232      << Attr.getName() << 1 /*union*/;
1233    return;
1234  }
1235
1236  if (!RD->isDefinition()) {
1237    S.Diag(Attr.getLoc(),
1238        diag::warn_transparent_union_attribute_not_definition);
1239    return;
1240  }
1241
1242  RecordDecl::field_iterator Field = RD->field_begin(S.Context),
1243                          FieldEnd = RD->field_end(S.Context);
1244  if (Field == FieldEnd) {
1245    S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1246    return;
1247  }
1248
1249  FieldDecl *FirstField = *Field;
1250  QualType FirstType = FirstField->getType();
1251  if (FirstType->isFloatingType() || FirstType->isVectorType()) {
1252    S.Diag(FirstField->getLocation(),
1253           diag::warn_transparent_union_attribute_floating);
1254    return;
1255  }
1256
1257  uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1258  uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1259  for (; Field != FieldEnd; ++Field) {
1260    QualType FieldType = Field->getType();
1261    if (S.Context.getTypeSize(FieldType) != FirstSize ||
1262        S.Context.getTypeAlign(FieldType) != FirstAlign) {
1263      // Warn if we drop the attribute.
1264      bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
1265      unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
1266                                 : S.Context.getTypeAlign(FieldType);
1267      S.Diag(Field->getLocation(),
1268          diag::warn_transparent_union_attribute_field_size_align)
1269        << isSize << Field->getDeclName() << FieldBits;
1270      unsigned FirstBits = isSize? FirstSize : FirstAlign;
1271      S.Diag(FirstField->getLocation(),
1272             diag::note_transparent_union_first_field_size_align)
1273        << isSize << FirstBits;
1274      return;
1275    }
1276  }
1277
1278  RD->addAttr(::new (S.Context) TransparentUnionAttr());
1279}
1280
1281static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1282  // check the attribute arguments.
1283  if (Attr.getNumArgs() != 1) {
1284    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1285    return;
1286  }
1287  Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
1288  StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
1289
1290  // Make sure that there is a string literal as the annotation's single
1291  // argument.
1292  if (!SE) {
1293    S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
1294    return;
1295  }
1296  d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
1297                                                        SE->getByteLength())));
1298}
1299
1300static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1301  // check the attribute arguments.
1302  if (Attr.getNumArgs() > 1) {
1303    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1304    return;
1305  }
1306
1307  unsigned Align = 0;
1308  if (Attr.getNumArgs() == 0) {
1309    // FIXME: This should be the target specific maximum alignment.
1310    // (For now we just use 128 bits which is the maximum on X86).
1311    Align = 128;
1312    d->addAttr(::new (S.Context) AlignedAttr(Align));
1313    return;
1314  }
1315
1316  Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1317  llvm::APSInt Alignment(32);
1318  if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
1319    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1320      << "aligned" << alignmentExpr->getSourceRange();
1321    return;
1322  }
1323  if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
1324    S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
1325      << alignmentExpr->getSourceRange();
1326    return;
1327  }
1328
1329  d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
1330}
1331
1332/// HandleModeAttr - This attribute modifies the width of a decl with
1333/// primitive type.
1334///
1335/// Despite what would be logical, the mode attribute is a decl attribute,
1336/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
1337/// 'G' be HImode, not an intermediate pointer.
1338///
1339static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1340  // This attribute isn't documented, but glibc uses it.  It changes
1341  // the width of an int or unsigned int to the specified size.
1342
1343  // Check that there aren't any arguments
1344  if (Attr.getNumArgs() != 0) {
1345    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1346    return;
1347  }
1348
1349  IdentifierInfo *Name = Attr.getParameterName();
1350  if (!Name) {
1351    S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
1352    return;
1353  }
1354  const char *Str = Name->getName();
1355  unsigned Len = Name->getLength();
1356
1357  // Normalize the attribute name, __foo__ becomes foo.
1358  if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1359      Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1360    Str += 2;
1361    Len -= 4;
1362  }
1363
1364  unsigned DestWidth = 0;
1365  bool IntegerMode = true;
1366  bool ComplexMode = false;
1367  switch (Len) {
1368  case 2:
1369    switch (Str[0]) {
1370    case 'Q': DestWidth = 8; break;
1371    case 'H': DestWidth = 16; break;
1372    case 'S': DestWidth = 32; break;
1373    case 'D': DestWidth = 64; break;
1374    case 'X': DestWidth = 96; break;
1375    case 'T': DestWidth = 128; break;
1376    }
1377    if (Str[1] == 'F') {
1378      IntegerMode = false;
1379    } else if (Str[1] == 'C') {
1380      IntegerMode = false;
1381      ComplexMode = true;
1382    } else if (Str[1] != 'I') {
1383      DestWidth = 0;
1384    }
1385    break;
1386  case 4:
1387    // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1388    // pointer on PIC16 and other embedded platforms.
1389    if (!memcmp(Str, "word", 4))
1390      DestWidth = S.Context.Target.getPointerWidth(0);
1391    if (!memcmp(Str, "byte", 4))
1392      DestWidth = S.Context.Target.getCharWidth();
1393    break;
1394  case 7:
1395    if (!memcmp(Str, "pointer", 7))
1396      DestWidth = S.Context.Target.getPointerWidth(0);
1397    break;
1398  }
1399
1400  QualType OldTy;
1401  if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1402    OldTy = TD->getUnderlyingType();
1403  else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1404    OldTy = VD->getType();
1405  else {
1406    S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1407      << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
1408    return;
1409  }
1410
1411  if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType())
1412    S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1413  else if (IntegerMode) {
1414    if (!OldTy->isIntegralType())
1415      S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1416  } else if (ComplexMode) {
1417    if (!OldTy->isComplexType())
1418      S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1419  } else {
1420    if (!OldTy->isFloatingType())
1421      S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1422  }
1423
1424  // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1425  // and friends, at least with glibc.
1426  // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1427  // width on unusual platforms.
1428  // FIXME: Make sure floating-point mappings are accurate
1429  // FIXME: Support XF and TF types
1430  QualType NewTy;
1431  switch (DestWidth) {
1432  case 0:
1433    S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
1434    return;
1435  default:
1436    S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1437    return;
1438  case 8:
1439    if (!IntegerMode) {
1440      S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1441      return;
1442    }
1443    if (OldTy->isSignedIntegerType())
1444      NewTy = S.Context.SignedCharTy;
1445    else
1446      NewTy = S.Context.UnsignedCharTy;
1447    break;
1448  case 16:
1449    if (!IntegerMode) {
1450      S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1451      return;
1452    }
1453    if (OldTy->isSignedIntegerType())
1454      NewTy = S.Context.ShortTy;
1455    else
1456      NewTy = S.Context.UnsignedShortTy;
1457    break;
1458  case 32:
1459    if (!IntegerMode)
1460      NewTy = S.Context.FloatTy;
1461    else if (OldTy->isSignedIntegerType())
1462      NewTy = S.Context.IntTy;
1463    else
1464      NewTy = S.Context.UnsignedIntTy;
1465    break;
1466  case 64:
1467    if (!IntegerMode)
1468      NewTy = S.Context.DoubleTy;
1469    else if (OldTy->isSignedIntegerType())
1470      NewTy = S.Context.LongLongTy;
1471    else
1472      NewTy = S.Context.UnsignedLongLongTy;
1473    break;
1474  case 96:
1475    NewTy = S.Context.LongDoubleTy;
1476    break;
1477  case 128:
1478    if (!IntegerMode) {
1479      S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1480      return;
1481    }
1482    NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
1483    break;
1484  }
1485
1486  if (ComplexMode) {
1487    NewTy = S.Context.getComplexType(NewTy);
1488  }
1489
1490  // Install the new type.
1491  if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1492    TD->setUnderlyingType(NewTy);
1493  else
1494    cast<ValueDecl>(D)->setType(NewTy);
1495}
1496
1497static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1498  // check the attribute arguments.
1499  if (Attr.getNumArgs() > 0) {
1500    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1501    return;
1502  }
1503
1504  if (!isFunctionOrMethod(d)) {
1505    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1506      << Attr.getName() << 0 /*function*/;
1507    return;
1508  }
1509
1510  d->addAttr(::new (S.Context) NodebugAttr());
1511}
1512
1513static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1514  // check the attribute arguments.
1515  if (Attr.getNumArgs() != 0) {
1516    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1517    return;
1518  }
1519
1520  if (!isa<FunctionDecl>(d)) {
1521    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1522    << Attr.getName() << 0 /*function*/;
1523    return;
1524  }
1525
1526  d->addAttr(::new (S.Context) NoinlineAttr());
1527}
1528
1529static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1530  // check the attribute arguments.
1531  if (Attr.getNumArgs() != 0) {
1532    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1533    return;
1534  }
1535
1536  FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1537  if (Fn == 0) {
1538    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1539      << Attr.getName() << 0 /*function*/;
1540    return;
1541  }
1542
1543  if (!Fn->isInline()) {
1544    S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
1545    return;
1546  }
1547
1548  d->addAttr(::new (S.Context) GNUInlineAttr());
1549}
1550
1551static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1552  // check the attribute arguments.
1553  if (Attr.getNumArgs() != 1) {
1554    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1555    return;
1556  }
1557
1558  if (!isFunctionOrMethod(d)) {
1559    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1560    << Attr.getName() << 0 /*function*/;
1561    return;
1562  }
1563
1564  Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1565  llvm::APSInt NumParams(32);
1566  if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1567    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1568      << "regparm" << NumParamsExpr->getSourceRange();
1569    return;
1570  }
1571
1572  if (S.Context.Target.getRegParmMax() == 0) {
1573    S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
1574      << NumParamsExpr->getSourceRange();
1575    return;
1576  }
1577
1578  if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
1579    S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1580      << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
1581    return;
1582  }
1583
1584  d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
1585}
1586
1587//===----------------------------------------------------------------------===//
1588// Checker-specific attribute handlers.
1589//===----------------------------------------------------------------------===//
1590
1591static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1592                                        Sema &S) {
1593
1594  QualType RetTy;
1595
1596  if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1597    RetTy = MD->getResultType();
1598  else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1599    RetTy = FD->getResultType();
1600  else {
1601    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1602        << Attr.getName() << 3 /* function or method */;
1603    return;
1604  }
1605
1606  if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAsPointerType())) {
1607    S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1608      << Attr.getName();
1609    return;
1610  }
1611
1612  switch (Attr.getKind()) {
1613    default:
1614      assert(0 && "invalid ownership attribute");
1615      return;
1616    case AttributeList::AT_cf_returns_retained:
1617      d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
1618      return;
1619    case AttributeList::AT_ns_returns_retained:
1620      d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
1621      return;
1622  };
1623}
1624
1625//===----------------------------------------------------------------------===//
1626// Top Level Sema Entry Points
1627//===----------------------------------------------------------------------===//
1628
1629/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
1630/// the attribute applies to decls.  If the attribute is a type attribute, just
1631/// silently ignore it.
1632static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
1633  switch (Attr.getKind()) {
1634  case AttributeList::AT_IBOutlet:    HandleIBOutletAttr  (D, Attr, S); break;
1635  case AttributeList::AT_address_space:
1636  case AttributeList::AT_objc_gc:
1637    // Ignore these, these are type attributes, handled by ProcessTypeAttributes.
1638    break;
1639  case AttributeList::AT_alias:       HandleAliasAttr     (D, Attr, S); break;
1640  case AttributeList::AT_aligned:     HandleAlignedAttr   (D, Attr, S); break;
1641  case AttributeList::AT_always_inline:
1642    HandleAlwaysInlineAttr  (D, Attr, S); break;
1643  case AttributeList::AT_analyzer_noreturn:
1644    HandleAnalyzerNoReturnAttr  (D, Attr, S); break;
1645  case AttributeList::AT_annotate:    HandleAnnotateAttr  (D, Attr, S); break;
1646  case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1647  case AttributeList::AT_deprecated:  HandleDeprecatedAttr(D, Attr, S); break;
1648  case AttributeList::AT_destructor:  HandleDestructorAttr(D, Attr, S); break;
1649  case AttributeList::AT_dllexport:   HandleDLLExportAttr (D, Attr, S); break;
1650  case AttributeList::AT_dllimport:   HandleDLLImportAttr (D, Attr, S); break;
1651  case AttributeList::AT_ext_vector_type:
1652    HandleExtVectorTypeAttr(D, Attr, S);
1653    break;
1654  case AttributeList::AT_fastcall:    HandleFastCallAttr  (D, Attr, S); break;
1655  case AttributeList::AT_format:      HandleFormatAttr    (D, Attr, S); break;
1656  case AttributeList::AT_gnu_inline:  HandleGNUInlineAttr(D, Attr, S); break;
1657  case AttributeList::AT_mode:        HandleModeAttr      (D, Attr, S); break;
1658  case AttributeList::AT_nonnull:     HandleNonNullAttr   (D, Attr, S); break;
1659  case AttributeList::AT_noreturn:    HandleNoReturnAttr  (D, Attr, S); break;
1660  case AttributeList::AT_nothrow:     HandleNothrowAttr   (D, Attr, S); break;
1661
1662  // Checker-specific.
1663  case AttributeList::AT_ns_returns_retained:
1664  case AttributeList::AT_cf_returns_retained:
1665    HandleNSReturnsRetainedAttr(D, Attr, S); break;
1666
1667  case AttributeList::AT_packed:      HandlePackedAttr    (D, Attr, S); break;
1668  case AttributeList::AT_section:     HandleSectionAttr   (D, Attr, S); break;
1669  case AttributeList::AT_stdcall:     HandleStdCallAttr   (D, Attr, S); break;
1670  case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
1671  case AttributeList::AT_unused:      HandleUnusedAttr    (D, Attr, S); break;
1672  case AttributeList::AT_used:        HandleUsedAttr      (D, Attr, S); break;
1673  case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
1674  case AttributeList::AT_visibility:  HandleVisibilityAttr(D, Attr, S); break;
1675  case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1676    break;
1677  case AttributeList::AT_weak:        HandleWeakAttr      (D, Attr, S); break;
1678  case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
1679  case AttributeList::AT_transparent_union:
1680    HandleTransparentUnionAttr(D, Attr, S);
1681    break;
1682  case AttributeList::AT_objc_exception:
1683    HandleObjCExceptionAttr(D, Attr, S);
1684    break;
1685  case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
1686  case AttributeList::AT_nsobject:    HandleObjCNSObject  (D, Attr, S); break;
1687  case AttributeList::AT_blocks:      HandleBlocksAttr    (D, Attr, S); break;
1688  case AttributeList::AT_sentinel:    HandleSentinelAttr  (D, Attr, S); break;
1689  case AttributeList::AT_const:       HandleConstAttr     (D, Attr, S); break;
1690  case AttributeList::AT_pure:        HandlePureAttr      (D, Attr, S); break;
1691  case AttributeList::AT_cleanup:     HandleCleanupAttr   (D, Attr, S); break;
1692  case AttributeList::AT_nodebug:     HandleNodebugAttr   (D, Attr, S); break;
1693  case AttributeList::AT_noinline:    HandleNoinlineAttr  (D, Attr, S); break;
1694  case AttributeList::AT_regparm:     HandleRegparmAttr   (D, Attr, S); break;
1695  case AttributeList::IgnoredAttribute:
1696  case AttributeList::AT_no_instrument_function:  // Interacts with -pg.
1697    // Just ignore
1698    break;
1699  default:
1700    S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1701    break;
1702  }
1703}
1704
1705/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1706/// attribute list to the specified decl, ignoring any type attributes.
1707void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1708  while (AttrList) {
1709    ProcessDeclAttribute(D, *AttrList, *this);
1710    AttrList = AttrList->getNext();
1711  }
1712}
1713
1714/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1715/// it, apply them to D.  This is a bit tricky because PD can have attributes
1716/// specified in many different places, and we need to find and apply them all.
1717void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
1718  // Apply decl attributes from the DeclSpec if present.
1719  if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
1720    ProcessDeclAttributeList(D, Attrs);
1721
1722  // Walk the declarator structure, applying decl attributes that were in a type
1723  // position to the decl itself.  This handles cases like:
1724  //   int *__attr__(x)** D;
1725  // when X is a decl attribute.
1726  for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1727    if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
1728      ProcessDeclAttributeList(D, Attrs);
1729
1730  // Finally, apply any attributes on the decl itself.
1731  if (const AttributeList *Attrs = PD.getAttributes())
1732    ProcessDeclAttributeList(D, Attrs);
1733}
1734