Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1 | //===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements decl-related attribute processing. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 15 | #include "TargetAttributesSema.h" |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | acc5f3e | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
| 19 | #include "clang/AST/Expr.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 21 | #include "clang/Basic/TargetInfo.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 22 | #include "clang/Sema/DeclSpec.h" |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 23 | #include "clang/Sema/DelayedDiagnostic.h" |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 25 | using namespace clang; |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 26 | using namespace sema; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 27 | |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 28 | /// These constants match the enumerated choices of |
| 29 | /// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type. |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 30 | enum AttributeDeclType { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 31 | ExpectedFunction, |
| 32 | ExpectedUnion, |
| 33 | ExpectedVariableOrFunction, |
| 34 | ExpectedFunctionOrMethod, |
| 35 | ExpectedParameter, |
| 36 | ExpectedParameterOrMethod, |
| 37 | ExpectedFunctionMethodOrBlock, |
| 38 | ExpectedClassOrVirtualMethod, |
| 39 | ExpectedFunctionMethodOrParameter, |
| 40 | ExpectedClass, |
| 41 | ExpectedVirtualMethod, |
| 42 | ExpectedClassMember, |
| 43 | ExpectedVariable, |
| 44 | ExpectedMethod, |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 45 | ExpectedVariableFunctionOrLabel, |
| 46 | ExpectedFieldOrGlobalVar |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 49 | //===----------------------------------------------------------------------===// |
| 50 | // Helper functions |
| 51 | //===----------------------------------------------------------------------===// |
| 52 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 53 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 54 | bool blocksToo = true) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 55 | QualType Ty; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 56 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 57 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 58 | else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 59 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 60 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 61 | Ty = decl->getUnderlyingType(); |
| 62 | else |
| 63 | return 0; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 65 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 66 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 755f9d2 | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 67 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 68 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 69 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 70 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 73 | // FIXME: We should provide an abstraction around a method or function |
| 74 | // to provide the following bits of information. |
| 75 | |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 76 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 77 | /// type (function or function-typed variable). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 78 | static bool isFunction(const Decl *D) { |
| 79 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 83 | /// type (function or function-typed variable) or an Objective-C |
| 84 | /// method. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 85 | static bool isFunctionOrMethod(const Decl *D) { |
| 86 | return isFunction(D)|| isa<ObjCMethodDecl>(D); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 89 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 90 | /// type (function or function-typed variable) or an Objective-C |
| 91 | /// method or a block. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 92 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 93 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 94 | return true; |
| 95 | // check for block is more involved. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 96 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 97 | QualType Ty = V->getType(); |
| 98 | return Ty->isBlockPointerType(); |
| 99 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 100 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 101 | } |
| 102 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 103 | /// Return true if the given decl has a declarator that should have |
| 104 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 105 | static bool hasDeclarator(const Decl *D) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 106 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 107 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 108 | isa<ObjCPropertyDecl>(D); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 111 | /// hasFunctionProto - Return true if the given decl has a argument |
| 112 | /// information. This decl should have already passed |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 113 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 114 | static bool hasFunctionProto(const Decl *D) { |
| 115 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 116 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 117 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 118 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 119 | return true; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 124 | /// arguments. It is an error to call this on a K&R function (use |
| 125 | /// hasFunctionProto first). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 126 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 127 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 128 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 129 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 130 | return BD->getNumParams(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 131 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 134 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 135 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 136 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 137 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 138 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 139 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 140 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 143 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 144 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 145 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 146 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 149 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 150 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 151 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 152 | return proto->isVariadic(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 153 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | db9a0ae | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 154 | return BD->isVariadic(); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 155 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 156 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 160 | static bool isInstanceMethod(const Decl *D) { |
| 161 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 162 | return MethodDecl->isInstance(); |
| 163 | return false; |
| 164 | } |
| 165 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 166 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 167 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 168 | if (!PT) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 169 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 170 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 171 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 172 | if (!Cls) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 173 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 174 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 175 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 176 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 177 | // FIXME: Should we walk the chain of classes? |
| 178 | return ClsName == &Ctx.Idents.get("NSString") || |
| 179 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 180 | } |
| 181 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 182 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 183 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 184 | if (!PT) |
| 185 | return false; |
| 186 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 187 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 188 | if (!RT) |
| 189 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 190 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 191 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 192 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 193 | return false; |
| 194 | |
| 195 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 196 | } |
| 197 | |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 198 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 199 | unsigned int Num) { |
| 200 | if (Attr.getNumArgs() != Num) { |
| 201 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num; |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | return true; |
| 206 | } |
| 207 | |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 208 | /// |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 209 | /// \brief Check the total number of argumenation, whether parsed by clang |
| 210 | /// as arguments or parameters. Outputs a warning. |
| 211 | /// \return false if the number of argumenation units does not match expectation |
| 212 | /// |
| 213 | static bool checkAttributeNumArgsPlusParams(Sema &S, const AttributeList &Attr, |
| 214 | unsigned int Num, |
| 215 | bool moreok = false) { |
| 216 | unsigned int numArgsPlusParams = 0; |
| 217 | |
| 218 | if (Attr.getParameterName()) |
| 219 | numArgsPlusParams++; |
| 220 | |
| 221 | numArgsPlusParams += Attr.getNumArgs(); |
| 222 | |
| 223 | if (moreok && numArgsPlusParams < Num) { |
| 224 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | if (!moreok && numArgsPlusParams != Num) { |
| 229 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num; |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | /// |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 237 | /// \brief Check if passed in Decl is a field or potentially shared global var |
| 238 | /// \return true if the Decl is a field or potentially shared global variable |
| 239 | /// |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 240 | static bool mayBeSharedVariable(const Decl *D) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 241 | if (isa<FieldDecl>(D)) |
| 242 | return true; |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 243 | if (const VarDecl *vd = dyn_cast<VarDecl>(D)) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 244 | return (vd->hasGlobalStorage() && !(vd->isThreadSpecified())); |
| 245 | |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | /// |
| 250 | /// \brief Check if passed in Decl is a pointer type. |
| 251 | /// Note that this function may produce an error message. |
| 252 | /// \return true if the Decl is a pointer type; false otherwise |
| 253 | /// |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 254 | static bool checkIsPointer(Sema &S, const Decl *D, const AttributeList &Attr) { |
| 255 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 256 | QualType QT = vd->getType(); |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 257 | if (QT->isAnyPointerType()) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 258 | return true; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 259 | S.Diag(Attr.getLoc(), diag::warn_pointer_attribute_wrong_type) |
| 260 | << Attr.getName()->getName() << QT; |
| 261 | } else { |
| 262 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 263 | << Attr.getName(); |
| 264 | } |
| 265 | return false; |
| 266 | } |
| 267 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 268 | //===----------------------------------------------------------------------===// |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 269 | // Attribute Implementations |
| 270 | //===----------------------------------------------------------------------===// |
| 271 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 272 | // FIXME: All this manual attribute parsing code is gross. At the |
| 273 | // least add some helper functions to check most argument patterns (# |
| 274 | // and types of args). |
| 275 | |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 276 | static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 277 | bool pointer = false) { |
| 278 | assert(!Attr.isInvalid()); |
| 279 | |
| 280 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 281 | return; |
| 282 | |
| 283 | // D must be either a member field or global (potentially shared) variable. |
| 284 | if (!mayBeSharedVariable(D)) { |
| 285 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 286 | << Attr.getName() << 15; /*fields and global vars*/; |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | if (pointer && !checkIsPointer(S, D, Attr)) |
| 291 | return; |
| 292 | |
| 293 | if (pointer) |
| 294 | D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getLoc(), S.Context)); |
| 295 | else |
| 296 | D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getLoc(), S.Context)); |
| 297 | } |
| 298 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 299 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 300 | bool pointer = false) { |
| 301 | assert(!Attr.isInvalid()); |
| 302 | |
| 303 | if (!checkAttributeNumArgsPlusParams(S, Attr, 1)) |
| 304 | return; |
| 305 | |
| 306 | // D must be either a member field or global (potentially shared) variable. |
| 307 | if (!mayBeSharedVariable(D)) { |
| 308 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 309 | << Attr.getName() << 15; /*fields and global vars*/; |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | if (pointer && !checkIsPointer(S, D, Attr)) |
| 314 | return; |
| 315 | |
| 316 | if (pointer) |
| 317 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getLoc(), S.Context)); |
| 318 | else |
| 319 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getLoc(), S.Context)); |
| 320 | } |
| 321 | |
| 322 | |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 323 | static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 324 | bool scoped = false) { |
| 325 | assert(!Attr.isInvalid()); |
| 326 | |
| 327 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 328 | return; |
| 329 | |
| 330 | if (!isa<CXXRecordDecl>(D)) { |
| 331 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 332 | << Attr.getName() << ExpectedClass; |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | if (scoped) |
| 337 | D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getLoc(), S.Context)); |
| 338 | else |
| 339 | D->addAttr(::new (S.Context) LockableAttr(Attr.getLoc(), S.Context)); |
| 340 | } |
| 341 | |
| 342 | static void handleNoThreadSafetyAttr(Sema &S, Decl *D, |
| 343 | const AttributeList &Attr) { |
| 344 | assert(!Attr.isInvalid()); |
| 345 | |
| 346 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 347 | return; |
| 348 | |
| 349 | if (!isFunction(D)) { |
| 350 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 351 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getLoc(), |
| 356 | S.Context)); |
| 357 | } |
| 358 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 359 | static void handleAcquireOrderAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 360 | bool before) { |
| 361 | assert(!Attr.isInvalid()); |
| 362 | |
| 363 | if (!checkAttributeNumArgsPlusParams(S, Attr, 1, /*moreok=*/true)) |
| 364 | return; |
| 365 | |
| 366 | // D must be either a member field or global (potentially shared) variable. |
| 367 | if (!mayBeSharedVariable(D)) { |
| 368 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 369 | << Attr.getName() << 15; /*fields and global vars*/; |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | if (before) |
| 374 | D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getLoc(), S.Context)); |
| 375 | else |
| 376 | D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getLoc(), S.Context)); |
| 377 | } |
| 378 | |
| 379 | static void handleLockFunAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 380 | bool exclusive = false) { |
| 381 | assert(!Attr.isInvalid()); |
| 382 | |
| 383 | // zero or more arguments ok |
| 384 | |
| 385 | if (!isFunction(D)) { |
| 386 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 387 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | if (exclusive) |
| 392 | D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getLoc(), |
| 393 | S.Context)); |
| 394 | else |
| 395 | D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getLoc(), |
| 396 | S.Context)); |
| 397 | } |
| 398 | |
| 399 | static void handleTrylockFunAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 400 | bool exclusive = false) { |
| 401 | assert(!Attr.isInvalid()); |
| 402 | |
| 403 | if (!checkAttributeNumArgsPlusParams(S, Attr, 1, /*moreok=*/true)) |
| 404 | return; |
| 405 | |
| 406 | if (!isFunction(D)) { |
| 407 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 408 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | if (exclusive) |
| 413 | D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getLoc(), |
| 414 | S.Context)); |
| 415 | else |
| 416 | D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getLoc(), |
| 417 | S.Context)); |
| 418 | |
| 419 | } |
| 420 | |
| 421 | static void handleLocksRequiredAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 422 | bool exclusive = false) { |
| 423 | assert(!Attr.isInvalid()); |
| 424 | |
| 425 | if (!checkAttributeNumArgsPlusParams(S, Attr, 1, /*moreok=*/true)) |
| 426 | return; |
| 427 | |
| 428 | if (!isFunction(D)) { |
| 429 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 430 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 431 | return; |
| 432 | } |
| 433 | |
| 434 | if (exclusive) |
| 435 | D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getLoc(), |
| 436 | S.Context)); |
| 437 | else |
| 438 | D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getLoc(), |
| 439 | S.Context)); |
| 440 | } |
| 441 | |
| 442 | |
| 443 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
| 444 | const AttributeList &Attr) { |
| 445 | assert(!Attr.isInvalid()); |
| 446 | |
| 447 | // zero or more arguments ok |
| 448 | |
| 449 | if (!isFunction(D)) { |
| 450 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 451 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getLoc(), S.Context)); |
| 456 | } |
| 457 | |
| 458 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
| 459 | const AttributeList &Attr) { |
| 460 | assert(!Attr.isInvalid()); |
| 461 | |
| 462 | if (!checkAttributeNumArgsPlusParams(S, Attr, 1)) |
| 463 | return; |
| 464 | |
| 465 | if (!isFunction(D)) { |
| 466 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 467 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getLoc(), S.Context)); |
| 472 | } |
| 473 | |
| 474 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
| 475 | const AttributeList &Attr) { |
| 476 | assert(!Attr.isInvalid()); |
| 477 | |
| 478 | if (!checkAttributeNumArgsPlusParams(S, Attr, 1, /*moreok=*/true)) |
| 479 | return; |
| 480 | |
| 481 | if (!isFunction(D)) { |
| 482 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 483 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getLoc(), S.Context)); |
| 488 | } |
| 489 | |
| 490 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 491 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 492 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 493 | TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 494 | if (tDecl == 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 495 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 496 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 497 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 498 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 499 | QualType curType = tDecl->getUnderlyingType(); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 500 | |
| 501 | Expr *sizeExpr; |
| 502 | |
| 503 | // Special case where the argument is a template id. |
| 504 | if (Attr.getParameterName()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 505 | CXXScopeSpec SS; |
| 506 | UnqualifiedId id; |
| 507 | id.setIdentifier(Attr.getParameterName(), Attr.getLoc()); |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 508 | |
| 509 | ExprResult Size = S.ActOnIdExpression(scope, SS, id, false, false); |
| 510 | if (Size.isInvalid()) |
| 511 | return; |
| 512 | |
| 513 | sizeExpr = Size.get(); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 514 | } else { |
| 515 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 516 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 517 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 518 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 519 | sizeExpr = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 520 | } |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 521 | |
| 522 | // Instantiate/Install the vector type, and let Sema build the type for us. |
| 523 | // This will run the reguired checks. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 524 | QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc()); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 525 | if (!T.isNull()) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 526 | // FIXME: preserve the old source info. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 527 | tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T)); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 528 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 529 | // Remember this typedef decl, we will need it later for diagnostics. |
| 530 | S.ExtVectorDecls.push_back(tDecl); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 531 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 534 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 535 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 536 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 537 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 538 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 539 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 540 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context)); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 541 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 542 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 543 | // has no effect. |
| 544 | if (!FD->getType()->isIncompleteType() && |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 545 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 546 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 547 | << Attr.getName() << FD->getType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 548 | else |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 549 | FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 550 | } else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 551 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 554 | static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 555 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Fariborz Jahanian | c1a0a73 | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 556 | TD->addAttr(::new (S.Context) MsStructAttr(Attr.getLoc(), S.Context)); |
| 557 | else |
| 558 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 559 | } |
| 560 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 561 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 562 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 563 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 564 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 565 | |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 566 | // The IBAction attributes only apply to instance methods. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 567 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 568 | if (MD->isInstanceMethod()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 569 | D->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context)); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 570 | return; |
| 571 | } |
| 572 | |
Ted Kremenek | 4ee2bb1 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 573 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 576 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 577 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 578 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 579 | return; |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 580 | |
| 581 | // The IBOutlet attributes only apply to instance variables of |
Ted Kremenek | efbddd2 | 2010-02-17 02:37:45 +0000 | [diff] [blame] | 582 | // Objective-C classes. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 583 | if (isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D)) { |
| 584 | D->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context)); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 585 | return; |
Ted Kremenek | efbddd2 | 2010-02-17 02:37:45 +0000 | [diff] [blame] | 586 | } |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 587 | |
Ted Kremenek | 4ee2bb1 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 588 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 591 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 592 | const AttributeList &Attr) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 593 | |
| 594 | // The iboutletcollection attribute can have zero or one arguments. |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 595 | if (Attr.getParameterName() && Attr.getNumArgs() > 0) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 596 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 597 | return; |
| 598 | } |
| 599 | |
| 600 | // The IBOutletCollection attributes only apply to instance variables of |
| 601 | // Objective-C classes. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 602 | if (!(isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) { |
Ted Kremenek | 4ee2bb1 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 603 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 604 | return; |
| 605 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 606 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
Fariborz Jahanian | 3a3400b | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 607 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
| 608 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type) |
| 609 | << VD->getType() << 0; |
| 610 | return; |
| 611 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 612 | if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
Fariborz Jahanian | 3a3400b | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 613 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
| 614 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type) |
| 615 | << PD->getType() << 1; |
| 616 | return; |
| 617 | } |
| 618 | |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 619 | IdentifierInfo *II = Attr.getParameterName(); |
| 620 | if (!II) |
| 621 | II = &S.Context.Idents.get("id"); |
Fariborz Jahanian | 3a3400b | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 622 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 623 | ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 624 | S.getScopeForContext(D->getDeclContext()->getParent())); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 625 | if (!TypeRep) { |
| 626 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 627 | return; |
| 628 | } |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 629 | QualType QT = TypeRep.get(); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 630 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 631 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 632 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 633 | // treated as __attribute__((iboutletcollection())). |
| 634 | if (!QT->isObjCIdType() && !QT->isObjCClassType() && |
| 635 | !QT->isObjCObjectType()) { |
| 636 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 637 | return; |
| 638 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 639 | D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context, |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 640 | QT)); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 643 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 644 | if (const RecordType *UT = T->getAsUnionType()) |
| 645 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 646 | RecordDecl *UD = UT->getDecl(); |
| 647 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 648 | itend = UD->field_end(); it != itend; ++it) { |
| 649 | QualType QT = it->getType(); |
| 650 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 651 | T = QT; |
| 652 | return; |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 658 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 659 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 660 | // ignore it as well |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 661 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 662 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 663 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 664 | return; |
| 665 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 666 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 667 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 668 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 669 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 670 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 671 | |
| 672 | // The nonnull attribute only applies to pointers. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 673 | SmallVector<unsigned, 10> NonNullArgs; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 674 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 675 | for (AttributeList::arg_iterator I=Attr.arg_begin(), |
| 676 | E=Attr.arg_end(); I!=E; ++I) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 677 | |
| 678 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 679 | // The argument must be an integer constant expression. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 680 | Expr *Ex = *I; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 681 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 682 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 683 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 684 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 685 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 686 | return; |
| 687 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 688 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 689 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 690 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 691 | if (x < 1 || x > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 692 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 693 | << "nonnull" << I.getArgNum() << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 694 | return; |
| 695 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 696 | |
Ted Kremenek | 465172f | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 697 | --x; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 698 | if (HasImplicitThisParam) { |
| 699 | if (x == 0) { |
| 700 | S.Diag(Attr.getLoc(), |
| 701 | diag::err_attribute_invalid_implicit_this_argument) |
| 702 | << "nonnull" << Ex->getSourceRange(); |
| 703 | return; |
| 704 | } |
| 705 | --x; |
| 706 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 707 | |
| 708 | // Is the function argument a pointer type? |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 709 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 710 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 711 | |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 712 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 713 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | c9ef405 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 714 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 715 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 716 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 717 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 718 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 719 | NonNullArgs.push_back(x); |
| 720 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 721 | |
| 722 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 723 | // arguments have a nonnull attribute. |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 724 | if (NonNullArgs.empty()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 725 | for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) { |
| 726 | QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 727 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 728 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 729 | NonNullArgs.push_back(I); |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 730 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 731 | |
Ted Kremenek | ee1c08c | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 732 | // No pointer arguments? |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 733 | if (NonNullArgs.empty()) { |
| 734 | // Warn the trivial case only if attribute is not coming from a |
| 735 | // macro instantiation. |
| 736 | if (Attr.getLoc().isFileID()) |
| 737 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 738 | return; |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 739 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 740 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 741 | |
| 742 | unsigned* start = &NonNullArgs[0]; |
| 743 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 744 | llvm::array_pod_sort(start, start + size); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 745 | D->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start, |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 746 | size)); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 749 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 750 | // This attribute must be applied to a function declaration. |
| 751 | // The first argument to the attribute must be a string, |
| 752 | // the name of the resource, for example "malloc". |
| 753 | // The following arguments must be argument indexes, the arguments must be |
| 754 | // of integer type for Returns, otherwise of pointer type. |
| 755 | // The difference between Holds and Takes is that a pointer may still be used |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 756 | // after being held. free() should be __attribute((ownership_takes)), whereas |
| 757 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 758 | |
| 759 | if (!AL.getParameterName()) { |
| 760 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string) |
| 761 | << AL.getName()->getName() << 1; |
| 762 | return; |
| 763 | } |
| 764 | // Figure out our Kind, and check arguments while we're at it. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 765 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 766 | switch (AL.getKind()) { |
| 767 | case AttributeList::AT_ownership_takes: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 768 | K = OwnershipAttr::Takes; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 769 | if (AL.getNumArgs() < 1) { |
| 770 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 771 | return; |
| 772 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 773 | break; |
| 774 | case AttributeList::AT_ownership_holds: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 775 | K = OwnershipAttr::Holds; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 776 | if (AL.getNumArgs() < 1) { |
| 777 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 778 | return; |
| 779 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 780 | break; |
| 781 | case AttributeList::AT_ownership_returns: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 782 | K = OwnershipAttr::Returns; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 783 | if (AL.getNumArgs() > 1) { |
| 784 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 785 | << AL.getNumArgs() + 1; |
| 786 | return; |
| 787 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 788 | break; |
| 789 | default: |
| 790 | // This should never happen given how we are called. |
| 791 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 794 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 795 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 796 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 797 | return; |
| 798 | } |
| 799 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 800 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 801 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 802 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 803 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 804 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 805 | StringRef Module = AL.getParameterName()->getName(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 806 | |
| 807 | // Normalize the argument, __foo__ becomes foo. |
| 808 | if (Module.startswith("__") && Module.endswith("__")) |
| 809 | Module = Module.substr(2, Module.size() - 4); |
| 810 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 811 | SmallVector<unsigned, 10> OwnershipArgs; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 812 | |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 813 | for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E; |
| 814 | ++I) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 815 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 816 | Expr *IdxExpr = *I; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 817 | llvm::APSInt ArgNum(32); |
| 818 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 819 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 820 | S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int) |
| 821 | << AL.getName()->getName() << IdxExpr->getSourceRange(); |
| 822 | continue; |
| 823 | } |
| 824 | |
| 825 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
| 826 | |
| 827 | if (x > NumArgs || x < 1) { |
| 828 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 829 | << AL.getName()->getName() << x << IdxExpr->getSourceRange(); |
| 830 | continue; |
| 831 | } |
| 832 | --x; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 833 | if (HasImplicitThisParam) { |
| 834 | if (x == 0) { |
| 835 | S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 836 | << "ownership" << IdxExpr->getSourceRange(); |
| 837 | return; |
| 838 | } |
| 839 | --x; |
| 840 | } |
| 841 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 842 | switch (K) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 843 | case OwnershipAttr::Takes: |
| 844 | case OwnershipAttr::Holds: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 845 | // Is the function argument a pointer type? |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 846 | QualType T = getFunctionOrMethodArgType(D, x); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 847 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| 848 | // FIXME: Should also highlight argument in decl. |
| 849 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 850 | << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 851 | << "pointer" |
| 852 | << IdxExpr->getSourceRange(); |
| 853 | continue; |
| 854 | } |
| 855 | break; |
| 856 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 857 | case OwnershipAttr::Returns: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 858 | if (AL.getNumArgs() > 1) { |
| 859 | // Is the function argument an integer type? |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 860 | Expr *IdxExpr = AL.getArg(0); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 861 | llvm::APSInt ArgNum(32); |
| 862 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 863 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 864 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
| 865 | << "ownership_returns" << "integer" |
| 866 | << IdxExpr->getSourceRange(); |
| 867 | return; |
| 868 | } |
| 869 | } |
| 870 | break; |
| 871 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 872 | default: |
| 873 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 874 | } // switch |
| 875 | |
| 876 | // Check we don't have a conflict with another ownership attribute. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 877 | for (specific_attr_iterator<OwnershipAttr> |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 878 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 879 | e = D->specific_attr_end<OwnershipAttr>(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 880 | i != e; ++i) { |
| 881 | if ((*i)->getOwnKind() != K) { |
| 882 | for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); |
| 883 | I!=E; ++I) { |
| 884 | if (x == *I) { |
| 885 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 886 | << AL.getName()->getName() << "ownership_*"; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | } |
| 890 | } |
| 891 | OwnershipArgs.push_back(x); |
| 892 | } |
| 893 | |
| 894 | unsigned* start = OwnershipArgs.data(); |
| 895 | unsigned size = OwnershipArgs.size(); |
| 896 | llvm::array_pod_sort(start, start + size); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 897 | |
| 898 | if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { |
| 899 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 900 | return; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 901 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 902 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 903 | D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module, |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 904 | start, size)); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 905 | } |
| 906 | |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 907 | /// Whether this declaration has internal linkage for the purposes of |
| 908 | /// things that want to complain about things not have internal linkage. |
| 909 | static bool hasEffectivelyInternalLinkage(NamedDecl *D) { |
| 910 | switch (D->getLinkage()) { |
| 911 | case NoLinkage: |
| 912 | case InternalLinkage: |
| 913 | return true; |
| 914 | |
| 915 | // Template instantiations that go from external to unique-external |
| 916 | // shouldn't get diagnosed. |
| 917 | case UniqueExternalLinkage: |
| 918 | return true; |
| 919 | |
| 920 | case ExternalLinkage: |
| 921 | return false; |
| 922 | } |
| 923 | llvm_unreachable("unknown linkage kind!"); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 924 | return false; |
| 925 | } |
| 926 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 927 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 928 | // Check the attribute arguments. |
| 929 | if (Attr.getNumArgs() > 1) { |
| 930 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 931 | return; |
| 932 | } |
| 933 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 934 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 935 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 936 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 937 | return; |
| 938 | } |
| 939 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 940 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 941 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 942 | // gcc rejects |
| 943 | // class c { |
| 944 | // static int a __attribute__((weakref ("v2"))); |
| 945 | // static int b() __attribute__((weakref ("f3"))); |
| 946 | // }; |
| 947 | // and ignores the attributes of |
| 948 | // void f(void) { |
| 949 | // static int a __attribute__((weakref ("v2"))); |
| 950 | // } |
| 951 | // we reject them |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 952 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 953 | if (!Ctx->isFileContext()) { |
| 954 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 955 | nd->getNameAsString(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 956 | return; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | // The GCC manual says |
| 960 | // |
| 961 | // At present, a declaration to which `weakref' is attached can only |
| 962 | // be `static'. |
| 963 | // |
| 964 | // It also says |
| 965 | // |
| 966 | // Without a TARGET, |
| 967 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 968 | // equivalent to `weak'. |
| 969 | // |
| 970 | // gcc 4.4.1 will accept |
| 971 | // int a7 __attribute__((weakref)); |
| 972 | // as |
| 973 | // int a7 __attribute__((weak)); |
| 974 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 975 | // it if this behaviour is actually used. |
| 976 | |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 977 | if (!hasEffectivelyInternalLinkage(nd)) { |
| 978 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 979 | return; |
| 980 | } |
| 981 | |
| 982 | // GCC rejects |
| 983 | // static ((alias ("y"), weakref)). |
| 984 | // Should we? How to check that weakref is before or after alias? |
| 985 | |
| 986 | if (Attr.getNumArgs() == 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 987 | Expr *Arg = Attr.getArg(0); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 988 | Arg = Arg->IgnoreParenCasts(); |
| 989 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 990 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 991 | if (!Str || !Str->isAscii()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 992 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 993 | << "weakref" << 1; |
| 994 | return; |
| 995 | } |
| 996 | // GCC will accept anything as the argument of weakref. Should we |
| 997 | // check for an existing decl? |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 998 | D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 999 | Str->getString())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1002 | D->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context)); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1005 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1006 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1007 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1008 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1009 | return; |
| 1010 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1011 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1012 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1013 | Arg = Arg->IgnoreParenCasts(); |
| 1014 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1015 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1016 | if (!Str || !Str->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1017 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1018 | << "alias" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1019 | return; |
| 1020 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1021 | |
Daniel Dunbar | db57a4c | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 1022 | if (S.Context.Target.getTriple().isOSDarwin()) { |
Rafael Espindola | f5fe292 | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1023 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1024 | return; |
| 1025 | } |
| 1026 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1027 | // FIXME: check if target symbol exists in current file |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1028 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1029 | D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1030 | Str->getString())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1033 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1034 | // Check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1035 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1036 | return; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1037 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1038 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1039 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1040 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1041 | return; |
| 1042 | } |
| 1043 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1044 | D->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context)); |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1047 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 1048 | const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1049 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1050 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1051 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1052 | return; |
| 1053 | } |
| 1054 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1055 | if (!isa<FunctionDecl>(D)) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1056 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1057 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1058 | return; |
| 1059 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1060 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1061 | D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context)); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1064 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1065 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1066 | if (Attr.hasParameterOrArguments()) { |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1067 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1068 | return; |
| 1069 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1070 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1071 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1072 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1073 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1074 | D->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context)); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1075 | return; |
| 1076 | } |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1079 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1082 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1083 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1084 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1085 | return; |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1086 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1087 | D->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context)); |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1090 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1091 | assert(!Attr.isInvalid()); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1092 | if (isa<VarDecl>(D)) |
| 1093 | D->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context)); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1094 | else |
| 1095 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1096 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1099 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1100 | assert(!Attr.isInvalid()); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1101 | if (isa<VarDecl>(D)) |
| 1102 | D->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context)); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1103 | else |
| 1104 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1105 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1108 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1109 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1110 | |
| 1111 | if (S.CheckNoReturnAttr(attr)) return; |
| 1112 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1113 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1114 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1115 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1116 | return; |
| 1117 | } |
| 1118 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1119 | D->addAttr(::new (S.Context) NoReturnAttr(attr.getLoc(), S.Context)); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1123 | if (attr.hasParameterOrArguments()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1124 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1125 | attr.setInvalid(); |
| 1126 | return true; |
| 1127 | } |
| 1128 | |
| 1129 | return false; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1132 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1133 | const AttributeList &Attr) { |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1134 | |
| 1135 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1136 | // because 'analyzer_noreturn' does not impact the type. |
| 1137 | |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1138 | if(!checkAttributeNumArgs(S, Attr, 0)) |
| 1139 | return; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1140 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1141 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1142 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1143 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1144 | && !VD->getType()->isFunctionPointerType())) { |
| 1145 | S.Diag(Attr.getLoc(), |
| 1146 | Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type |
| 1147 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1148 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1149 | return; |
| 1150 | } |
| 1151 | } |
| 1152 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1153 | D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1156 | // PS3 PPU-specific. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1157 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1158 | /* |
| 1159 | Returning a Vector Class in Registers |
| 1160 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1161 | According to the PPU ABI specifications, a class with a single member of |
| 1162 | vector type is returned in memory when used as the return value of a function. |
| 1163 | This results in inefficient code when implementing vector classes. To return |
| 1164 | the value in a single vector register, add the vecreturn attribute to the |
| 1165 | class definition. This attribute is also applicable to struct types. |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1166 | |
| 1167 | Example: |
| 1168 | |
| 1169 | struct Vector |
| 1170 | { |
| 1171 | __vector float xyzw; |
| 1172 | } __attribute__((vecreturn)); |
| 1173 | |
| 1174 | Vector Add(Vector lhs, Vector rhs) |
| 1175 | { |
| 1176 | Vector result; |
| 1177 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1178 | return result; // This will be returned in a register |
| 1179 | } |
| 1180 | */ |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1181 | if (!isa<RecordDecl>(D)) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1182 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1183 | << Attr.getName() << ExpectedClass; |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1184 | return; |
| 1185 | } |
| 1186 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1187 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1188 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1189 | return; |
| 1190 | } |
| 1191 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1192 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1193 | int count = 0; |
| 1194 | |
| 1195 | if (!isa<CXXRecordDecl>(record)) { |
| 1196 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1197 | return; |
| 1198 | } |
| 1199 | |
| 1200 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1201 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1202 | return; |
| 1203 | } |
| 1204 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1205 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1206 | iter != record->field_end(); iter++) { |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1207 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1208 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1209 | return; |
| 1210 | } |
| 1211 | count++; |
| 1212 | } |
| 1213 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1214 | D->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context)); |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1217 | static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1218 | if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1219 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1220 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1221 | return; |
| 1222 | } |
| 1223 | // FIXME: Actually store the attribute on the declaration |
| 1224 | } |
| 1225 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1226 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1227 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1228 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1229 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1230 | return; |
| 1231 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1232 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1233 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
| 1234 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1235 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1236 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1237 | return; |
| 1238 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1239 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1240 | D->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context)); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1243 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1244 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1245 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1246 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1247 | return; |
| 1248 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1249 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1250 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Daniel Dunbar | 186204b | 2009-02-13 22:48:56 +0000 | [diff] [blame] | 1251 | if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1252 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 1253 | return; |
| 1254 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1255 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1256 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1257 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1258 | return; |
| 1259 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1260 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1261 | D->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context)); |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1264 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1265 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1266 | if (Attr.getNumArgs() > 1) { |
| 1267 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1268 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1269 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1270 | |
| 1271 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1272 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1273 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1274 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1275 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1276 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1277 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1278 | << "constructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1279 | return; |
| 1280 | } |
| 1281 | priority = Idx.getZExtValue(); |
| 1282 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1283 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1284 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1285 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1286 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1287 | return; |
| 1288 | } |
| 1289 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1290 | D->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1291 | priority)); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1294 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1295 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1296 | if (Attr.getNumArgs() > 1) { |
| 1297 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1298 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1299 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1300 | |
| 1301 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1302 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1303 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1304 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1305 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1306 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1307 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1308 | << "destructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1309 | return; |
| 1310 | } |
| 1311 | priority = Idx.getZExtValue(); |
| 1312 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1313 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1314 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1315 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1316 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1317 | return; |
| 1318 | } |
| 1319 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1320 | D->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1321 | priority)); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1324 | static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1325 | unsigned NumArgs = Attr.getNumArgs(); |
| 1326 | if (NumArgs > 1) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1327 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1328 | return; |
| 1329 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1330 | |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1331 | // Handle the case where deprecated attribute has a text message. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1332 | StringRef Str; |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1333 | if (NumArgs == 1) { |
| 1334 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1335 | if (!SE) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1336 | S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string) |
| 1337 | << "deprecated"; |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1338 | return; |
| 1339 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1340 | Str = SE->getString(); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1341 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1342 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1343 | D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context, Str)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1346 | static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1347 | unsigned NumArgs = Attr.getNumArgs(); |
| 1348 | if (NumArgs > 1) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1349 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1350 | return; |
| 1351 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1352 | |
Fariborz Jahanian | c784dc1 | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 1353 | // Handle the case where unavailable attribute has a text message. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1354 | StringRef Str; |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1355 | if (NumArgs == 1) { |
| 1356 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
Fariborz Jahanian | c784dc1 | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 1357 | if (!SE) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1358 | S.Diag(Attr.getArg(0)->getLocStart(), |
Fariborz Jahanian | c784dc1 | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 1359 | diag::err_attribute_not_string) << "unavailable"; |
| 1360 | return; |
| 1361 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1362 | Str = SE->getString(); |
Fariborz Jahanian | c784dc1 | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 1363 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1364 | D->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context, Str)); |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1367 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 1368 | const AttributeList &Attr) { |
| 1369 | unsigned NumArgs = Attr.getNumArgs(); |
| 1370 | if (NumArgs > 0) { |
| 1371 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 1372 | return; |
| 1373 | } |
| 1374 | |
| 1375 | D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr( |
| 1376 | Attr.getLoc(), S.Context)); |
| 1377 | } |
| 1378 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1379 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 1380 | const AttributeList &Attr) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1381 | IdentifierInfo *Platform = Attr.getParameterName(); |
| 1382 | SourceLocation PlatformLoc = Attr.getParameterLoc(); |
| 1383 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1384 | StringRef PlatformName |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1385 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1386 | if (PlatformName.empty()) { |
| 1387 | S.Diag(PlatformLoc, diag::warn_availability_unknown_platform) |
| 1388 | << Platform; |
| 1389 | |
| 1390 | PlatformName = Platform->getName(); |
| 1391 | } |
| 1392 | |
| 1393 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 1394 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 1395 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1396 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1397 | |
| 1398 | // Ensure that Introduced < Deprecated < Obsoleted (although not all |
| 1399 | // of these steps are needed). |
| 1400 | if (Introduced.isValid() && Deprecated.isValid() && |
| 1401 | !(Introduced.Version < Deprecated.Version)) { |
| 1402 | S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering) |
| 1403 | << 1 << PlatformName << Deprecated.Version.getAsString() |
| 1404 | << 0 << Introduced.Version.getAsString(); |
| 1405 | return; |
| 1406 | } |
| 1407 | |
| 1408 | if (Introduced.isValid() && Obsoleted.isValid() && |
| 1409 | !(Introduced.Version < Obsoleted.Version)) { |
| 1410 | S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering) |
| 1411 | << 2 << PlatformName << Obsoleted.Version.getAsString() |
| 1412 | << 0 << Introduced.Version.getAsString(); |
| 1413 | return; |
| 1414 | } |
| 1415 | |
| 1416 | if (Deprecated.isValid() && Obsoleted.isValid() && |
| 1417 | !(Deprecated.Version < Obsoleted.Version)) { |
| 1418 | S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering) |
| 1419 | << 2 << PlatformName << Obsoleted.Version.getAsString() |
| 1420 | << 1 << Deprecated.Version.getAsString(); |
| 1421 | return; |
| 1422 | } |
| 1423 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1424 | D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getLoc(), S.Context, |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1425 | Platform, |
| 1426 | Introduced.Version, |
| 1427 | Deprecated.Version, |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1428 | Obsoleted.Version, |
| 1429 | IsUnavailable)); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1432 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1433 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1434 | if(!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1435 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1436 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1437 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1438 | Arg = Arg->IgnoreParenCasts(); |
| 1439 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1440 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1441 | if (!Str || !Str->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1442 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1443 | << "visibility" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1444 | return; |
| 1445 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1446 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1447 | StringRef TypeStr = Str->getString(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1448 | VisibilityAttr::VisibilityType type; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1449 | |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1450 | if (TypeStr == "default") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1451 | type = VisibilityAttr::Default; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1452 | else if (TypeStr == "hidden") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1453 | type = VisibilityAttr::Hidden; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1454 | else if (TypeStr == "internal") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1455 | type = VisibilityAttr::Hidden; // FIXME |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1456 | else if (TypeStr == "protected") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1457 | type = VisibilityAttr::Protected; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1458 | else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1459 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1460 | return; |
| 1461 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1462 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1463 | D->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1466 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 1467 | const AttributeList &Attr) { |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1468 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 1469 | if (!method) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1470 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1471 | << ExpectedMethod; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1472 | return; |
| 1473 | } |
| 1474 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1475 | if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) { |
| 1476 | if (!Attr.getParameterName() && Attr.getNumArgs() == 1) { |
| 1477 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1478 | << "objc_method_family" << 1; |
| 1479 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1480 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1481 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1482 | Attr.setInvalid(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1483 | return; |
| 1484 | } |
| 1485 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1486 | StringRef param = Attr.getParameterName()->getName(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1487 | ObjCMethodFamilyAttr::FamilyKind family; |
| 1488 | if (param == "none") |
| 1489 | family = ObjCMethodFamilyAttr::OMF_None; |
| 1490 | else if (param == "alloc") |
| 1491 | family = ObjCMethodFamilyAttr::OMF_alloc; |
| 1492 | else if (param == "copy") |
| 1493 | family = ObjCMethodFamilyAttr::OMF_copy; |
| 1494 | else if (param == "init") |
| 1495 | family = ObjCMethodFamilyAttr::OMF_init; |
| 1496 | else if (param == "mutableCopy") |
| 1497 | family = ObjCMethodFamilyAttr::OMF_mutableCopy; |
| 1498 | else if (param == "new") |
| 1499 | family = ObjCMethodFamilyAttr::OMF_new; |
| 1500 | else { |
| 1501 | // Just warn and ignore it. This is future-proof against new |
| 1502 | // families being used in system headers. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1503 | S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1504 | return; |
| 1505 | } |
| 1506 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1507 | if (family == ObjCMethodFamilyAttr::OMF_init && |
| 1508 | !method->getResultType()->isObjCObjectPointerType()) { |
| 1509 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 1510 | << method->getResultType(); |
| 1511 | // Ignore the attribute. |
| 1512 | return; |
| 1513 | } |
| 1514 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1515 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getLoc(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1516 | S.Context, family)); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1519 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 1520 | const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1521 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1522 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1523 | |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1524 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 1525 | if (OCI == 0) { |
| 1526 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 1527 | return; |
| 1528 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1529 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1530 | D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1533 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1534 | if (Attr.getNumArgs() != 0) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 1535 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1536 | return; |
| 1537 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1538 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1539 | QualType T = TD->getUnderlyingType(); |
| 1540 | if (!T->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1541 | !T->getAs<PointerType>()->getPointeeType()->isRecordType()) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1542 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 1543 | return; |
| 1544 | } |
| 1545 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1546 | D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context)); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1549 | static void |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1550 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1551 | if (Attr.getNumArgs() != 0) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 1552 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1553 | return; |
| 1554 | } |
| 1555 | |
| 1556 | if (!isa<FunctionDecl>(D)) { |
| 1557 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 1558 | return; |
| 1559 | } |
| 1560 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1561 | D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context)); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1564 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1565 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1566 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1567 | << "blocks" << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1568 | return; |
| 1569 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1570 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1571 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1572 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1573 | return; |
| 1574 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1575 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1576 | BlocksAttr::BlockType type; |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 1577 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1578 | type = BlocksAttr::ByRef; |
| 1579 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1580 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1581 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1582 | return; |
| 1583 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1584 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1585 | D->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type)); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1588 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1589 | // check the attribute arguments. |
| 1590 | if (Attr.getNumArgs() > 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1591 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1592 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1595 | int sentinel = 0; |
| 1596 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1597 | Expr *E = Attr.getArg(0); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1598 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1599 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1600 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1601 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1602 | << "sentinel" << 1 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1603 | return; |
| 1604 | } |
| 1605 | sentinel = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1606 | |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1607 | if (sentinel < 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1608 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 1609 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1610 | return; |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | int nullPos = 0; |
| 1615 | if (Attr.getNumArgs() > 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1616 | Expr *E = Attr.getArg(1); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1617 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1618 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1619 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1620 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1621 | << "sentinel" << 2 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1622 | return; |
| 1623 | } |
| 1624 | nullPos = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1625 | |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1626 | if (nullPos > 1 || nullPos < 0) { |
| 1627 | // FIXME: This error message could be improved, it would be nice |
| 1628 | // to say what the bounds actually are. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1629 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 1630 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1631 | return; |
| 1632 | } |
| 1633 | } |
| 1634 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1635 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1636 | const FunctionType *FT = FD->getType()->getAs<FunctionType>(); |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 1637 | assert(FT && "FunctionDecl has non-function type?"); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1638 | |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 1639 | if (isa<FunctionNoProtoType>(FT)) { |
| 1640 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 1641 | return; |
| 1642 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1643 | |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 1644 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 1645 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1646 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1647 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1648 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1649 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 1650 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1651 | return; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1652 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1653 | } else if (isa<BlockDecl>(D)) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1654 | // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the |
| 1655 | // caller. |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1656 | ; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1657 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1658 | QualType Ty = V->getType(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 1659 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1660 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1661 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1662 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 1663 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 1664 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1665 | return; |
| 1666 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1667 | } else { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1668 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1669 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1670 | return; |
| 1671 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1672 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1673 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1674 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1675 | return; |
| 1676 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1677 | D->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1678 | nullPos)); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1681 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1682 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1683 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1684 | return; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1685 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1686 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1687 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1688 | << Attr.getName() << ExpectedFunctionOrMethod; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1689 | return; |
| 1690 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1691 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1692 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 1693 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 1694 | << Attr.getName() << 0; |
Nuno Lopes | f857798 | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 1695 | return; |
| 1696 | } |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1697 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 1698 | if (MD->getResultType()->isVoidType()) { |
| 1699 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 1700 | << Attr.getName() << 1; |
| 1701 | return; |
| 1702 | } |
| 1703 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1704 | D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1707 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1708 | // check the attribute arguments. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1709 | if (Attr.hasParameterOrArguments()) { |
| 1710 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1711 | return; |
| 1712 | } |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1713 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1714 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
| 1715 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1716 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | f23ecd9 | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 1717 | return; |
| 1718 | } |
| 1719 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1720 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1721 | |
| 1722 | // 'weak' only applies to declarations with external linkage. |
| 1723 | if (hasEffectivelyInternalLinkage(nd)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1724 | S.Diag(Attr.getLoc(), diag::err_attribute_weak_static); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1725 | return; |
| 1726 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1727 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1728 | nd->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1731 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1732 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1733 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1734 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1735 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1736 | |
| 1737 | // weak_import only applies to variable & function declarations. |
| 1738 | bool isDef = false; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1739 | if (!D->canBeWeakImported(isDef)) { |
| 1740 | if (isDef) |
| 1741 | S.Diag(Attr.getLoc(), |
| 1742 | diag::warn_attribute_weak_import_invalid_on_definition) |
| 1743 | << "weak_import" << 2 /*variable and function*/; |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 1744 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Daniel Dunbar | db57a4c | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 1745 | (S.Context.Target.getTriple().isOSDarwin() && |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 1746 | isa<ObjCInterfaceDecl>(D))) { |
| 1747 | // Nothing to warn about here. |
| 1748 | } else |
Fariborz Jahanian | c034974 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 1749 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1750 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1751 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1752 | return; |
| 1753 | } |
| 1754 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1755 | D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context)); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1758 | static void handleReqdWorkGroupSize(Sema &S, Decl *D, |
| 1759 | const AttributeList &Attr) { |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1760 | // Attribute has 3 arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1761 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1762 | return; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1763 | |
| 1764 | unsigned WGSize[3]; |
| 1765 | for (unsigned i = 0; i < 3; ++i) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1766 | Expr *E = Attr.getArg(i); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1767 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1768 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1769 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1770 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1771 | << "reqd_work_group_size" << E->getSourceRange(); |
| 1772 | return; |
| 1773 | } |
| 1774 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 1775 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1776 | D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context, |
| 1777 | WGSize[0], WGSize[1], |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1778 | WGSize[2])); |
| 1779 | } |
| 1780 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1781 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1782 | // Attribute has no arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1783 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1784 | return; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1785 | |
| 1786 | // Make sure that there is a string literal as the sections's single |
| 1787 | // argument. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1788 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1789 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1790 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1791 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section"; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1792 | return; |
| 1793 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1794 | |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1795 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | bb377ed | 2009-11-30 17:08:26 +0000 | [diff] [blame] | 1796 | std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString()); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 1797 | if (!Error.empty()) { |
| 1798 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 1799 | << Error; |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1800 | return; |
| 1801 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1802 | |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 1803 | // This attribute cannot be applied to local variables. |
| 1804 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 1805 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 1806 | return; |
| 1807 | } |
| 1808 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1809 | D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context, |
| 1810 | SE->getString())); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1813 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1814 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1815 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1816 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1817 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1818 | return; |
| 1819 | } |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 1820 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1821 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 1822 | if (Existing->getLocation().isInvalid()) |
| 1823 | Existing->setLocation(Attr.getLoc()); |
| 1824 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1825 | D->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context)); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 1826 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1829 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1830 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1831 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1832 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1833 | return; |
| 1834 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1835 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1836 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 1837 | if (Existing->getLocation().isInvalid()) |
| 1838 | Existing->setLocation(Attr.getLoc()); |
| 1839 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1840 | D->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context)); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 1841 | } |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1844 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1845 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1846 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1847 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1848 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1849 | D->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context)); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1852 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1853 | if (!Attr.getParameterName()) { |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1854 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1855 | return; |
| 1856 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1857 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1858 | if (Attr.getNumArgs() != 0) { |
| 1859 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1860 | return; |
| 1861 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1862 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1863 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1864 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1865 | if (!VD || !VD->hasLocalStorage()) { |
| 1866 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 1867 | return; |
| 1868 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1869 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1870 | // Look up the function |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1871 | // FIXME: Lookup probably isn't looking in the right place |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1872 | NamedDecl *CleanupDecl |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1873 | = S.LookupSingleName(S.TUScope, Attr.getParameterName(), |
| 1874 | Attr.getParameterLoc(), Sema::LookupOrdinaryName); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1875 | if (!CleanupDecl) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1876 | S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1877 | Attr.getParameterName(); |
| 1878 | return; |
| 1879 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1880 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1881 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 1882 | if (!FD) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1883 | S.Diag(Attr.getParameterLoc(), |
| 1884 | diag::err_attribute_cleanup_arg_not_function) |
| 1885 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1886 | return; |
| 1887 | } |
| 1888 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1889 | if (FD->getNumParams() != 1) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1890 | S.Diag(Attr.getParameterLoc(), |
| 1891 | diag::err_attribute_cleanup_func_must_take_one_arg) |
| 1892 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1893 | return; |
| 1894 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1895 | |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1896 | // We're currently more strict than GCC about what function types we accept. |
| 1897 | // If this ever proves to be a problem it should be easy to fix. |
| 1898 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 1899 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 1900 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 1901 | ParamTy, Ty) != Sema::Compatible) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1902 | S.Diag(Attr.getParameterLoc(), |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1903 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 1904 | Attr.getParameterName() << ParamTy << Ty; |
| 1905 | return; |
| 1906 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1907 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1908 | D->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD)); |
Argyrios Kyrtzidis | 223ae5c | 2010-12-06 17:51:53 +0000 | [diff] [blame] | 1909 | S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1912 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
| 1913 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1914 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1915 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1916 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1917 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1918 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1919 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1920 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1921 | return; |
| 1922 | } |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1923 | |
| 1924 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1925 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1926 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1927 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1928 | unsigned FirstIdx = 1; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1929 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1930 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1931 | Expr *IdxExpr = Attr.getArg(0); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1932 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1933 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 1934 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1935 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 1936 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 1937 | return; |
| 1938 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1939 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1940 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
| 1941 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1942 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 1943 | return; |
| 1944 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1945 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1946 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1947 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1948 | if (HasImplicitThisParam) { |
| 1949 | if (ArgIdx == 0) { |
| 1950 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 1951 | << "format_arg" << IdxExpr->getSourceRange(); |
| 1952 | return; |
| 1953 | } |
| 1954 | ArgIdx--; |
| 1955 | } |
| 1956 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1957 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1958 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1959 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1960 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 1961 | if (not_nsstring_type && |
| 1962 | !isCFStringType(Ty, S.Context) && |
| 1963 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1964 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1965 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 1966 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1967 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1968 | << IdxExpr->getSourceRange(); |
| 1969 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1970 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1971 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1972 | if (!isNSStringType(Ty, S.Context) && |
| 1973 | !isCFStringType(Ty, S.Context) && |
| 1974 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1975 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1976 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 1977 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1978 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1979 | << IdxExpr->getSourceRange(); |
| 1980 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1983 | D->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context, |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1984 | Idx.getZExtValue())); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1987 | enum FormatAttrKind { |
| 1988 | CFStringFormat, |
| 1989 | NSStringFormat, |
| 1990 | StrftimeFormat, |
| 1991 | SupportedFormat, |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 1992 | IgnoredFormat, |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1993 | InvalidFormat |
| 1994 | }; |
| 1995 | |
| 1996 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 1997 | /// types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1998 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1999 | // Check for formats that get handled specially. |
| 2000 | if (Format == "NSString") |
| 2001 | return NSStringFormat; |
| 2002 | if (Format == "CFString") |
| 2003 | return CFStringFormat; |
| 2004 | if (Format == "strftime") |
| 2005 | return StrftimeFormat; |
| 2006 | |
| 2007 | // Otherwise, check for supported formats. |
| 2008 | if (Format == "scanf" || Format == "printf" || Format == "printf0" || |
| 2009 | Format == "strfmon" || Format == "cmn_err" || Format == "strftime" || |
| 2010 | Format == "NSString" || Format == "CFString" || Format == "vcmn_err" || |
Chris Lattner | cd5b306 | 2011-02-18 17:05:55 +0000 | [diff] [blame] | 2011 | Format == "zcmn_err" || |
| 2012 | Format == "kprintf") // OpenBSD. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2013 | return SupportedFormat; |
| 2014 | |
Duncan Sands | bc52595 | 2010-03-23 14:44:19 +0000 | [diff] [blame] | 2015 | if (Format == "gcc_diag" || Format == "gcc_cdiag" || |
| 2016 | Format == "gcc_cxxdiag" || Format == "gcc_tdiag") |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2017 | return IgnoredFormat; |
| 2018 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2019 | return InvalidFormat; |
| 2020 | } |
| 2021 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2022 | /// Handle __attribute__((init_priority(priority))) attributes based on |
| 2023 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2024 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2025 | const AttributeList &Attr) { |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2026 | if (!S.getLangOptions().CPlusPlus) { |
| 2027 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2028 | return; |
| 2029 | } |
| 2030 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2031 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2032 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2033 | Attr.setInvalid(); |
| 2034 | return; |
| 2035 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2036 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2037 | if (S.Context.getAsArrayType(T)) |
| 2038 | T = S.Context.getBaseElementType(T); |
| 2039 | if (!T->getAs<RecordType>()) { |
| 2040 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2041 | Attr.setInvalid(); |
| 2042 | return; |
| 2043 | } |
| 2044 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2045 | if (Attr.getNumArgs() != 1) { |
| 2046 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2047 | Attr.setInvalid(); |
| 2048 | return; |
| 2049 | } |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2050 | Expr *priorityExpr = Attr.getArg(0); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2051 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2052 | llvm::APSInt priority(32); |
| 2053 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 2054 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
| 2055 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 2056 | << "init_priority" << priorityExpr->getSourceRange(); |
| 2057 | Attr.setInvalid(); |
| 2058 | return; |
| 2059 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 2060 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2061 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2062 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 2063 | << priorityExpr->getSourceRange(); |
| 2064 | Attr.setInvalid(); |
| 2065 | return; |
| 2066 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2067 | D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2068 | prioritynum)); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2071 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
| 2072 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2073 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2074 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2075 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2076 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2077 | << "format" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2078 | return; |
| 2079 | } |
| 2080 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2081 | if (Attr.getNumArgs() != 2) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2082 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2083 | return; |
| 2084 | } |
| 2085 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2086 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2087 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2088 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2089 | return; |
| 2090 | } |
| 2091 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2092 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2093 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2094 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 2095 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2096 | unsigned FirstIdx = 1; |
| 2097 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2098 | StringRef Format = Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2099 | |
| 2100 | // Normalize the argument, __foo__ becomes foo. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2101 | if (Format.startswith("__") && Format.endswith("__")) |
| 2102 | Format = Format.substr(2, Format.size() - 4); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2103 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2104 | // Check for supported formats. |
| 2105 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2106 | |
| 2107 | if (Kind == IgnoredFormat) |
| 2108 | return; |
| 2109 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2110 | if (Kind == InvalidFormat) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2111 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 2112 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2113 | return; |
| 2114 | } |
| 2115 | |
| 2116 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2117 | Expr *IdxExpr = Attr.getArg(0); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2118 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2119 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 2120 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2121 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2122 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2123 | return; |
| 2124 | } |
| 2125 | |
| 2126 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2127 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2128 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2129 | return; |
| 2130 | } |
| 2131 | |
| 2132 | // FIXME: Do we need to bounds check? |
| 2133 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2134 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2135 | if (HasImplicitThisParam) { |
| 2136 | if (ArgIdx == 0) { |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2137 | S.Diag(Attr.getLoc(), |
| 2138 | diag::err_format_attribute_implicit_this_format_string) |
| 2139 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2140 | return; |
| 2141 | } |
| 2142 | ArgIdx--; |
| 2143 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2144 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2145 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2146 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2147 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2148 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2149 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2150 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2151 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2152 | return; |
| 2153 | } |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2154 | } else if (Kind == NSStringFormat) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2155 | // FIXME: do we need to check if the type is NSString*? What are the |
| 2156 | // semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2157 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2158 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2159 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2160 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2161 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2162 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2163 | } else if (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2164 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2165 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2166 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2167 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2168 | return; |
| 2169 | } |
| 2170 | |
| 2171 | // check the 3rd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2172 | Expr *FirstArgExpr = Attr.getArg(1); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2173 | llvm::APSInt FirstArg(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2174 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 2175 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2176 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2177 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2178 | return; |
| 2179 | } |
| 2180 | |
| 2181 | // check if the function is variadic if the 3rd argument non-zero |
| 2182 | if (FirstArg != 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2183 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2184 | ++NumArgs; // +1 for ... |
| 2185 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2186 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2187 | return; |
| 2188 | } |
| 2189 | } |
| 2190 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2191 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 2192 | // variable the input is just the current time + the format string. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2193 | if (Kind == StrftimeFormat) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2194 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2195 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 2196 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2197 | return; |
| 2198 | } |
| 2199 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 2200 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2201 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2202 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2203 | return; |
| 2204 | } |
| 2205 | |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2206 | // Check whether we already have an equivalent format attribute. |
| 2207 | for (specific_attr_iterator<FormatAttr> |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2208 | i = D->specific_attr_begin<FormatAttr>(), |
| 2209 | e = D->specific_attr_end<FormatAttr>(); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2210 | i != e ; ++i) { |
| 2211 | FormatAttr *f = *i; |
| 2212 | if (f->getType() == Format && |
| 2213 | f->getFormatIdx() == (int)Idx.getZExtValue() && |
| 2214 | f->getFirstArg() == (int)FirstArg.getZExtValue()) { |
| 2215 | // If we don't have a valid location for this attribute, adopt the |
| 2216 | // location. |
| 2217 | if (f->getLocation().isInvalid()) |
| 2218 | f->setLocation(Attr.getLoc()); |
| 2219 | return; |
| 2220 | } |
| 2221 | } |
| 2222 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2223 | D->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format, |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2224 | Idx.getZExtValue(), |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2225 | FirstArg.getZExtValue())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2228 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 2229 | const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2230 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2231 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2232 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2233 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2234 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2235 | // Try to find the underlying union declaration. |
| 2236 | RecordDecl *RD = 0; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2237 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2238 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 2239 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 2240 | else |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2241 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2242 | |
| 2243 | if (!RD || !RD->isUnion()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2244 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2245 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2246 | return; |
| 2247 | } |
| 2248 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2249 | if (!RD->isDefinition()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2250 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2251 | diag::warn_transparent_union_attribute_not_definition); |
| 2252 | return; |
| 2253 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2254 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2255 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 2256 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2257 | if (Field == FieldEnd) { |
| 2258 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 2259 | return; |
| 2260 | } |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2261 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2262 | FieldDecl *FirstField = *Field; |
| 2263 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2264 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2265 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2266 | diag::warn_transparent_union_attribute_floating) |
| 2267 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2268 | return; |
| 2269 | } |
| 2270 | |
| 2271 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 2272 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 2273 | for (; Field != FieldEnd; ++Field) { |
| 2274 | QualType FieldType = Field->getType(); |
| 2275 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 2276 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 2277 | // Warn if we drop the attribute. |
| 2278 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2279 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2280 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2281 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2282 | diag::warn_transparent_union_attribute_field_size_align) |
| 2283 | << isSize << Field->getDeclName() << FieldBits; |
| 2284 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2285 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2286 | diag::note_transparent_union_first_field_size_align) |
| 2287 | << isSize << FirstBits; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2288 | return; |
| 2289 | } |
| 2290 | } |
| 2291 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2292 | RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2295 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2296 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2297 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2298 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2299 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2300 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2301 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2302 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2303 | // Make sure that there is a string literal as the annotation's single |
| 2304 | // argument. |
| 2305 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2306 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate"; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2307 | return; |
| 2308 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2309 | D->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2310 | SE->getString())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2313 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2314 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2315 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2316 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2317 | return; |
| 2318 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2319 | |
| 2320 | //FIXME: The C++0x version of this attribute has more limited applicabilty |
| 2321 | // than GNU's, and should error out when it is used to specify a |
| 2322 | // weaker alignment, rather than being silently ignored. |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2323 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2324 | if (Attr.getNumArgs() == 0) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2325 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2326 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2327 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2328 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2329 | S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0)); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2330 | } |
| 2331 | |
| 2332 | void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) { |
| 2333 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 2334 | // Save dependent expressions in the AST to be instantiated. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2335 | D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E)); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2336 | return; |
| 2337 | } |
| 2338 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2339 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2340 | llvm::APSInt Alignment(32); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2341 | if (!E->isIntegerConstantExpr(Alignment, Context)) { |
| 2342 | Diag(AttrLoc, diag::err_attribute_argument_not_int) |
| 2343 | << "aligned" << E->getSourceRange(); |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2344 | return; |
| 2345 | } |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2346 | if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2347 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 2348 | << E->getSourceRange(); |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2349 | return; |
| 2350 | } |
| 2351 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2352 | D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E)); |
| 2353 | } |
| 2354 | |
| 2355 | void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) { |
| 2356 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 2357 | // FIXME: Perform checking of type validity |
| 2358 | D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS)); |
| 2359 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2360 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2361 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 2362 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2363 | /// type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2364 | /// |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2365 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 2366 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 2367 | /// HImode, not an intermediate pointer. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2368 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2369 | // This attribute isn't documented, but glibc uses it. It changes |
| 2370 | // the width of an int or unsigned int to the specified size. |
| 2371 | |
| 2372 | // Check that there aren't any arguments |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2373 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2374 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2375 | |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2376 | |
| 2377 | IdentifierInfo *Name = Attr.getParameterName(); |
| 2378 | if (!Name) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2379 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2380 | return; |
| 2381 | } |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2382 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2383 | StringRef Str = Attr.getParameterName()->getName(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2384 | |
| 2385 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2386 | if (Str.startswith("__") && Str.endswith("__")) |
| 2387 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2388 | |
| 2389 | unsigned DestWidth = 0; |
| 2390 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2391 | bool ComplexMode = false; |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2392 | switch (Str.size()) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2393 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2394 | switch (Str[0]) { |
| 2395 | case 'Q': DestWidth = 8; break; |
| 2396 | case 'H': DestWidth = 16; break; |
| 2397 | case 'S': DestWidth = 32; break; |
| 2398 | case 'D': DestWidth = 64; break; |
| 2399 | case 'X': DestWidth = 96; break; |
| 2400 | case 'T': DestWidth = 128; break; |
| 2401 | } |
| 2402 | if (Str[1] == 'F') { |
| 2403 | IntegerMode = false; |
| 2404 | } else if (Str[1] == 'C') { |
| 2405 | IntegerMode = false; |
| 2406 | ComplexMode = true; |
| 2407 | } else if (Str[1] != 'I') { |
| 2408 | DestWidth = 0; |
| 2409 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2410 | break; |
| 2411 | case 4: |
| 2412 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 2413 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2414 | if (Str == "word") |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2415 | DestWidth = S.Context.Target.getPointerWidth(0); |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2416 | else if (Str == "byte") |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2417 | DestWidth = S.Context.Target.getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2418 | break; |
| 2419 | case 7: |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2420 | if (Str == "pointer") |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2421 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2422 | break; |
| 2423 | } |
| 2424 | |
| 2425 | QualType OldTy; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2426 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2427 | OldTy = TD->getUnderlyingType(); |
| 2428 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2429 | OldTy = VD->getType(); |
| 2430 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2431 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
| 2432 | << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc()); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2433 | return; |
| 2434 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2435 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2436 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2437 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 2438 | else if (IntegerMode) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2439 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2440 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2441 | } else if (ComplexMode) { |
| 2442 | if (!OldTy->isComplexType()) |
| 2443 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2444 | } else { |
| 2445 | if (!OldTy->isFloatingType()) |
| 2446 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2447 | } |
| 2448 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2449 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 2450 | // and friends, at least with glibc. |
| 2451 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 2452 | // width on unusual platforms. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2453 | // FIXME: Make sure floating-point mappings are accurate |
| 2454 | // FIXME: Support XF and TF types |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2455 | QualType NewTy; |
| 2456 | switch (DestWidth) { |
| 2457 | case 0: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2458 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2459 | return; |
| 2460 | default: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2461 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2462 | return; |
| 2463 | case 8: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2464 | if (!IntegerMode) { |
| 2465 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 2466 | return; |
| 2467 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2468 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2469 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2470 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2471 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2472 | break; |
| 2473 | case 16: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2474 | if (!IntegerMode) { |
| 2475 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 2476 | return; |
| 2477 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2478 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2479 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2480 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2481 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2482 | break; |
| 2483 | case 32: |
| 2484 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2485 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2486 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2487 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2488 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2489 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2490 | break; |
| 2491 | case 64: |
| 2492 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2493 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2494 | else if (OldTy->isSignedIntegerType()) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 2495 | if (S.Context.Target.getLongWidth() == 64) |
| 2496 | NewTy = S.Context.LongTy; |
| 2497 | else |
| 2498 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2499 | else |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 2500 | if (S.Context.Target.getLongWidth() == 64) |
| 2501 | NewTy = S.Context.UnsignedLongTy; |
| 2502 | else |
| 2503 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2504 | break; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2505 | case 96: |
| 2506 | NewTy = S.Context.LongDoubleTy; |
| 2507 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2508 | case 128: |
| 2509 | if (!IntegerMode) { |
| 2510 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 2511 | return; |
| 2512 | } |
Anders Carlsson | f5f7d86 | 2009-12-29 07:07:36 +0000 | [diff] [blame] | 2513 | if (OldTy->isSignedIntegerType()) |
| 2514 | NewTy = S.Context.Int128Ty; |
| 2515 | else |
| 2516 | NewTy = S.Context.UnsignedInt128Ty; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2517 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2520 | if (ComplexMode) { |
| 2521 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
| 2524 | // Install the new type. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2525 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 2526 | // FIXME: preserve existing source info. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2527 | TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy)); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 2528 | } else |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2529 | cast<ValueDecl>(D)->setType(NewTy); |
| 2530 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2531 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2532 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2533 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2534 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2535 | return; |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 2536 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2537 | if (!isFunctionOrMethod(D)) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2538 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2539 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2540 | return; |
| 2541 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2542 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2543 | D->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context)); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2544 | } |
| 2545 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2546 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2547 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2548 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2549 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2550 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2551 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2552 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2553 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2554 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2555 | return; |
| 2556 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2557 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2558 | D->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context)); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2561 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 2562 | const AttributeList &Attr) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2563 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2564 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2565 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2566 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2567 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2568 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2569 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2570 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2571 | return; |
| 2572 | } |
| 2573 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2574 | D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2575 | S.Context)); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2576 | } |
| 2577 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2578 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2579 | if (S.LangOpts.CUDA) { |
| 2580 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2581 | if (Attr.hasParameterOrArguments()) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2582 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2583 | return; |
| 2584 | } |
| 2585 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2586 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2587 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2588 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2589 | return; |
| 2590 | } |
| 2591 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2592 | D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2593 | } else { |
| 2594 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 2595 | } |
| 2596 | } |
| 2597 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2598 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2599 | if (S.LangOpts.CUDA) { |
| 2600 | // check the attribute arguments. |
| 2601 | if (Attr.getNumArgs() != 0) { |
| 2602 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2603 | return; |
| 2604 | } |
| 2605 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2606 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2607 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2608 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2609 | return; |
| 2610 | } |
| 2611 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2612 | D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2613 | } else { |
| 2614 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 2615 | } |
| 2616 | } |
| 2617 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2618 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2619 | if (S.LangOpts.CUDA) { |
| 2620 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2621 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2622 | return; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2623 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2624 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2625 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2626 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2627 | return; |
| 2628 | } |
| 2629 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2630 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 2631 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 2632 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 2633 | if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) { |
| 2634 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 2635 | << FD->getType() |
| 2636 | << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(), |
| 2637 | "void"); |
| 2638 | } else { |
| 2639 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 2640 | << FD->getType(); |
| 2641 | } |
| 2642 | return; |
| 2643 | } |
| 2644 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2645 | D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2646 | } else { |
| 2647 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 2648 | } |
| 2649 | } |
| 2650 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2651 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2652 | if (S.LangOpts.CUDA) { |
| 2653 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2654 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2655 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2656 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2657 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2658 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2659 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2660 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2661 | return; |
| 2662 | } |
| 2663 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2664 | D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2665 | } else { |
| 2666 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 2667 | } |
| 2668 | } |
| 2669 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2670 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2671 | if (S.LangOpts.CUDA) { |
| 2672 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2673 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2674 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2675 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2676 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2677 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2678 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2679 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2680 | return; |
| 2681 | } |
| 2682 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2683 | D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2684 | } else { |
| 2685 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 2686 | } |
| 2687 | } |
| 2688 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2689 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2690 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2691 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2692 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2693 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2694 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 2695 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2696 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2697 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2698 | return; |
| 2699 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2700 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 2701 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 2702 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 2703 | return; |
| 2704 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2705 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2706 | D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2709 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2710 | if (hasDeclarator(D)) return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2711 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2712 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2713 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 2714 | CallingConv CC; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2715 | if (S.CheckCallingConvAttr(Attr, CC)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2716 | return; |
| 2717 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2718 | if (!isa<ObjCMethodDecl>(D)) { |
| 2719 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2720 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2721 | return; |
| 2722 | } |
| 2723 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2724 | switch (Attr.getKind()) { |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2725 | case AttributeList::AT_fastcall: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2726 | D->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2727 | return; |
| 2728 | case AttributeList::AT_stdcall: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2729 | D->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2730 | return; |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2731 | case AttributeList::AT_thiscall: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2732 | D->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context)); |
Douglas Gregor | 04633eb | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 2733 | return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2734 | case AttributeList::AT_cdecl: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2735 | D->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2736 | return; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2737 | case AttributeList::AT_pascal: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2738 | D->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context)); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2739 | return; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2740 | case AttributeList::AT_pcs: { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2741 | Expr *Arg = Attr.getArg(0); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2742 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2743 | if (!Str || !Str->isAscii()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2744 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2745 | << "pcs" << 1; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2746 | Attr.setInvalid(); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2747 | return; |
| 2748 | } |
| 2749 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2750 | StringRef StrRef = Str->getString(); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2751 | PcsAttr::PCSType PCS; |
| 2752 | if (StrRef == "aapcs") |
| 2753 | PCS = PcsAttr::AAPCS; |
| 2754 | else if (StrRef == "aapcs-vfp") |
| 2755 | PCS = PcsAttr::AAPCS_VFP; |
| 2756 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2757 | S.Diag(Attr.getLoc(), diag::err_invalid_pcs); |
| 2758 | Attr.setInvalid(); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2759 | return; |
| 2760 | } |
| 2761 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2762 | D->addAttr(::new (S.Context) PcsAttr(Attr.getLoc(), S.Context, PCS)); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2763 | } |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2764 | default: |
| 2765 | llvm_unreachable("unexpected attribute kind"); |
| 2766 | return; |
| 2767 | } |
| 2768 | } |
| 2769 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2770 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 2771 | assert(!Attr.isInvalid()); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2772 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context)); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2773 | } |
| 2774 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2775 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) { |
| 2776 | if (attr.isInvalid()) |
| 2777 | return true; |
| 2778 | |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2779 | if ((attr.getNumArgs() != 0 && |
| 2780 | !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) || |
| 2781 | attr.getParameterName()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2782 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2783 | attr.setInvalid(); |
| 2784 | return true; |
| 2785 | } |
| 2786 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2787 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 2788 | // move to TargetAttributesSema one day. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2789 | switch (attr.getKind()) { |
| 2790 | case AttributeList::AT_cdecl: CC = CC_C; break; |
| 2791 | case AttributeList::AT_fastcall: CC = CC_X86FastCall; break; |
| 2792 | case AttributeList::AT_stdcall: CC = CC_X86StdCall; break; |
| 2793 | case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break; |
| 2794 | case AttributeList::AT_pascal: CC = CC_X86Pascal; break; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2795 | case AttributeList::AT_pcs: { |
| 2796 | Expr *Arg = attr.getArg(0); |
| 2797 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2798 | if (!Str || !Str->isAscii()) { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2799 | Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 2800 | << "pcs" << 1; |
| 2801 | attr.setInvalid(); |
| 2802 | return true; |
| 2803 | } |
| 2804 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2805 | StringRef StrRef = Str->getString(); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2806 | if (StrRef == "aapcs") { |
| 2807 | CC = CC_AAPCS; |
| 2808 | break; |
| 2809 | } else if (StrRef == "aapcs-vfp") { |
| 2810 | CC = CC_AAPCS_VFP; |
| 2811 | break; |
| 2812 | } |
| 2813 | // FALLS THROUGH |
| 2814 | } |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2815 | default: llvm_unreachable("unexpected attribute kind"); return true; |
| 2816 | } |
| 2817 | |
| 2818 | return false; |
| 2819 | } |
| 2820 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2821 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2822 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2823 | |
| 2824 | unsigned numParams; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2825 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2826 | return; |
| 2827 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2828 | if (!isa<ObjCMethodDecl>(D)) { |
| 2829 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2830 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 2831 | return; |
| 2832 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2833 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2834 | D->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context, numParams)); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
| 2837 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 2838 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2839 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 2840 | if (Attr.isInvalid()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2841 | return true; |
| 2842 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2843 | if (Attr.getNumArgs() != 1) { |
| 2844 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2845 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2846 | return true; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 2847 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2848 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2849 | Expr *NumParamsExpr = Attr.getArg(0); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2850 | llvm::APSInt NumParams(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2851 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2852 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2853 | Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2854 | << "regparm" << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2855 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2856 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2859 | if (Context.Target.getRegParmMax() == 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2860 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2861 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2862 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2863 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2866 | numParams = NumParams.getZExtValue(); |
| 2867 | if (numParams > Context.Target.getRegParmMax()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2868 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2869 | << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2870 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2871 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2874 | return false; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2877 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2878 | if (S.LangOpts.CUDA) { |
| 2879 | // check the attribute arguments. |
| 2880 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2881 | // FIXME: 0 is not okay. |
| 2882 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2883 | return; |
| 2884 | } |
| 2885 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2886 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2887 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2888 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2889 | return; |
| 2890 | } |
| 2891 | |
| 2892 | Expr *MaxThreadsExpr = Attr.getArg(0); |
| 2893 | llvm::APSInt MaxThreads(32); |
| 2894 | if (MaxThreadsExpr->isTypeDependent() || |
| 2895 | MaxThreadsExpr->isValueDependent() || |
| 2896 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
| 2897 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 2898 | << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange(); |
| 2899 | return; |
| 2900 | } |
| 2901 | |
| 2902 | llvm::APSInt MinBlocks(32); |
| 2903 | if (Attr.getNumArgs() > 1) { |
| 2904 | Expr *MinBlocksExpr = Attr.getArg(1); |
| 2905 | if (MinBlocksExpr->isTypeDependent() || |
| 2906 | MinBlocksExpr->isValueDependent() || |
| 2907 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
| 2908 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 2909 | << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange(); |
| 2910 | return; |
| 2911 | } |
| 2912 | } |
| 2913 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2914 | D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context, |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2915 | MaxThreads.getZExtValue(), |
| 2916 | MinBlocks.getZExtValue())); |
| 2917 | } else { |
| 2918 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 2919 | } |
| 2920 | } |
| 2921 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2922 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2923 | // Checker-specific attribute handlers. |
| 2924 | //===----------------------------------------------------------------------===// |
| 2925 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2926 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
| 2927 | return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type); |
| 2928 | } |
| 2929 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
| 2930 | return type->isPointerType() || isValidSubjectOfNSAttribute(S, type); |
| 2931 | } |
| 2932 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2933 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2934 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2935 | if (!param) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2936 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
| 2937 | << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedParameter; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2938 | return; |
| 2939 | } |
| 2940 | |
| 2941 | bool typeOK, cf; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2942 | if (Attr.getKind() == AttributeList::AT_ns_consumed) { |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2943 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 2944 | cf = false; |
| 2945 | } else { |
| 2946 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 2947 | cf = true; |
| 2948 | } |
| 2949 | |
| 2950 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2951 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
| 2952 | << SourceRange(Attr.getLoc()) << Attr.getName() << cf; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2953 | return; |
| 2954 | } |
| 2955 | |
| 2956 | if (cf) |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2957 | param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getLoc(), S.Context)); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2958 | else |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2959 | param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getLoc(), S.Context)); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2960 | } |
| 2961 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2962 | static void handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 2963 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2964 | if (!isa<ObjCMethodDecl>(D)) { |
| 2965 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
| 2966 | << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedMethod; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2967 | return; |
| 2968 | } |
| 2969 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2970 | D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getLoc(), S.Context)); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2973 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 2974 | const AttributeList &Attr) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2975 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2976 | QualType returnType; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2977 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2978 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2979 | returnType = MD->getResultType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2980 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
Fariborz Jahanian | 831fb96 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 2981 | returnType = PD->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2982 | else if (S.getLangOptions().ObjCAutoRefCount && hasDeclarator(D) && |
| 2983 | (Attr.getKind() == AttributeList::AT_ns_returns_retained)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2984 | return; // ignore: was handled as a type attribute |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2985 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2986 | returnType = FD->getResultType(); |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 2987 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2988 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
| 2989 | << SourceRange(Attr.getLoc()) << Attr.getName() |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2990 | << ExpectedFunctionOrMethod; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2991 | return; |
| 2992 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2993 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2994 | bool typeOK; |
| 2995 | bool cf; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2996 | switch (Attr.getKind()) { |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2997 | default: llvm_unreachable("invalid ownership attribute"); return; |
| 2998 | case AttributeList::AT_ns_returns_autoreleased: |
| 2999 | case AttributeList::AT_ns_returns_retained: |
| 3000 | case AttributeList::AT_ns_returns_not_retained: |
| 3001 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 3002 | cf = false; |
| 3003 | break; |
| 3004 | |
| 3005 | case AttributeList::AT_cf_returns_retained: |
| 3006 | case AttributeList::AT_cf_returns_not_retained: |
| 3007 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 3008 | cf = true; |
| 3009 | break; |
| 3010 | } |
| 3011 | |
| 3012 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3013 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 3014 | << SourceRange(Attr.getLoc()) |
| 3015 | << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3016 | return; |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3017 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3018 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3019 | switch (Attr.getKind()) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3020 | default: |
| 3021 | assert(0 && "invalid ownership attribute"); |
| 3022 | return; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3023 | case AttributeList::AT_ns_returns_autoreleased: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3024 | D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getLoc(), |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3025 | S.Context)); |
| 3026 | return; |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3027 | case AttributeList::AT_cf_returns_not_retained: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3028 | D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3029 | S.Context)); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3030 | return; |
| 3031 | case AttributeList::AT_ns_returns_not_retained: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3032 | D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3033 | S.Context)); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3034 | return; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3035 | case AttributeList::AT_cf_returns_retained: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3036 | D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3037 | S.Context)); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3038 | return; |
| 3039 | case AttributeList::AT_ns_returns_retained: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3040 | D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3041 | S.Context)); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3042 | return; |
| 3043 | }; |
| 3044 | } |
| 3045 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3046 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 3047 | const AttributeList &attr) { |
| 3048 | SourceLocation loc = attr.getLoc(); |
| 3049 | |
| 3050 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 3051 | |
| 3052 | if (!isa<ObjCMethodDecl>(method)) { |
| 3053 | S.Diag(method->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 3054 | << SourceRange(loc, loc) << attr.getName() << 13 /* methods */; |
| 3055 | return; |
| 3056 | } |
| 3057 | |
| 3058 | // Check that the method returns a normal pointer. |
| 3059 | QualType resultType = method->getResultType(); |
| 3060 | if (!resultType->isPointerType() || resultType->isObjCRetainableType()) { |
| 3061 | S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 3062 | << SourceRange(loc) |
| 3063 | << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2; |
| 3064 | |
| 3065 | // Drop the attribute. |
| 3066 | return; |
| 3067 | } |
| 3068 | |
| 3069 | method->addAttr( |
| 3070 | ::new (S.Context) ObjCReturnsInnerPointerAttr(loc, S.Context)); |
| 3071 | } |
| 3072 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3073 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 3074 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3075 | if (hasDeclarator(D)) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3076 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3077 | SourceLocation L = Attr.getLoc(); |
| 3078 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 3079 | << SourceRange(L, L) << Attr.getName() << 12 /* variable */; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3082 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 3083 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3084 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
| 3085 | SourceLocation L = Attr.getLoc(); |
| 3086 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 3087 | << SourceRange(L, L) << Attr.getName() << 12 /* variable */; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3088 | return; |
| 3089 | } |
| 3090 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3091 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3092 | QualType type = vd->getType(); |
| 3093 | |
| 3094 | if (!type->isDependentType() && |
| 3095 | !type->isObjCLifetimeType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3096 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3097 | << type; |
| 3098 | return; |
| 3099 | } |
| 3100 | |
| 3101 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 3102 | |
| 3103 | // If we have no lifetime yet, check the lifetime we're presumably |
| 3104 | // going to infer. |
| 3105 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 3106 | lifetime = type->getObjCARCImplicitLifetime(); |
| 3107 | |
| 3108 | switch (lifetime) { |
| 3109 | case Qualifiers::OCL_None: |
| 3110 | assert(type->isDependentType() && |
| 3111 | "didn't infer lifetime for non-dependent type?"); |
| 3112 | break; |
| 3113 | |
| 3114 | case Qualifiers::OCL_Weak: // meaningful |
| 3115 | case Qualifiers::OCL_Strong: // meaningful |
| 3116 | break; |
| 3117 | |
| 3118 | case Qualifiers::OCL_ExplicitNone: |
| 3119 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3120 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3121 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 3122 | break; |
| 3123 | } |
| 3124 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3125 | D->addAttr(::new (S.Context) |
| 3126 | ObjCPreciseLifetimeAttr(Attr.getLoc(), S.Context)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3127 | } |
| 3128 | |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 3129 | static bool isKnownDeclSpecAttr(const AttributeList &Attr) { |
| 3130 | return Attr.getKind() == AttributeList::AT_dllimport || |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3131 | Attr.getKind() == AttributeList::AT_dllexport || |
| 3132 | Attr.getKind() == AttributeList::AT_uuid; |
| 3133 | } |
| 3134 | |
| 3135 | //===----------------------------------------------------------------------===// |
| 3136 | // Microsoft specific attribute handlers. |
| 3137 | //===----------------------------------------------------------------------===// |
| 3138 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3139 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3140 | if (S.LangOpts.Microsoft || S.LangOpts.Borland) { |
| 3141 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3142 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3143 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3144 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3145 | Expr *Arg = Attr.getArg(0); |
| 3146 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3147 | if (!Str || !Str->isAscii()) { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3148 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 3149 | << "uuid" << 1; |
| 3150 | return; |
| 3151 | } |
| 3152 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3153 | StringRef StrRef = Str->getString(); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3154 | |
| 3155 | bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' && |
| 3156 | StrRef.back() == '}'; |
| 3157 | |
| 3158 | // Validate GUID length. |
| 3159 | if (IsCurly && StrRef.size() != 38) { |
| 3160 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 3161 | return; |
| 3162 | } |
| 3163 | if (!IsCurly && StrRef.size() != 36) { |
| 3164 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 3165 | return; |
| 3166 | } |
| 3167 | |
| 3168 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 3169 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3170 | StringRef::iterator I = StrRef.begin(); |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 3171 | if (IsCurly) // Skip the optional '{' |
| 3172 | ++I; |
| 3173 | |
| 3174 | for (int i = 0; i < 36; ++i) { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3175 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 3176 | if (*I != '-') { |
| 3177 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 3178 | return; |
| 3179 | } |
| 3180 | } else if (!isxdigit(*I)) { |
| 3181 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 3182 | return; |
| 3183 | } |
| 3184 | I++; |
| 3185 | } |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3186 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3187 | D->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context, |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3188 | Str->getString())); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3189 | } else |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3190 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid"; |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3193 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3194 | // Top Level Sema Entry Points |
| 3195 | //===----------------------------------------------------------------------===// |
| 3196 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3197 | static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 3198 | const AttributeList &Attr) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3199 | switch (Attr.getKind()) { |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3200 | case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break; |
| 3201 | case AttributeList::AT_host: handleHostAttr (S, D, Attr); break; |
| 3202 | case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break; |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3203 | default: |
| 3204 | break; |
| 3205 | } |
| 3206 | } |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3207 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3208 | static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 3209 | const AttributeList &Attr) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3210 | switch (Attr.getKind()) { |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3211 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 3212 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 3213 | case AttributeList::AT_IBOutletCollection: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3214 | handleIBOutletCollection(S, D, Attr); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3215 | case AttributeList::AT_address_space: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3216 | case AttributeList::AT_opencl_image_access: |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 3217 | case AttributeList::AT_objc_gc: |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3218 | case AttributeList::AT_vector_size: |
Bob Wilson | 4211bb6 | 2010-11-16 00:32:24 +0000 | [diff] [blame] | 3219 | case AttributeList::AT_neon_vector_type: |
| 3220 | case AttributeList::AT_neon_polyvector_type: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3221 | // Ignore these, these are type attributes, handled by |
| 3222 | // ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3223 | break; |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3224 | case AttributeList::AT_device: |
| 3225 | case AttributeList::AT_host: |
| 3226 | case AttributeList::AT_overloadable: |
| 3227 | // Ignore, this is a non-inheritable attribute, handled |
| 3228 | // by ProcessNonInheritableDeclAttr. |
| 3229 | break; |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3230 | case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break; |
| 3231 | case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3232 | case AttributeList::AT_always_inline: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3233 | handleAlwaysInlineAttr (S, D, Attr); break; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 3234 | case AttributeList::AT_analyzer_noreturn: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3235 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
| 3236 | case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break; |
| 3237 | case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3238 | case AttributeList::AT_carries_dependency: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3239 | handleDependencyAttr (S, D, Attr); break; |
| 3240 | case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break; |
| 3241 | case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break; |
| 3242 | case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break; |
| 3243 | case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break; |
| 3244 | case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3245 | case AttributeList::AT_ext_vector_type: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3246 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3247 | break; |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3248 | case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break; |
| 3249 | case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break; |
| 3250 | case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break; |
| 3251 | case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3252 | case AttributeList::AT_launch_bounds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3253 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3254 | break; |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3255 | case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break; |
| 3256 | case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break; |
| 3257 | case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break; |
| 3258 | case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break; |
| 3259 | case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 3260 | case AttributeList::AT_ownership_returns: |
| 3261 | case AttributeList::AT_ownership_takes: |
| 3262 | case AttributeList::AT_ownership_holds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3263 | handleOwnershipAttr (S, D, Attr); break; |
| 3264 | case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break; |
| 3265 | case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break; |
| 3266 | case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break; |
| 3267 | case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break; |
| 3268 | case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3269 | |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 3270 | case AttributeList::AT_objc_ownership: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3271 | handleObjCOwnershipAttr(S, D, Attr); break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3272 | case AttributeList::AT_objc_precise_lifetime: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3273 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3274 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3275 | case AttributeList::AT_objc_returns_inner_pointer: |
| 3276 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 3277 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3278 | // Checker-specific. |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3279 | case AttributeList::AT_cf_consumed: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3280 | case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3281 | case AttributeList::AT_ns_consumes_self: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3282 | handleNSConsumesSelfAttr(S, D, Attr); break; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3283 | |
| 3284 | case AttributeList::AT_ns_returns_autoreleased: |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3285 | case AttributeList::AT_ns_returns_not_retained: |
| 3286 | case AttributeList::AT_cf_returns_not_retained: |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3287 | case AttributeList::AT_ns_returns_retained: |
| 3288 | case AttributeList::AT_cf_returns_retained: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3289 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3290 | |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 3291 | case AttributeList::AT_reqd_wg_size: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3292 | handleReqdWorkGroupSize(S, D, Attr); break; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 3293 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3294 | case AttributeList::AT_init_priority: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3295 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3296 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3297 | case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break; |
| 3298 | case AttributeList::AT_MsStruct: handleMsStructAttr (S, D, Attr); break; |
| 3299 | case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break; |
| 3300 | case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break; |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 3301 | case AttributeList::AT_arc_weakref_unavailable: |
| 3302 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 3303 | break; |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3304 | case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break; |
| 3305 | case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break; |
| 3306 | case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break; |
| 3307 | case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 3308 | break; |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3309 | case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break; |
| 3310 | case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break; |
| 3311 | case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3312 | case AttributeList::AT_transparent_union: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3313 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3314 | break; |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 3315 | case AttributeList::AT_objc_exception: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3316 | handleObjCExceptionAttr(S, D, Attr); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 3317 | break; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 3318 | case AttributeList::AT_objc_method_family: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3319 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 3320 | break; |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3321 | case AttributeList::AT_nsobject: handleObjCNSObject (S, D, Attr); break; |
| 3322 | case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break; |
| 3323 | case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break; |
| 3324 | case AttributeList::AT_const: handleConstAttr (S, D, Attr); break; |
| 3325 | case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break; |
| 3326 | case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break; |
| 3327 | case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break; |
| 3328 | case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break; |
| 3329 | case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3330 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 3331 | // Just ignore |
| 3332 | break; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3333 | case AttributeList::AT_no_instrument_function: // Interacts with -pg. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3334 | handleNoInstrumentFunctionAttr(S, D, Attr); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3335 | break; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3336 | case AttributeList::AT_stdcall: |
| 3337 | case AttributeList::AT_cdecl: |
| 3338 | case AttributeList::AT_fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3339 | case AttributeList::AT_thiscall: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3340 | case AttributeList::AT_pascal: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3341 | case AttributeList::AT_pcs: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3342 | handleCallConvAttr(S, D, Attr); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3343 | break; |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3344 | case AttributeList::AT_opencl_kernel_function: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3345 | handleOpenCLKernelAttr(S, D, Attr); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3346 | break; |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3347 | case AttributeList::AT_uuid: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3348 | handleUuidAttr(S, D, Attr); |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3349 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 3350 | |
| 3351 | // Thread safety attributes: |
| 3352 | case AttributeList::AT_guarded_var: |
| 3353 | handleGuardedVarAttr(S, D, Attr); |
| 3354 | break; |
| 3355 | case AttributeList::AT_pt_guarded_var: |
| 3356 | handleGuardedVarAttr(S, D, Attr, /*pointer = */true); |
| 3357 | break; |
| 3358 | case AttributeList::AT_scoped_lockable: |
| 3359 | handleLockableAttr(S, D, Attr, /*scoped = */true); |
| 3360 | break; |
| 3361 | case AttributeList::AT_no_thread_safety_analysis: |
| 3362 | handleNoThreadSafetyAttr(S, D, Attr); |
| 3363 | break; |
| 3364 | case AttributeList::AT_lockable: |
| 3365 | handleLockableAttr(S, D, Attr); |
| 3366 | break; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 3367 | case AttributeList::AT_guarded_by: |
| 3368 | handleGuardedByAttr(S, D, Attr); |
| 3369 | break; |
| 3370 | case AttributeList::AT_pt_guarded_by: |
| 3371 | handleGuardedByAttr(S, D, Attr, /*pointer = */true); |
| 3372 | break; |
| 3373 | case AttributeList::AT_exclusive_lock_function: |
| 3374 | handleLockFunAttr(S, D, Attr, /*exclusive = */true); |
| 3375 | break; |
| 3376 | case AttributeList::AT_exclusive_locks_required: |
| 3377 | handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true); |
| 3378 | break; |
| 3379 | case AttributeList::AT_exclusive_trylock_function: |
| 3380 | handleTrylockFunAttr(S, D, Attr, /*exclusive = */true); |
| 3381 | break; |
| 3382 | case AttributeList::AT_lock_returned: |
| 3383 | handleLockReturnedAttr(S, D, Attr); |
| 3384 | break; |
| 3385 | case AttributeList::AT_locks_excluded: |
| 3386 | handleLocksExcludedAttr(S, D, Attr); |
| 3387 | break; |
| 3388 | case AttributeList::AT_shared_lock_function: |
| 3389 | handleLockFunAttr(S, D, Attr); |
| 3390 | break; |
| 3391 | case AttributeList::AT_shared_locks_required: |
| 3392 | handleLocksRequiredAttr(S, D, Attr); |
| 3393 | break; |
| 3394 | case AttributeList::AT_shared_trylock_function: |
| 3395 | handleTrylockFunAttr(S, D, Attr); |
| 3396 | break; |
| 3397 | case AttributeList::AT_unlock_function: |
| 3398 | handleUnlockFunAttr(S, D, Attr); |
| 3399 | break; |
| 3400 | case AttributeList::AT_acquired_before: |
| 3401 | handleAcquireOrderAttr(S, D, Attr, /*before = */true); |
| 3402 | break; |
| 3403 | case AttributeList::AT_acquired_after: |
| 3404 | handleAcquireOrderAttr(S, D, Attr, /*before = */false); |
| 3405 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 3406 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3407 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3408 | // Ask target about the attribute. |
| 3409 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 3410 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Chandler Carruth | 7d5c45e | 2010-07-08 09:42:26 +0000 | [diff] [blame] | 3411 | S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored) |
| 3412 | << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3413 | break; |
| 3414 | } |
| 3415 | } |
| 3416 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3417 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 3418 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 3419 | /// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to |
| 3420 | /// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4). |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3421 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 3422 | const AttributeList &Attr, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3423 | bool NonInheritable, bool Inheritable) { |
| 3424 | if (Attr.isInvalid()) |
| 3425 | return; |
| 3426 | |
| 3427 | if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr)) |
| 3428 | // FIXME: Try to deal with other __declspec attributes! |
| 3429 | return; |
| 3430 | |
| 3431 | if (NonInheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3432 | ProcessNonInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3433 | |
| 3434 | if (Inheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3435 | ProcessInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3438 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 3439 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3440 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3441 | const AttributeList *AttrList, |
| 3442 | bool NonInheritable, bool Inheritable) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 3443 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3444 | ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 3445 | } |
| 3446 | |
| 3447 | // GCC accepts |
| 3448 | // static int a9 __attribute__((weakref)); |
| 3449 | // but that looks really pointless. We reject it. |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3450 | if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 3451 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 3452 | dyn_cast<NamedDecl>(D)->getNameAsString(); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 3453 | return; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3454 | } |
| 3455 | } |
| 3456 | |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3457 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
| 3458 | /// #pragma weak needs a non-definition decl and source may not have one |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3459 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) { |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 3460 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3461 | NamedDecl *NewD = 0; |
| 3462 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 3463 | NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3464 | FD->getInnerLocStart(), |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3465 | FD->getLocation(), DeclarationName(II), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3466 | FD->getType(), FD->getTypeSourceInfo()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3467 | if (FD->getQualifier()) { |
| 3468 | FunctionDecl *NewFD = cast<FunctionDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3469 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3470 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3471 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 3472 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3473 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3474 | VD->getType(), VD->getTypeSourceInfo(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3475 | VD->getStorageClass(), |
| 3476 | VD->getStorageClassAsWritten()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3477 | if (VD->getQualifier()) { |
| 3478 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3479 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3480 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3481 | } |
| 3482 | return NewD; |
| 3483 | } |
| 3484 | |
| 3485 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak |
| 3486 | /// applied to it, possibly with an alias. |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 3487 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 3488 | if (W.getUsed()) return; // only do this once |
| 3489 | W.setUsed(true); |
| 3490 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 3491 | IdentifierInfo *NDId = ND->getIdentifier(); |
| 3492 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3493 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 3494 | NDId->getName())); |
| 3495 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 3496 | WeakTopLevelDecl.push_back(NewD); |
| 3497 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 3498 | // to insert Decl at TU scope, sorry. |
| 3499 | DeclContext *SavedContext = CurContext; |
| 3500 | CurContext = Context.getTranslationUnitDecl(); |
| 3501 | PushOnScopeChains(NewD, S); |
| 3502 | CurContext = SavedContext; |
| 3503 | } else { // just add weak to existing |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3504 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3505 | } |
| 3506 | } |
| 3507 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3508 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 3509 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 3510 | /// specified in many different places, and we need to find and apply them all. |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3511 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD, |
| 3512 | bool NonInheritable, bool Inheritable) { |
John McCall | d4aff0e | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 3513 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 3514 | // have to do this. |
Douglas Gregor | 31e37b2 | 2011-07-28 18:09:57 +0000 | [diff] [blame] | 3515 | if (Inheritable) { |
| 3516 | LoadExternalWeakUndeclaredIdentifiers(); |
| 3517 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 3518 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
| 3519 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 3520 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 3521 | = WeakUndeclaredIdentifiers.find(Id); |
| 3522 | if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) { |
| 3523 | WeakInfo W = I->second; |
| 3524 | DeclApplyPragmaWeak(S, ND, W); |
| 3525 | WeakUndeclaredIdentifiers[Id] = W; |
| 3526 | } |
John McCall | d4aff0e | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 3527 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3528 | } |
| 3529 | } |
| 3530 | } |
| 3531 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3532 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3533 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3534 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3535 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3536 | // Walk the declarator structure, applying decl attributes that were in a type |
| 3537 | // position to the decl itself. This handles cases like: |
| 3538 | // int *__attr__(x)** D; |
| 3539 | // when X is a decl attribute. |
| 3540 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 3541 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3542 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3543 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3544 | // Finally, apply any attributes on the decl itself. |
| 3545 | if (const AttributeList *Attrs = PD.getAttributes()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3546 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3547 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3548 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3549 | /// Is the given declaration allowed to use a forbidden type? |
| 3550 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 3551 | // Private ivars are always okay. Unfortunately, people don't |
| 3552 | // always properly make their ivars private, even in system headers. |
| 3553 | // Plus we need to make fields okay, too. |
| 3554 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl)) |
| 3555 | return false; |
| 3556 | |
| 3557 | // Require it to be declared in a system header. |
| 3558 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 3559 | } |
| 3560 | |
| 3561 | /// Handle a delayed forbidden-type diagnostic. |
| 3562 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 3563 | Decl *decl) { |
| 3564 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 3565 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 3566 | "this system declaration uses an unsupported type")); |
| 3567 | return; |
| 3568 | } |
| 3569 | |
| 3570 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 3571 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 3572 | diag.Triggered = true; |
| 3573 | } |
| 3574 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3575 | // This duplicates a vector push_back but hides the need to know the |
| 3576 | // size of the type. |
| 3577 | void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) { |
| 3578 | assert(StackSize <= StackCapacity); |
| 3579 | |
| 3580 | // Grow the stack if necessary. |
| 3581 | if (StackSize == StackCapacity) { |
| 3582 | unsigned newCapacity = 2 * StackCapacity + 2; |
| 3583 | char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)]; |
| 3584 | const char *oldBuffer = (const char*) Stack; |
| 3585 | |
| 3586 | if (StackCapacity) |
| 3587 | memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic)); |
| 3588 | |
| 3589 | delete[] oldBuffer; |
| 3590 | Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer); |
| 3591 | StackCapacity = newCapacity; |
| 3592 | } |
| 3593 | |
| 3594 | assert(StackSize < StackCapacity); |
| 3595 | new (&Stack[StackSize++]) DelayedDiagnostic(diag); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3596 | } |
| 3597 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3598 | void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state, |
| 3599 | Decl *decl) { |
| 3600 | DelayedDiagnostics &DD = S.DelayedDiagnostics; |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3601 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3602 | // Check the invariants. |
| 3603 | assert(DD.StackSize >= state.SavedStackSize); |
| 3604 | assert(state.SavedStackSize >= DD.ActiveStackBase); |
| 3605 | assert(DD.ParsingDepth > 0); |
| 3606 | |
| 3607 | // Drop the parsing depth. |
| 3608 | DD.ParsingDepth--; |
| 3609 | |
| 3610 | // If there are no active diagnostics, we're done. |
| 3611 | if (DD.StackSize == DD.ActiveStackBase) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3612 | return; |
| 3613 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3614 | // We only want to actually emit delayed diagnostics when we |
| 3615 | // successfully parsed a decl. |
Argyrios Kyrtzidis | a7bf7bb | 2011-06-24 19:59:27 +0000 | [diff] [blame] | 3616 | if (decl && !decl->isInvalidDecl()) { |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3617 | // We emit all the active diagnostics, not just those starting |
| 3618 | // from the saved state. The idea is this: we get one push for a |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3619 | // decl spec and another for each declarator; in a decl group like: |
| 3620 | // deprecated_typedef foo, *bar, baz(); |
| 3621 | // only the declarator pops will be passed decls. This is correct; |
| 3622 | // we really do need to consider delayed diagnostics from the decl spec |
| 3623 | // for each of the different declarations. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3624 | for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) { |
| 3625 | DelayedDiagnostic &diag = DD.Stack[i]; |
| 3626 | if (diag.Triggered) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3627 | continue; |
| 3628 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3629 | switch (diag.Kind) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3630 | case DelayedDiagnostic::Deprecation: |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3631 | S.HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3632 | break; |
| 3633 | |
| 3634 | case DelayedDiagnostic::Access: |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3635 | S.HandleDelayedAccessCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3636 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3637 | |
| 3638 | case DelayedDiagnostic::ForbiddenType: |
| 3639 | handleDelayedForbiddenType(S, diag, decl); |
| 3640 | break; |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3641 | } |
| 3642 | } |
| 3643 | } |
| 3644 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3645 | // Destroy all the delayed diagnostics we're about to pop off. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3646 | for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i) |
Douglas Gregor | 2923380 | 2011-03-23 15:13:44 +0000 | [diff] [blame] | 3647 | DD.Stack[i].Destroy(); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3648 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3649 | DD.StackSize = state.SavedStackSize; |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3650 | } |
| 3651 | |
| 3652 | static bool isDeclDeprecated(Decl *D) { |
| 3653 | do { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 3654 | if (D->isDeprecated()) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3655 | return true; |
| 3656 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 3657 | return false; |
| 3658 | } |
| 3659 | |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 3660 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3661 | Decl *Ctx) { |
| 3662 | if (isDeclDeprecated(Ctx)) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3663 | return; |
| 3664 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3665 | DD.Triggered = true; |
Benjamin Kramer | ce2d186 | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3666 | if (!DD.getDeprecationMessage().empty()) |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 3667 | Diag(DD.Loc, diag::warn_deprecated_message) |
Benjamin Kramer | ce2d186 | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3668 | << DD.getDeprecationDecl()->getDeclName() |
| 3669 | << DD.getDeprecationMessage(); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 3670 | else |
| 3671 | Diag(DD.Loc, diag::warn_deprecated) |
Benjamin Kramer | ce2d186 | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3672 | << DD.getDeprecationDecl()->getDeclName(); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3673 | } |
| 3674 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3675 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 3676 | SourceLocation Loc, |
Fariborz Jahanian | 89ebaed | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 3677 | const ObjCInterfaceDecl *UnknownObjCClass) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3678 | // Delay if we're currently parsing a declaration. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3679 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
| 3680 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message)); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3681 | return; |
| 3682 | } |
| 3683 | |
| 3684 | // Otherwise, don't warn if our current context is deprecated. |
| 3685 | if (isDeclDeprecated(cast<Decl>(CurContext))) |
| 3686 | return; |
Benjamin Kramer | ce2d186 | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3687 | if (!Message.empty()) |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 3688 | Diag(Loc, diag::warn_deprecated_message) << D->getDeclName() |
| 3689 | << Message; |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 3690 | else { |
Peter Collingbourne | 743b82b | 2011-01-02 19:53:12 +0000 | [diff] [blame] | 3691 | if (!UnknownObjCClass) |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 3692 | Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
Fariborz Jahanian | 89ebaed | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 3693 | else { |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 3694 | Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName(); |
Fariborz Jahanian | 89ebaed | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 3695 | Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 3696 | } |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 3697 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3698 | } |