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" |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | acc5f3e | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
| 21 | #include "clang/AST/Expr.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 24 | #include "clang/Sema/DeclSpec.h" |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 25 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 26 | #include "clang/Sema/Lookup.h" |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 28 | using namespace clang; |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 29 | using namespace sema; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 30 | |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 31 | /// These constants match the enumerated choices of |
| 32 | /// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 33 | enum AttributeDeclKind { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 34 | ExpectedFunction, |
| 35 | ExpectedUnion, |
| 36 | ExpectedVariableOrFunction, |
| 37 | ExpectedFunctionOrMethod, |
| 38 | ExpectedParameter, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 39 | ExpectedFunctionMethodOrBlock, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 40 | ExpectedFunctionMethodOrParameter, |
| 41 | ExpectedClass, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 42 | ExpectedVariable, |
| 43 | ExpectedMethod, |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 44 | ExpectedVariableFunctionOrLabel, |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 45 | ExpectedFieldOrGlobalVar, |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 46 | ExpectedStruct, |
| 47 | ExpectedTLSVar |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 50 | //===----------------------------------------------------------------------===// |
| 51 | // Helper functions |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 54 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 55 | bool blocksToo = true) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 56 | QualType Ty; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 57 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 58 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 59 | else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 60 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 61 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 62 | Ty = decl->getUnderlyingType(); |
| 63 | else |
| 64 | return 0; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 66 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 67 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 755f9d2 | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 68 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 69 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 70 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 71 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 74 | // FIXME: We should provide an abstraction around a method or function |
| 75 | // to provide the following bits of information. |
| 76 | |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 77 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 78 | /// type (function or function-typed variable). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 79 | static bool isFunction(const Decl *D) { |
| 80 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 84 | /// type (function or function-typed variable) or an Objective-C |
| 85 | /// method. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 86 | static bool isFunctionOrMethod(const Decl *D) { |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 87 | return isFunction(D) || isa<ObjCMethodDecl>(D); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 90 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 91 | /// type (function or function-typed variable) or an Objective-C |
| 92 | /// method or a block. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 93 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 94 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 95 | return true; |
| 96 | // check for block is more involved. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 97 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 98 | QualType Ty = V->getType(); |
| 99 | return Ty->isBlockPointerType(); |
| 100 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 101 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 102 | } |
| 103 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 104 | /// Return true if the given decl has a declarator that should have |
| 105 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 106 | static bool hasDeclarator(const Decl *D) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 107 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 108 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 109 | isa<ObjCPropertyDecl>(D); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 112 | /// hasFunctionProto - Return true if the given decl has a argument |
| 113 | /// information. This decl should have already passed |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 114 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 115 | static bool hasFunctionProto(const Decl *D) { |
| 116 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 117 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 118 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 119 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 125 | /// arguments. It is an error to call this on a K&R function (use |
| 126 | /// hasFunctionProto first). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 127 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 128 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 129 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 130 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 131 | return BD->getNumParams(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 132 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 135 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 136 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 137 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 138 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 139 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 140 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 141 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 144 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 145 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 146 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 147 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 150 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 151 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 152 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 153 | return proto->isVariadic(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 154 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | db9a0ae | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 155 | return BD->isVariadic(); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 156 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 157 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 161 | static bool isInstanceMethod(const Decl *D) { |
| 162 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 163 | return MethodDecl->isInstance(); |
| 164 | return false; |
| 165 | } |
| 166 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 167 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 168 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 169 | if (!PT) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 170 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 171 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 172 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 173 | if (!Cls) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 174 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 175 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 176 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 177 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 178 | // FIXME: Should we walk the chain of classes? |
| 179 | return ClsName == &Ctx.Idents.get("NSString") || |
| 180 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 181 | } |
| 182 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 183 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 184 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 185 | if (!PT) |
| 186 | return false; |
| 187 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 188 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 189 | if (!RT) |
| 190 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 191 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 192 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 193 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 194 | return false; |
| 195 | |
| 196 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 197 | } |
| 198 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 199 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 200 | /// output an error. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 201 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 202 | unsigned int Num) { |
| 203 | if (Attr.getNumArgs() != Num) { |
| 204 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num; |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | return true; |
| 209 | } |
| 210 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 211 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 212 | /// \brief Check if the attribute has at least as many args as Num. May |
| 213 | /// output an error. |
| 214 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
| 215 | unsigned int Num) { |
| 216 | if (Attr.getNumArgs() < Num) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 217 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| 218 | return false; |
| 219 | } |
| 220 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 221 | return true; |
| 222 | } |
| 223 | |
| 224 | /// |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 225 | /// \brief Check if passed in Decl is a field or potentially shared global var |
| 226 | /// \return true if the Decl is a field or potentially shared global variable |
| 227 | /// |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 228 | static bool mayBeSharedVariable(const Decl *D) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 229 | if (isa<FieldDecl>(D)) |
| 230 | return true; |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 231 | if (const VarDecl *vd = dyn_cast<VarDecl>(D)) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 232 | return (vd->hasGlobalStorage() && !(vd->isThreadSpecified())); |
| 233 | |
| 234 | return false; |
| 235 | } |
| 236 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 237 | /// \brief Check if the passed-in expression is of type int or bool. |
| 238 | static bool isIntOrBool(Expr *Exp) { |
| 239 | QualType QT = Exp->getType(); |
| 240 | return QT->isBooleanType() || QT->isIntegerType(); |
| 241 | } |
| 242 | |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 243 | |
| 244 | // Check to see if the type is a smart pointer of some kind. We assume |
| 245 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 246 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 247 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 248 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
| 249 | if (Res1.first == Res1.second) |
| 250 | return false; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 251 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 252 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 253 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
| 254 | if (Res2.first == Res2.second) |
| 255 | return false; |
| 256 | |
| 257 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 260 | /// \brief Check if passed in Decl is a pointer type. |
| 261 | /// Note that this function may produce an error message. |
| 262 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 263 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 264 | const AttributeList &Attr) { |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 265 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 266 | QualType QT = vd->getType(); |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 267 | if (QT->isAnyPointerType()) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 268 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 269 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 270 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 271 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 272 | // (We don't want to force template instantiation if we can avoid it, |
| 273 | // since that would alter the order in which templates are instantiated.) |
| 274 | if (RT->isIncompleteType()) |
| 275 | return true; |
| 276 | |
| 277 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 278 | return true; |
| 279 | } |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 280 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 281 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 282 | << Attr.getName()->getName() << QT; |
| 283 | } else { |
| 284 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 285 | << Attr.getName(); |
| 286 | } |
| 287 | return false; |
| 288 | } |
| 289 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 290 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 291 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 292 | static const RecordType *getRecordType(QualType QT) { |
| 293 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 294 | return RT; |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 295 | |
| 296 | // Now check if we point to record type. |
| 297 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 298 | return PT->getPointeeType()->getAs<RecordType>(); |
| 299 | |
| 300 | return 0; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 301 | } |
| 302 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 303 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 304 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 305 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 306 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
| 307 | if (RT->getDecl()->getAttr<LockableAttr>()) |
| 308 | return true; |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 313 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 314 | /// resolves to a lockable object. |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 315 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 316 | QualType Ty) { |
| 317 | const RecordType *RT = getRecordType(Ty); |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 318 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 319 | // Warn if could not get record type for this argument. |
Benjamin Kramer | d77ba89 | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 320 | if (!RT) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 321 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 322 | << Attr.getName() << Ty.getAsString(); |
| 323 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 324 | } |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 325 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 326 | // Don't check for lockable if the class hasn't been defined yet. |
DeLesley Hutchins | 634b293 | 2012-02-16 17:15:51 +0000 | [diff] [blame] | 327 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 328 | return; |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 329 | |
| 330 | // Allow smart pointers to be used as lockable objects. |
| 331 | // FIXME -- Check the type that the smart pointer points to. |
| 332 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 333 | return; |
| 334 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 335 | // Check if the type is lockable. |
| 336 | RecordDecl *RD = RT->getDecl(); |
| 337 | if (RD->getAttr<LockableAttr>()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 338 | return; |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 339 | |
| 340 | // Else check if any base classes are lockable. |
| 341 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 342 | CXXBasePaths BPaths(false, false); |
| 343 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 344 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 345 | } |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 346 | |
| 347 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 348 | << Attr.getName() << Ty.getAsString(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 351 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 352 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 353 | /// \param Sidx The attribute argument index to start checking with. |
| 354 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 355 | /// parameter list. |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 356 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 357 | const AttributeList &Attr, |
| 358 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 359 | int Sidx = 0, |
| 360 | bool ParamIdxOk = false) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 361 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 362 | Expr *ArgExp = Attr.getArg(Idx); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 363 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 364 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 365 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 366 | Args.push_back(ArgExp); |
| 367 | continue; |
| 368 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 369 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 370 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
| 371 | // Ignore empty strings without warnings |
| 372 | if (StrLit->getLength() == 0) |
| 373 | continue; |
| 374 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 375 | // We allow constant strings to be used as a placeholder for expressions |
| 376 | // that are not valid C++ syntax, but warn that they are ignored. |
| 377 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 378 | Attr.getName(); |
| 379 | continue; |
| 380 | } |
| 381 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 382 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 383 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 384 | // A pointer to member expression of the form &MyClass::mu is treated |
| 385 | // specially -- we need to look at the type of the member. |
| 386 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 387 | if (UOp->getOpcode() == UO_AddrOf) |
| 388 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 389 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 390 | ArgTy = DRE->getDecl()->getType(); |
| 391 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 392 | // First see if we can just cast to record type, or point to record type. |
| 393 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 394 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 395 | // Now check if we index into a record type function param. |
| 396 | if(!RT && ParamIdxOk) { |
| 397 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 398 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 399 | if(FD && IL) { |
| 400 | unsigned int NumParams = FD->getNumParams(); |
| 401 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 402 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 403 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 404 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 405 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 406 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 407 | continue; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 408 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 409 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 413 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 414 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 415 | Args.push_back(ArgExp); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 416 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 419 | //===----------------------------------------------------------------------===// |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 420 | // Attribute Implementations |
| 421 | //===----------------------------------------------------------------------===// |
| 422 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 423 | // FIXME: All this manual attribute parsing code is gross. At the |
| 424 | // least add some helper functions to check most argument patterns (# |
| 425 | // and types of args). |
| 426 | |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 427 | enum ThreadAttributeDeclKind { |
| 428 | ThreadExpectedFieldOrGlobalVar, |
| 429 | ThreadExpectedFunctionOrMethod, |
| 430 | ThreadExpectedClassOrStruct |
| 431 | }; |
| 432 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 433 | static bool checkGuardedVarAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 434 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 435 | assert(!Attr.isInvalid()); |
| 436 | |
| 437 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 438 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 439 | |
| 440 | // D must be either a member field or global (potentially shared) variable. |
| 441 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 442 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 443 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 444 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 447 | return true; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 450 | static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 451 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 452 | return; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 453 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 454 | D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context)); |
| 455 | } |
| 456 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 457 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 458 | const AttributeList &Attr) { |
| 459 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 460 | return; |
| 461 | |
| 462 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 463 | return; |
| 464 | |
| 465 | D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context)); |
| 466 | } |
| 467 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 468 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 469 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 470 | Expr* &Arg) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 471 | assert(!Attr.isInvalid()); |
| 472 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 473 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 474 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 475 | |
| 476 | // D must be either a member field or global (potentially shared) variable. |
| 477 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 478 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 479 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 480 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 481 | } |
| 482 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 483 | SmallVector<Expr*, 1> Args; |
| 484 | // check that all arguments are lockable objects |
| 485 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 486 | unsigned Size = Args.size(); |
| 487 | if (Size != 1) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 488 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 489 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 490 | Arg = Args[0]; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 491 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 492 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 495 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 496 | Expr *Arg = 0; |
| 497 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 498 | return; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 499 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 500 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| 501 | } |
| 502 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 503 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 504 | const AttributeList &Attr) { |
| 505 | Expr *Arg = 0; |
| 506 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 507 | return; |
| 508 | |
| 509 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 510 | return; |
| 511 | |
| 512 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| 513 | S.Context, Arg)); |
| 514 | } |
| 515 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 516 | static bool checkLockableAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 517 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 518 | assert(!Attr.isInvalid()); |
| 519 | |
| 520 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 521 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 522 | |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 523 | // FIXME: Lockable structs for C code. |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 524 | if (!isa<CXXRecordDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 525 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 526 | << Attr.getName() << ThreadExpectedClassOrStruct; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 527 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 530 | return true; |
| 531 | } |
| 532 | |
| 533 | static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 534 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 535 | return; |
| 536 | |
| 537 | D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); |
| 538 | } |
| 539 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 540 | static void handleScopedLockableAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 541 | const AttributeList &Attr) { |
| 542 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 543 | return; |
| 544 | |
| 545 | D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context)); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | static void handleNoThreadSafetyAttr(Sema &S, Decl *D, |
| 549 | const AttributeList &Attr) { |
| 550 | assert(!Attr.isInvalid()); |
| 551 | |
| 552 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 553 | return; |
| 554 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 555 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 556 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 557 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 558 | return; |
| 559 | } |
| 560 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 561 | D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(), |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 562 | S.Context)); |
| 563 | } |
| 564 | |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 565 | static void handleNoAddressSafetyAttr(Sema &S, Decl *D, |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 566 | const AttributeList &Attr) { |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 567 | assert(!Attr.isInvalid()); |
| 568 | |
| 569 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 570 | return; |
| 571 | |
| 572 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 573 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 574 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 575 | return; |
| 576 | } |
| 577 | |
| 578 | D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(), |
Nick Lewycky | f50b6fe | 2012-07-24 01:37:23 +0000 | [diff] [blame] | 579 | S.Context)); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 582 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 583 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 584 | SmallVector<Expr*, 1> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 585 | assert(!Attr.isInvalid()); |
| 586 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 587 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 588 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 589 | |
| 590 | // D must be either a member field or global (potentially shared) variable. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 591 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 592 | if (!VD || !mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 593 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 594 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 595 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 596 | } |
| 597 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 598 | // Check that this attribute only applies to lockable types. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 599 | QualType QT = VD->getType(); |
| 600 | if (!QT->isDependentType()) { |
| 601 | const RecordType *RT = getRecordType(QT); |
| 602 | if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 603 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 604 | << Attr.getName(); |
| 605 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 609 | // Check that all arguments are lockable objects. |
| 610 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 611 | if (Args.size() == 0) |
| 612 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 613 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 614 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 617 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 618 | const AttributeList &Attr) { |
| 619 | SmallVector<Expr*, 1> Args; |
| 620 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 621 | return; |
| 622 | |
| 623 | Expr **StartArg = &Args[0]; |
| 624 | D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context, |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 625 | StartArg, Args.size())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 628 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 629 | const AttributeList &Attr) { |
| 630 | SmallVector<Expr*, 1> Args; |
| 631 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 632 | return; |
| 633 | |
| 634 | Expr **StartArg = &Args[0]; |
| 635 | D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context, |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 636 | StartArg, Args.size())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 639 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 640 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 641 | SmallVector<Expr*, 1> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 642 | assert(!Attr.isInvalid()); |
| 643 | |
| 644 | // zero or more arguments ok |
| 645 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 646 | // check that the attribute is applied to a function |
| 647 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 648 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 649 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 650 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 653 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 654 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 655 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 656 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 659 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 660 | const AttributeList &Attr) { |
| 661 | SmallVector<Expr*, 1> Args; |
| 662 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 663 | return; |
| 664 | |
| 665 | unsigned Size = Args.size(); |
| 666 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 667 | D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(), |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 668 | S.Context, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 669 | StartArg, Size)); |
| 670 | } |
| 671 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 672 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 673 | const AttributeList &Attr) { |
| 674 | SmallVector<Expr*, 1> Args; |
| 675 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 676 | return; |
| 677 | |
| 678 | unsigned Size = Args.size(); |
| 679 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 680 | D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(), |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 681 | S.Context, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 682 | StartArg, Size)); |
| 683 | } |
| 684 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 685 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 686 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 687 | SmallVector<Expr*, 2> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 688 | assert(!Attr.isInvalid()); |
| 689 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 690 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 691 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 692 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 693 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 694 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 695 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 696 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 699 | if (!isIntOrBool(Attr.getArg(0))) { |
| 700 | S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 701 | << Attr.getName(); |
| 702 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 706 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 707 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 708 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 711 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 712 | const AttributeList &Attr) { |
| 713 | SmallVector<Expr*, 2> Args; |
| 714 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 715 | return; |
| 716 | |
| 717 | unsigned Size = Args.size(); |
| 718 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 719 | D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(), |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 720 | S.Context, |
| 721 | Attr.getArg(0), |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 722 | StartArg, Size)); |
| 723 | } |
| 724 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 725 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 726 | const AttributeList &Attr) { |
| 727 | SmallVector<Expr*, 2> Args; |
| 728 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 729 | return; |
| 730 | |
| 731 | unsigned Size = Args.size(); |
| 732 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 733 | D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(), |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 734 | S.Context, |
| 735 | Attr.getArg(0), |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 736 | StartArg, Size)); |
| 737 | } |
| 738 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 739 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 740 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 741 | SmallVector<Expr*, 1> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 742 | assert(!Attr.isInvalid()); |
| 743 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 744 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 745 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 746 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 747 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 748 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 749 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 750 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 753 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 754 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 755 | if (Args.size() == 0) |
| 756 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 757 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 758 | return true; |
| 759 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 760 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 761 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 762 | const AttributeList &Attr) { |
| 763 | SmallVector<Expr*, 1> Args; |
| 764 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 765 | return; |
| 766 | |
| 767 | Expr **StartArg = &Args[0]; |
| 768 | D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(), |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 769 | S.Context, |
| 770 | StartArg, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 771 | Args.size())); |
| 772 | } |
| 773 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 774 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 775 | const AttributeList &Attr) { |
| 776 | SmallVector<Expr*, 1> Args; |
| 777 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 778 | return; |
| 779 | |
| 780 | Expr **StartArg = &Args[0]; |
| 781 | D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(), |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 782 | S.Context, |
| 783 | StartArg, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 784 | Args.size())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 787 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 788 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 789 | assert(!Attr.isInvalid()); |
| 790 | |
| 791 | // zero or more arguments ok |
| 792 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 793 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 794 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 795 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 796 | return; |
| 797 | } |
| 798 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 799 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 800 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 801 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 802 | unsigned Size = Args.size(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 803 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 804 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 805 | D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 806 | StartArg, Size)); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 810 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 811 | assert(!Attr.isInvalid()); |
| 812 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 813 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 814 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 815 | Expr *Arg = Attr.getArg(0); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 816 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 817 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 818 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 819 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 820 | return; |
| 821 | } |
| 822 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 823 | if (Arg->isTypeDependent()) |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 824 | return; |
| 825 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 826 | // check that the argument is lockable object |
DeLesley Hutchins | f26efd7 | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 827 | SmallVector<Expr*, 1> Args; |
| 828 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 829 | unsigned Size = Args.size(); |
| 830 | if (Size == 0) |
| 831 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 832 | |
DeLesley Hutchins | f26efd7 | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 833 | D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context, |
| 834 | Args[0])); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 838 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 839 | assert(!Attr.isInvalid()); |
| 840 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 841 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 842 | return; |
| 843 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 844 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 845 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 846 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 847 | return; |
| 848 | } |
| 849 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 850 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 851 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 852 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 853 | unsigned Size = Args.size(); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 854 | if (Size == 0) |
| 855 | return; |
| 856 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 857 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 858 | D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 859 | StartArg, Size)); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 863 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 864 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 865 | TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 866 | if (tDecl == 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 867 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 868 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 869 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 870 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 871 | QualType curType = tDecl->getUnderlyingType(); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 872 | |
| 873 | Expr *sizeExpr; |
| 874 | |
| 875 | // Special case where the argument is a template id. |
| 876 | if (Attr.getParameterName()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 877 | CXXScopeSpec SS; |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 878 | SourceLocation TemplateKWLoc; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 879 | UnqualifiedId id; |
| 880 | id.setIdentifier(Attr.getParameterName(), Attr.getLoc()); |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 881 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 882 | ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id, |
| 883 | false, false); |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 884 | if (Size.isInvalid()) |
| 885 | return; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 886 | |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 887 | sizeExpr = Size.get(); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 888 | } else { |
| 889 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 890 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 891 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 892 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 893 | sizeExpr = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 894 | } |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 895 | |
| 896 | // Instantiate/Install the vector type, and let Sema build the type for us. |
| 897 | // This will run the reguired checks. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 898 | QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc()); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 899 | if (!T.isNull()) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 900 | // FIXME: preserve the old source info. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 901 | tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T)); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 902 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 903 | // Remember this typedef decl, we will need it later for diagnostics. |
| 904 | S.ExtVectorDecls.push_back(tDecl); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 905 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 908 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 909 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 910 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 911 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 912 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 913 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 914 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 915 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 916 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 917 | // has no effect. |
| 918 | if (!FD->getType()->isIncompleteType() && |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 919 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 920 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 921 | << Attr.getName() << FD->getType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 922 | else |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 923 | FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 924 | } else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 925 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 928 | static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 929 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 930 | TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context)); |
Fariborz Jahanian | c1a0a73 | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 931 | else |
| 932 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 933 | } |
| 934 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 935 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 936 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 937 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 938 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 939 | |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 940 | // The IBAction attributes only apply to instance methods. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 941 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 942 | if (MD->isInstanceMethod()) { |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 943 | D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context)); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 944 | return; |
| 945 | } |
| 946 | |
Ted Kremenek | 4ee2bb1 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 947 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 950 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 951 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 952 | // variables or properties of Objective-C classes. The outlet must also |
| 953 | // have an object reference type. |
| 954 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 955 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 0bfaf06 | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 956 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 957 | << Attr.getName() << VD->getType() << 0; |
| 958 | return false; |
| 959 | } |
| 960 | } |
| 961 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 962 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 963 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 964 | << Attr.getName() << PD->getType() << 1; |
| 965 | return false; |
| 966 | } |
| 967 | } |
| 968 | else { |
| 969 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 970 | return false; |
| 971 | } |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 972 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 973 | return true; |
| 974 | } |
| 975 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 976 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 977 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 978 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 979 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 980 | |
| 981 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 982 | return; |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 983 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 984 | D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context)); |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 987 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 988 | const AttributeList &Attr) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 989 | |
| 990 | // The iboutletcollection attribute can have zero or one arguments. |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 991 | if (Attr.getParameterName() && Attr.getNumArgs() > 0) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 992 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 993 | return; |
| 994 | } |
| 995 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 996 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 997 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 998 | |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 999 | IdentifierInfo *II = Attr.getParameterName(); |
| 1000 | if (!II) |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1001 | II = &S.Context.Idents.get("NSObject"); |
Fariborz Jahanian | 3a3400b | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 1002 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1003 | ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1004 | S.getScopeForContext(D->getDeclContext()->getParent())); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1005 | if (!TypeRep) { |
| 1006 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1007 | return; |
| 1008 | } |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1009 | QualType QT = TypeRep.get(); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1010 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1011 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1012 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1013 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1014 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1015 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1016 | return; |
| 1017 | } |
Argyrios Kyrtzidis | f1e7af3 | 2011-09-13 18:41:59 +0000 | [diff] [blame] | 1018 | D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context, |
| 1019 | QT, Attr.getParameterLoc())); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1022 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1023 | if (const RecordType *UT = T->getAsUnionType()) |
| 1024 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1025 | RecordDecl *UD = UT->getDecl(); |
| 1026 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1027 | itend = UD->field_end(); it != itend; ++it) { |
| 1028 | QualType QT = it->getType(); |
| 1029 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1030 | T = QT; |
| 1031 | return; |
| 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1037 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nuno Lopes | 174930d | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1038 | if (!isFunctionOrMethod(D)) { |
| 1039 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1040 | << "alloc_size" << ExpectedFunctionOrMethod; |
| 1041 | return; |
| 1042 | } |
| 1043 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1044 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1045 | return; |
| 1046 | |
| 1047 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1048 | // counted from one. |
| 1049 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1050 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| 1051 | |
| 1052 | SmallVector<unsigned, 8> SizeArgs; |
| 1053 | |
| 1054 | for (AttributeList::arg_iterator I = Attr.arg_begin(), |
| 1055 | E = Attr.arg_end(); I!=E; ++I) { |
| 1056 | // The argument must be an integer constant expression. |
| 1057 | Expr *Ex = *I; |
| 1058 | llvm::APSInt ArgNum; |
| 1059 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 1060 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1061 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1062 | << "alloc_size" << Ex->getSourceRange(); |
| 1063 | return; |
| 1064 | } |
| 1065 | |
| 1066 | uint64_t x = ArgNum.getZExtValue(); |
| 1067 | |
| 1068 | if (x < 1 || x > NumArgs) { |
| 1069 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1070 | << "alloc_size" << I.getArgNum() << Ex->getSourceRange(); |
| 1071 | return; |
| 1072 | } |
| 1073 | |
| 1074 | --x; |
| 1075 | if (HasImplicitThisParam) { |
| 1076 | if (x == 0) { |
| 1077 | S.Diag(Attr.getLoc(), |
| 1078 | diag::err_attribute_invalid_implicit_this_argument) |
| 1079 | << "alloc_size" << Ex->getSourceRange(); |
| 1080 | return; |
| 1081 | } |
| 1082 | --x; |
| 1083 | } |
| 1084 | |
| 1085 | // check if the function argument is of an integer type |
| 1086 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
| 1087 | if (!T->isIntegerType()) { |
| 1088 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1089 | << "alloc_size" << Ex->getSourceRange(); |
| 1090 | return; |
| 1091 | } |
| 1092 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1093 | SizeArgs.push_back(x); |
| 1094 | } |
| 1095 | |
| 1096 | // check if the function returns a pointer |
| 1097 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 1098 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
| 1099 | << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
| 1100 | } |
| 1101 | |
Nuno Lopes | 96c67d1 | 2012-06-18 16:27:56 +0000 | [diff] [blame] | 1102 | D->addAttr(::new (S.Context) AllocSizeAttr(Attr.getRange(), S.Context, |
| 1103 | SizeArgs.data(), SizeArgs.size())); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1106 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1107 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1108 | // ignore it as well |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1109 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1110 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1111 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1112 | return; |
| 1113 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1114 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1115 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1116 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1117 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1118 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1119 | |
| 1120 | // The nonnull attribute only applies to pointers. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1121 | SmallVector<unsigned, 10> NonNullArgs; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1122 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1123 | for (AttributeList::arg_iterator I=Attr.arg_begin(), |
| 1124 | E=Attr.arg_end(); I!=E; ++I) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1125 | |
| 1126 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1127 | // The argument must be an integer constant expression. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1128 | Expr *Ex = *I; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1129 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1130 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 1131 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1132 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1133 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1134 | return; |
| 1135 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1136 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1137 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1138 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1139 | if (x < 1 || x > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1140 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 1141 | << "nonnull" << I.getArgNum() << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1142 | return; |
| 1143 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1144 | |
Ted Kremenek | 465172f | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 1145 | --x; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1146 | if (HasImplicitThisParam) { |
| 1147 | if (x == 0) { |
| 1148 | S.Diag(Attr.getLoc(), |
| 1149 | diag::err_attribute_invalid_implicit_this_argument) |
| 1150 | << "nonnull" << Ex->getSourceRange(); |
| 1151 | return; |
| 1152 | } |
| 1153 | --x; |
| 1154 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1155 | |
| 1156 | // Is the function argument a pointer type? |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1157 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1158 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1159 | |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1160 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1161 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | c9ef405 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1162 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1163 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1164 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1165 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1166 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1167 | NonNullArgs.push_back(x); |
| 1168 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1169 | |
| 1170 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1171 | // arguments have a nonnull attribute. |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1172 | if (NonNullArgs.empty()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1173 | for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) { |
| 1174 | QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1175 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1176 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 1177 | NonNullArgs.push_back(I); |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1178 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1179 | |
Ted Kremenek | ee1c08c | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1180 | // No pointer arguments? |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1181 | if (NonNullArgs.empty()) { |
| 1182 | // Warn the trivial case only if attribute is not coming from a |
| 1183 | // macro instantiation. |
| 1184 | if (Attr.getLoc().isFileID()) |
| 1185 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1186 | return; |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1187 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1188 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1189 | |
| 1190 | unsigned* start = &NonNullArgs[0]; |
| 1191 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1192 | llvm::array_pod_sort(start, start + size); |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1193 | D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start, |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1194 | size)); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1197 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1198 | // This attribute must be applied to a function declaration. |
| 1199 | // The first argument to the attribute must be a string, |
| 1200 | // the name of the resource, for example "malloc". |
| 1201 | // The following arguments must be argument indexes, the arguments must be |
| 1202 | // of integer type for Returns, otherwise of pointer type. |
| 1203 | // 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] | 1204 | // after being held. free() should be __attribute((ownership_takes)), whereas |
| 1205 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1206 | |
| 1207 | if (!AL.getParameterName()) { |
| 1208 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1209 | << AL.getName()->getName() << 1; |
| 1210 | return; |
| 1211 | } |
| 1212 | // Figure out our Kind, and check arguments while we're at it. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1213 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1214 | switch (AL.getKind()) { |
| 1215 | case AttributeList::AT_ownership_takes: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1216 | K = OwnershipAttr::Takes; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1217 | if (AL.getNumArgs() < 1) { |
| 1218 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1219 | return; |
| 1220 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1221 | break; |
| 1222 | case AttributeList::AT_ownership_holds: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1223 | K = OwnershipAttr::Holds; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1224 | if (AL.getNumArgs() < 1) { |
| 1225 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1226 | return; |
| 1227 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1228 | break; |
| 1229 | case AttributeList::AT_ownership_returns: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1230 | K = OwnershipAttr::Returns; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1231 | if (AL.getNumArgs() > 1) { |
| 1232 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1233 | << AL.getNumArgs() + 1; |
| 1234 | return; |
| 1235 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1236 | break; |
| 1237 | default: |
| 1238 | // This should never happen given how we are called. |
| 1239 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1242 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1243 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1244 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1245 | return; |
| 1246 | } |
| 1247 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1248 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1249 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1250 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1251 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1252 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1253 | StringRef Module = AL.getParameterName()->getName(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1254 | |
| 1255 | // Normalize the argument, __foo__ becomes foo. |
| 1256 | if (Module.startswith("__") && Module.endswith("__")) |
| 1257 | Module = Module.substr(2, Module.size() - 4); |
| 1258 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1259 | SmallVector<unsigned, 10> OwnershipArgs; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1260 | |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1261 | for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E; |
| 1262 | ++I) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1263 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1264 | Expr *IdxExpr = *I; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1265 | llvm::APSInt ArgNum(32); |
| 1266 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1267 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1268 | S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int) |
| 1269 | << AL.getName()->getName() << IdxExpr->getSourceRange(); |
| 1270 | continue; |
| 1271 | } |
| 1272 | |
| 1273 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
| 1274 | |
| 1275 | if (x > NumArgs || x < 1) { |
| 1276 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1277 | << AL.getName()->getName() << x << IdxExpr->getSourceRange(); |
| 1278 | continue; |
| 1279 | } |
| 1280 | --x; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1281 | if (HasImplicitThisParam) { |
| 1282 | if (x == 0) { |
| 1283 | S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 1284 | << "ownership" << IdxExpr->getSourceRange(); |
| 1285 | return; |
| 1286 | } |
| 1287 | --x; |
| 1288 | } |
| 1289 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1290 | switch (K) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1291 | case OwnershipAttr::Takes: |
| 1292 | case OwnershipAttr::Holds: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1293 | // Is the function argument a pointer type? |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1294 | QualType T = getFunctionOrMethodArgType(D, x); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1295 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| 1296 | // FIXME: Should also highlight argument in decl. |
| 1297 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1298 | << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1299 | << "pointer" |
| 1300 | << IdxExpr->getSourceRange(); |
| 1301 | continue; |
| 1302 | } |
| 1303 | break; |
| 1304 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1305 | case OwnershipAttr::Returns: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1306 | if (AL.getNumArgs() > 1) { |
| 1307 | // Is the function argument an integer type? |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1308 | Expr *IdxExpr = AL.getArg(0); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1309 | llvm::APSInt ArgNum(32); |
| 1310 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1311 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1312 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
| 1313 | << "ownership_returns" << "integer" |
| 1314 | << IdxExpr->getSourceRange(); |
| 1315 | return; |
| 1316 | } |
| 1317 | } |
| 1318 | break; |
| 1319 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1320 | } // switch |
| 1321 | |
| 1322 | // Check we don't have a conflict with another ownership attribute. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1323 | for (specific_attr_iterator<OwnershipAttr> |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1324 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1325 | e = D->specific_attr_end<OwnershipAttr>(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1326 | i != e; ++i) { |
| 1327 | if ((*i)->getOwnKind() != K) { |
| 1328 | for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); |
| 1329 | I!=E; ++I) { |
| 1330 | if (x == *I) { |
| 1331 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1332 | << AL.getName()->getName() << "ownership_*"; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1333 | } |
| 1334 | } |
| 1335 | } |
| 1336 | } |
| 1337 | OwnershipArgs.push_back(x); |
| 1338 | } |
| 1339 | |
| 1340 | unsigned* start = OwnershipArgs.data(); |
| 1341 | unsigned size = OwnershipArgs.size(); |
| 1342 | llvm::array_pod_sort(start, start + size); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1343 | |
| 1344 | if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { |
| 1345 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1346 | return; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1347 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1348 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1349 | D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module, |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1350 | start, size)); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1353 | /// Whether this declaration has internal linkage for the purposes of |
| 1354 | /// things that want to complain about things not have internal linkage. |
| 1355 | static bool hasEffectivelyInternalLinkage(NamedDecl *D) { |
| 1356 | switch (D->getLinkage()) { |
| 1357 | case NoLinkage: |
| 1358 | case InternalLinkage: |
| 1359 | return true; |
| 1360 | |
| 1361 | // Template instantiations that go from external to unique-external |
| 1362 | // shouldn't get diagnosed. |
| 1363 | case UniqueExternalLinkage: |
| 1364 | return true; |
| 1365 | |
| 1366 | case ExternalLinkage: |
| 1367 | return false; |
| 1368 | } |
| 1369 | llvm_unreachable("unknown linkage kind!"); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1372 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1373 | // Check the attribute arguments. |
| 1374 | if (Attr.getNumArgs() > 1) { |
| 1375 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1376 | return; |
| 1377 | } |
| 1378 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1379 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1380 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1381 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1382 | return; |
| 1383 | } |
| 1384 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1385 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1386 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1387 | // gcc rejects |
| 1388 | // class c { |
| 1389 | // static int a __attribute__((weakref ("v2"))); |
| 1390 | // static int b() __attribute__((weakref ("f3"))); |
| 1391 | // }; |
| 1392 | // and ignores the attributes of |
| 1393 | // void f(void) { |
| 1394 | // static int a __attribute__((weakref ("v2"))); |
| 1395 | // } |
| 1396 | // we reject them |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1397 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1398 | if (!Ctx->isFileContext()) { |
| 1399 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1400 | nd->getNameAsString(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1401 | return; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | // The GCC manual says |
| 1405 | // |
| 1406 | // At present, a declaration to which `weakref' is attached can only |
| 1407 | // be `static'. |
| 1408 | // |
| 1409 | // It also says |
| 1410 | // |
| 1411 | // Without a TARGET, |
| 1412 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1413 | // equivalent to `weak'. |
| 1414 | // |
| 1415 | // gcc 4.4.1 will accept |
| 1416 | // int a7 __attribute__((weakref)); |
| 1417 | // as |
| 1418 | // int a7 __attribute__((weak)); |
| 1419 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1420 | // it if this behaviour is actually used. |
| 1421 | |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1422 | if (!hasEffectivelyInternalLinkage(nd)) { |
| 1423 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1424 | return; |
| 1425 | } |
| 1426 | |
| 1427 | // GCC rejects |
| 1428 | // static ((alias ("y"), weakref)). |
| 1429 | // Should we? How to check that weakref is before or after alias? |
| 1430 | |
| 1431 | if (Attr.getNumArgs() == 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1432 | Expr *Arg = Attr.getArg(0); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1433 | Arg = Arg->IgnoreParenCasts(); |
| 1434 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1435 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1436 | if (!Str || !Str->isAscii()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1437 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1438 | << "weakref" << 1; |
| 1439 | return; |
| 1440 | } |
| 1441 | // GCC will accept anything as the argument of weakref. Should we |
| 1442 | // check for an existing decl? |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1443 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1444 | Str->getString())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1447 | D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context)); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1450 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1451 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1452 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1453 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1454 | return; |
| 1455 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1456 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1457 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1458 | Arg = Arg->IgnoreParenCasts(); |
| 1459 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1460 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1461 | if (!Str || !Str->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1462 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1463 | << "alias" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1464 | return; |
| 1465 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1466 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1467 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | f5fe292 | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1468 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1469 | return; |
| 1470 | } |
| 1471 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1472 | // FIXME: check if target symbol exists in current file |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1473 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1474 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1475 | Str->getString())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1478 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1479 | // Check the attribute arguments. |
| 1480 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1481 | return; |
| 1482 | |
| 1483 | if (!isa<FunctionDecl>(D)) { |
| 1484 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1485 | << Attr.getName() << ExpectedFunction; |
| 1486 | return; |
| 1487 | } |
| 1488 | |
| 1489 | if (D->hasAttr<HotAttr>()) { |
| 1490 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1491 | << Attr.getName() << "hot"; |
| 1492 | return; |
| 1493 | } |
| 1494 | |
| 1495 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context)); |
| 1496 | } |
| 1497 | |
| 1498 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1499 | // Check the attribute arguments. |
| 1500 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1501 | return; |
| 1502 | |
| 1503 | if (!isa<FunctionDecl>(D)) { |
| 1504 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1505 | << Attr.getName() << ExpectedFunction; |
| 1506 | return; |
| 1507 | } |
| 1508 | |
| 1509 | if (D->hasAttr<ColdAttr>()) { |
| 1510 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1511 | << Attr.getName() << "cold"; |
| 1512 | return; |
| 1513 | } |
| 1514 | |
| 1515 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context)); |
| 1516 | } |
| 1517 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1518 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1519 | // Check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1520 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1521 | return; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1522 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1523 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1524 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1525 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1526 | return; |
| 1527 | } |
| 1528 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1529 | D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context)); |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1532 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 1533 | const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1534 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1535 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1536 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1537 | return; |
| 1538 | } |
| 1539 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1540 | if (!isa<FunctionDecl>(D)) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1541 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1542 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1543 | return; |
| 1544 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1545 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1546 | D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context)); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1549 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1550 | const AttributeList &Attr) { |
| 1551 | // Check the attribute arguments. |
| 1552 | if (Attr.getNumArgs() != 1) { |
| 1553 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1554 | return; |
| 1555 | } |
| 1556 | |
| 1557 | Expr *Arg = Attr.getArg(0); |
| 1558 | Arg = Arg->IgnoreParenCasts(); |
| 1559 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1560 | |
| 1561 | // Check that it is a string. |
| 1562 | if (!Str) { |
| 1563 | S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model"; |
| 1564 | return; |
| 1565 | } |
| 1566 | |
| 1567 | if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) { |
| 1568 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1569 | << Attr.getName() << ExpectedTLSVar; |
| 1570 | return; |
| 1571 | } |
| 1572 | |
| 1573 | // Check that the value. |
| 1574 | StringRef Model = Str->getString(); |
| 1575 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1576 | && Model != "initial-exec" && Model != "local-exec") { |
| 1577 | S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg); |
| 1578 | return; |
| 1579 | } |
| 1580 | |
| 1581 | D->addAttr(::new (S.Context) TLSModelAttr(Attr.getRange(), S.Context, |
| 1582 | Model)); |
| 1583 | } |
| 1584 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1585 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1586 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1587 | if (Attr.hasParameterOrArguments()) { |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1588 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1589 | return; |
| 1590 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1591 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1592 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1593 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1594 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1595 | D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context)); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1596 | return; |
| 1597 | } |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1600 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1603 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1604 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1605 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1606 | return; |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1607 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1608 | D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context)); |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1611 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1612 | assert(!Attr.isInvalid()); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1613 | if (isa<VarDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1614 | D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context)); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1615 | else |
| 1616 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1617 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1620 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1621 | assert(!Attr.isInvalid()); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1622 | if (isa<VarDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1623 | D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context)); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1624 | else |
| 1625 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1626 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1629 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1630 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1631 | |
| 1632 | if (S.CheckNoReturnAttr(attr)) return; |
| 1633 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1634 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1635 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1636 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1637 | return; |
| 1638 | } |
| 1639 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1640 | D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context)); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1644 | if (attr.hasParameterOrArguments()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1645 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1646 | attr.setInvalid(); |
| 1647 | return true; |
| 1648 | } |
| 1649 | |
| 1650 | return false; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1653 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1654 | const AttributeList &Attr) { |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1655 | |
| 1656 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1657 | // because 'analyzer_noreturn' does not impact the type. |
| 1658 | |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1659 | if(!checkAttributeNumArgs(S, Attr, 0)) |
| 1660 | return; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1661 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1662 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1663 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1664 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1665 | && !VD->getType()->isFunctionPointerType())) { |
| 1666 | S.Diag(Attr.getLoc(), |
| 1667 | Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type |
| 1668 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1669 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1670 | return; |
| 1671 | } |
| 1672 | } |
| 1673 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1674 | D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1677 | // PS3 PPU-specific. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1678 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1679 | /* |
| 1680 | Returning a Vector Class in Registers |
| 1681 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1682 | According to the PPU ABI specifications, a class with a single member of |
| 1683 | vector type is returned in memory when used as the return value of a function. |
| 1684 | This results in inefficient code when implementing vector classes. To return |
| 1685 | the value in a single vector register, add the vecreturn attribute to the |
| 1686 | class definition. This attribute is also applicable to struct types. |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1687 | |
| 1688 | Example: |
| 1689 | |
| 1690 | struct Vector |
| 1691 | { |
| 1692 | __vector float xyzw; |
| 1693 | } __attribute__((vecreturn)); |
| 1694 | |
| 1695 | Vector Add(Vector lhs, Vector rhs) |
| 1696 | { |
| 1697 | Vector result; |
| 1698 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1699 | return result; // This will be returned in a register |
| 1700 | } |
| 1701 | */ |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1702 | if (!isa<RecordDecl>(D)) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1703 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1704 | << Attr.getName() << ExpectedClass; |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1705 | return; |
| 1706 | } |
| 1707 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1708 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1709 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1710 | return; |
| 1711 | } |
| 1712 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1713 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1714 | int count = 0; |
| 1715 | |
| 1716 | if (!isa<CXXRecordDecl>(record)) { |
| 1717 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1718 | return; |
| 1719 | } |
| 1720 | |
| 1721 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1722 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1723 | return; |
| 1724 | } |
| 1725 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1726 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1727 | iter != record->field_end(); iter++) { |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1728 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1729 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1730 | return; |
| 1731 | } |
| 1732 | count++; |
| 1733 | } |
| 1734 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1735 | D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context)); |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1738 | static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1739 | if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1740 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1741 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1742 | return; |
| 1743 | } |
| 1744 | // FIXME: Actually store the attribute on the declaration |
| 1745 | } |
| 1746 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1747 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1748 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1749 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1750 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1751 | return; |
| 1752 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1753 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1754 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 568eae4 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1755 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1756 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1757 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1758 | return; |
| 1759 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1760 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1761 | D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context)); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1764 | static void handleReturnsTwiceAttr(Sema &S, Decl *D, |
| 1765 | const AttributeList &Attr) { |
| 1766 | // check the attribute arguments. |
| 1767 | if (Attr.hasParameterOrArguments()) { |
| 1768 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1769 | return; |
| 1770 | } |
| 1771 | |
| 1772 | if (!isa<FunctionDecl>(D)) { |
| 1773 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1774 | << Attr.getName() << ExpectedFunction; |
| 1775 | return; |
| 1776 | } |
| 1777 | |
| 1778 | D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context)); |
| 1779 | } |
| 1780 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1781 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1782 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1783 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1784 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1785 | return; |
| 1786 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1787 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1788 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Daniel Dunbar | 186204b | 2009-02-13 22:48:56 +0000 | [diff] [blame] | 1789 | if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1790 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 1791 | return; |
| 1792 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1793 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1794 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1795 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1796 | return; |
| 1797 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1798 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1799 | D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context)); |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1802 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1803 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1804 | if (Attr.getNumArgs() > 1) { |
| 1805 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1806 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1807 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1808 | |
| 1809 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1810 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1811 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1812 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1813 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1814 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1815 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1816 | << "constructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1817 | return; |
| 1818 | } |
| 1819 | priority = Idx.getZExtValue(); |
| 1820 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1821 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1822 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1823 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1824 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1825 | return; |
| 1826 | } |
| 1827 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1828 | D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1829 | priority)); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1832 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1833 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1834 | if (Attr.getNumArgs() > 1) { |
| 1835 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1836 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1837 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1838 | |
| 1839 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1840 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1841 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1842 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1843 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1844 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1845 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1846 | << "destructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1847 | return; |
| 1848 | } |
| 1849 | priority = Idx.getZExtValue(); |
| 1850 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1851 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1852 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1853 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1854 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1855 | return; |
| 1856 | } |
| 1857 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1858 | D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1859 | priority)); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1862 | template <typename AttrTy> |
| 1863 | static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr, |
| 1864 | const char *Name) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1865 | unsigned NumArgs = Attr.getNumArgs(); |
| 1866 | if (NumArgs > 1) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1867 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1868 | return; |
| 1869 | } |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1870 | |
| 1871 | // Handle the case where the attribute has a text message. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1872 | StringRef Str; |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1873 | if (NumArgs == 1) { |
| 1874 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1875 | if (!SE) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1876 | S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string) |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1877 | << Name; |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1878 | return; |
| 1879 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1880 | Str = SE->getString(); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1881 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1882 | |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1883 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str)); |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1886 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 1887 | const AttributeList &Attr) { |
| 1888 | unsigned NumArgs = Attr.getNumArgs(); |
| 1889 | if (NumArgs > 0) { |
| 1890 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 1891 | return; |
| 1892 | } |
| 1893 | |
| 1894 | D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr( |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1895 | Attr.getRange(), S.Context)); |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 1898 | static void handleObjCRootClassAttr(Sema &S, Decl *D, |
| 1899 | const AttributeList &Attr) { |
| 1900 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 1901 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 1902 | return; |
| 1903 | } |
| 1904 | |
| 1905 | unsigned NumArgs = Attr.getNumArgs(); |
| 1906 | if (NumArgs > 0) { |
| 1907 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 1908 | return; |
| 1909 | } |
| 1910 | |
| 1911 | D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context)); |
| 1912 | } |
| 1913 | |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1914 | static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 1915 | const AttributeList &Attr) { |
Fariborz Jahanian | 341b8be | 2012-01-03 22:52:32 +0000 | [diff] [blame] | 1916 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 1917 | S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis); |
| 1918 | return; |
| 1919 | } |
| 1920 | |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 1921 | unsigned NumArgs = Attr.getNumArgs(); |
| 1922 | if (NumArgs > 0) { |
| 1923 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 1924 | return; |
| 1925 | } |
| 1926 | |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1927 | D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr( |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 1928 | Attr.getRange(), S.Context)); |
| 1929 | } |
| 1930 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 1931 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 1932 | IdentifierInfo *Platform, |
| 1933 | VersionTuple Introduced, |
| 1934 | VersionTuple Deprecated, |
| 1935 | VersionTuple Obsoleted) { |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1936 | StringRef PlatformName |
| 1937 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1938 | if (PlatformName.empty()) |
| 1939 | PlatformName = Platform->getName(); |
| 1940 | |
| 1941 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 1942 | // of these steps are needed). |
| 1943 | if (!Introduced.empty() && !Deprecated.empty() && |
| 1944 | !(Introduced <= Deprecated)) { |
| 1945 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1946 | << 1 << PlatformName << Deprecated.getAsString() |
| 1947 | << 0 << Introduced.getAsString(); |
| 1948 | return true; |
| 1949 | } |
| 1950 | |
| 1951 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 1952 | !(Introduced <= Obsoleted)) { |
| 1953 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1954 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1955 | << 0 << Introduced.getAsString(); |
| 1956 | return true; |
| 1957 | } |
| 1958 | |
| 1959 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 1960 | !(Deprecated <= Obsoleted)) { |
| 1961 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1962 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1963 | << 1 << Deprecated.getAsString(); |
| 1964 | return true; |
| 1965 | } |
| 1966 | |
| 1967 | return false; |
| 1968 | } |
| 1969 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1970 | AvailabilityAttr *Sema::mergeAvailabilityAttr(Decl *D, SourceRange Range, |
| 1971 | IdentifierInfo *Platform, |
| 1972 | VersionTuple Introduced, |
| 1973 | VersionTuple Deprecated, |
| 1974 | VersionTuple Obsoleted, |
| 1975 | bool IsUnavailable, |
| 1976 | StringRef Message) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1977 | VersionTuple MergedIntroduced = Introduced; |
| 1978 | VersionTuple MergedDeprecated = Deprecated; |
| 1979 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1980 | bool FoundAny = false; |
| 1981 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1982 | if (D->hasAttrs()) { |
| 1983 | AttrVec &Attrs = D->getAttrs(); |
| 1984 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 1985 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 1986 | if (!OldAA) { |
| 1987 | ++i; |
| 1988 | continue; |
| 1989 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1990 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1991 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 1992 | if (OldPlatform != Platform) { |
| 1993 | ++i; |
| 1994 | continue; |
| 1995 | } |
| 1996 | |
| 1997 | FoundAny = true; |
| 1998 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 1999 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2000 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2001 | bool OldIsUnavailable = OldAA->getUnavailable(); |
| 2002 | StringRef OldMessage = OldAA->getMessage(); |
| 2003 | |
| 2004 | if ((!OldIntroduced.empty() && !Introduced.empty() && |
| 2005 | OldIntroduced != Introduced) || |
| 2006 | (!OldDeprecated.empty() && !Deprecated.empty() && |
| 2007 | OldDeprecated != Deprecated) || |
| 2008 | (!OldObsoleted.empty() && !Obsoleted.empty() && |
| 2009 | OldObsoleted != Obsoleted) || |
| 2010 | (OldIsUnavailable != IsUnavailable) || |
| 2011 | (OldMessage != Message)) { |
| 2012 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2013 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2014 | Attrs.erase(Attrs.begin() + i); |
| 2015 | --e; |
| 2016 | continue; |
| 2017 | } |
| 2018 | |
| 2019 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2020 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2021 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2022 | |
| 2023 | if (MergedIntroduced2.empty()) |
| 2024 | MergedIntroduced2 = OldIntroduced; |
| 2025 | if (MergedDeprecated2.empty()) |
| 2026 | MergedDeprecated2 = OldDeprecated; |
| 2027 | if (MergedObsoleted2.empty()) |
| 2028 | MergedObsoleted2 = OldObsoleted; |
| 2029 | |
| 2030 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2031 | MergedIntroduced2, MergedDeprecated2, |
| 2032 | MergedObsoleted2)) { |
| 2033 | Attrs.erase(Attrs.begin() + i); |
| 2034 | --e; |
| 2035 | continue; |
| 2036 | } |
| 2037 | |
| 2038 | MergedIntroduced = MergedIntroduced2; |
| 2039 | MergedDeprecated = MergedDeprecated2; |
| 2040 | MergedObsoleted = MergedObsoleted2; |
| 2041 | ++i; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2042 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | if (FoundAny && |
| 2046 | MergedIntroduced == Introduced && |
| 2047 | MergedDeprecated == Deprecated && |
| 2048 | MergedObsoleted == Obsoleted) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2049 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2050 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2051 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2052 | MergedDeprecated, MergedObsoleted)) { |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2053 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 2054 | Introduced, Deprecated, |
| 2055 | Obsoleted, IsUnavailable, Message); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2056 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2057 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2060 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2061 | const AttributeList &Attr) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2062 | IdentifierInfo *Platform = Attr.getParameterName(); |
| 2063 | SourceLocation PlatformLoc = Attr.getParameterLoc(); |
| 2064 | |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2065 | if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2066 | S.Diag(PlatformLoc, diag::warn_availability_unknown_platform) |
| 2067 | << Platform; |
| 2068 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2069 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2070 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2071 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2072 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2073 | StringRef Str; |
| 2074 | const StringLiteral *SE = |
| 2075 | dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr()); |
| 2076 | if (SE) |
| 2077 | Str = SE->getString(); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2078 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2079 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(D, Attr.getRange(), |
| 2080 | Platform, |
| 2081 | Introduced.Version, |
| 2082 | Deprecated.Version, |
| 2083 | Obsoleted.Version, |
| 2084 | IsUnavailable, Str); |
| 2085 | if (NewAttr) |
| 2086 | D->addAttr(NewAttr); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2087 | } |
| 2088 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2089 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
| 2090 | VisibilityAttr::VisibilityType Vis) { |
Rafael Espindola | dd44f34 | 2012-05-10 03:01:34 +0000 | [diff] [blame] | 2091 | if (isa<TypedefNameDecl>(D)) { |
| 2092 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility"; |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2093 | return NULL; |
Rafael Espindola | dd44f34 | 2012-05-10 03:01:34 +0000 | [diff] [blame] | 2094 | } |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2095 | VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>(); |
| 2096 | if (ExistingAttr) { |
| 2097 | VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility(); |
| 2098 | if (ExistingVis == Vis) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2099 | return NULL; |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2100 | Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2101 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2102 | D->dropAttr<VisibilityAttr>(); |
| 2103 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2104 | return ::new (Context) VisibilityAttr(Range, Context, Vis); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2107 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2108 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2109 | if(!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2110 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2111 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2112 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2113 | Arg = Arg->IgnoreParenCasts(); |
| 2114 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2115 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2116 | if (!Str || !Str->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2117 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2118 | << "visibility" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2119 | return; |
| 2120 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2121 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2122 | StringRef TypeStr = Str->getString(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2123 | VisibilityAttr::VisibilityType type; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2124 | |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2125 | if (TypeStr == "default") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2126 | type = VisibilityAttr::Default; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2127 | else if (TypeStr == "hidden") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2128 | type = VisibilityAttr::Hidden; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2129 | else if (TypeStr == "internal") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2130 | type = VisibilityAttr::Hidden; // FIXME |
John McCall | 4188760 | 2012-01-29 01:20:30 +0000 | [diff] [blame] | 2131 | else if (TypeStr == "protected") { |
| 2132 | // Complain about attempts to use protected visibility on targets |
| 2133 | // (like Darwin) that don't support it. |
| 2134 | if (!S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2135 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2136 | type = VisibilityAttr::Default; |
| 2137 | } else { |
| 2138 | type = VisibilityAttr::Protected; |
| 2139 | } |
| 2140 | } else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2141 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2142 | return; |
| 2143 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2144 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2145 | VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type); |
| 2146 | if (NewAttr) |
| 2147 | D->addAttr(NewAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2150 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2151 | const AttributeList &Attr) { |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2152 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 2153 | if (!method) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2154 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2155 | << ExpectedMethod; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2156 | return; |
| 2157 | } |
| 2158 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2159 | if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) { |
| 2160 | if (!Attr.getParameterName() && Attr.getNumArgs() == 1) { |
| 2161 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2162 | << "objc_method_family" << 1; |
| 2163 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2164 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2165 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2166 | Attr.setInvalid(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2167 | return; |
| 2168 | } |
| 2169 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2170 | StringRef param = Attr.getParameterName()->getName(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2171 | ObjCMethodFamilyAttr::FamilyKind family; |
| 2172 | if (param == "none") |
| 2173 | family = ObjCMethodFamilyAttr::OMF_None; |
| 2174 | else if (param == "alloc") |
| 2175 | family = ObjCMethodFamilyAttr::OMF_alloc; |
| 2176 | else if (param == "copy") |
| 2177 | family = ObjCMethodFamilyAttr::OMF_copy; |
| 2178 | else if (param == "init") |
| 2179 | family = ObjCMethodFamilyAttr::OMF_init; |
| 2180 | else if (param == "mutableCopy") |
| 2181 | family = ObjCMethodFamilyAttr::OMF_mutableCopy; |
| 2182 | else if (param == "new") |
| 2183 | family = ObjCMethodFamilyAttr::OMF_new; |
| 2184 | else { |
| 2185 | // Just warn and ignore it. This is future-proof against new |
| 2186 | // families being used in system headers. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2187 | S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2188 | return; |
| 2189 | } |
| 2190 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2191 | if (family == ObjCMethodFamilyAttr::OMF_init && |
| 2192 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2193 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2194 | << method->getResultType(); |
| 2195 | // Ignore the attribute. |
| 2196 | return; |
| 2197 | } |
| 2198 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2199 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2200 | S.Context, family)); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2203 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 2204 | const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2205 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2206 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2207 | |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2208 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 2209 | if (OCI == 0) { |
| 2210 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 2211 | return; |
| 2212 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2213 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2214 | D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2217 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2218 | if (Attr.getNumArgs() != 0) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 2219 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2220 | return; |
| 2221 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2222 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2223 | QualType T = TD->getUnderlyingType(); |
| 2224 | if (!T->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2225 | !T->getAs<PointerType>()->getPointeeType()->isRecordType()) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2226 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2227 | return; |
| 2228 | } |
| 2229 | } |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2230 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2231 | QualType T = PD->getType(); |
| 2232 | if (!T->isPointerType() || |
| 2233 | !T->getAs<PointerType>()->getPointeeType()->isRecordType()) { |
| 2234 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2235 | return; |
| 2236 | } |
| 2237 | } |
| 2238 | else { |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2239 | // It is okay to include this attribute on properties, e.g.: |
| 2240 | // |
| 2241 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2242 | // |
| 2243 | // In this case it follows tradition and suppresses an error in the above |
| 2244 | // case. |
Fariborz Jahanian | 9b2eb7b | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2245 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2246 | } |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2247 | D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context)); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2250 | static void |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2251 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2252 | if (Attr.getNumArgs() != 0) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 2253 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2254 | return; |
| 2255 | } |
| 2256 | |
| 2257 | if (!isa<FunctionDecl>(D)) { |
| 2258 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 2259 | return; |
| 2260 | } |
| 2261 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2262 | D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context)); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2263 | } |
| 2264 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2265 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2266 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2267 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2268 | << "blocks" << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2269 | return; |
| 2270 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2271 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2272 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2273 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2274 | return; |
| 2275 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2276 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2277 | BlocksAttr::BlockType type; |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 2278 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2279 | type = BlocksAttr::ByRef; |
| 2280 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2281 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2282 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2283 | return; |
| 2284 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2285 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2286 | D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type)); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2289 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2290 | // check the attribute arguments. |
| 2291 | if (Attr.getNumArgs() > 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2292 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2293 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2296 | unsigned sentinel = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2297 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2298 | Expr *E = Attr.getArg(0); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2299 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2300 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2301 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2302 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2303 | << "sentinel" << 1 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2304 | return; |
| 2305 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2306 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2307 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2308 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2309 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2310 | return; |
| 2311 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2312 | |
| 2313 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2316 | unsigned nullPos = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2317 | if (Attr.getNumArgs() > 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2318 | Expr *E = Attr.getArg(1); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2319 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2320 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2321 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2322 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2323 | << "sentinel" << 2 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2324 | return; |
| 2325 | } |
| 2326 | nullPos = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2327 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2328 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2329 | // FIXME: This error message could be improved, it would be nice |
| 2330 | // to say what the bounds actually are. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2331 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2332 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2333 | return; |
| 2334 | } |
| 2335 | } |
| 2336 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2337 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2338 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2339 | if (isa<FunctionNoProtoType>(FT)) { |
| 2340 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2341 | return; |
| 2342 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2343 | |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2344 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2345 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2346 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2347 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2348 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2349 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2350 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2351 | return; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2352 | } |
Eli Friedman | a0b2ba1 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2353 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2354 | if (!BD->isVariadic()) { |
| 2355 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2356 | return; |
| 2357 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2358 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2359 | QualType Ty = V->getType(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2360 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2361 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2362 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2363 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2364 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2365 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2366 | return; |
| 2367 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2368 | } else { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2369 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2370 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2371 | return; |
| 2372 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2373 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2374 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2375 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2376 | return; |
| 2377 | } |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2378 | D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2379 | nullPos)); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2382 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2383 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2384 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2385 | return; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2386 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2387 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2388 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2389 | << Attr.getName() << ExpectedFunctionOrMethod; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2390 | return; |
| 2391 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2392 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2393 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2394 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2395 | << Attr.getName() << 0; |
Nuno Lopes | f857798 | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2396 | return; |
| 2397 | } |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2398 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2399 | if (MD->getResultType()->isVoidType()) { |
| 2400 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2401 | << Attr.getName() << 1; |
| 2402 | return; |
| 2403 | } |
| 2404 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2405 | D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2408 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2409 | // check the attribute arguments. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2410 | if (Attr.hasParameterOrArguments()) { |
| 2411 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2412 | return; |
| 2413 | } |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2414 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2415 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
Fariborz Jahanian | 13c7fcc | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 2416 | if (isa<CXXRecordDecl>(D)) { |
| 2417 | D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| 2418 | return; |
| 2419 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2420 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2421 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | f23ecd9 | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 2422 | return; |
| 2423 | } |
| 2424 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2425 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 2426 | |
| 2427 | // 'weak' only applies to declarations with external linkage. |
| 2428 | if (hasEffectivelyInternalLinkage(nd)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2429 | S.Diag(Attr.getLoc(), diag::err_attribute_weak_static); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2430 | return; |
| 2431 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2432 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2433 | nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2436 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2437 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2438 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2439 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2440 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2441 | |
| 2442 | // weak_import only applies to variable & function declarations. |
| 2443 | bool isDef = false; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2444 | if (!D->canBeWeakImported(isDef)) { |
| 2445 | if (isDef) |
| 2446 | S.Diag(Attr.getLoc(), |
| 2447 | diag::warn_attribute_weak_import_invalid_on_definition) |
| 2448 | << "weak_import" << 2 /*variable and function*/; |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2449 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2450 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 90eed21 | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2451 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2452 | // Nothing to warn about here. |
| 2453 | } else |
Fariborz Jahanian | c034974 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2454 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2455 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2456 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2457 | return; |
| 2458 | } |
| 2459 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2460 | D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context)); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2463 | // Handles reqd_work_group_size and work_group_size_hint. |
| 2464 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2465 | const AttributeList &Attr) { |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2466 | assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2467 | || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint); |
| 2468 | |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2469 | // Attribute has 3 arguments. |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2470 | if (!checkAttributeNumArgs(S, Attr, 3)) return; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2471 | |
| 2472 | unsigned WGSize[3]; |
| 2473 | for (unsigned i = 0; i < 3; ++i) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2474 | Expr *E = Attr.getArg(i); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2475 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2476 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2477 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2478 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2479 | << Attr.getName()->getName() << E->getSourceRange(); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2480 | return; |
| 2481 | } |
| 2482 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 2483 | } |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2484 | |
| 2485 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2486 | && D->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 2487 | ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>(); |
| 2488 | if (!(A->getXDim() == WGSize[0] && |
| 2489 | A->getYDim() == WGSize[1] && |
| 2490 | A->getZDim() == WGSize[2])) { |
| 2491 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2492 | Attr.getName(); |
| 2493 | } |
| 2494 | } |
| 2495 | |
| 2496 | if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint |
| 2497 | && D->hasAttr<WorkGroupSizeHintAttr>()) { |
| 2498 | WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>(); |
| 2499 | if (!(A->getXDim() == WGSize[0] && |
| 2500 | A->getYDim() == WGSize[1] && |
| 2501 | A->getZDim() == WGSize[2])) { |
| 2502 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2503 | Attr.getName(); |
| 2504 | } |
| 2505 | } |
| 2506 | |
| 2507 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize) |
| 2508 | D->addAttr(::new (S.Context) |
| 2509 | ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, |
| 2510 | WGSize[0], WGSize[1], WGSize[2])); |
| 2511 | else |
| 2512 | D->addAttr(::new (S.Context) |
| 2513 | WorkGroupSizeHintAttr(Attr.getRange(), S.Context, |
| 2514 | WGSize[0], WGSize[1], WGSize[2])); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2517 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
| 2518 | StringRef Name) { |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2519 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2520 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2521 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2522 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2523 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2524 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2525 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2526 | return ::new (Context) SectionAttr(Range, Context, Name); |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2529 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2530 | // Attribute has no arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2531 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2532 | return; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2533 | |
| 2534 | // Make sure that there is a string literal as the sections's single |
| 2535 | // argument. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2536 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2537 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2538 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2539 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section"; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2540 | return; |
| 2541 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2542 | |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2543 | // If the target wants to validate the section specifier, make it happen. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2544 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString()); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2545 | if (!Error.empty()) { |
| 2546 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 2547 | << Error; |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2548 | return; |
| 2549 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2550 | |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2551 | // This attribute cannot be applied to local variables. |
| 2552 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 2553 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 2554 | return; |
| 2555 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2556 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), |
| 2557 | SE->getString()); |
| 2558 | if (NewAttr) |
| 2559 | D->addAttr(NewAttr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2562 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2563 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2564 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2565 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2566 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2567 | return; |
| 2568 | } |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2569 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2570 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2571 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2572 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2573 | } else { |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2574 | D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context)); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2575 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2576 | } |
| 2577 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2578 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2579 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2580 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2581 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2582 | return; |
| 2583 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2584 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2585 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2586 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2587 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2588 | } else { |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2589 | D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context)); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2590 | } |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2593 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2594 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2595 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2596 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2597 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2598 | D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context)); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2601 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2602 | if (!Attr.getParameterName()) { |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2603 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2604 | return; |
| 2605 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2606 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2607 | if (Attr.getNumArgs() != 0) { |
| 2608 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2609 | return; |
| 2610 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2611 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2612 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2613 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2614 | if (!VD || !VD->hasLocalStorage()) { |
| 2615 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 2616 | return; |
| 2617 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2618 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2619 | // Look up the function |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2620 | // FIXME: Lookup probably isn't looking in the right place |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2621 | NamedDecl *CleanupDecl |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2622 | = S.LookupSingleName(S.TUScope, Attr.getParameterName(), |
| 2623 | Attr.getParameterLoc(), Sema::LookupOrdinaryName); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2624 | if (!CleanupDecl) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2625 | S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2626 | Attr.getParameterName(); |
| 2627 | return; |
| 2628 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2629 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2630 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 2631 | if (!FD) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2632 | S.Diag(Attr.getParameterLoc(), |
| 2633 | diag::err_attribute_cleanup_arg_not_function) |
| 2634 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2635 | return; |
| 2636 | } |
| 2637 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2638 | if (FD->getNumParams() != 1) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2639 | S.Diag(Attr.getParameterLoc(), |
| 2640 | diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2641 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2642 | return; |
| 2643 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2644 | |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2645 | // We're currently more strict than GCC about what function types we accept. |
| 2646 | // If this ever proves to be a problem it should be easy to fix. |
| 2647 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2648 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2649 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2650 | ParamTy, Ty) != Sema::Compatible) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2651 | S.Diag(Attr.getParameterLoc(), |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2652 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 2653 | Attr.getParameterName() << ParamTy << Ty; |
| 2654 | return; |
| 2655 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2656 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2657 | D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD)); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2658 | S.MarkFunctionReferenced(Attr.getParameterLoc(), FD); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2659 | } |
| 2660 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2661 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
| 2662 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2663 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2664 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2665 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2666 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2667 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2668 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2669 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2670 | return; |
| 2671 | } |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2672 | |
| 2673 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2674 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2675 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 2676 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2677 | unsigned FirstIdx = 1; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2678 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2679 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2680 | Expr *IdxExpr = Attr.getArg(0); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2681 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2682 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 2683 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2684 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 2685 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 2686 | return; |
| 2687 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2688 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2689 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
| 2690 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 2691 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 2692 | return; |
| 2693 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2694 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2695 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2696 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2697 | if (HasImplicitThisParam) { |
| 2698 | if (ArgIdx == 0) { |
| 2699 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 2700 | << "format_arg" << IdxExpr->getSourceRange(); |
| 2701 | return; |
| 2702 | } |
| 2703 | ArgIdx--; |
| 2704 | } |
| 2705 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2706 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2707 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2708 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2709 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2710 | if (not_nsstring_type && |
| 2711 | !isCFStringType(Ty, S.Context) && |
| 2712 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2713 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2714 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2715 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2716 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2717 | << IdxExpr->getSourceRange(); |
| 2718 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2719 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2720 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2721 | if (!isNSStringType(Ty, S.Context) && |
| 2722 | !isCFStringType(Ty, S.Context) && |
| 2723 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2724 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2725 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2726 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2727 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2728 | << IdxExpr->getSourceRange(); |
| 2729 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2732 | D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context, |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2733 | Idx.getZExtValue())); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2736 | enum FormatAttrKind { |
| 2737 | CFStringFormat, |
| 2738 | NSStringFormat, |
| 2739 | StrftimeFormat, |
| 2740 | SupportedFormat, |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2741 | IgnoredFormat, |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2742 | InvalidFormat |
| 2743 | }; |
| 2744 | |
| 2745 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2746 | /// types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2747 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2748 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2749 | // Check for formats that get handled specially. |
| 2750 | .Case("NSString", NSStringFormat) |
| 2751 | .Case("CFString", CFStringFormat) |
| 2752 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2753 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2754 | // Otherwise, check for supported formats. |
| 2755 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2756 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2757 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2758 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2759 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2760 | .Default(InvalidFormat); |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2761 | } |
| 2762 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2763 | /// Handle __attribute__((init_priority(priority))) attributes based on |
| 2764 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2765 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2766 | const AttributeList &Attr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2767 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2768 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2769 | return; |
| 2770 | } |
| 2771 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2772 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2773 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2774 | Attr.setInvalid(); |
| 2775 | return; |
| 2776 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2777 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2778 | if (S.Context.getAsArrayType(T)) |
| 2779 | T = S.Context.getBaseElementType(T); |
| 2780 | if (!T->getAs<RecordType>()) { |
| 2781 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2782 | Attr.setInvalid(); |
| 2783 | return; |
| 2784 | } |
| 2785 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2786 | if (Attr.getNumArgs() != 1) { |
| 2787 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2788 | Attr.setInvalid(); |
| 2789 | return; |
| 2790 | } |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2791 | Expr *priorityExpr = Attr.getArg(0); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2792 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2793 | llvm::APSInt priority(32); |
| 2794 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 2795 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
| 2796 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 2797 | << "init_priority" << priorityExpr->getSourceRange(); |
| 2798 | Attr.setInvalid(); |
| 2799 | return; |
| 2800 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 2801 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2802 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2803 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 2804 | << priorityExpr->getSourceRange(); |
| 2805 | Attr.setInvalid(); |
| 2806 | return; |
| 2807 | } |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2808 | D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2809 | prioritynum)); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2812 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format, |
| 2813 | int FormatIdx, int FirstArg) { |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2814 | // Check whether we already have an equivalent format attribute. |
| 2815 | for (specific_attr_iterator<FormatAttr> |
| 2816 | i = D->specific_attr_begin<FormatAttr>(), |
| 2817 | e = D->specific_attr_end<FormatAttr>(); |
| 2818 | i != e ; ++i) { |
| 2819 | FormatAttr *f = *i; |
| 2820 | if (f->getType() == Format && |
| 2821 | f->getFormatIdx() == FormatIdx && |
| 2822 | f->getFirstArg() == FirstArg) { |
| 2823 | // If we don't have a valid location for this attribute, adopt the |
| 2824 | // location. |
| 2825 | if (f->getLocation().isInvalid()) |
| 2826 | f->setRange(Range); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2827 | return NULL; |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2828 | } |
| 2829 | } |
| 2830 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2831 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 2832 | FirstArg); |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2835 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
| 2836 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2837 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2838 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2839 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2840 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2841 | << "format" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2842 | return; |
| 2843 | } |
| 2844 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2845 | if (Attr.getNumArgs() != 2) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2846 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2847 | return; |
| 2848 | } |
| 2849 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2850 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2851 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2852 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2853 | return; |
| 2854 | } |
| 2855 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2856 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2857 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2858 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 2859 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2860 | unsigned FirstIdx = 1; |
| 2861 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2862 | StringRef Format = Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2863 | |
| 2864 | // Normalize the argument, __foo__ becomes foo. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2865 | if (Format.startswith("__") && Format.endswith("__")) |
| 2866 | Format = Format.substr(2, Format.size() - 4); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2867 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2868 | // Check for supported formats. |
| 2869 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2870 | |
| 2871 | if (Kind == IgnoredFormat) |
| 2872 | return; |
| 2873 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2874 | if (Kind == InvalidFormat) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2875 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 2876 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2877 | return; |
| 2878 | } |
| 2879 | |
| 2880 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2881 | Expr *IdxExpr = Attr.getArg(0); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2882 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2883 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 2884 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2885 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2886 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2887 | return; |
| 2888 | } |
| 2889 | |
| 2890 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2891 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2892 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2893 | return; |
| 2894 | } |
| 2895 | |
| 2896 | // FIXME: Do we need to bounds check? |
| 2897 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2898 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2899 | if (HasImplicitThisParam) { |
| 2900 | if (ArgIdx == 0) { |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2901 | S.Diag(Attr.getLoc(), |
| 2902 | diag::err_format_attribute_implicit_this_format_string) |
| 2903 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2904 | return; |
| 2905 | } |
| 2906 | ArgIdx--; |
| 2907 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2908 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2909 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2910 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2911 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2912 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2913 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2914 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2915 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2916 | return; |
| 2917 | } |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2918 | } else if (Kind == NSStringFormat) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2919 | // FIXME: do we need to check if the type is NSString*? What are the |
| 2920 | // semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2921 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2922 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2923 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2924 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2925 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2926 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2927 | } else if (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2928 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2929 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2930 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2931 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2932 | return; |
| 2933 | } |
| 2934 | |
| 2935 | // check the 3rd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2936 | Expr *FirstArgExpr = Attr.getArg(1); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2937 | llvm::APSInt FirstArg(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2938 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 2939 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2940 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2941 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2942 | return; |
| 2943 | } |
| 2944 | |
| 2945 | // check if the function is variadic if the 3rd argument non-zero |
| 2946 | if (FirstArg != 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2947 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2948 | ++NumArgs; // +1 for ... |
| 2949 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2950 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2951 | return; |
| 2952 | } |
| 2953 | } |
| 2954 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2955 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 2956 | // variable the input is just the current time + the format string. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2957 | if (Kind == StrftimeFormat) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2958 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2959 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 2960 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2961 | return; |
| 2962 | } |
| 2963 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 2964 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2965 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2966 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2967 | return; |
| 2968 | } |
| 2969 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2970 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format, |
| 2971 | Idx.getZExtValue(), |
| 2972 | FirstArg.getZExtValue()); |
| 2973 | if (NewAttr) |
| 2974 | D->addAttr(NewAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2977 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 2978 | const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2979 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2980 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2981 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2982 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2983 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2984 | // Try to find the underlying union declaration. |
| 2985 | RecordDecl *RD = 0; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2986 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2987 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 2988 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 2989 | else |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2990 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2991 | |
| 2992 | if (!RD || !RD->isUnion()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2993 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2994 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2995 | return; |
| 2996 | } |
| 2997 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2998 | if (!RD->isCompleteDefinition()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2999 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3000 | diag::warn_transparent_union_attribute_not_definition); |
| 3001 | return; |
| 3002 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3003 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3004 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3005 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3006 | if (Field == FieldEnd) { |
| 3007 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3008 | return; |
| 3009 | } |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3010 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3011 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3012 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3013 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3014 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3015 | diag::warn_transparent_union_attribute_floating) |
| 3016 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3017 | return; |
| 3018 | } |
| 3019 | |
| 3020 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3021 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3022 | for (; Field != FieldEnd; ++Field) { |
| 3023 | QualType FieldType = Field->getType(); |
| 3024 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 3025 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 3026 | // Warn if we drop the attribute. |
| 3027 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3028 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3029 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3030 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3031 | diag::warn_transparent_union_attribute_field_size_align) |
| 3032 | << isSize << Field->getDeclName() << FieldBits; |
| 3033 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3034 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3035 | diag::note_transparent_union_first_field_size_align) |
| 3036 | << isSize << FirstBits; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3037 | return; |
| 3038 | } |
| 3039 | } |
| 3040 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3041 | RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3042 | } |
| 3043 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3044 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3045 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3046 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3047 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3048 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3049 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3050 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3051 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3052 | // Make sure that there is a string literal as the annotation's single |
| 3053 | // argument. |
| 3054 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3055 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate"; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3056 | return; |
| 3057 | } |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3058 | |
| 3059 | // Don't duplicate annotations that are already set. |
| 3060 | for (specific_attr_iterator<AnnotateAttr> |
| 3061 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 3062 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
| 3063 | if ((*i)->getAnnotation() == SE->getString()) |
| 3064 | return; |
| 3065 | } |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3066 | D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3067 | SE->getString())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3070 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3071 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3072 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3073 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3074 | return; |
| 3075 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3076 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3077 | //FIXME: The C++0x version of this attribute has more limited applicabilty |
| 3078 | // than GNU's, and should error out when it is used to specify a |
| 3079 | // weaker alignment, rather than being silently ignored. |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3080 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3081 | if (Attr.getNumArgs() == 0) { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3082 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 3083 | true, 0, Attr.isDeclspecAttribute())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3084 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3085 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3086 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3087 | S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0), |
| 3088 | Attr.isDeclspecAttribute()); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3091 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 3092 | bool isDeclSpec) { |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 3093 | // FIXME: Handle pack-expansions here. |
| 3094 | if (DiagnoseUnexpandedParameterPack(E)) |
| 3095 | return; |
| 3096 | |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3097 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3098 | // Save dependent expressions in the AST to be instantiated. |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3099 | D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E, |
| 3100 | isDeclSpec)); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3101 | return; |
| 3102 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3103 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3104 | SourceLocation AttrLoc = AttrRange.getBegin(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3105 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3106 | llvm::APSInt Alignment(32); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3107 | ExprResult ICE |
| 3108 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3109 | diag::err_aligned_attribute_argument_not_int, |
| 3110 | /*AllowFold*/ false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3111 | if (ICE.isInvalid()) |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3112 | return; |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3113 | if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3114 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 3115 | << E->getSourceRange(); |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3116 | return; |
| 3117 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3118 | if (isDeclSpec) { |
| 3119 | // We've already verified it's a power of 2, now let's make sure it's |
| 3120 | // 8192 or less. |
| 3121 | if (Alignment.getZExtValue() > 8192) { |
| 3122 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
| 3123 | << E->getSourceRange(); |
| 3124 | return; |
| 3125 | } |
| 3126 | } |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3127 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3128 | D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take(), |
| 3129 | isDeclSpec)); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3130 | } |
| 3131 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3132 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
| 3133 | bool isDeclSpec) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3134 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3135 | // FIXME: Perform checking of type validity |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3136 | D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3137 | isDeclSpec)); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3138 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3139 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3140 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3141 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3142 | /// type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3143 | /// |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3144 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3145 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3146 | /// HImode, not an intermediate pointer. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3147 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3148 | // This attribute isn't documented, but glibc uses it. It changes |
| 3149 | // the width of an int or unsigned int to the specified size. |
| 3150 | |
| 3151 | // Check that there aren't any arguments |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3152 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3153 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3154 | |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3155 | |
| 3156 | IdentifierInfo *Name = Attr.getParameterName(); |
| 3157 | if (!Name) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3158 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3159 | return; |
| 3160 | } |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3161 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3162 | StringRef Str = Attr.getParameterName()->getName(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3163 | |
| 3164 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3165 | if (Str.startswith("__") && Str.endswith("__")) |
| 3166 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3167 | |
| 3168 | unsigned DestWidth = 0; |
| 3169 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3170 | bool ComplexMode = false; |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3171 | switch (Str.size()) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3172 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3173 | switch (Str[0]) { |
| 3174 | case 'Q': DestWidth = 8; break; |
| 3175 | case 'H': DestWidth = 16; break; |
| 3176 | case 'S': DestWidth = 32; break; |
| 3177 | case 'D': DestWidth = 64; break; |
| 3178 | case 'X': DestWidth = 96; break; |
| 3179 | case 'T': DestWidth = 128; break; |
| 3180 | } |
| 3181 | if (Str[1] == 'F') { |
| 3182 | IntegerMode = false; |
| 3183 | } else if (Str[1] == 'C') { |
| 3184 | IntegerMode = false; |
| 3185 | ComplexMode = true; |
| 3186 | } else if (Str[1] != 'I') { |
| 3187 | DestWidth = 0; |
| 3188 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3189 | break; |
| 3190 | case 4: |
| 3191 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3192 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3193 | if (Str == "word") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3194 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3195 | else if (Str == "byte") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3196 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3197 | break; |
| 3198 | case 7: |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3199 | if (Str == "pointer") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3200 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3201 | break; |
| 3202 | } |
| 3203 | |
| 3204 | QualType OldTy; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3205 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3206 | OldTy = TD->getUnderlyingType(); |
| 3207 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3208 | OldTy = VD->getType(); |
| 3209 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3210 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3211 | << "mode" << Attr.getRange(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3212 | return; |
| 3213 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3214 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3215 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3216 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3217 | else if (IntegerMode) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3218 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3219 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3220 | } else if (ComplexMode) { |
| 3221 | if (!OldTy->isComplexType()) |
| 3222 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3223 | } else { |
| 3224 | if (!OldTy->isFloatingType()) |
| 3225 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3226 | } |
| 3227 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3228 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3229 | // and friends, at least with glibc. |
| 3230 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 3231 | // width on unusual platforms. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3232 | // FIXME: Make sure floating-point mappings are accurate |
| 3233 | // FIXME: Support XF and TF types |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3234 | QualType NewTy; |
| 3235 | switch (DestWidth) { |
| 3236 | case 0: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3237 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3238 | return; |
| 3239 | default: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3240 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3241 | return; |
| 3242 | case 8: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3243 | if (!IntegerMode) { |
| 3244 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3245 | return; |
| 3246 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3247 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3248 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3249 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3250 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3251 | break; |
| 3252 | case 16: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3253 | if (!IntegerMode) { |
| 3254 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3255 | return; |
| 3256 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3257 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3258 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3259 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3260 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3261 | break; |
| 3262 | case 32: |
| 3263 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3264 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3265 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3266 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3267 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3268 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3269 | break; |
| 3270 | case 64: |
| 3271 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3272 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3273 | else if (OldTy->isSignedIntegerType()) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3274 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3275 | NewTy = S.Context.LongTy; |
| 3276 | else |
| 3277 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3278 | else |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3279 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3280 | NewTy = S.Context.UnsignedLongTy; |
| 3281 | else |
| 3282 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3283 | break; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3284 | case 96: |
| 3285 | NewTy = S.Context.LongDoubleTy; |
| 3286 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3287 | case 128: |
| 3288 | if (!IntegerMode) { |
| 3289 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3290 | return; |
| 3291 | } |
Anders Carlsson | f5f7d86 | 2009-12-29 07:07:36 +0000 | [diff] [blame] | 3292 | if (OldTy->isSignedIntegerType()) |
| 3293 | NewTy = S.Context.Int128Ty; |
| 3294 | else |
| 3295 | NewTy = S.Context.UnsignedInt128Ty; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3296 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3299 | if (ComplexMode) { |
| 3300 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3301 | } |
| 3302 | |
| 3303 | // Install the new type. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3304 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 3305 | // FIXME: preserve existing source info. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3306 | TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy)); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 3307 | } else |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3308 | cast<ValueDecl>(D)->setType(NewTy); |
| 3309 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3310 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3311 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3312 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3313 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3314 | return; |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 3315 | |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3316 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3317 | if (!VD->hasGlobalStorage()) |
| 3318 | S.Diag(Attr.getLoc(), |
| 3319 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3320 | << Attr.getName(); |
| 3321 | } else if (!isFunctionOrMethod(D)) { |
| 3322 | S.Diag(Attr.getLoc(), |
| 3323 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3324 | << Attr.getName(); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3325 | return; |
| 3326 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3327 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3328 | D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context)); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3329 | } |
| 3330 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3331 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3332 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3333 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3334 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3335 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3336 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3337 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3338 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3339 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3340 | return; |
| 3341 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3342 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3343 | D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context)); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3344 | } |
| 3345 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3346 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 3347 | const AttributeList &Attr) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3348 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3349 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3350 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3351 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3352 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3353 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3354 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3355 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3356 | return; |
| 3357 | } |
| 3358 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3359 | D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3360 | S.Context)); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3361 | } |
| 3362 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3363 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3364 | if (S.LangOpts.CUDA) { |
| 3365 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 3366 | if (Attr.hasParameterOrArguments()) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3367 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 3368 | return; |
| 3369 | } |
| 3370 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3371 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3372 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3373 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3374 | return; |
| 3375 | } |
| 3376 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3377 | D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3378 | } else { |
| 3379 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 3380 | } |
| 3381 | } |
| 3382 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3383 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3384 | if (S.LangOpts.CUDA) { |
| 3385 | // check the attribute arguments. |
| 3386 | if (Attr.getNumArgs() != 0) { |
| 3387 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 3388 | return; |
| 3389 | } |
| 3390 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3391 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3392 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3393 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3394 | return; |
| 3395 | } |
| 3396 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3397 | D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3398 | } else { |
| 3399 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 3400 | } |
| 3401 | } |
| 3402 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3403 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3404 | if (S.LangOpts.CUDA) { |
| 3405 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3406 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3407 | return; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3408 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3409 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3410 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3411 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3412 | return; |
| 3413 | } |
| 3414 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3415 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3416 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 3417 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3418 | if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) { |
| 3419 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3420 | << FD->getType() |
| 3421 | << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(), |
| 3422 | "void"); |
| 3423 | } else { |
| 3424 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3425 | << FD->getType(); |
| 3426 | } |
| 3427 | return; |
| 3428 | } |
| 3429 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3430 | D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3431 | } else { |
| 3432 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 3433 | } |
| 3434 | } |
| 3435 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3436 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3437 | if (S.LangOpts.CUDA) { |
| 3438 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3439 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3440 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3441 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3442 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3443 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3444 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3445 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3446 | return; |
| 3447 | } |
| 3448 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3449 | D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3450 | } else { |
| 3451 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 3452 | } |
| 3453 | } |
| 3454 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3455 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3456 | if (S.LangOpts.CUDA) { |
| 3457 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3458 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3459 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3460 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3461 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3462 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3463 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3464 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3465 | return; |
| 3466 | } |
| 3467 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3468 | D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3469 | } else { |
| 3470 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 3471 | } |
| 3472 | } |
| 3473 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3474 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3475 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3476 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3477 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3478 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3479 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3480 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3481 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3482 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3483 | return; |
| 3484 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3485 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3486 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3487 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3488 | return; |
| 3489 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3490 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3491 | D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3494 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3495 | if (hasDeclarator(D)) return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3496 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3497 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3498 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3499 | CallingConv CC; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3500 | if (S.CheckCallingConvAttr(Attr, CC)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3501 | return; |
| 3502 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3503 | if (!isa<ObjCMethodDecl>(D)) { |
| 3504 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3505 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3506 | return; |
| 3507 | } |
| 3508 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3509 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3510 | case AttributeList::AT_FastCall: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3511 | D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3512 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3513 | case AttributeList::AT_StdCall: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3514 | D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3515 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3516 | case AttributeList::AT_ThisCall: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3517 | D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context)); |
Douglas Gregor | 04633eb | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3518 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3519 | case AttributeList::AT_CDecl: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3520 | D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3521 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3522 | case AttributeList::AT_Pascal: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3523 | D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context)); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3524 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3525 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3526 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame^] | 3527 | switch (CC) { |
| 3528 | case CC_AAPCS: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3529 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame^] | 3530 | break; |
| 3531 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3532 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame^] | 3533 | break; |
| 3534 | default: |
| 3535 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3538 | D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS)); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3539 | } |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3540 | default: |
| 3541 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3542 | } |
| 3543 | } |
| 3544 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3545 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 3546 | assert(!Attr.isInvalid()); |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3547 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3548 | } |
| 3549 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3550 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) { |
| 3551 | if (attr.isInvalid()) |
| 3552 | return true; |
| 3553 | |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3554 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
| 3555 | if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) { |
| 3556 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3557 | attr.setInvalid(); |
| 3558 | return true; |
| 3559 | } |
| 3560 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3561 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 3562 | // move to TargetAttributesSema one day. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3563 | switch (attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3564 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3565 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3566 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3567 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3568 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
| 3569 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3570 | Expr *Arg = attr.getArg(0); |
| 3571 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3572 | if (!Str || !Str->isAscii()) { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3573 | Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 3574 | << "pcs" << 1; |
| 3575 | attr.setInvalid(); |
| 3576 | return true; |
| 3577 | } |
| 3578 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3579 | StringRef StrRef = Str->getString(); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3580 | if (StrRef == "aapcs") { |
| 3581 | CC = CC_AAPCS; |
| 3582 | break; |
| 3583 | } else if (StrRef == "aapcs-vfp") { |
| 3584 | CC = CC_AAPCS_VFP; |
| 3585 | break; |
| 3586 | } |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3587 | |
| 3588 | attr.setInvalid(); |
| 3589 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3590 | return true; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3591 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3592 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3593 | } |
| 3594 | |
| 3595 | return false; |
| 3596 | } |
| 3597 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3598 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3599 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3600 | |
| 3601 | unsigned numParams; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3602 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3603 | return; |
| 3604 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3605 | if (!isa<ObjCMethodDecl>(D)) { |
| 3606 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3607 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3608 | return; |
| 3609 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3610 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3611 | D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams)); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3612 | } |
| 3613 | |
| 3614 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3615 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3616 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3617 | if (Attr.isInvalid()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3618 | return true; |
| 3619 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3620 | if (Attr.getNumArgs() != 1) { |
| 3621 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 3622 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3623 | return true; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3624 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3625 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3626 | Expr *NumParamsExpr = Attr.getArg(0); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3627 | llvm::APSInt NumParams(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3628 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3629 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3630 | Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3631 | << "regparm" << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3632 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3633 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3634 | } |
| 3635 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3636 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3637 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3638 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3639 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3640 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3641 | } |
| 3642 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3643 | numParams = NumParams.getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3644 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3645 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3646 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3647 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3648 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3649 | } |
| 3650 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3651 | return false; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3654 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3655 | if (S.LangOpts.CUDA) { |
| 3656 | // check the attribute arguments. |
| 3657 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 3658 | // FIXME: 0 is not okay. |
| 3659 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3660 | return; |
| 3661 | } |
| 3662 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3663 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3664 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3665 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3666 | return; |
| 3667 | } |
| 3668 | |
| 3669 | Expr *MaxThreadsExpr = Attr.getArg(0); |
| 3670 | llvm::APSInt MaxThreads(32); |
| 3671 | if (MaxThreadsExpr->isTypeDependent() || |
| 3672 | MaxThreadsExpr->isValueDependent() || |
| 3673 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
| 3674 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 3675 | << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange(); |
| 3676 | return; |
| 3677 | } |
| 3678 | |
| 3679 | llvm::APSInt MinBlocks(32); |
| 3680 | if (Attr.getNumArgs() > 1) { |
| 3681 | Expr *MinBlocksExpr = Attr.getArg(1); |
| 3682 | if (MinBlocksExpr->isTypeDependent() || |
| 3683 | MinBlocksExpr->isValueDependent() || |
| 3684 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
| 3685 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 3686 | << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange(); |
| 3687 | return; |
| 3688 | } |
| 3689 | } |
| 3690 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3691 | D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3692 | MaxThreads.getZExtValue(), |
| 3693 | MinBlocks.getZExtValue())); |
| 3694 | } else { |
| 3695 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 3696 | } |
| 3697 | } |
| 3698 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3699 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3700 | // Checker-specific attribute handlers. |
| 3701 | //===----------------------------------------------------------------------===// |
| 3702 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3703 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3704 | return type->isDependentType() || |
| 3705 | type->isObjCObjectPointerType() || |
| 3706 | S.Context.isObjCNSObjectType(type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3707 | } |
| 3708 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3709 | return type->isDependentType() || |
| 3710 | type->isPointerType() || |
| 3711 | isValidSubjectOfNSAttribute(S, type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3712 | } |
| 3713 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3714 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3715 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3716 | if (!param) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3717 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3718 | << Attr.getRange() << Attr.getName() << ExpectedParameter; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3719 | return; |
| 3720 | } |
| 3721 | |
| 3722 | bool typeOK, cf; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3723 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3724 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 3725 | cf = false; |
| 3726 | } else { |
| 3727 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 3728 | cf = true; |
| 3729 | } |
| 3730 | |
| 3731 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3732 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3733 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3734 | return; |
| 3735 | } |
| 3736 | |
| 3737 | if (cf) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3738 | param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context)); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3739 | else |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3740 | param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context)); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3741 | } |
| 3742 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3743 | static void handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 3744 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3745 | if (!isa<ObjCMethodDecl>(D)) { |
| 3746 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3747 | << Attr.getRange() << Attr.getName() << ExpectedMethod; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3748 | return; |
| 3749 | } |
| 3750 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3751 | D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context)); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3752 | } |
| 3753 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3754 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 3755 | const AttributeList &Attr) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3756 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3757 | QualType returnType; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3758 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3759 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3760 | returnType = MD->getResultType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3761 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
Fariborz Jahanian | 831fb96 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 3762 | returnType = PD->getType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3763 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3764 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3765 | return; // ignore: was handled as a type attribute |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3766 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3767 | returnType = FD->getResultType(); |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3768 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3769 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3770 | << Attr.getRange() << Attr.getName() |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3771 | << ExpectedFunctionOrMethod; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3772 | return; |
| 3773 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3774 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3775 | bool typeOK; |
| 3776 | bool cf; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3777 | switch (Attr.getKind()) { |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3778 | default: llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3779 | case AttributeList::AT_NSReturnsAutoreleased: |
| 3780 | case AttributeList::AT_NSReturnsRetained: |
| 3781 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3782 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 3783 | cf = false; |
| 3784 | break; |
| 3785 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3786 | case AttributeList::AT_CFReturnsRetained: |
| 3787 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3788 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 3789 | cf = true; |
| 3790 | break; |
| 3791 | } |
| 3792 | |
| 3793 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3794 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3795 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3796 | return; |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3797 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3798 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3799 | switch (Attr.getKind()) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3800 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3801 | llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3802 | case AttributeList::AT_NSReturnsAutoreleased: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3803 | D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(), |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3804 | S.Context)); |
| 3805 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3806 | case AttributeList::AT_CFReturnsNotRetained: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3807 | D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3808 | S.Context)); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3809 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3810 | case AttributeList::AT_NSReturnsNotRetained: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3811 | D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3812 | S.Context)); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3813 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3814 | case AttributeList::AT_CFReturnsRetained: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3815 | D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3816 | S.Context)); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3817 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3818 | case AttributeList::AT_NSReturnsRetained: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3819 | D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3820 | S.Context)); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3821 | return; |
| 3822 | }; |
| 3823 | } |
| 3824 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3825 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 3826 | const AttributeList &attr) { |
| 3827 | SourceLocation loc = attr.getLoc(); |
| 3828 | |
| 3829 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 3830 | |
Fariborz Jahanian | 94d55d7 | 2012-04-21 17:51:44 +0000 | [diff] [blame] | 3831 | if (!method) { |
Fariborz Jahanian | 0e78afb | 2012-04-20 22:00:46 +0000 | [diff] [blame] | 3832 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3833 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3834 | return; |
| 3835 | } |
| 3836 | |
| 3837 | // Check that the method returns a normal pointer. |
| 3838 | QualType resultType = method->getResultType(); |
Fariborz Jahanian | f2e5945 | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 3839 | |
| 3840 | if (!resultType->isReferenceType() && |
| 3841 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3842 | S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 3843 | << SourceRange(loc) |
| 3844 | << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2; |
| 3845 | |
| 3846 | // Drop the attribute. |
| 3847 | return; |
| 3848 | } |
| 3849 | |
| 3850 | method->addAttr( |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3851 | ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context)); |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3852 | } |
| 3853 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3854 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 3855 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
| 3856 | if (!isa<FunctionDecl>(D)) { |
| 3857 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3858 | << A.getRange() << A.getName() << ExpectedFunction; |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3859 | return; |
| 3860 | } |
| 3861 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3862 | bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3863 | |
| 3864 | // Check whether there's a conflicting attribute already present. |
| 3865 | Attr *Existing; |
| 3866 | if (IsAudited) { |
| 3867 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 3868 | } else { |
| 3869 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 3870 | } |
| 3871 | if (Existing) { |
| 3872 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 3873 | << A.getName() |
| 3874 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 3875 | << A.getRange() << Existing->getRange(); |
| 3876 | return; |
| 3877 | } |
| 3878 | |
| 3879 | // All clear; add the attribute. |
| 3880 | if (IsAudited) { |
| 3881 | D->addAttr( |
| 3882 | ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context)); |
| 3883 | } else { |
| 3884 | D->addAttr( |
| 3885 | ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context)); |
| 3886 | } |
| 3887 | } |
| 3888 | |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3889 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 3890 | const AttributeList &Attr) { |
| 3891 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 3892 | if (!RD || RD->isUnion()) { |
| 3893 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3894 | << Attr.getRange() << Attr.getName() << ExpectedStruct; |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3895 | } |
| 3896 | |
| 3897 | IdentifierInfo *ParmName = Attr.getParameterName(); |
| 3898 | |
| 3899 | // In Objective-C, verify that the type names an Objective-C type. |
| 3900 | // We don't want to check this outside of ObjC because people sometimes |
| 3901 | // do crazy C declarations of Objective-C types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3902 | if (ParmName && S.getLangOpts().ObjC1) { |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3903 | // Check for an existing type with this name. |
| 3904 | LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(), |
| 3905 | Sema::LookupOrdinaryName); |
| 3906 | if (S.LookupName(R, Sc)) { |
| 3907 | NamedDecl *Target = R.getFoundDecl(); |
| 3908 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 3909 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 3910 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 3911 | } |
| 3912 | } |
| 3913 | } |
| 3914 | |
| 3915 | D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context, |
| 3916 | ParmName)); |
| 3917 | } |
| 3918 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3919 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 3920 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3921 | if (hasDeclarator(D)) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3922 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3923 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3924 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3927 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 3928 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3929 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3930 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3931 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3932 | return; |
| 3933 | } |
| 3934 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3935 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3936 | QualType type = vd->getType(); |
| 3937 | |
| 3938 | if (!type->isDependentType() && |
| 3939 | !type->isObjCLifetimeType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3940 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3941 | << type; |
| 3942 | return; |
| 3943 | } |
| 3944 | |
| 3945 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 3946 | |
| 3947 | // If we have no lifetime yet, check the lifetime we're presumably |
| 3948 | // going to infer. |
| 3949 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 3950 | lifetime = type->getObjCARCImplicitLifetime(); |
| 3951 | |
| 3952 | switch (lifetime) { |
| 3953 | case Qualifiers::OCL_None: |
| 3954 | assert(type->isDependentType() && |
| 3955 | "didn't infer lifetime for non-dependent type?"); |
| 3956 | break; |
| 3957 | |
| 3958 | case Qualifiers::OCL_Weak: // meaningful |
| 3959 | case Qualifiers::OCL_Strong: // meaningful |
| 3960 | break; |
| 3961 | |
| 3962 | case Qualifiers::OCL_ExplicitNone: |
| 3963 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3964 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3965 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 3966 | break; |
| 3967 | } |
| 3968 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3969 | D->addAttr(::new (S.Context) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3970 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3971 | } |
| 3972 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3973 | //===----------------------------------------------------------------------===// |
| 3974 | // Microsoft specific attribute handlers. |
| 3975 | //===----------------------------------------------------------------------===// |
| 3976 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3977 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3978 | if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) { |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3979 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3980 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3981 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3982 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3983 | Expr *Arg = Attr.getArg(0); |
| 3984 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3985 | if (!Str || !Str->isAscii()) { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3986 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 3987 | << "uuid" << 1; |
| 3988 | return; |
| 3989 | } |
| 3990 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3991 | StringRef StrRef = Str->getString(); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3992 | |
| 3993 | bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' && |
| 3994 | StrRef.back() == '}'; |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3995 | |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3996 | // Validate GUID length. |
| 3997 | if (IsCurly && StrRef.size() != 38) { |
| 3998 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 3999 | return; |
| 4000 | } |
| 4001 | if (!IsCurly && StrRef.size() != 36) { |
| 4002 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4003 | return; |
| 4004 | } |
| 4005 | |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4006 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4007 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4008 | StringRef::iterator I = StrRef.begin(); |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4009 | if (IsCurly) // Skip the optional '{' |
| 4010 | ++I; |
| 4011 | |
| 4012 | for (int i = 0; i < 36; ++i) { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4013 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 4014 | if (*I != '-') { |
| 4015 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4016 | return; |
| 4017 | } |
| 4018 | } else if (!isxdigit(*I)) { |
| 4019 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4020 | return; |
| 4021 | } |
| 4022 | I++; |
| 4023 | } |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4024 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4025 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4026 | Str->getString())); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4027 | } else |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4028 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid"; |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4029 | } |
| 4030 | |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4031 | static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4032 | if (S.LangOpts.MicrosoftExt) { |
| 4033 | AttributeList::Kind Kind = Attr.getKind(); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4034 | if (Kind == AttributeList::AT_SingleInheritance) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4035 | D->addAttr( |
| 4036 | ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context)); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4037 | else if (Kind == AttributeList::AT_MultipleInheritance) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4038 | D->addAttr( |
| 4039 | ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context)); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4040 | else if (Kind == AttributeList::AT_VirtualInheritance) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4041 | D->addAttr( |
| 4042 | ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context)); |
| 4043 | } else |
| 4044 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4045 | } |
| 4046 | |
| 4047 | static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4048 | if (S.LangOpts.MicrosoftExt) { |
| 4049 | AttributeList::Kind Kind = Attr.getKind(); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4050 | if (Kind == AttributeList::AT_Ptr32) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4051 | D->addAttr( |
| 4052 | ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context)); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4053 | else if (Kind == AttributeList::AT_Ptr64) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4054 | D->addAttr( |
| 4055 | ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context)); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4056 | else if (Kind == AttributeList::AT_Win64) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4057 | D->addAttr( |
| 4058 | ::new (S.Context) Win64Attr(Attr.getRange(), S.Context)); |
| 4059 | } else |
| 4060 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4061 | } |
| 4062 | |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4063 | static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4064 | if (S.LangOpts.MicrosoftExt) |
| 4065 | D->addAttr(::new (S.Context) ForceInlineAttr(Attr.getRange(), S.Context)); |
| 4066 | else |
| 4067 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4068 | } |
| 4069 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4070 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4071 | // Top Level Sema Entry Points |
| 4072 | //===----------------------------------------------------------------------===// |
| 4073 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4074 | static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4075 | const AttributeList &Attr) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4076 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4077 | case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break; |
| 4078 | case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break; |
| 4079 | case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break; |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4080 | default: |
| 4081 | break; |
| 4082 | } |
| 4083 | } |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4084 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4085 | static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4086 | const AttributeList &Attr) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4087 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4088 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 4089 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 4090 | case AttributeList::AT_IBOutletCollection: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4091 | handleIBOutletCollection(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4092 | case AttributeList::AT_AddressSpace: |
| 4093 | case AttributeList::AT_OpenCLImageAccess: |
| 4094 | case AttributeList::AT_ObjCGC: |
| 4095 | case AttributeList::AT_VectorSize: |
| 4096 | case AttributeList::AT_NeonVectorType: |
| 4097 | case AttributeList::AT_NeonPolyVectorType: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4098 | // Ignore these, these are type attributes, handled by |
| 4099 | // ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4100 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4101 | case AttributeList::AT_CUDADevice: |
| 4102 | case AttributeList::AT_CUDAHost: |
| 4103 | case AttributeList::AT_Overloadable: |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4104 | // Ignore, this is a non-inheritable attribute, handled |
| 4105 | // by ProcessNonInheritableDeclAttr. |
| 4106 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4107 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 4108 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 4109 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 4110 | case AttributeList::AT_AlwaysInline: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4111 | handleAlwaysInlineAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4112 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4113 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 4114 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4115 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 4116 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 4117 | case AttributeList::AT_CarriesDependency: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4118 | handleDependencyAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4119 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
| 4120 | case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break; |
| 4121 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
| 4122 | case AttributeList::AT_Deprecated: |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4123 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated"); |
| 4124 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4125 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 4126 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4127 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4128 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4129 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4130 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4131 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
| 4132 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4133 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4134 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4135 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4136 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
| 4137 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
| 4138 | case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break; |
| 4139 | case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break; |
| 4140 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4141 | case AttributeList::AT_ownership_returns: |
| 4142 | case AttributeList::AT_ownership_takes: |
| 4143 | case AttributeList::AT_ownership_holds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4144 | handleOwnershipAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4145 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4146 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
| 4147 | case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break; |
| 4148 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
| 4149 | case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break; |
| 4150 | case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break; |
| 4151 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4152 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4153 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4154 | handleObjCOwnershipAttr(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4155 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4156 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4157 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4158 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4159 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4160 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4161 | case AttributeList::AT_NSBridged: |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4162 | handleNSBridgedAttr(S, scope, D, Attr); break; |
| 4163 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4164 | case AttributeList::AT_CFAuditedTransfer: |
| 4165 | case AttributeList::AT_CFUnknownTransfer: |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4166 | handleCFTransferAttr(S, D, Attr); break; |
| 4167 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4168 | // Checker-specific. |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4169 | case AttributeList::AT_CFConsumed: |
| 4170 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4171 | case AttributeList::AT_NSConsumesSelf: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4172 | handleNSConsumesSelfAttr(S, D, Attr); break; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4173 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4174 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4175 | case AttributeList::AT_NSReturnsNotRetained: |
| 4176 | case AttributeList::AT_CFReturnsNotRetained: |
| 4177 | case AttributeList::AT_NSReturnsRetained: |
| 4178 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4179 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4180 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4181 | case AttributeList::AT_WorkGroupSizeHint: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4182 | case AttributeList::AT_ReqdWorkGroupSize: |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4183 | handleWorkGroupSize(S, D, Attr); break; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 4184 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4185 | case AttributeList::AT_InitPriority: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4186 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4187 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4188 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4189 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4190 | case AttributeList::AT_Unavailable: |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4191 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable"); |
| 4192 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4193 | case AttributeList::AT_ArcWeakrefUnavailable: |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 4194 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 4195 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4196 | case AttributeList::AT_ObjCRootClass: |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4197 | handleObjCRootClassAttr(S, D, Attr); |
| 4198 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4199 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 4200 | handleObjCRequiresPropertyDefsAttr (S, D, Attr); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 4201 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4202 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4203 | case AttributeList::AT_ReturnsTwice: |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 4204 | handleReturnsTwiceAttr(S, D, Attr); |
| 4205 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4206 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
| 4207 | case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break; |
| 4208 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4209 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4210 | case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; |
| 4211 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4212 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4213 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4214 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4215 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4216 | case AttributeList::AT_ObjCException: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4217 | handleObjCExceptionAttr(S, D, Attr); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 4218 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4219 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4220 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4221 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4222 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4223 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4224 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
| 4225 | case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break; |
| 4226 | case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break; |
| 4227 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4228 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
| 4229 | case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break; |
| 4230 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4231 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4232 | // Just ignore |
| 4233 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4234 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4235 | handleNoInstrumentFunctionAttr(S, D, Attr); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 4236 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4237 | case AttributeList::AT_StdCall: |
| 4238 | case AttributeList::AT_CDecl: |
| 4239 | case AttributeList::AT_FastCall: |
| 4240 | case AttributeList::AT_ThisCall: |
| 4241 | case AttributeList::AT_Pascal: |
| 4242 | case AttributeList::AT_Pcs: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4243 | handleCallConvAttr(S, D, Attr); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4244 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4245 | case AttributeList::AT_OpenCLKernel: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4246 | handleOpenCLKernelAttr(S, D, Attr); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4247 | break; |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4248 | |
| 4249 | // Microsoft attributes: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4250 | case AttributeList::AT_MsStruct: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4251 | handleMsStructAttr(S, D, Attr); |
| 4252 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4253 | case AttributeList::AT_Uuid: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4254 | handleUuidAttr(S, D, Attr); |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4255 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4256 | case AttributeList::AT_SingleInheritance: |
| 4257 | case AttributeList::AT_MultipleInheritance: |
| 4258 | case AttributeList::AT_VirtualInheritance: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4259 | handleInheritanceAttr(S, D, Attr); |
| 4260 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4261 | case AttributeList::AT_Win64: |
| 4262 | case AttributeList::AT_Ptr32: |
| 4263 | case AttributeList::AT_Ptr64: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4264 | handlePortabilityAttr(S, D, Attr); |
| 4265 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4266 | case AttributeList::AT_ForceInline: |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4267 | handleForceInlineAttr(S, D, Attr); |
| 4268 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4269 | |
| 4270 | // Thread safety attributes: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4271 | case AttributeList::AT_GuardedVar: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4272 | handleGuardedVarAttr(S, D, Attr); |
| 4273 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4274 | case AttributeList::AT_PtGuardedVar: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4275 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4276 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4277 | case AttributeList::AT_ScopedLockable: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4278 | handleScopedLockableAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4279 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4280 | case AttributeList::AT_NoAddressSafetyAnalysis: |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4281 | handleNoAddressSafetyAttr(S, D, Attr); |
| 4282 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4283 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4284 | handleNoThreadSafetyAttr(S, D, Attr); |
| 4285 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4286 | case AttributeList::AT_Lockable: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4287 | handleLockableAttr(S, D, Attr); |
| 4288 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4289 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4290 | handleGuardedByAttr(S, D, Attr); |
| 4291 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4292 | case AttributeList::AT_PtGuardedBy: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4293 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4294 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4295 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4296 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4297 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4298 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4299 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4300 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4301 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4302 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4303 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4304 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4305 | handleLockReturnedAttr(S, D, Attr); |
| 4306 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4307 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4308 | handleLocksExcludedAttr(S, D, Attr); |
| 4309 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4310 | case AttributeList::AT_SharedLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4311 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4312 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4313 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4314 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4315 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4316 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4317 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4318 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4319 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4320 | handleUnlockFunAttr(S, D, Attr); |
| 4321 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4322 | case AttributeList::AT_AcquiredBefore: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4323 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4324 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4325 | case AttributeList::AT_AcquiredAfter: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4326 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4327 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4328 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4329 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4330 | // Ask target about the attribute. |
| 4331 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 4332 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4333 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 4334 | diag::warn_unhandled_ms_attribute_ignored : |
| 4335 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4336 | break; |
| 4337 | } |
| 4338 | } |
| 4339 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4340 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4341 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 4342 | /// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to |
| 4343 | /// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4). |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4344 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4345 | const AttributeList &Attr, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4346 | bool NonInheritable, bool Inheritable) { |
| 4347 | if (Attr.isInvalid()) |
| 4348 | return; |
| 4349 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4350 | // Type attributes are still treated as declaration attributes by |
| 4351 | // ParseMicrosoftTypeAttributes and ParseBorlandTypeAttributes. We don't |
| 4352 | // want to process them, however, because we will simply warn about ignoring |
| 4353 | // them. So instead, we will bail out early. |
| 4354 | if (Attr.isMSTypespecAttribute()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4355 | return; |
| 4356 | |
| 4357 | if (NonInheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4358 | ProcessNonInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4359 | |
| 4360 | if (Inheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4361 | ProcessInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4362 | } |
| 4363 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4364 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4365 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4366 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4367 | const AttributeList *AttrList, |
| 4368 | bool NonInheritable, bool Inheritable) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4369 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 4370 | ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable); |
Rafael Espindola | 9b79fc9 | 2012-05-07 23:58:18 +0000 | [diff] [blame] | 4371 | } |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4372 | |
| 4373 | // GCC accepts |
| 4374 | // static int a9 __attribute__((weakref)); |
| 4375 | // but that looks really pointless. We reject it. |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4376 | if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4377 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4378 | dyn_cast<NamedDecl>(D)->getNameAsString(); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4379 | return; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4380 | } |
| 4381 | } |
| 4382 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4383 | // Annotation attributes are the only attributes allowed after an access |
| 4384 | // specifier. |
| 4385 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4386 | const AttributeList *AttrList) { |
| 4387 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4388 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4389 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4390 | } else { |
| 4391 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4392 | return true; |
| 4393 | } |
| 4394 | } |
| 4395 | |
| 4396 | return false; |
| 4397 | } |
| 4398 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4399 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 4400 | /// contains any decl attributes that we should warn about. |
| 4401 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 4402 | for ( ; A; A = A->getNext()) { |
| 4403 | // Only warn if the attribute is an unignored, non-type attribute. |
| 4404 | if (A->isUsedAsTypeAttr()) continue; |
| 4405 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 4406 | |
| 4407 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 4408 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 4409 | << A->getName() << A->getRange(); |
| 4410 | } else { |
| 4411 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 4412 | << A->getName() << A->getRange(); |
| 4413 | } |
| 4414 | } |
| 4415 | } |
| 4416 | |
| 4417 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 4418 | /// used to build a declaration, complain about any decl attributes |
| 4419 | /// which might be lying around on it. |
| 4420 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 4421 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 4422 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 4423 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 4424 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 4425 | } |
| 4426 | |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4427 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4428 | /// \#pragma weak needs a non-definition decl and source may not have one. |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4429 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 4430 | SourceLocation Loc) { |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4431 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4432 | NamedDecl *NewD = 0; |
| 4433 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4434 | FunctionDecl *NewFD; |
| 4435 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 4436 | // FIXME: Mangling? |
| 4437 | // FIXME: Is the qualifier info correct? |
| 4438 | // FIXME: Is the DeclContext correct? |
| 4439 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 4440 | Loc, Loc, DeclarationName(II), |
| 4441 | FD->getType(), FD->getTypeSourceInfo(), |
| 4442 | SC_None, SC_None, |
| 4443 | false/*isInlineSpecified*/, |
| 4444 | FD->hasPrototype(), |
| 4445 | false/*isConstexprSpecified*/); |
| 4446 | NewD = NewFD; |
| 4447 | |
| 4448 | if (FD->getQualifier()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4449 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4450 | |
| 4451 | // Fake up parameter variables; they are declared as if this were |
| 4452 | // a typedef. |
| 4453 | QualType FDTy = FD->getType(); |
| 4454 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 4455 | SmallVector<ParmVarDecl*, 16> Params; |
| 4456 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 4457 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 4458 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 4459 | Param->setScopeInfo(0, Params.size()); |
| 4460 | Params.push_back(Param); |
| 4461 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 4462 | NewFD->setParams(Params); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4463 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4464 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 4465 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4466 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4467 | VD->getType(), VD->getTypeSourceInfo(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 4468 | VD->getStorageClass(), |
| 4469 | VD->getStorageClassAsWritten()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4470 | if (VD->getQualifier()) { |
| 4471 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4472 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4473 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4474 | } |
| 4475 | return NewD; |
| 4476 | } |
| 4477 | |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4478 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4479 | /// applied to it, possibly with an alias. |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4480 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4481 | if (W.getUsed()) return; // only do this once |
| 4482 | W.setUsed(true); |
| 4483 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 4484 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4485 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4486 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 4487 | NDId->getName())); |
| 4488 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4489 | WeakTopLevelDecl.push_back(NewD); |
| 4490 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 4491 | // to insert Decl at TU scope, sorry. |
| 4492 | DeclContext *SavedContext = CurContext; |
| 4493 | CurContext = Context.getTranslationUnitDecl(); |
| 4494 | PushOnScopeChains(NewD, S); |
| 4495 | CurContext = SavedContext; |
| 4496 | } else { // just add weak to existing |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4497 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4498 | } |
| 4499 | } |
| 4500 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4501 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 4502 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 4503 | /// 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] | 4504 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD, |
| 4505 | bool NonInheritable, bool Inheritable) { |
John McCall | d4aff0e | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 4506 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 4507 | // have to do this. |
Douglas Gregor | 31e37b2 | 2011-07-28 18:09:57 +0000 | [diff] [blame] | 4508 | if (Inheritable) { |
| 4509 | LoadExternalWeakUndeclaredIdentifiers(); |
| 4510 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 4511 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
| 4512 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 4513 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 4514 | = WeakUndeclaredIdentifiers.find(Id); |
| 4515 | if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) { |
| 4516 | WeakInfo W = I->second; |
| 4517 | DeclApplyPragmaWeak(S, ND, W); |
| 4518 | WeakUndeclaredIdentifiers[Id] = W; |
| 4519 | } |
John McCall | d4aff0e | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 4520 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4521 | } |
| 4522 | } |
| 4523 | } |
| 4524 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4525 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4526 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4527 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4528 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4529 | // Walk the declarator structure, applying decl attributes that were in a type |
| 4530 | // position to the decl itself. This handles cases like: |
| 4531 | // int *__attr__(x)** D; |
| 4532 | // when X is a decl attribute. |
| 4533 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 4534 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4535 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4536 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4537 | // Finally, apply any attributes on the decl itself. |
| 4538 | if (const AttributeList *Attrs = PD.getAttributes()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4539 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4540 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4541 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4542 | /// Is the given declaration allowed to use a forbidden type? |
| 4543 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 4544 | // Private ivars are always okay. Unfortunately, people don't |
| 4545 | // always properly make their ivars private, even in system headers. |
| 4546 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | a6b3380 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 4547 | // Function declarations in sys headers will be marked unavailable. |
| 4548 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 4549 | !isa<FunctionDecl>(decl)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4550 | return false; |
| 4551 | |
| 4552 | // Require it to be declared in a system header. |
| 4553 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 4554 | } |
| 4555 | |
| 4556 | /// Handle a delayed forbidden-type diagnostic. |
| 4557 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 4558 | Decl *decl) { |
| 4559 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 4560 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 4561 | "this system declaration uses an unsupported type")); |
| 4562 | return; |
| 4563 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4564 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4565 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 4566 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4567 | // kind of forbidden type messages on unavailable functions. |
| 4568 | if (FD->hasAttr<UnavailableAttr>() && |
| 4569 | diag.getForbiddenTypeDiagnostic() == |
| 4570 | diag::err_arc_array_param_no_ownership) { |
| 4571 | diag.Triggered = true; |
| 4572 | return; |
| 4573 | } |
| 4574 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4575 | |
| 4576 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 4577 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 4578 | diag.Triggered = true; |
| 4579 | } |
| 4580 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4581 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 4582 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4583 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4584 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4585 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4586 | // When delaying diagnostics to run in the context of a parsed |
| 4587 | // declaration, we only want to actually emit anything if parsing |
| 4588 | // succeeds. |
| 4589 | if (!decl) return; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4590 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4591 | // We emit all the active diagnostics in this pool or any of its |
| 4592 | // parents. In general, we'll get one pool for the decl spec |
| 4593 | // and a child pool for each declarator; in a decl group like: |
| 4594 | // deprecated_typedef foo, *bar, baz(); |
| 4595 | // only the declarator pops will be passed decls. This is correct; |
| 4596 | // we really do need to consider delayed diagnostics from the decl spec |
| 4597 | // for each of the different declarations. |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4598 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4599 | do { |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4600 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4601 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 4602 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 4603 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4604 | if (diag.Triggered) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4605 | continue; |
| 4606 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4607 | switch (diag.Kind) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4608 | case DelayedDiagnostic::Deprecation: |
John McCall | e8c904f | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 4609 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 4610 | if (!decl->isInvalidDecl()) |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4611 | HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4612 | break; |
| 4613 | |
| 4614 | case DelayedDiagnostic::Access: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4615 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4616 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4617 | |
| 4618 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4619 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4620 | break; |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4621 | } |
| 4622 | } |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4623 | } while ((pool = pool->getParent())); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4624 | } |
| 4625 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4626 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 4627 | /// been delayed in the current context instead of in the given pool. |
| 4628 | /// Essentially, this just moves them to the current pool. |
| 4629 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 4630 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 4631 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 4632 | curPool->steal(pool); |
| 4633 | } |
| 4634 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4635 | static bool isDeclDeprecated(Decl *D) { |
| 4636 | do { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 4637 | if (D->isDeprecated()) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4638 | return true; |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 4639 | // A category implicitly has the availability of the interface. |
| 4640 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4641 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4642 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4643 | return false; |
| 4644 | } |
| 4645 | |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4646 | static void |
| 4647 | DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message, |
| 4648 | SourceLocation Loc, |
| 4649 | const ObjCInterfaceDecl *UnknownObjCClass) { |
| 4650 | DeclarationName Name = D->getDeclName(); |
| 4651 | if (!Message.empty()) { |
| 4652 | S.Diag(Loc, diag::warn_deprecated_message) << Name << Message; |
| 4653 | S.Diag(D->getLocation(), |
| 4654 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 4655 | : diag::note_previous_decl) << Name; |
| 4656 | } else if (!UnknownObjCClass) { |
| 4657 | S.Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 4658 | S.Diag(D->getLocation(), |
| 4659 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 4660 | : diag::note_previous_decl) << Name; |
| 4661 | } else { |
| 4662 | S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name; |
| 4663 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 4664 | } |
| 4665 | } |
| 4666 | |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 4667 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4668 | Decl *Ctx) { |
| 4669 | if (isDeclDeprecated(Ctx)) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4670 | return; |
| 4671 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4672 | DD.Triggered = true; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4673 | DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(), |
| 4674 | DD.getDeprecationMessage(), DD.Loc, |
| 4675 | DD.getUnknownObjCClass()); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4676 | } |
| 4677 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4678 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 4679 | SourceLocation Loc, |
Fariborz Jahanian | 89ebaed | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 4680 | const ObjCInterfaceDecl *UnknownObjCClass) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4681 | // Delay if we're currently parsing a declaration. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4682 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 4683 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 4684 | UnknownObjCClass, |
| 4685 | Message)); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4686 | return; |
| 4687 | } |
| 4688 | |
| 4689 | // Otherwise, don't warn if our current context is deprecated. |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 4690 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4691 | return; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4692 | DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4693 | } |