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" |
Daniel Dunbar | acc5f3e | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | acc5f3e | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 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, |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 40 | ExpectedFunctionMethodOrClass, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 41 | ExpectedFunctionMethodOrParameter, |
| 42 | ExpectedClass, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 43 | ExpectedVariable, |
| 44 | ExpectedMethod, |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 45 | ExpectedVariableFunctionOrLabel, |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 46 | ExpectedFieldOrGlobalVar, |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 47 | ExpectedStruct, |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 48 | ExpectedVariableFunctionOrTag, |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 49 | ExpectedTLSVar |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 52 | //===----------------------------------------------------------------------===// |
| 53 | // Helper functions |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 56 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 57 | bool blocksToo = true) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 58 | QualType Ty; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 59 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(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 FieldDecl *decl = dyn_cast<FieldDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 62 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 63 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 64 | Ty = decl->getUnderlyingType(); |
| 65 | else |
| 66 | return 0; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 68 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 69 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 755f9d2 | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 70 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 71 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 72 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 73 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 76 | // FIXME: We should provide an abstraction around a method or function |
| 77 | // to provide the following bits of information. |
| 78 | |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 79 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 80 | /// type (function or function-typed variable). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 81 | static bool isFunction(const Decl *D) { |
| 82 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 86 | /// type (function or function-typed variable) or an Objective-C |
| 87 | /// method. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 88 | static bool isFunctionOrMethod(const Decl *D) { |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 89 | return isFunction(D) || isa<ObjCMethodDecl>(D); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 92 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 93 | /// type (function or function-typed variable) or an Objective-C |
| 94 | /// method or a block. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 95 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 96 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 97 | return true; |
| 98 | // check for block is more involved. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 99 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 100 | QualType Ty = V->getType(); |
| 101 | return Ty->isBlockPointerType(); |
| 102 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 103 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 104 | } |
| 105 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 106 | /// Return true if the given decl has a declarator that should have |
| 107 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 108 | static bool hasDeclarator(const Decl *D) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 109 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 110 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 111 | isa<ObjCPropertyDecl>(D); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 114 | /// hasFunctionProto - Return true if the given decl has a argument |
| 115 | /// information. This decl should have already passed |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 116 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 117 | static bool hasFunctionProto(const Decl *D) { |
| 118 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 119 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 120 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 121 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 122 | return true; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 127 | /// arguments. It is an error to call this on a K&R function (use |
| 128 | /// hasFunctionProto first). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 129 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 130 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 131 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 132 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 133 | return BD->getNumParams(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 134 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 137 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 138 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 139 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 140 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 141 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 142 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 143 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 146 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 147 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 148 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 149 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 152 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 153 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 154 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 155 | return proto->isVariadic(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 156 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | db9a0ae | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 157 | return BD->isVariadic(); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 158 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 159 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 163 | static bool isInstanceMethod(const Decl *D) { |
| 164 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 165 | return MethodDecl->isInstance(); |
| 166 | return false; |
| 167 | } |
| 168 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 169 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 170 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 171 | if (!PT) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 172 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 173 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 174 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 175 | if (!Cls) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 176 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 177 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 178 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 180 | // FIXME: Should we walk the chain of classes? |
| 181 | return ClsName == &Ctx.Idents.get("NSString") || |
| 182 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 183 | } |
| 184 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 185 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 186 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 187 | if (!PT) |
| 188 | return false; |
| 189 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 190 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 191 | if (!RT) |
| 192 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 193 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 194 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 195 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 196 | return false; |
| 197 | |
| 198 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 199 | } |
| 200 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 201 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 202 | /// output an error. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 203 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 204 | unsigned int Num) { |
| 205 | if (Attr.getNumArgs() != Num) { |
| 206 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num; |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | return true; |
| 211 | } |
| 212 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 213 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 214 | /// \brief Check if the attribute has at least as many args as Num. May |
| 215 | /// output an error. |
| 216 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
| 217 | unsigned int Num) { |
| 218 | if (Attr.getNumArgs() < Num) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 219 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| 220 | return false; |
| 221 | } |
| 222 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 223 | return true; |
| 224 | } |
| 225 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 226 | /// \brief Check if IdxExpr is a valid argument index for a function or |
| 227 | /// instance method D. May output an error. |
| 228 | /// |
| 229 | /// \returns true if IdxExpr is a valid index. |
| 230 | static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D, |
| 231 | StringRef AttrName, |
| 232 | SourceLocation AttrLoc, |
| 233 | unsigned AttrArgNum, |
| 234 | const Expr *IdxExpr, |
| 235 | uint64_t &Idx) |
| 236 | { |
| 237 | assert(isFunctionOrMethod(D) && hasFunctionProto(D)); |
| 238 | |
| 239 | // In C++ the implicit 'this' function parameter also counts. |
| 240 | // Parameters are counted from one. |
| 241 | const bool HasImplicitThisParam = isInstanceMethod(D); |
| 242 | const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| 243 | const unsigned FirstIdx = 1; |
| 244 | |
| 245 | llvm::APSInt IdxInt; |
| 246 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 247 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
| 248 | S.Diag(AttrLoc, diag::err_attribute_argument_n_not_int) |
| 249 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | Idx = IdxInt.getLimitedValue(); |
| 254 | if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) { |
| 255 | S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds) |
| 256 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 257 | return false; |
| 258 | } |
| 259 | Idx--; // Convert to zero-based. |
| 260 | if (HasImplicitThisParam) { |
| 261 | if (Idx == 0) { |
| 262 | S.Diag(AttrLoc, |
| 263 | diag::err_attribute_invalid_implicit_this_argument) |
| 264 | << AttrName << IdxExpr->getSourceRange(); |
| 265 | return false; |
| 266 | } |
| 267 | --Idx; |
| 268 | } |
| 269 | |
| 270 | return true; |
| 271 | } |
| 272 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 273 | /// |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 274 | /// \brief Check if passed in Decl is a field or potentially shared global var |
| 275 | /// \return true if the Decl is a field or potentially shared global variable |
| 276 | /// |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 277 | static bool mayBeSharedVariable(const Decl *D) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 278 | if (isa<FieldDecl>(D)) |
| 279 | return true; |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 280 | if (const VarDecl *vd = dyn_cast<VarDecl>(D)) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 281 | return (vd->hasGlobalStorage() && !(vd->isThreadSpecified())); |
| 282 | |
| 283 | return false; |
| 284 | } |
| 285 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 286 | /// \brief Check if the passed-in expression is of type int or bool. |
| 287 | static bool isIntOrBool(Expr *Exp) { |
| 288 | QualType QT = Exp->getType(); |
| 289 | return QT->isBooleanType() || QT->isIntegerType(); |
| 290 | } |
| 291 | |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 292 | |
| 293 | // Check to see if the type is a smart pointer of some kind. We assume |
| 294 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 295 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 296 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 297 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 298 | if (Res1.empty()) |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 299 | return false; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 300 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 301 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 302 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 303 | if (Res2.empty()) |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 304 | return false; |
| 305 | |
| 306 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 309 | /// \brief Check if passed in Decl is a pointer type. |
| 310 | /// Note that this function may produce an error message. |
| 311 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 312 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 313 | const AttributeList &Attr) { |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 314 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 315 | QualType QT = vd->getType(); |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 316 | if (QT->isAnyPointerType()) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 317 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 318 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 319 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 320 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 321 | // (We don't want to force template instantiation if we can avoid it, |
| 322 | // since that would alter the order in which templates are instantiated.) |
| 323 | if (RT->isIncompleteType()) |
| 324 | return true; |
| 325 | |
| 326 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 327 | return true; |
| 328 | } |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 329 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 330 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 331 | << Attr.getName()->getName() << QT; |
| 332 | } else { |
| 333 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 334 | << Attr.getName(); |
| 335 | } |
| 336 | return false; |
| 337 | } |
| 338 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 339 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 340 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 341 | static const RecordType *getRecordType(QualType QT) { |
| 342 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 343 | return RT; |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 344 | |
| 345 | // Now check if we point to record type. |
| 346 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 347 | return PT->getPointeeType()->getAs<RecordType>(); |
| 348 | |
| 349 | return 0; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 350 | } |
| 351 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 352 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 353 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 354 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 355 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
| 356 | if (RT->getDecl()->getAttr<LockableAttr>()) |
| 357 | return true; |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 362 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 363 | /// resolves to a lockable object. |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 364 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 365 | QualType Ty) { |
| 366 | const RecordType *RT = getRecordType(Ty); |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 367 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 368 | // Warn if could not get record type for this argument. |
Benjamin Kramer | d77ba89 | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 369 | if (!RT) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 370 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 371 | << Attr.getName() << Ty.getAsString(); |
| 372 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 373 | } |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 374 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 375 | // 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] | 376 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 377 | return; |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 378 | |
| 379 | // Allow smart pointers to be used as lockable objects. |
| 380 | // FIXME -- Check the type that the smart pointer points to. |
| 381 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 382 | return; |
| 383 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 384 | // Check if the type is lockable. |
| 385 | RecordDecl *RD = RT->getDecl(); |
| 386 | if (RD->getAttr<LockableAttr>()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 387 | return; |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 388 | |
| 389 | // Else check if any base classes are lockable. |
| 390 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 391 | CXXBasePaths BPaths(false, false); |
| 392 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 393 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 394 | } |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 395 | |
| 396 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 397 | << Attr.getName() << Ty.getAsString(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 400 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 401 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 402 | /// \param Sidx The attribute argument index to start checking with. |
| 403 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 404 | /// parameter list. |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 405 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 406 | const AttributeList &Attr, |
| 407 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 408 | int Sidx = 0, |
| 409 | bool ParamIdxOk = false) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 410 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 411 | Expr *ArgExp = Attr.getArg(Idx); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 412 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 413 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 414 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 415 | Args.push_back(ArgExp); |
| 416 | continue; |
| 417 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 418 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 419 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 420 | if (StrLit->getLength() == 0 || |
| 421 | StrLit->getString() == StringRef("*")) { |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 422 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 423 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 424 | Args.push_back(ArgExp); |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 425 | continue; |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 426 | } |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 427 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 428 | // We allow constant strings to be used as a placeholder for expressions |
| 429 | // that are not valid C++ syntax, but warn that they are ignored. |
| 430 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 431 | Attr.getName(); |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 432 | Args.push_back(ArgExp); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 433 | continue; |
| 434 | } |
| 435 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 436 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 437 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 438 | // A pointer to member expression of the form &MyClass::mu is treated |
| 439 | // specially -- we need to look at the type of the member. |
| 440 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 441 | if (UOp->getOpcode() == UO_AddrOf) |
| 442 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 443 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 444 | ArgTy = DRE->getDecl()->getType(); |
| 445 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 446 | // First see if we can just cast to record type, or point to record type. |
| 447 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 448 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 449 | // Now check if we index into a record type function param. |
| 450 | if(!RT && ParamIdxOk) { |
| 451 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 452 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 453 | if(FD && IL) { |
| 454 | unsigned int NumParams = FD->getNumParams(); |
| 455 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 456 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 457 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 458 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 459 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 460 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 461 | continue; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 462 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 463 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 467 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 468 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 469 | Args.push_back(ArgExp); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 470 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 473 | //===----------------------------------------------------------------------===// |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 474 | // Attribute Implementations |
| 475 | //===----------------------------------------------------------------------===// |
| 476 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 477 | // FIXME: All this manual attribute parsing code is gross. At the |
| 478 | // least add some helper functions to check most argument patterns (# |
| 479 | // and types of args). |
| 480 | |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 481 | enum ThreadAttributeDeclKind { |
| 482 | ThreadExpectedFieldOrGlobalVar, |
| 483 | ThreadExpectedFunctionOrMethod, |
| 484 | ThreadExpectedClassOrStruct |
| 485 | }; |
| 486 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 487 | static bool checkGuardedVarAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 488 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 489 | assert(!Attr.isInvalid()); |
| 490 | |
| 491 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 492 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 493 | |
| 494 | // D must be either a member field or global (potentially shared) variable. |
| 495 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 496 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 497 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 498 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 501 | return true; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 504 | static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 505 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 506 | return; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 507 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 508 | D->addAttr(::new (S.Context) |
| 509 | GuardedVarAttr(Attr.getRange(), S.Context, |
| 510 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 513 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 514 | const AttributeList &Attr) { |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 515 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 516 | return; |
| 517 | |
| 518 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 519 | return; |
| 520 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 521 | D->addAttr(::new (S.Context) |
| 522 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 523 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 526 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 527 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 528 | Expr* &Arg) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 529 | assert(!Attr.isInvalid()); |
| 530 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 531 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 532 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 533 | |
| 534 | // D must be either a member field or global (potentially shared) variable. |
| 535 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 536 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 537 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 538 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 539 | } |
| 540 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 541 | SmallVector<Expr*, 1> Args; |
| 542 | // check that all arguments are lockable objects |
| 543 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 544 | unsigned Size = Args.size(); |
| 545 | if (Size != 1) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 546 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 547 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 548 | Arg = Args[0]; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 549 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 550 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 553 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 554 | Expr *Arg = 0; |
| 555 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 556 | return; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 557 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 558 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| 559 | } |
| 560 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 561 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 562 | const AttributeList &Attr) { |
| 563 | Expr *Arg = 0; |
| 564 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 565 | return; |
| 566 | |
| 567 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 568 | return; |
| 569 | |
| 570 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| 571 | S.Context, Arg)); |
| 572 | } |
| 573 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 574 | static bool checkLockableAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 575 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 576 | assert(!Attr.isInvalid()); |
| 577 | |
| 578 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 579 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 580 | |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 581 | // FIXME: Lockable structs for C code. |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 582 | if (!isa<CXXRecordDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 583 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 584 | << Attr.getName() << ThreadExpectedClassOrStruct; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 585 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 588 | return true; |
| 589 | } |
| 590 | |
| 591 | static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 592 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 593 | return; |
| 594 | |
| 595 | D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); |
| 596 | } |
| 597 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 598 | static void handleScopedLockableAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 599 | const AttributeList &Attr) { |
| 600 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 601 | return; |
| 602 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 603 | D->addAttr(::new (S.Context) |
| 604 | ScopedLockableAttr(Attr.getRange(), S.Context, |
| 605 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | static void handleNoThreadSafetyAttr(Sema &S, Decl *D, |
| 609 | const AttributeList &Attr) { |
| 610 | assert(!Attr.isInvalid()); |
| 611 | |
| 612 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 613 | return; |
| 614 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 615 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 616 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 617 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 618 | return; |
| 619 | } |
| 620 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 621 | D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(), |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 622 | S.Context)); |
| 623 | } |
| 624 | |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 625 | static void handleNoAddressSafetyAttr(Sema &S, Decl *D, |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 626 | const AttributeList &Attr) { |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 627 | assert(!Attr.isInvalid()); |
| 628 | |
| 629 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 630 | return; |
| 631 | |
| 632 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 633 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 634 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 635 | return; |
| 636 | } |
| 637 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 638 | D->addAttr(::new (S.Context) |
| 639 | NoAddressSafetyAnalysisAttr(Attr.getRange(), S.Context, |
| 640 | Attr.getAttributeSpellingListIndex())); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 643 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 644 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 645 | SmallVector<Expr*, 1> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 646 | assert(!Attr.isInvalid()); |
| 647 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 648 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 649 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 650 | |
| 651 | // D must be either a member field or global (potentially shared) variable. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 652 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 653 | if (!VD || !mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 654 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 655 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 656 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 657 | } |
| 658 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 659 | // Check that this attribute only applies to lockable types. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 660 | QualType QT = VD->getType(); |
| 661 | if (!QT->isDependentType()) { |
| 662 | const RecordType *RT = getRecordType(QT); |
| 663 | if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 664 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 665 | << Attr.getName(); |
| 666 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 670 | // Check that all arguments are lockable objects. |
| 671 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 672 | if (Args.size() == 0) |
| 673 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 674 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 675 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 678 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 679 | const AttributeList &Attr) { |
| 680 | SmallVector<Expr*, 1> Args; |
| 681 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 682 | return; |
| 683 | |
| 684 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 685 | D->addAttr(::new (S.Context) |
| 686 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 687 | StartArg, Args.size(), |
| 688 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 691 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 692 | const AttributeList &Attr) { |
| 693 | SmallVector<Expr*, 1> Args; |
| 694 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 695 | return; |
| 696 | |
| 697 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 698 | D->addAttr(::new (S.Context) |
| 699 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 700 | StartArg, Args.size(), |
| 701 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 704 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 705 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 706 | SmallVector<Expr*, 1> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 707 | assert(!Attr.isInvalid()); |
| 708 | |
| 709 | // zero or more arguments ok |
| 710 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 711 | // check that the attribute is applied to a function |
| 712 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 713 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 714 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 715 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 718 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 719 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 720 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 721 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 724 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 725 | const AttributeList &Attr) { |
| 726 | SmallVector<Expr*, 1> Args; |
| 727 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 728 | return; |
| 729 | |
| 730 | unsigned Size = Args.size(); |
| 731 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 732 | D->addAttr(::new (S.Context) |
| 733 | SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 734 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 737 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 738 | const AttributeList &Attr) { |
| 739 | SmallVector<Expr*, 1> Args; |
| 740 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 741 | return; |
| 742 | |
| 743 | unsigned Size = Args.size(); |
| 744 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 745 | D->addAttr(::new (S.Context) |
| 746 | ExclusiveLockFunctionAttr(Attr.getRange(), S.Context, |
| 747 | StartArg, Size, |
| 748 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 751 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 752 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 753 | SmallVector<Expr*, 2> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 754 | assert(!Attr.isInvalid()); |
| 755 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 756 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 757 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 758 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 759 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 760 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 761 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 762 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 765 | if (!isIntOrBool(Attr.getArg(0))) { |
| 766 | 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] | 767 | << Attr.getName(); |
| 768 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 772 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 773 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 774 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 777 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 778 | const AttributeList &Attr) { |
| 779 | SmallVector<Expr*, 2> Args; |
| 780 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 781 | return; |
| 782 | |
| 783 | unsigned Size = Args.size(); |
| 784 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 785 | D->addAttr(::new (S.Context) |
| 786 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
| 787 | Attr.getArg(0), StartArg, Size, |
| 788 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 791 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 792 | const AttributeList &Attr) { |
| 793 | SmallVector<Expr*, 2> Args; |
| 794 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 795 | return; |
| 796 | |
| 797 | unsigned Size = Args.size(); |
| 798 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 799 | D->addAttr(::new (S.Context) |
| 800 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
| 801 | Attr.getArg(0), StartArg, Size, |
| 802 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 805 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 806 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 807 | SmallVector<Expr*, 1> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 808 | assert(!Attr.isInvalid()); |
| 809 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 810 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 811 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 812 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 813 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 814 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 815 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 816 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 819 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 820 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 821 | if (Args.size() == 0) |
| 822 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 823 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 824 | return true; |
| 825 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 826 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 827 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 828 | const AttributeList &Attr) { |
| 829 | SmallVector<Expr*, 1> Args; |
| 830 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 831 | return; |
| 832 | |
| 833 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 834 | D->addAttr(::new (S.Context) |
| 835 | ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context, |
| 836 | StartArg, Args.size(), |
| 837 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 840 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 841 | const AttributeList &Attr) { |
| 842 | SmallVector<Expr*, 1> Args; |
| 843 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 844 | return; |
| 845 | |
| 846 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 847 | D->addAttr(::new (S.Context) |
| 848 | SharedLocksRequiredAttr(Attr.getRange(), S.Context, |
| 849 | StartArg, Args.size(), |
| 850 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 853 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 854 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 855 | assert(!Attr.isInvalid()); |
| 856 | |
| 857 | // zero or more arguments ok |
| 858 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 859 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 860 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 861 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 862 | return; |
| 863 | } |
| 864 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 865 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 866 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 867 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 868 | unsigned Size = Args.size(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 869 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 870 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 871 | D->addAttr(::new (S.Context) |
| 872 | UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 873 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 877 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 878 | assert(!Attr.isInvalid()); |
| 879 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 880 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 881 | return; |
| 882 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 883 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 884 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 885 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 886 | return; |
| 887 | } |
| 888 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 889 | // check that the argument is lockable object |
DeLesley Hutchins | f26efd7 | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 890 | SmallVector<Expr*, 1> Args; |
| 891 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 892 | unsigned Size = Args.size(); |
| 893 | if (Size == 0) |
| 894 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 895 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 896 | D->addAttr(::new (S.Context) |
| 897 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 898 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 902 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 903 | assert(!Attr.isInvalid()); |
| 904 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 905 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 906 | return; |
| 907 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 908 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 909 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 910 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 911 | return; |
| 912 | } |
| 913 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 914 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 915 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 916 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 917 | unsigned Size = Args.size(); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 918 | if (Size == 0) |
| 919 | return; |
| 920 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 921 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 922 | D->addAttr(::new (S.Context) |
| 923 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 924 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 928 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 929 | const AttributeList &Attr) { |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 930 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 931 | if (TD == 0) { |
| 932 | // __attribute__((ext_vector_type(N))) can only be applied to typedefs |
| 933 | // and type-ids. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 934 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 935 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 936 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 937 | |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 938 | // Remember this typedef decl, we will need it later for diagnostics. |
| 939 | S.ExtVectorDecls.push_back(TD); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 942 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 943 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 944 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 945 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 946 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 947 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 948 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 949 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 950 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 951 | // has no effect. |
Eli Friedman | b68ec6b | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 952 | if (!FD->getType()->isDependentType() && |
| 953 | !FD->getType()->isIncompleteType() && |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 954 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 955 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 956 | << Attr.getName() << FD->getType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 957 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 958 | FD->addAttr(::new (S.Context) |
| 959 | PackedAttr(Attr.getRange(), S.Context, |
| 960 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 961 | } else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 962 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 965 | static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 5f608ae | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 966 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 967 | RD->addAttr(::new (S.Context) |
| 968 | MsStructAttr(Attr.getRange(), S.Context, |
| 969 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | c1a0a73 | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 970 | else |
| 971 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 972 | } |
| 973 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 974 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 975 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 976 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 977 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 978 | |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 979 | // The IBAction attributes only apply to instance methods. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 980 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 981 | if (MD->isInstanceMethod()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 982 | D->addAttr(::new (S.Context) |
| 983 | IBActionAttr(Attr.getRange(), S.Context, |
| 984 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 985 | return; |
| 986 | } |
| 987 | |
Ted Kremenek | 4ee2bb1 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 988 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 991 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 992 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 993 | // variables or properties of Objective-C classes. The outlet must also |
| 994 | // have an object reference type. |
| 995 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 996 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 0bfaf06 | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 997 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 998 | << Attr.getName() << VD->getType() << 0; |
| 999 | return false; |
| 1000 | } |
| 1001 | } |
| 1002 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1003 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1004 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1005 | << Attr.getName() << PD->getType() << 1; |
| 1006 | return false; |
| 1007 | } |
| 1008 | } |
| 1009 | else { |
| 1010 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1011 | return false; |
| 1012 | } |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1013 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1014 | return true; |
| 1015 | } |
| 1016 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1017 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1018 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1019 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1020 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1021 | |
| 1022 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1023 | return; |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1024 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1025 | D->addAttr(::new (S.Context) |
| 1026 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1027 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1030 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1031 | const AttributeList &Attr) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1032 | |
| 1033 | // The iboutletcollection attribute can have zero or one arguments. |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1034 | if (Attr.getParameterName() && Attr.getNumArgs() > 0) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1035 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1036 | return; |
| 1037 | } |
| 1038 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1039 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1040 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1041 | |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1042 | IdentifierInfo *II = Attr.getParameterName(); |
| 1043 | if (!II) |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1044 | II = &S.Context.Idents.get("NSObject"); |
Fariborz Jahanian | 3a3400b | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 1045 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1046 | ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1047 | S.getScopeForContext(D->getDeclContext()->getParent())); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1048 | if (!TypeRep) { |
| 1049 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1050 | return; |
| 1051 | } |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1052 | QualType QT = TypeRep.get(); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1053 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1054 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1055 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1056 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1057 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1058 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1059 | return; |
| 1060 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1061 | D->addAttr(::new (S.Context) |
| 1062 | IBOutletCollectionAttr(Attr.getRange(),S.Context, |
| 1063 | QT, Attr.getParameterLoc(), |
| 1064 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1067 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1068 | if (const RecordType *UT = T->getAsUnionType()) |
| 1069 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1070 | RecordDecl *UD = UT->getDecl(); |
| 1071 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1072 | itend = UD->field_end(); it != itend; ++it) { |
| 1073 | QualType QT = it->getType(); |
| 1074 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1075 | T = QT; |
| 1076 | return; |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1082 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nuno Lopes | 174930d | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1083 | if (!isFunctionOrMethod(D)) { |
| 1084 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1085 | << "alloc_size" << ExpectedFunctionOrMethod; |
| 1086 | return; |
| 1087 | } |
| 1088 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1089 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1090 | return; |
| 1091 | |
| 1092 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1093 | // counted from one. |
| 1094 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1095 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| 1096 | |
| 1097 | SmallVector<unsigned, 8> SizeArgs; |
| 1098 | |
| 1099 | for (AttributeList::arg_iterator I = Attr.arg_begin(), |
| 1100 | E = Attr.arg_end(); I!=E; ++I) { |
| 1101 | // The argument must be an integer constant expression. |
| 1102 | Expr *Ex = *I; |
| 1103 | llvm::APSInt ArgNum; |
| 1104 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 1105 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1106 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1107 | << "alloc_size" << Ex->getSourceRange(); |
| 1108 | return; |
| 1109 | } |
| 1110 | |
| 1111 | uint64_t x = ArgNum.getZExtValue(); |
| 1112 | |
| 1113 | if (x < 1 || x > NumArgs) { |
| 1114 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1115 | << "alloc_size" << I.getArgNum() << Ex->getSourceRange(); |
| 1116 | return; |
| 1117 | } |
| 1118 | |
| 1119 | --x; |
| 1120 | if (HasImplicitThisParam) { |
| 1121 | if (x == 0) { |
| 1122 | S.Diag(Attr.getLoc(), |
| 1123 | diag::err_attribute_invalid_implicit_this_argument) |
| 1124 | << "alloc_size" << Ex->getSourceRange(); |
| 1125 | return; |
| 1126 | } |
| 1127 | --x; |
| 1128 | } |
| 1129 | |
| 1130 | // check if the function argument is of an integer type |
| 1131 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
| 1132 | if (!T->isIntegerType()) { |
| 1133 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1134 | << "alloc_size" << Ex->getSourceRange(); |
| 1135 | return; |
| 1136 | } |
| 1137 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1138 | SizeArgs.push_back(x); |
| 1139 | } |
| 1140 | |
| 1141 | // check if the function returns a pointer |
| 1142 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 1143 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
| 1144 | << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
| 1145 | } |
| 1146 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1147 | D->addAttr(::new (S.Context) |
| 1148 | AllocSizeAttr(Attr.getRange(), S.Context, |
| 1149 | SizeArgs.data(), SizeArgs.size(), |
| 1150 | Attr.getAttributeSpellingListIndex())); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1153 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1154 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1155 | // ignore it as well |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1156 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1157 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1158 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1159 | return; |
| 1160 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1161 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1162 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1163 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1164 | bool HasImplicitThisParam = isInstanceMethod(D); |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1165 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1166 | |
| 1167 | // The nonnull attribute only applies to pointers. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1168 | SmallVector<unsigned, 10> NonNullArgs; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1169 | |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1170 | for (AttributeList::arg_iterator I = Attr.arg_begin(), |
| 1171 | E = Attr.arg_end(); I != E; ++I) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1172 | // The argument must be an integer constant expression. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1173 | Expr *Ex = *I; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1174 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1175 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 1176 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1177 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1178 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1179 | return; |
| 1180 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1181 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1182 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1183 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1184 | if (x < 1 || x > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1185 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 1186 | << "nonnull" << I.getArgNum() << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1187 | return; |
| 1188 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1189 | |
Ted Kremenek | 465172f | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 1190 | --x; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1191 | if (HasImplicitThisParam) { |
| 1192 | if (x == 0) { |
| 1193 | S.Diag(Attr.getLoc(), |
| 1194 | diag::err_attribute_invalid_implicit_this_argument) |
| 1195 | << "nonnull" << Ex->getSourceRange(); |
| 1196 | return; |
| 1197 | } |
| 1198 | --x; |
| 1199 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1200 | |
| 1201 | // Is the function argument a pointer type? |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1202 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1203 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1204 | |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1205 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1206 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | c9ef405 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1207 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1208 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1209 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1210 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1211 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1212 | NonNullArgs.push_back(x); |
| 1213 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1214 | |
| 1215 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1216 | // arguments have a nonnull attribute. |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1217 | if (NonNullArgs.empty()) { |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1218 | for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) { |
| 1219 | QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1220 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1221 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1222 | NonNullArgs.push_back(i); |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1223 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1224 | |
Ted Kremenek | ee1c08c | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1225 | // No pointer arguments? |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1226 | if (NonNullArgs.empty()) { |
| 1227 | // Warn the trivial case only if attribute is not coming from a |
| 1228 | // macro instantiation. |
| 1229 | if (Attr.getLoc().isFileID()) |
| 1230 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1231 | return; |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1232 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1233 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1234 | |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1235 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1236 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1237 | llvm::array_pod_sort(start, start + size); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1238 | D->addAttr(::new (S.Context) |
| 1239 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1240 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1243 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1244 | // This attribute must be applied to a function declaration. |
| 1245 | // The first argument to the attribute must be a string, |
| 1246 | // the name of the resource, for example "malloc". |
| 1247 | // The following arguments must be argument indexes, the arguments must be |
| 1248 | // of integer type for Returns, otherwise of pointer type. |
| 1249 | // 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] | 1250 | // after being held. free() should be __attribute((ownership_takes)), whereas |
| 1251 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1252 | |
| 1253 | if (!AL.getParameterName()) { |
| 1254 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1255 | << AL.getName()->getName() << 1; |
| 1256 | return; |
| 1257 | } |
| 1258 | // Figure out our Kind, and check arguments while we're at it. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1259 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1260 | switch (AL.getKind()) { |
| 1261 | case AttributeList::AT_ownership_takes: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1262 | K = OwnershipAttr::Takes; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1263 | if (AL.getNumArgs() < 1) { |
| 1264 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1265 | return; |
| 1266 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1267 | break; |
| 1268 | case AttributeList::AT_ownership_holds: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1269 | K = OwnershipAttr::Holds; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1270 | if (AL.getNumArgs() < 1) { |
| 1271 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1272 | return; |
| 1273 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1274 | break; |
| 1275 | case AttributeList::AT_ownership_returns: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1276 | K = OwnershipAttr::Returns; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1277 | if (AL.getNumArgs() > 1) { |
| 1278 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1279 | << AL.getNumArgs() + 1; |
| 1280 | return; |
| 1281 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1282 | break; |
| 1283 | default: |
| 1284 | // This should never happen given how we are called. |
| 1285 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1288 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1289 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1290 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1291 | return; |
| 1292 | } |
| 1293 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1294 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1295 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1296 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1297 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1298 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1299 | StringRef Module = AL.getParameterName()->getName(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1300 | |
| 1301 | // Normalize the argument, __foo__ becomes foo. |
| 1302 | if (Module.startswith("__") && Module.endswith("__")) |
| 1303 | Module = Module.substr(2, Module.size() - 4); |
| 1304 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1305 | SmallVector<unsigned, 10> OwnershipArgs; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1306 | |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1307 | for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E; |
| 1308 | ++I) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1309 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1310 | Expr *IdxExpr = *I; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1311 | llvm::APSInt ArgNum(32); |
| 1312 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1313 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1314 | S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int) |
| 1315 | << AL.getName()->getName() << IdxExpr->getSourceRange(); |
| 1316 | continue; |
| 1317 | } |
| 1318 | |
| 1319 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
| 1320 | |
| 1321 | if (x > NumArgs || x < 1) { |
| 1322 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1323 | << AL.getName()->getName() << x << IdxExpr->getSourceRange(); |
| 1324 | continue; |
| 1325 | } |
| 1326 | --x; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1327 | if (HasImplicitThisParam) { |
| 1328 | if (x == 0) { |
| 1329 | S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 1330 | << "ownership" << IdxExpr->getSourceRange(); |
| 1331 | return; |
| 1332 | } |
| 1333 | --x; |
| 1334 | } |
| 1335 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1336 | switch (K) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1337 | case OwnershipAttr::Takes: |
| 1338 | case OwnershipAttr::Holds: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1339 | // Is the function argument a pointer type? |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1340 | QualType T = getFunctionOrMethodArgType(D, x); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1341 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| 1342 | // FIXME: Should also highlight argument in decl. |
| 1343 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1344 | << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1345 | << "pointer" |
| 1346 | << IdxExpr->getSourceRange(); |
| 1347 | continue; |
| 1348 | } |
| 1349 | break; |
| 1350 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1351 | case OwnershipAttr::Returns: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1352 | if (AL.getNumArgs() > 1) { |
| 1353 | // Is the function argument an integer type? |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1354 | Expr *IdxExpr = AL.getArg(0); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1355 | llvm::APSInt ArgNum(32); |
| 1356 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1357 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1358 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
| 1359 | << "ownership_returns" << "integer" |
| 1360 | << IdxExpr->getSourceRange(); |
| 1361 | return; |
| 1362 | } |
| 1363 | } |
| 1364 | break; |
| 1365 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1366 | } // switch |
| 1367 | |
| 1368 | // Check we don't have a conflict with another ownership attribute. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1369 | for (specific_attr_iterator<OwnershipAttr> |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1370 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1371 | e = D->specific_attr_end<OwnershipAttr>(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1372 | i != e; ++i) { |
| 1373 | if ((*i)->getOwnKind() != K) { |
| 1374 | for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); |
| 1375 | I!=E; ++I) { |
| 1376 | if (x == *I) { |
| 1377 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1378 | << AL.getName()->getName() << "ownership_*"; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1379 | } |
| 1380 | } |
| 1381 | } |
| 1382 | } |
| 1383 | OwnershipArgs.push_back(x); |
| 1384 | } |
| 1385 | |
| 1386 | unsigned* start = OwnershipArgs.data(); |
| 1387 | unsigned size = OwnershipArgs.size(); |
| 1388 | llvm::array_pod_sort(start, start + size); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1389 | |
| 1390 | if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { |
| 1391 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1392 | return; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1393 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1394 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1395 | D->addAttr(::new (S.Context) |
| 1396 | OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size, |
| 1397 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1400 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1401 | // Check the attribute arguments. |
| 1402 | if (Attr.getNumArgs() > 1) { |
| 1403 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1404 | return; |
| 1405 | } |
| 1406 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1407 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1408 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1409 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1410 | return; |
| 1411 | } |
| 1412 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1413 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1414 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1415 | // gcc rejects |
| 1416 | // class c { |
| 1417 | // static int a __attribute__((weakref ("v2"))); |
| 1418 | // static int b() __attribute__((weakref ("f3"))); |
| 1419 | // }; |
| 1420 | // and ignores the attributes of |
| 1421 | // void f(void) { |
| 1422 | // static int a __attribute__((weakref ("v2"))); |
| 1423 | // } |
| 1424 | // we reject them |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1425 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1426 | if (!Ctx->isFileContext()) { |
| 1427 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1428 | nd->getNameAsString(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1429 | return; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | // The GCC manual says |
| 1433 | // |
| 1434 | // At present, a declaration to which `weakref' is attached can only |
| 1435 | // be `static'. |
| 1436 | // |
| 1437 | // It also says |
| 1438 | // |
| 1439 | // Without a TARGET, |
| 1440 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1441 | // equivalent to `weak'. |
| 1442 | // |
| 1443 | // gcc 4.4.1 will accept |
| 1444 | // int a7 __attribute__((weakref)); |
| 1445 | // as |
| 1446 | // int a7 __attribute__((weak)); |
| 1447 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1448 | // it if this behaviour is actually used. |
| 1449 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1450 | // GCC rejects |
| 1451 | // static ((alias ("y"), weakref)). |
| 1452 | // Should we? How to check that weakref is before or after alias? |
| 1453 | |
| 1454 | if (Attr.getNumArgs() == 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1455 | Expr *Arg = Attr.getArg(0); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1456 | Arg = Arg->IgnoreParenCasts(); |
| 1457 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1458 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1459 | if (!Str || !Str->isAscii()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1460 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1461 | << "weakref" << 1; |
| 1462 | return; |
| 1463 | } |
| 1464 | // GCC will accept anything as the argument of weakref. Should we |
| 1465 | // check for an existing decl? |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1466 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1467 | Str->getString())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1470 | D->addAttr(::new (S.Context) |
| 1471 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1472 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1475 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1476 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1477 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1478 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1479 | return; |
| 1480 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1481 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1482 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1483 | Arg = Arg->IgnoreParenCasts(); |
| 1484 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1485 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1486 | if (!Str || !Str->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1487 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1488 | << "alias" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1489 | return; |
| 1490 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1491 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1492 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | f5fe292 | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1493 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1494 | return; |
| 1495 | } |
| 1496 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1497 | // FIXME: check if target symbol exists in current file |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1498 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1499 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1500 | Str->getString(), |
| 1501 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1504 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1505 | // Check the attribute arguments. |
| 1506 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1507 | return; |
| 1508 | |
| 1509 | if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) { |
| 1510 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1511 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1512 | return; |
| 1513 | } |
| 1514 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1515 | D->addAttr(::new (S.Context) |
| 1516 | MinSizeAttr(Attr.getRange(), S.Context, |
| 1517 | Attr.getAttributeSpellingListIndex())); |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1520 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1521 | // Check the attribute arguments. |
| 1522 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1523 | return; |
| 1524 | |
| 1525 | if (!isa<FunctionDecl>(D)) { |
| 1526 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1527 | << Attr.getName() << ExpectedFunction; |
| 1528 | return; |
| 1529 | } |
| 1530 | |
| 1531 | if (D->hasAttr<HotAttr>()) { |
| 1532 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1533 | << Attr.getName() << "hot"; |
| 1534 | return; |
| 1535 | } |
| 1536 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1537 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1538 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1542 | // Check the attribute arguments. |
| 1543 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1544 | return; |
| 1545 | |
| 1546 | if (!isa<FunctionDecl>(D)) { |
| 1547 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1548 | << Attr.getName() << ExpectedFunction; |
| 1549 | return; |
| 1550 | } |
| 1551 | |
| 1552 | if (D->hasAttr<ColdAttr>()) { |
| 1553 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1554 | << Attr.getName() << "cold"; |
| 1555 | return; |
| 1556 | } |
| 1557 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1558 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1559 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1562 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1563 | // Check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1564 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1565 | return; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1566 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1567 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1568 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1569 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1570 | return; |
| 1571 | } |
| 1572 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1573 | D->addAttr(::new (S.Context) |
| 1574 | NakedAttr(Attr.getRange(), S.Context, |
| 1575 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1578 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 1579 | const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1580 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1581 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1582 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1583 | return; |
| 1584 | } |
| 1585 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1586 | if (!isa<FunctionDecl>(D)) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1587 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1588 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1589 | return; |
| 1590 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1591 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1592 | D->addAttr(::new (S.Context) |
| 1593 | AlwaysInlineAttr(Attr.getRange(), S.Context, |
| 1594 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1597 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1598 | const AttributeList &Attr) { |
| 1599 | // Check the attribute arguments. |
| 1600 | if (Attr.getNumArgs() != 1) { |
| 1601 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1602 | return; |
| 1603 | } |
| 1604 | |
| 1605 | Expr *Arg = Attr.getArg(0); |
| 1606 | Arg = Arg->IgnoreParenCasts(); |
| 1607 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1608 | |
| 1609 | // Check that it is a string. |
| 1610 | if (!Str) { |
| 1611 | S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model"; |
| 1612 | return; |
| 1613 | } |
| 1614 | |
| 1615 | if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) { |
| 1616 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1617 | << Attr.getName() << ExpectedTLSVar; |
| 1618 | return; |
| 1619 | } |
| 1620 | |
| 1621 | // Check that the value. |
| 1622 | StringRef Model = Str->getString(); |
| 1623 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1624 | && Model != "initial-exec" && Model != "local-exec") { |
| 1625 | S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg); |
| 1626 | return; |
| 1627 | } |
| 1628 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1629 | D->addAttr(::new (S.Context) |
| 1630 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1631 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1634 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1635 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1636 | if (Attr.hasParameterOrArguments()) { |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1637 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1638 | return; |
| 1639 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1640 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1641 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1642 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1643 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1644 | D->addAttr(::new (S.Context) |
| 1645 | MallocAttr(Attr.getRange(), S.Context, |
| 1646 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1647 | return; |
| 1648 | } |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1651 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1654 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1655 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1656 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1657 | return; |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1658 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1659 | D->addAttr(::new (S.Context) |
| 1660 | MayAliasAttr(Attr.getRange(), S.Context, |
| 1661 | Attr.getAttributeSpellingListIndex())); |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1664 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1665 | assert(!Attr.isInvalid()); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1666 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1667 | D->addAttr(::new (S.Context) |
| 1668 | NoCommonAttr(Attr.getRange(), S.Context, |
| 1669 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1670 | else |
| 1671 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1672 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1675 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1676 | assert(!Attr.isInvalid()); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1677 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1678 | D->addAttr(::new (S.Context) |
| 1679 | CommonAttr(Attr.getRange(), S.Context, |
| 1680 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1681 | else |
| 1682 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1683 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1686 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1687 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1688 | |
| 1689 | if (S.CheckNoReturnAttr(attr)) return; |
| 1690 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1691 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1692 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1693 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1694 | return; |
| 1695 | } |
| 1696 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1697 | D->addAttr(::new (S.Context) |
| 1698 | NoReturnAttr(attr.getRange(), S.Context, |
| 1699 | attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1703 | if (attr.hasParameterOrArguments()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1704 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1705 | attr.setInvalid(); |
| 1706 | return true; |
| 1707 | } |
| 1708 | |
| 1709 | return false; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1712 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1713 | const AttributeList &Attr) { |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1714 | |
| 1715 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1716 | // because 'analyzer_noreturn' does not impact the type. |
| 1717 | |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1718 | if(!checkAttributeNumArgs(S, Attr, 0)) |
| 1719 | return; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1720 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1721 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1722 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1723 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1724 | && !VD->getType()->isFunctionPointerType())) { |
| 1725 | S.Diag(Attr.getLoc(), |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1726 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1727 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1728 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1729 | return; |
| 1730 | } |
| 1731 | } |
| 1732 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1733 | D->addAttr(::new (S.Context) |
| 1734 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1735 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1738 | static void handleCXX11NoReturnAttr(Sema &S, Decl *D, |
| 1739 | const AttributeList &Attr) { |
| 1740 | // C++11 [dcl.attr.noreturn]p1: |
| 1741 | // The attribute may be applied to the declarator-id in a function |
| 1742 | // declaration. |
| 1743 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 1744 | if (!FD) { |
| 1745 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1746 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1747 | return; |
| 1748 | } |
| 1749 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1750 | D->addAttr(::new (S.Context) |
| 1751 | CXX11NoReturnAttr(Attr.getRange(), S.Context, |
| 1752 | Attr.getAttributeSpellingListIndex())); |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1755 | // PS3 PPU-specific. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1756 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1757 | /* |
| 1758 | Returning a Vector Class in Registers |
| 1759 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1760 | According to the PPU ABI specifications, a class with a single member of |
| 1761 | vector type is returned in memory when used as the return value of a function. |
| 1762 | This results in inefficient code when implementing vector classes. To return |
| 1763 | the value in a single vector register, add the vecreturn attribute to the |
| 1764 | class definition. This attribute is also applicable to struct types. |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1765 | |
| 1766 | Example: |
| 1767 | |
| 1768 | struct Vector |
| 1769 | { |
| 1770 | __vector float xyzw; |
| 1771 | } __attribute__((vecreturn)); |
| 1772 | |
| 1773 | Vector Add(Vector lhs, Vector rhs) |
| 1774 | { |
| 1775 | Vector result; |
| 1776 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1777 | return result; // This will be returned in a register |
| 1778 | } |
| 1779 | */ |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1780 | if (!isa<RecordDecl>(D)) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1781 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1782 | << Attr.getName() << ExpectedClass; |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1783 | return; |
| 1784 | } |
| 1785 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1786 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1787 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1788 | return; |
| 1789 | } |
| 1790 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1791 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1792 | int count = 0; |
| 1793 | |
| 1794 | if (!isa<CXXRecordDecl>(record)) { |
| 1795 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1796 | return; |
| 1797 | } |
| 1798 | |
| 1799 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1800 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1801 | return; |
| 1802 | } |
| 1803 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1804 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1805 | iter != record->field_end(); iter++) { |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1806 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1807 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1808 | return; |
| 1809 | } |
| 1810 | count++; |
| 1811 | } |
| 1812 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1813 | D->addAttr(::new (S.Context) |
| 1814 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1815 | Attr.getAttributeSpellingListIndex())); |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1818 | static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | d98f708 | 2013-01-28 01:15:38 +0000 | [diff] [blame] | 1819 | if (!isa<FunctionDecl>(D) && !isa<ParmVarDecl>(D)) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1820 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1821 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1822 | return; |
| 1823 | } |
| 1824 | // FIXME: Actually store the attribute on the declaration |
| 1825 | } |
| 1826 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1827 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1828 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1829 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1830 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1831 | return; |
| 1832 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1833 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1834 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 568eae4 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1835 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1836 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1837 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1838 | return; |
| 1839 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1840 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1841 | D->addAttr(::new (S.Context) |
| 1842 | UnusedAttr(Attr.getRange(), S.Context, |
| 1843 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1846 | static void handleReturnsTwiceAttr(Sema &S, Decl *D, |
| 1847 | const AttributeList &Attr) { |
| 1848 | // check the attribute arguments. |
| 1849 | if (Attr.hasParameterOrArguments()) { |
| 1850 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1851 | return; |
| 1852 | } |
| 1853 | |
| 1854 | if (!isa<FunctionDecl>(D)) { |
| 1855 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1856 | << Attr.getName() << ExpectedFunction; |
| 1857 | return; |
| 1858 | } |
| 1859 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1860 | D->addAttr(::new (S.Context) |
| 1861 | ReturnsTwiceAttr(Attr.getRange(), S.Context, |
| 1862 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1865 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1866 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1867 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1868 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1869 | return; |
| 1870 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1871 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1872 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Daniel Dunbar | 186204b | 2009-02-13 22:48:56 +0000 | [diff] [blame] | 1873 | if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1874 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 1875 | return; |
| 1876 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1877 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1878 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1879 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1880 | return; |
| 1881 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1882 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1883 | D->addAttr(::new (S.Context) |
| 1884 | UsedAttr(Attr.getRange(), S.Context, |
| 1885 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1888 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1889 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1890 | if (Attr.getNumArgs() > 1) { |
| 1891 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1892 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1893 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1894 | |
| 1895 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1896 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1897 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1898 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1899 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1900 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1901 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1902 | << "constructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1903 | return; |
| 1904 | } |
| 1905 | priority = Idx.getZExtValue(); |
| 1906 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1907 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1908 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1909 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1910 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1911 | return; |
| 1912 | } |
| 1913 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1914 | D->addAttr(::new (S.Context) |
| 1915 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1916 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1919 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1920 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1921 | if (Attr.getNumArgs() > 1) { |
| 1922 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1923 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1924 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1925 | |
| 1926 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1927 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1928 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1929 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1930 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1931 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1932 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1933 | << "destructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1934 | return; |
| 1935 | } |
| 1936 | priority = Idx.getZExtValue(); |
| 1937 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1938 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1939 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1940 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1941 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1942 | return; |
| 1943 | } |
| 1944 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1945 | D->addAttr(::new (S.Context) |
| 1946 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 1947 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1950 | template <typename AttrTy> |
| 1951 | static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr, |
| 1952 | const char *Name) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1953 | unsigned NumArgs = Attr.getNumArgs(); |
| 1954 | if (NumArgs > 1) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1955 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1956 | return; |
| 1957 | } |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1958 | |
| 1959 | // Handle the case where the attribute has a text message. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1960 | StringRef Str; |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1961 | if (NumArgs == 1) { |
| 1962 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1963 | if (!SE) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1964 | S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string) |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1965 | << Name; |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1966 | return; |
| 1967 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1968 | Str = SE->getString(); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1969 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1970 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1971 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 1972 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1975 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 1976 | const AttributeList &Attr) { |
| 1977 | unsigned NumArgs = Attr.getNumArgs(); |
| 1978 | if (NumArgs > 0) { |
| 1979 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 1980 | return; |
| 1981 | } |
| 1982 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1983 | D->addAttr(::new (S.Context) |
| 1984 | ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context, |
| 1985 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 1988 | static void handleObjCRootClassAttr(Sema &S, Decl *D, |
| 1989 | const AttributeList &Attr) { |
| 1990 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 1991 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 1992 | return; |
| 1993 | } |
| 1994 | |
| 1995 | unsigned NumArgs = Attr.getNumArgs(); |
| 1996 | if (NumArgs > 0) { |
| 1997 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 1998 | return; |
| 1999 | } |
| 2000 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2001 | D->addAttr(::new (S.Context) |
| 2002 | ObjCRootClassAttr(Attr.getRange(), S.Context, |
| 2003 | Attr.getAttributeSpellingListIndex())); |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2004 | } |
| 2005 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2006 | static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, |
| 2007 | const AttributeList &Attr) { |
Fariborz Jahanian | 341b8be | 2012-01-03 22:52:32 +0000 | [diff] [blame] | 2008 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 2009 | S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis); |
| 2010 | return; |
| 2011 | } |
| 2012 | |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2013 | unsigned NumArgs = Attr.getNumArgs(); |
| 2014 | if (NumArgs > 0) { |
| 2015 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 2016 | return; |
| 2017 | } |
| 2018 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2019 | D->addAttr(::new (S.Context) |
| 2020 | ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context, |
| 2021 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2024 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2025 | IdentifierInfo *Platform, |
| 2026 | VersionTuple Introduced, |
| 2027 | VersionTuple Deprecated, |
| 2028 | VersionTuple Obsoleted) { |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2029 | StringRef PlatformName |
| 2030 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2031 | if (PlatformName.empty()) |
| 2032 | PlatformName = Platform->getName(); |
| 2033 | |
| 2034 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2035 | // of these steps are needed). |
| 2036 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2037 | !(Introduced <= Deprecated)) { |
| 2038 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2039 | << 1 << PlatformName << Deprecated.getAsString() |
| 2040 | << 0 << Introduced.getAsString(); |
| 2041 | return true; |
| 2042 | } |
| 2043 | |
| 2044 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2045 | !(Introduced <= Obsoleted)) { |
| 2046 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2047 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2048 | << 0 << Introduced.getAsString(); |
| 2049 | return true; |
| 2050 | } |
| 2051 | |
| 2052 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2053 | !(Deprecated <= Obsoleted)) { |
| 2054 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2055 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2056 | << 1 << Deprecated.getAsString(); |
| 2057 | return true; |
| 2058 | } |
| 2059 | |
| 2060 | return false; |
| 2061 | } |
| 2062 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2063 | /// \brief Check whether the two versions match. |
| 2064 | /// |
| 2065 | /// If either version tuple is empty, then they are assumed to match. If |
| 2066 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2067 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2068 | bool BeforeIsOkay) { |
| 2069 | if (X.empty() || Y.empty()) |
| 2070 | return true; |
| 2071 | |
| 2072 | if (X == Y) |
| 2073 | return true; |
| 2074 | |
| 2075 | if (BeforeIsOkay && X < Y) |
| 2076 | return true; |
| 2077 | |
| 2078 | return false; |
| 2079 | } |
| 2080 | |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2081 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2082 | IdentifierInfo *Platform, |
| 2083 | VersionTuple Introduced, |
| 2084 | VersionTuple Deprecated, |
| 2085 | VersionTuple Obsoleted, |
| 2086 | bool IsUnavailable, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2087 | StringRef Message, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2088 | bool Override, |
| 2089 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2090 | VersionTuple MergedIntroduced = Introduced; |
| 2091 | VersionTuple MergedDeprecated = Deprecated; |
| 2092 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2093 | bool FoundAny = false; |
| 2094 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2095 | if (D->hasAttrs()) { |
| 2096 | AttrVec &Attrs = D->getAttrs(); |
| 2097 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 2098 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 2099 | if (!OldAA) { |
| 2100 | ++i; |
| 2101 | continue; |
| 2102 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2103 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2104 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2105 | if (OldPlatform != Platform) { |
| 2106 | ++i; |
| 2107 | continue; |
| 2108 | } |
| 2109 | |
| 2110 | FoundAny = true; |
| 2111 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2112 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2113 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2114 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2115 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2116 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 2117 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 2118 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 2119 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 72daa3f | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 2120 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2121 | if (Override) { |
| 2122 | int Which = -1; |
| 2123 | VersionTuple FirstVersion; |
| 2124 | VersionTuple SecondVersion; |
| 2125 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 2126 | Which = 0; |
| 2127 | FirstVersion = OldIntroduced; |
| 2128 | SecondVersion = Introduced; |
| 2129 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 2130 | Which = 1; |
| 2131 | FirstVersion = Deprecated; |
| 2132 | SecondVersion = OldDeprecated; |
| 2133 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 2134 | Which = 2; |
| 2135 | FirstVersion = Obsoleted; |
| 2136 | SecondVersion = OldObsoleted; |
| 2137 | } |
| 2138 | |
| 2139 | if (Which == -1) { |
| 2140 | Diag(OldAA->getLocation(), |
| 2141 | diag::warn_mismatched_availability_override_unavail) |
| 2142 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2143 | } else { |
| 2144 | Diag(OldAA->getLocation(), |
| 2145 | diag::warn_mismatched_availability_override) |
| 2146 | << Which |
| 2147 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2148 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 2149 | } |
| 2150 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2151 | } else { |
| 2152 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2153 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2154 | } |
| 2155 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2156 | Attrs.erase(Attrs.begin() + i); |
| 2157 | --e; |
| 2158 | continue; |
| 2159 | } |
| 2160 | |
| 2161 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2162 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2163 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2164 | |
| 2165 | if (MergedIntroduced2.empty()) |
| 2166 | MergedIntroduced2 = OldIntroduced; |
| 2167 | if (MergedDeprecated2.empty()) |
| 2168 | MergedDeprecated2 = OldDeprecated; |
| 2169 | if (MergedObsoleted2.empty()) |
| 2170 | MergedObsoleted2 = OldObsoleted; |
| 2171 | |
| 2172 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2173 | MergedIntroduced2, MergedDeprecated2, |
| 2174 | MergedObsoleted2)) { |
| 2175 | Attrs.erase(Attrs.begin() + i); |
| 2176 | --e; |
| 2177 | continue; |
| 2178 | } |
| 2179 | |
| 2180 | MergedIntroduced = MergedIntroduced2; |
| 2181 | MergedDeprecated = MergedDeprecated2; |
| 2182 | MergedObsoleted = MergedObsoleted2; |
| 2183 | ++i; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2184 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | if (FoundAny && |
| 2188 | MergedIntroduced == Introduced && |
| 2189 | MergedDeprecated == Deprecated && |
| 2190 | MergedObsoleted == Obsoleted) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2191 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2192 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2193 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2194 | MergedDeprecated, MergedObsoleted)) { |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2195 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 2196 | Introduced, Deprecated, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2197 | Obsoleted, IsUnavailable, Message, |
| 2198 | AttrSpellingListIndex); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2199 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2200 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2203 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2204 | const AttributeList &Attr) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2205 | IdentifierInfo *Platform = Attr.getParameterName(); |
| 2206 | SourceLocation PlatformLoc = Attr.getParameterLoc(); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2207 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2208 | |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2209 | if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2210 | S.Diag(PlatformLoc, diag::warn_availability_unknown_platform) |
| 2211 | << Platform; |
| 2212 | |
Rafael Espindola | 8c4222a | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2213 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 2214 | if (!ND) { |
| 2215 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2216 | return; |
| 2217 | } |
| 2218 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2219 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2220 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2221 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2222 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2223 | StringRef Str; |
| 2224 | const StringLiteral *SE = |
| 2225 | dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr()); |
| 2226 | if (SE) |
| 2227 | Str = SE->getString(); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2228 | |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2229 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2230 | Platform, |
| 2231 | Introduced.Version, |
| 2232 | Deprecated.Version, |
| 2233 | Obsoleted.Version, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2234 | IsUnavailable, Str, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2235 | /*Override=*/false, |
| 2236 | Index); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2237 | if (NewAttr) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2238 | D->addAttr(NewAttr); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2241 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2242 | VisibilityAttr::VisibilityType Vis, |
| 2243 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | dd44f34 | 2012-05-10 03:01:34 +0000 | [diff] [blame] | 2244 | if (isa<TypedefNameDecl>(D)) { |
| 2245 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility"; |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2246 | return NULL; |
Rafael Espindola | dd44f34 | 2012-05-10 03:01:34 +0000 | [diff] [blame] | 2247 | } |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2248 | VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>(); |
| 2249 | if (ExistingAttr) { |
| 2250 | VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility(); |
| 2251 | if (ExistingVis == Vis) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2252 | return NULL; |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2253 | Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2254 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2255 | D->dropAttr<VisibilityAttr>(); |
| 2256 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2257 | return ::new (Context) VisibilityAttr(Range, Context, Vis, |
| 2258 | AttrSpellingListIndex); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2261 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2262 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2263 | if(!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2264 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2265 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2266 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2267 | Arg = Arg->IgnoreParenCasts(); |
| 2268 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2269 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2270 | if (!Str || !Str->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2271 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2272 | << "visibility" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2273 | return; |
| 2274 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2275 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2276 | StringRef TypeStr = Str->getString(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2277 | VisibilityAttr::VisibilityType type; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2278 | |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2279 | if (TypeStr == "default") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2280 | type = VisibilityAttr::Default; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2281 | else if (TypeStr == "hidden") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2282 | type = VisibilityAttr::Hidden; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2283 | else if (TypeStr == "internal") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2284 | type = VisibilityAttr::Hidden; // FIXME |
John McCall | 4188760 | 2012-01-29 01:20:30 +0000 | [diff] [blame] | 2285 | else if (TypeStr == "protected") { |
| 2286 | // Complain about attempts to use protected visibility on targets |
| 2287 | // (like Darwin) that don't support it. |
| 2288 | if (!S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2289 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2290 | type = VisibilityAttr::Default; |
| 2291 | } else { |
| 2292 | type = VisibilityAttr::Protected; |
| 2293 | } |
| 2294 | } else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2295 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2296 | return; |
| 2297 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2298 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2299 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2300 | VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, |
| 2301 | Index); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2302 | if (NewAttr) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2303 | D->addAttr(NewAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2306 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2307 | const AttributeList &Attr) { |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2308 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 2309 | if (!method) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2310 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2311 | << ExpectedMethod; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2312 | return; |
| 2313 | } |
| 2314 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2315 | if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) { |
| 2316 | if (!Attr.getParameterName() && Attr.getNumArgs() == 1) { |
| 2317 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2318 | << "objc_method_family" << 1; |
| 2319 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2320 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2321 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2322 | Attr.setInvalid(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2323 | return; |
| 2324 | } |
| 2325 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2326 | StringRef param = Attr.getParameterName()->getName(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2327 | ObjCMethodFamilyAttr::FamilyKind family; |
| 2328 | if (param == "none") |
| 2329 | family = ObjCMethodFamilyAttr::OMF_None; |
| 2330 | else if (param == "alloc") |
| 2331 | family = ObjCMethodFamilyAttr::OMF_alloc; |
| 2332 | else if (param == "copy") |
| 2333 | family = ObjCMethodFamilyAttr::OMF_copy; |
| 2334 | else if (param == "init") |
| 2335 | family = ObjCMethodFamilyAttr::OMF_init; |
| 2336 | else if (param == "mutableCopy") |
| 2337 | family = ObjCMethodFamilyAttr::OMF_mutableCopy; |
| 2338 | else if (param == "new") |
| 2339 | family = ObjCMethodFamilyAttr::OMF_new; |
| 2340 | else { |
| 2341 | // Just warn and ignore it. This is future-proof against new |
| 2342 | // families being used in system headers. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2343 | S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2344 | return; |
| 2345 | } |
| 2346 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2347 | if (family == ObjCMethodFamilyAttr::OMF_init && |
| 2348 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2349 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2350 | << method->getResultType(); |
| 2351 | // Ignore the attribute. |
| 2352 | return; |
| 2353 | } |
| 2354 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2355 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2356 | S.Context, family)); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2359 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 2360 | const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2361 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2362 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2363 | |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2364 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 2365 | if (OCI == 0) { |
| 2366 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 2367 | return; |
| 2368 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2369 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2370 | D->addAttr(::new (S.Context) |
| 2371 | ObjCExceptionAttr(Attr.getRange(), S.Context, |
| 2372 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2375 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2376 | if (Attr.getNumArgs() != 0) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 2377 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2378 | return; |
| 2379 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2380 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2381 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2382 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2383 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2384 | return; |
| 2385 | } |
| 2386 | } |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2387 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2388 | QualType T = PD->getType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2389 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2390 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2391 | return; |
| 2392 | } |
| 2393 | } |
| 2394 | else { |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2395 | // It is okay to include this attribute on properties, e.g.: |
| 2396 | // |
| 2397 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2398 | // |
| 2399 | // In this case it follows tradition and suppresses an error in the above |
| 2400 | // case. |
Fariborz Jahanian | 9b2eb7b | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2401 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2402 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2403 | D->addAttr(::new (S.Context) |
| 2404 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2405 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2408 | static void |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2409 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2410 | if (Attr.getNumArgs() != 0) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 2411 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2412 | return; |
| 2413 | } |
| 2414 | |
| 2415 | if (!isa<FunctionDecl>(D)) { |
| 2416 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 2417 | return; |
| 2418 | } |
| 2419 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2420 | D->addAttr(::new (S.Context) |
| 2421 | OverloadableAttr(Attr.getRange(), S.Context, |
| 2422 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2425 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2426 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2427 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2428 | << "blocks" << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2429 | return; |
| 2430 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2431 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2432 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2433 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2434 | return; |
| 2435 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2436 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2437 | BlocksAttr::BlockType type; |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 2438 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2439 | type = BlocksAttr::ByRef; |
| 2440 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2441 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2442 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2443 | return; |
| 2444 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2445 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2446 | D->addAttr(::new (S.Context) |
| 2447 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2448 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2451 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2452 | // check the attribute arguments. |
| 2453 | if (Attr.getNumArgs() > 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2454 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2455 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2458 | unsigned sentinel = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2459 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2460 | Expr *E = Attr.getArg(0); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2461 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2462 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2463 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2464 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2465 | << "sentinel" << 1 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2466 | return; |
| 2467 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2468 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2469 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2470 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2471 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2472 | return; |
| 2473 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2474 | |
| 2475 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2478 | unsigned nullPos = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2479 | if (Attr.getNumArgs() > 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2480 | Expr *E = Attr.getArg(1); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2481 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2482 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2483 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2484 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2485 | << "sentinel" << 2 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2486 | return; |
| 2487 | } |
| 2488 | nullPos = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2489 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2490 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2491 | // FIXME: This error message could be improved, it would be nice |
| 2492 | // to say what the bounds actually are. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2493 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2494 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2495 | return; |
| 2496 | } |
| 2497 | } |
| 2498 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2499 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2500 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2501 | if (isa<FunctionNoProtoType>(FT)) { |
| 2502 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2503 | return; |
| 2504 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2505 | |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2506 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2507 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2508 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2509 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2510 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2511 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2512 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2513 | return; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2514 | } |
Eli Friedman | a0b2ba1 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2515 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2516 | if (!BD->isVariadic()) { |
| 2517 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2518 | return; |
| 2519 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2520 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2521 | QualType Ty = V->getType(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2522 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2523 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2524 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2525 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2526 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2527 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2528 | return; |
| 2529 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2530 | } else { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2531 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2532 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2533 | return; |
| 2534 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2535 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2536 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2537 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2538 | return; |
| 2539 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2540 | D->addAttr(::new (S.Context) |
| 2541 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2542 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2543 | } |
| 2544 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2545 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2546 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2547 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2548 | return; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2549 | |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 2550 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2551 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Kaelyn Uhrain | d449c79 | 2012-11-13 00:18:47 +0000 | [diff] [blame] | 2552 | << Attr.getName() << ExpectedFunctionMethodOrClass; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2553 | return; |
| 2554 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2555 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2556 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2557 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2558 | << Attr.getName() << 0; |
Nuno Lopes | f857798 | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2559 | return; |
| 2560 | } |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2561 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2562 | if (MD->getResultType()->isVoidType()) { |
| 2563 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2564 | << Attr.getName() << 1; |
| 2565 | return; |
| 2566 | } |
| 2567 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2568 | D->addAttr(::new (S.Context) |
| 2569 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2570 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2573 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2574 | // check the attribute arguments. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2575 | if (Attr.hasParameterOrArguments()) { |
| 2576 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2577 | return; |
| 2578 | } |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2579 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2580 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
Fariborz Jahanian | 13c7fcc | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 2581 | if (isa<CXXRecordDecl>(D)) { |
| 2582 | D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| 2583 | return; |
| 2584 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2585 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2586 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | f23ecd9 | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 2587 | return; |
| 2588 | } |
| 2589 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2590 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 2591 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2592 | nd->addAttr(::new (S.Context) |
| 2593 | WeakAttr(Attr.getRange(), S.Context, |
| 2594 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2595 | } |
| 2596 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2597 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2598 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2599 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2600 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2601 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2602 | |
| 2603 | // weak_import only applies to variable & function declarations. |
| 2604 | bool isDef = false; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2605 | if (!D->canBeWeakImported(isDef)) { |
| 2606 | if (isDef) |
| 2607 | S.Diag(Attr.getLoc(), |
| 2608 | diag::warn_attribute_weak_import_invalid_on_definition) |
| 2609 | << "weak_import" << 2 /*variable and function*/; |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2610 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2611 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 90eed21 | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2612 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2613 | // Nothing to warn about here. |
| 2614 | } else |
Fariborz Jahanian | c034974 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2615 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2616 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2617 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2618 | return; |
| 2619 | } |
| 2620 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2621 | D->addAttr(::new (S.Context) |
| 2622 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2623 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2626 | // Handles reqd_work_group_size and work_group_size_hint. |
| 2627 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2628 | const AttributeList &Attr) { |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2629 | assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2630 | || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint); |
| 2631 | |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2632 | // Attribute has 3 arguments. |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2633 | if (!checkAttributeNumArgs(S, Attr, 3)) return; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2634 | |
| 2635 | unsigned WGSize[3]; |
| 2636 | for (unsigned i = 0; i < 3; ++i) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2637 | Expr *E = Attr.getArg(i); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2638 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2639 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2640 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2641 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2642 | << Attr.getName()->getName() << E->getSourceRange(); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2643 | return; |
| 2644 | } |
| 2645 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 2646 | } |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2647 | |
| 2648 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2649 | && D->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 2650 | ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>(); |
| 2651 | if (!(A->getXDim() == WGSize[0] && |
| 2652 | A->getYDim() == WGSize[1] && |
| 2653 | A->getZDim() == WGSize[2])) { |
| 2654 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2655 | Attr.getName(); |
| 2656 | } |
| 2657 | } |
| 2658 | |
| 2659 | if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint |
| 2660 | && D->hasAttr<WorkGroupSizeHintAttr>()) { |
| 2661 | WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>(); |
| 2662 | if (!(A->getXDim() == WGSize[0] && |
| 2663 | A->getYDim() == WGSize[1] && |
| 2664 | A->getZDim() == WGSize[2])) { |
| 2665 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2666 | Attr.getName(); |
| 2667 | } |
| 2668 | } |
| 2669 | |
| 2670 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize) |
| 2671 | D->addAttr(::new (S.Context) |
| 2672 | ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2673 | WGSize[0], WGSize[1], WGSize[2], |
| 2674 | Attr.getAttributeSpellingListIndex())); |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2675 | else |
| 2676 | D->addAttr(::new (S.Context) |
| 2677 | WorkGroupSizeHintAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2678 | WGSize[0], WGSize[1], WGSize[2], |
| 2679 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2680 | } |
| 2681 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2682 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2683 | StringRef Name, |
| 2684 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2685 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2686 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2687 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2688 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2689 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2690 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2691 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2692 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2693 | AttrSpellingListIndex); |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2696 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2697 | // Attribute has no arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2698 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2699 | return; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2700 | |
| 2701 | // Make sure that there is a string literal as the sections's single |
| 2702 | // argument. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2703 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2704 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2705 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2706 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section"; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2707 | return; |
| 2708 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2709 | |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2710 | // If the target wants to validate the section specifier, make it happen. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2711 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString()); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2712 | if (!Error.empty()) { |
| 2713 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 2714 | << Error; |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2715 | return; |
| 2716 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2717 | |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2718 | // This attribute cannot be applied to local variables. |
| 2719 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 2720 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 2721 | return; |
| 2722 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2723 | |
| 2724 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2725 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2726 | SE->getString(), Index); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2727 | if (NewAttr) |
| 2728 | D->addAttr(NewAttr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2729 | } |
| 2730 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2731 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2732 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2733 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2734 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2735 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2736 | return; |
| 2737 | } |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2738 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2739 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2740 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2741 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2742 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2743 | D->addAttr(::new (S.Context) |
| 2744 | NoThrowAttr(Attr.getRange(), S.Context, |
| 2745 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2746 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2749 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2750 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2751 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2752 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2753 | return; |
| 2754 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2755 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2756 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2757 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2758 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2759 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2760 | D->addAttr(::new (S.Context) |
| 2761 | ConstAttr(Attr.getRange(), S.Context, |
| 2762 | Attr.getAttributeSpellingListIndex() )); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2763 | } |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2766 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2767 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2768 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2769 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2770 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2771 | D->addAttr(::new (S.Context) |
| 2772 | PureAttr(Attr.getRange(), S.Context, |
| 2773 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2776 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2777 | if (!Attr.getParameterName()) { |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2778 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2779 | return; |
| 2780 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2781 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2782 | if (Attr.getNumArgs() != 0) { |
| 2783 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2784 | return; |
| 2785 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2786 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2787 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2788 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2789 | if (!VD || !VD->hasLocalStorage()) { |
| 2790 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 2791 | return; |
| 2792 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2793 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2794 | // Look up the function |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2795 | // FIXME: Lookup probably isn't looking in the right place |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2796 | NamedDecl *CleanupDecl |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2797 | = S.LookupSingleName(S.TUScope, Attr.getParameterName(), |
| 2798 | Attr.getParameterLoc(), Sema::LookupOrdinaryName); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2799 | if (!CleanupDecl) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2800 | S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2801 | Attr.getParameterName(); |
| 2802 | return; |
| 2803 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2804 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2805 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 2806 | if (!FD) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2807 | S.Diag(Attr.getParameterLoc(), |
| 2808 | diag::err_attribute_cleanup_arg_not_function) |
| 2809 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2810 | return; |
| 2811 | } |
| 2812 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2813 | if (FD->getNumParams() != 1) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2814 | S.Diag(Attr.getParameterLoc(), |
| 2815 | diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2816 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2817 | return; |
| 2818 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2819 | |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2820 | // We're currently more strict than GCC about what function types we accept. |
| 2821 | // If this ever proves to be a problem it should be easy to fix. |
| 2822 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2823 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2824 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2825 | ParamTy, Ty) != Sema::Compatible) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2826 | S.Diag(Attr.getParameterLoc(), |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2827 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 2828 | Attr.getParameterName() << ParamTy << Ty; |
| 2829 | return; |
| 2830 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2831 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2832 | D->addAttr(::new (S.Context) |
| 2833 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2834 | Attr.getAttributeSpellingListIndex())); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2835 | S.MarkFunctionReferenced(Attr.getParameterLoc(), FD); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2836 | } |
| 2837 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2838 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2839 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2840 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2841 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2842 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2843 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2844 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2845 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2846 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2847 | return; |
| 2848 | } |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2849 | |
| 2850 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2851 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2852 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 2853 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2854 | unsigned FirstIdx = 1; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2855 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2856 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2857 | Expr *IdxExpr = Attr.getArg(0); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2858 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2859 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 2860 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2861 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 2862 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 2863 | return; |
| 2864 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2865 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2866 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
| 2867 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 2868 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 2869 | return; |
| 2870 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2871 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2872 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2873 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2874 | if (HasImplicitThisParam) { |
| 2875 | if (ArgIdx == 0) { |
| 2876 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 2877 | << "format_arg" << IdxExpr->getSourceRange(); |
| 2878 | return; |
| 2879 | } |
| 2880 | ArgIdx--; |
| 2881 | } |
| 2882 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2883 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2884 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2885 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2886 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2887 | if (not_nsstring_type && |
| 2888 | !isCFStringType(Ty, S.Context) && |
| 2889 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2890 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2891 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2892 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2893 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2894 | << IdxExpr->getSourceRange(); |
| 2895 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2896 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2897 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2898 | if (!isNSStringType(Ty, S.Context) && |
| 2899 | !isCFStringType(Ty, S.Context) && |
| 2900 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2901 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2902 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2903 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2904 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2905 | << IdxExpr->getSourceRange(); |
| 2906 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2907 | } |
| 2908 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2909 | D->addAttr(::new (S.Context) |
| 2910 | FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(), |
| 2911 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2914 | enum FormatAttrKind { |
| 2915 | CFStringFormat, |
| 2916 | NSStringFormat, |
| 2917 | StrftimeFormat, |
| 2918 | SupportedFormat, |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2919 | IgnoredFormat, |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2920 | InvalidFormat |
| 2921 | }; |
| 2922 | |
| 2923 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2924 | /// types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2925 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2926 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2927 | // Check for formats that get handled specially. |
| 2928 | .Case("NSString", NSStringFormat) |
| 2929 | .Case("CFString", CFStringFormat) |
| 2930 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2931 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2932 | // Otherwise, check for supported formats. |
| 2933 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2934 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2935 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2936 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2937 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2938 | .Default(InvalidFormat); |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2941 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2942 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2943 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2944 | const AttributeList &Attr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2945 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2946 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2947 | return; |
| 2948 | } |
| 2949 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2950 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2951 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2952 | Attr.setInvalid(); |
| 2953 | return; |
| 2954 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2955 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2956 | if (S.Context.getAsArrayType(T)) |
| 2957 | T = S.Context.getBaseElementType(T); |
| 2958 | if (!T->getAs<RecordType>()) { |
| 2959 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2960 | Attr.setInvalid(); |
| 2961 | return; |
| 2962 | } |
| 2963 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2964 | if (Attr.getNumArgs() != 1) { |
| 2965 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2966 | Attr.setInvalid(); |
| 2967 | return; |
| 2968 | } |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2969 | Expr *priorityExpr = Attr.getArg(0); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2970 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2971 | llvm::APSInt priority(32); |
| 2972 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 2973 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
| 2974 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 2975 | << "init_priority" << priorityExpr->getSourceRange(); |
| 2976 | Attr.setInvalid(); |
| 2977 | return; |
| 2978 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 2979 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2980 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2981 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 2982 | << priorityExpr->getSourceRange(); |
| 2983 | Attr.setInvalid(); |
| 2984 | return; |
| 2985 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2986 | D->addAttr(::new (S.Context) |
| 2987 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 2988 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2989 | } |
| 2990 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2991 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2992 | int FormatIdx, int FirstArg, |
| 2993 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2994 | // Check whether we already have an equivalent format attribute. |
| 2995 | for (specific_attr_iterator<FormatAttr> |
| 2996 | i = D->specific_attr_begin<FormatAttr>(), |
| 2997 | e = D->specific_attr_end<FormatAttr>(); |
| 2998 | i != e ; ++i) { |
| 2999 | FormatAttr *f = *i; |
| 3000 | if (f->getType() == Format && |
| 3001 | f->getFormatIdx() == FormatIdx && |
| 3002 | f->getFirstArg() == FirstArg) { |
| 3003 | // If we don't have a valid location for this attribute, adopt the |
| 3004 | // location. |
| 3005 | if (f->getLocation().isInvalid()) |
| 3006 | f->setRange(Range); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3007 | return NULL; |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3008 | } |
| 3009 | } |
| 3010 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3011 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg, |
| 3012 | AttrSpellingListIndex); |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3015 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3016 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3017 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3018 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3019 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3020 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3021 | << "format" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3022 | return; |
| 3023 | } |
| 3024 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3025 | if (Attr.getNumArgs() != 2) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3026 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3027 | return; |
| 3028 | } |
| 3029 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3030 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3031 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3032 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3033 | return; |
| 3034 | } |
| 3035 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3036 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3037 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3038 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 3039 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3040 | unsigned FirstIdx = 1; |
| 3041 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3042 | StringRef Format = Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3043 | |
| 3044 | // Normalize the argument, __foo__ becomes foo. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3045 | if (Format.startswith("__") && Format.endswith("__")) |
| 3046 | Format = Format.substr(2, Format.size() - 4); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3047 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3048 | // Check for supported formats. |
| 3049 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3050 | |
| 3051 | if (Kind == IgnoredFormat) |
| 3052 | return; |
| 3053 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3054 | if (Kind == InvalidFormat) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3055 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3056 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3057 | return; |
| 3058 | } |
| 3059 | |
| 3060 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3061 | Expr *IdxExpr = Attr.getArg(0); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3062 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3063 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 3064 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3065 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3066 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3067 | return; |
| 3068 | } |
| 3069 | |
| 3070 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3071 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3072 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3073 | return; |
| 3074 | } |
| 3075 | |
| 3076 | // FIXME: Do we need to bounds check? |
| 3077 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3078 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3079 | if (HasImplicitThisParam) { |
| 3080 | if (ArgIdx == 0) { |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3081 | S.Diag(Attr.getLoc(), |
| 3082 | diag::err_format_attribute_implicit_this_format_string) |
| 3083 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3084 | return; |
| 3085 | } |
| 3086 | ArgIdx--; |
| 3087 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3088 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3089 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3090 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3091 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3092 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3093 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3094 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3095 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3096 | return; |
| 3097 | } |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3098 | } else if (Kind == NSStringFormat) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3099 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3100 | // semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3101 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3102 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3103 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3104 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3105 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3106 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3107 | } else if (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3108 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3109 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3110 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3111 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3112 | return; |
| 3113 | } |
| 3114 | |
| 3115 | // check the 3rd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3116 | Expr *FirstArgExpr = Attr.getArg(1); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3117 | llvm::APSInt FirstArg(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3118 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 3119 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3120 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3121 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3122 | return; |
| 3123 | } |
| 3124 | |
| 3125 | // check if the function is variadic if the 3rd argument non-zero |
| 3126 | if (FirstArg != 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3127 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3128 | ++NumArgs; // +1 for ... |
| 3129 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3130 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3131 | return; |
| 3132 | } |
| 3133 | } |
| 3134 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3135 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3136 | // variable the input is just the current time + the format string. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3137 | if (Kind == StrftimeFormat) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3138 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3139 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 3140 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3141 | return; |
| 3142 | } |
| 3143 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3144 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3145 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3146 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3147 | return; |
| 3148 | } |
| 3149 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3150 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format, |
| 3151 | Idx.getZExtValue(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3152 | FirstArg.getZExtValue(), |
| 3153 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3154 | if (NewAttr) |
| 3155 | D->addAttr(NewAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3156 | } |
| 3157 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3158 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 3159 | const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3160 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3161 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3162 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3163 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3164 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3165 | // Try to find the underlying union declaration. |
| 3166 | RecordDecl *RD = 0; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3167 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3168 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3169 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3170 | else |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3171 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3172 | |
| 3173 | if (!RD || !RD->isUnion()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3174 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3175 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3176 | return; |
| 3177 | } |
| 3178 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3179 | if (!RD->isCompleteDefinition()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3180 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3181 | diag::warn_transparent_union_attribute_not_definition); |
| 3182 | return; |
| 3183 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3184 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3185 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3186 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3187 | if (Field == FieldEnd) { |
| 3188 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3189 | return; |
| 3190 | } |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3191 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3192 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3193 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3194 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3195 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3196 | diag::warn_transparent_union_attribute_floating) |
| 3197 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3198 | return; |
| 3199 | } |
| 3200 | |
| 3201 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3202 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3203 | for (; Field != FieldEnd; ++Field) { |
| 3204 | QualType FieldType = Field->getType(); |
| 3205 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 3206 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 3207 | // Warn if we drop the attribute. |
| 3208 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3209 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3210 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3211 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3212 | diag::warn_transparent_union_attribute_field_size_align) |
| 3213 | << isSize << Field->getDeclName() << FieldBits; |
| 3214 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3215 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3216 | diag::note_transparent_union_first_field_size_align) |
| 3217 | << isSize << FirstBits; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3218 | return; |
| 3219 | } |
| 3220 | } |
| 3221 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3222 | RD->addAttr(::new (S.Context) |
| 3223 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 3224 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3227 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3228 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3229 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3230 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3231 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3232 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3233 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3234 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3235 | // Make sure that there is a string literal as the annotation's single |
| 3236 | // argument. |
| 3237 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3238 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate"; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3239 | return; |
| 3240 | } |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3241 | |
| 3242 | // Don't duplicate annotations that are already set. |
| 3243 | for (specific_attr_iterator<AnnotateAttr> |
| 3244 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 3245 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
| 3246 | if ((*i)->getAnnotation() == SE->getString()) |
| 3247 | return; |
| 3248 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3249 | |
| 3250 | D->addAttr(::new (S.Context) |
| 3251 | AnnotateAttr(Attr.getRange(), S.Context, SE->getString(), |
| 3252 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3255 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3256 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3257 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3258 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3259 | return; |
| 3260 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3261 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3262 | //FIXME: The C++0x version of this attribute has more limited applicabilty |
| 3263 | // than GNU's, and should error out when it is used to specify a |
| 3264 | // weaker alignment, rather than being silently ignored. |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3265 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3266 | if (Attr.getNumArgs() == 0) { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3267 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3268 | true, 0, Attr.isDeclspecAttribute(), |
| 3269 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3270 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3271 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3272 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3273 | S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3274 | Attr.isDeclspecAttribute(), |
| 3275 | Attr.getAttributeSpellingListIndex()); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3276 | } |
| 3277 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3278 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3279 | bool isDeclSpec, unsigned SpellingListIndex) { |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 3280 | // FIXME: Handle pack-expansions here. |
| 3281 | if (DiagnoseUnexpandedParameterPack(E)) |
| 3282 | return; |
| 3283 | |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3284 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3285 | // Save dependent expressions in the AST to be instantiated. |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3286 | D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3287 | isDeclSpec, SpellingListIndex)); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3288 | return; |
| 3289 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3290 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3291 | SourceLocation AttrLoc = AttrRange.getBegin(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3292 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3293 | llvm::APSInt Alignment(32); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3294 | ExprResult ICE |
| 3295 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3296 | diag::err_aligned_attribute_argument_not_int, |
| 3297 | /*AllowFold*/ false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3298 | if (ICE.isInvalid()) |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3299 | return; |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3300 | if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3301 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 3302 | << E->getSourceRange(); |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3303 | return; |
| 3304 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3305 | if (isDeclSpec) { |
| 3306 | // We've already verified it's a power of 2, now let's make sure it's |
| 3307 | // 8192 or less. |
| 3308 | if (Alignment.getZExtValue() > 8192) { |
| 3309 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
| 3310 | << E->getSourceRange(); |
| 3311 | return; |
| 3312 | } |
| 3313 | } |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3314 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3315 | D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3316 | isDeclSpec, SpellingListIndex)); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3317 | } |
| 3318 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3319 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
| 3320 | bool isDeclSpec) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3321 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3322 | // FIXME: Perform checking of type validity |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3323 | D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3324 | isDeclSpec)); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3325 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3326 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3327 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3328 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3329 | /// type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3330 | /// |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3331 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3332 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3333 | /// HImode, not an intermediate pointer. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3334 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3335 | // This attribute isn't documented, but glibc uses it. It changes |
| 3336 | // the width of an int or unsigned int to the specified size. |
| 3337 | |
| 3338 | // Check that there aren't any arguments |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3339 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3340 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3341 | |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3342 | |
| 3343 | IdentifierInfo *Name = Attr.getParameterName(); |
| 3344 | if (!Name) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3345 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3346 | return; |
| 3347 | } |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3348 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3349 | StringRef Str = Attr.getParameterName()->getName(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3350 | |
| 3351 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3352 | if (Str.startswith("__") && Str.endswith("__")) |
| 3353 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3354 | |
| 3355 | unsigned DestWidth = 0; |
| 3356 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3357 | bool ComplexMode = false; |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3358 | switch (Str.size()) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3359 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3360 | switch (Str[0]) { |
| 3361 | case 'Q': DestWidth = 8; break; |
| 3362 | case 'H': DestWidth = 16; break; |
| 3363 | case 'S': DestWidth = 32; break; |
| 3364 | case 'D': DestWidth = 64; break; |
| 3365 | case 'X': DestWidth = 96; break; |
| 3366 | case 'T': DestWidth = 128; break; |
| 3367 | } |
| 3368 | if (Str[1] == 'F') { |
| 3369 | IntegerMode = false; |
| 3370 | } else if (Str[1] == 'C') { |
| 3371 | IntegerMode = false; |
| 3372 | ComplexMode = true; |
| 3373 | } else if (Str[1] != 'I') { |
| 3374 | DestWidth = 0; |
| 3375 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3376 | break; |
| 3377 | case 4: |
| 3378 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3379 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3380 | if (Str == "word") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3381 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3382 | else if (Str == "byte") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3383 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3384 | break; |
| 3385 | case 7: |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3386 | if (Str == "pointer") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3387 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3388 | break; |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3389 | case 11: |
| 3390 | if (Str == "unwind_word") |
Rafael Espindola | 0b1de54 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3391 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3392 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3393 | } |
| 3394 | |
| 3395 | QualType OldTy; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3396 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3397 | OldTy = TD->getUnderlyingType(); |
| 3398 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3399 | OldTy = VD->getType(); |
| 3400 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3401 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3402 | << "mode" << Attr.getRange(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3403 | return; |
| 3404 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3405 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3406 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3407 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3408 | else if (IntegerMode) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3409 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3410 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3411 | } else if (ComplexMode) { |
| 3412 | if (!OldTy->isComplexType()) |
| 3413 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3414 | } else { |
| 3415 | if (!OldTy->isFloatingType()) |
| 3416 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3417 | } |
| 3418 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3419 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3420 | // and friends, at least with glibc. |
| 3421 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 3422 | // width on unusual platforms. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3423 | // FIXME: Make sure floating-point mappings are accurate |
| 3424 | // FIXME: Support XF and TF types |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3425 | QualType NewTy; |
| 3426 | switch (DestWidth) { |
| 3427 | case 0: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3428 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3429 | return; |
| 3430 | default: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3431 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3432 | return; |
| 3433 | case 8: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3434 | if (!IntegerMode) { |
| 3435 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3436 | return; |
| 3437 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3438 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3439 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3440 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3441 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3442 | break; |
| 3443 | case 16: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3444 | if (!IntegerMode) { |
| 3445 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3446 | return; |
| 3447 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3448 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3449 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3450 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3451 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3452 | break; |
| 3453 | case 32: |
| 3454 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3455 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3456 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3457 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3458 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3459 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3460 | break; |
| 3461 | case 64: |
| 3462 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3463 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3464 | else if (OldTy->isSignedIntegerType()) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3465 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3466 | NewTy = S.Context.LongTy; |
| 3467 | else |
| 3468 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3469 | else |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3470 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3471 | NewTy = S.Context.UnsignedLongTy; |
| 3472 | else |
| 3473 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3474 | break; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3475 | case 96: |
| 3476 | NewTy = S.Context.LongDoubleTy; |
| 3477 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3478 | case 128: |
| 3479 | if (!IntegerMode) { |
| 3480 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3481 | return; |
| 3482 | } |
Anders Carlsson | f5f7d86 | 2009-12-29 07:07:36 +0000 | [diff] [blame] | 3483 | if (OldTy->isSignedIntegerType()) |
| 3484 | NewTy = S.Context.Int128Ty; |
| 3485 | else |
| 3486 | NewTy = S.Context.UnsignedInt128Ty; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3487 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3490 | if (ComplexMode) { |
| 3491 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | // Install the new type. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3495 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 3496 | // FIXME: preserve existing source info. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3497 | TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy)); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 3498 | } else |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3499 | cast<ValueDecl>(D)->setType(NewTy); |
| 3500 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3501 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3502 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3503 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3504 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3505 | return; |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 3506 | |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3507 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3508 | if (!VD->hasGlobalStorage()) |
| 3509 | S.Diag(Attr.getLoc(), |
| 3510 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3511 | << Attr.getName(); |
| 3512 | } else if (!isFunctionOrMethod(D)) { |
| 3513 | S.Diag(Attr.getLoc(), |
| 3514 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3515 | << Attr.getName(); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3516 | return; |
| 3517 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3518 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3519 | D->addAttr(::new (S.Context) |
| 3520 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3521 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3522 | } |
| 3523 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3524 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3525 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3526 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3527 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3528 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3529 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3530 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3531 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3532 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3533 | return; |
| 3534 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3535 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3536 | D->addAttr(::new (S.Context) |
| 3537 | NoInlineAttr(Attr.getRange(), S.Context, |
| 3538 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3541 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 3542 | const AttributeList &Attr) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3543 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3544 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3545 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3546 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3547 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3548 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3549 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3550 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3551 | return; |
| 3552 | } |
| 3553 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3554 | D->addAttr(::new (S.Context) |
| 3555 | NoInstrumentFunctionAttr(Attr.getRange(), S.Context, |
| 3556 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3557 | } |
| 3558 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3559 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3560 | if (S.LangOpts.CUDA) { |
| 3561 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 3562 | if (Attr.hasParameterOrArguments()) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3563 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 3564 | return; |
| 3565 | } |
| 3566 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3567 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3568 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3569 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3570 | return; |
| 3571 | } |
| 3572 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3573 | D->addAttr(::new (S.Context) |
| 3574 | CUDAConstantAttr(Attr.getRange(), S.Context, |
| 3575 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3576 | } else { |
| 3577 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 3578 | } |
| 3579 | } |
| 3580 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3581 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3582 | if (S.LangOpts.CUDA) { |
| 3583 | // check the attribute arguments. |
| 3584 | if (Attr.getNumArgs() != 0) { |
| 3585 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 3586 | return; |
| 3587 | } |
| 3588 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3589 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3590 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3591 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3592 | return; |
| 3593 | } |
| 3594 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3595 | D->addAttr(::new (S.Context) |
| 3596 | CUDADeviceAttr(Attr.getRange(), S.Context, |
| 3597 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3598 | } else { |
| 3599 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 3600 | } |
| 3601 | } |
| 3602 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3603 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3604 | if (S.LangOpts.CUDA) { |
| 3605 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3606 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3607 | return; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3608 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3609 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3610 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3611 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3612 | return; |
| 3613 | } |
| 3614 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3615 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3616 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 3617 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3618 | if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) { |
| 3619 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3620 | << FD->getType() |
| 3621 | << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(), |
| 3622 | "void"); |
| 3623 | } else { |
| 3624 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3625 | << FD->getType(); |
| 3626 | } |
| 3627 | return; |
| 3628 | } |
| 3629 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3630 | D->addAttr(::new (S.Context) |
| 3631 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
| 3632 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3633 | } else { |
| 3634 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 3635 | } |
| 3636 | } |
| 3637 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3638 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3639 | if (S.LangOpts.CUDA) { |
| 3640 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3641 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3642 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3643 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3644 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3645 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3646 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3647 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3648 | return; |
| 3649 | } |
| 3650 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3651 | D->addAttr(::new (S.Context) |
| 3652 | CUDAHostAttr(Attr.getRange(), S.Context, |
| 3653 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3654 | } else { |
| 3655 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 3656 | } |
| 3657 | } |
| 3658 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3659 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3660 | if (S.LangOpts.CUDA) { |
| 3661 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3662 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3663 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3664 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3665 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3666 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3667 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3668 | return; |
| 3669 | } |
| 3670 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3671 | D->addAttr(::new (S.Context) |
| 3672 | CUDASharedAttr(Attr.getRange(), S.Context, |
| 3673 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3674 | } else { |
| 3675 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 3676 | } |
| 3677 | } |
| 3678 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3679 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3680 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3681 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3682 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3683 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3684 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3685 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3686 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3687 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3688 | return; |
| 3689 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3690 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3691 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3692 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3693 | return; |
| 3694 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3695 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3696 | D->addAttr(::new (S.Context) |
| 3697 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3698 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3699 | } |
| 3700 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3701 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3702 | if (hasDeclarator(D)) return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3703 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3704 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3705 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3706 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3707 | CallingConv CC; |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3708 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3709 | return; |
| 3710 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3711 | if (!isa<ObjCMethodDecl>(D)) { |
| 3712 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3713 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3714 | return; |
| 3715 | } |
| 3716 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3717 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3718 | case AttributeList::AT_FastCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3719 | D->addAttr(::new (S.Context) |
| 3720 | FastCallAttr(Attr.getRange(), S.Context, |
| 3721 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3722 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3723 | case AttributeList::AT_StdCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3724 | D->addAttr(::new (S.Context) |
| 3725 | StdCallAttr(Attr.getRange(), S.Context, |
| 3726 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3727 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3728 | case AttributeList::AT_ThisCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3729 | D->addAttr(::new (S.Context) |
| 3730 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3731 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 04633eb | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3732 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3733 | case AttributeList::AT_CDecl: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3734 | D->addAttr(::new (S.Context) |
| 3735 | CDeclAttr(Attr.getRange(), S.Context, |
| 3736 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3737 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3738 | case AttributeList::AT_Pascal: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3739 | D->addAttr(::new (S.Context) |
| 3740 | PascalAttr(Attr.getRange(), S.Context, |
| 3741 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3742 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3743 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3744 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3745 | switch (CC) { |
| 3746 | case CC_AAPCS: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3747 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3748 | break; |
| 3749 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3750 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3751 | break; |
| 3752 | default: |
| 3753 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3754 | } |
| 3755 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3756 | D->addAttr(::new (S.Context) |
| 3757 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3758 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3759 | return; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3760 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3761 | case AttributeList::AT_PnaclCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3762 | D->addAttr(::new (S.Context) |
| 3763 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 3764 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3765 | return; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3766 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3767 | D->addAttr(::new (S.Context) |
| 3768 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3769 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3770 | return; |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3771 | |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3772 | default: |
| 3773 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3774 | } |
| 3775 | } |
| 3776 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3777 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 3778 | assert(!Attr.isInvalid()); |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3779 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3780 | } |
| 3781 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3782 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3783 | const FunctionDecl *FD) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3784 | if (attr.isInvalid()) |
| 3785 | return true; |
| 3786 | |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3787 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
| 3788 | if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) { |
| 3789 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3790 | attr.setInvalid(); |
| 3791 | return true; |
| 3792 | } |
| 3793 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3794 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 3795 | // move to TargetAttributesSema one day. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3796 | switch (attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3797 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3798 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3799 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3800 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3801 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
| 3802 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3803 | Expr *Arg = attr.getArg(0); |
| 3804 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3805 | if (!Str || !Str->isAscii()) { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3806 | Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 3807 | << "pcs" << 1; |
| 3808 | attr.setInvalid(); |
| 3809 | return true; |
| 3810 | } |
| 3811 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3812 | StringRef StrRef = Str->getString(); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3813 | if (StrRef == "aapcs") { |
| 3814 | CC = CC_AAPCS; |
| 3815 | break; |
| 3816 | } else if (StrRef == "aapcs-vfp") { |
| 3817 | CC = CC_AAPCS_VFP; |
| 3818 | break; |
| 3819 | } |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3820 | |
| 3821 | attr.setInvalid(); |
| 3822 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3823 | return true; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3824 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3825 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3826 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3827 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3828 | } |
| 3829 | |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3830 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3831 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 3832 | if (A == TargetInfo::CCCR_Warning) { |
| 3833 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3834 | |
| 3835 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3836 | if (FD) |
| 3837 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3838 | TargetInfo::CCMT_NonMember; |
| 3839 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3840 | } |
| 3841 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3842 | return false; |
| 3843 | } |
| 3844 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3845 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3846 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3847 | |
| 3848 | unsigned numParams; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3849 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3850 | return; |
| 3851 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3852 | if (!isa<ObjCMethodDecl>(D)) { |
| 3853 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3854 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3855 | return; |
| 3856 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3857 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3858 | D->addAttr(::new (S.Context) |
| 3859 | RegparmAttr(Attr.getRange(), S.Context, numParams, |
| 3860 | Attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
| 3863 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3864 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3865 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3866 | if (Attr.isInvalid()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3867 | return true; |
| 3868 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3869 | if (Attr.getNumArgs() != 1) { |
| 3870 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 3871 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3872 | return true; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3873 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3874 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3875 | Expr *NumParamsExpr = Attr.getArg(0); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3876 | llvm::APSInt NumParams(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3877 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3878 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3879 | Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3880 | << "regparm" << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3881 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3882 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3883 | } |
| 3884 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3885 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3886 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3887 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3888 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3889 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3890 | } |
| 3891 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3892 | numParams = NumParams.getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3893 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3894 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3895 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3896 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3897 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3900 | return false; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3901 | } |
| 3902 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3903 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3904 | if (S.LangOpts.CUDA) { |
| 3905 | // check the attribute arguments. |
| 3906 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 3907 | // FIXME: 0 is not okay. |
| 3908 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3909 | return; |
| 3910 | } |
| 3911 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3912 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3913 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3914 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3915 | return; |
| 3916 | } |
| 3917 | |
| 3918 | Expr *MaxThreadsExpr = Attr.getArg(0); |
| 3919 | llvm::APSInt MaxThreads(32); |
| 3920 | if (MaxThreadsExpr->isTypeDependent() || |
| 3921 | MaxThreadsExpr->isValueDependent() || |
| 3922 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
| 3923 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 3924 | << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange(); |
| 3925 | return; |
| 3926 | } |
| 3927 | |
| 3928 | llvm::APSInt MinBlocks(32); |
| 3929 | if (Attr.getNumArgs() > 1) { |
| 3930 | Expr *MinBlocksExpr = Attr.getArg(1); |
| 3931 | if (MinBlocksExpr->isTypeDependent() || |
| 3932 | MinBlocksExpr->isValueDependent() || |
| 3933 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
| 3934 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 3935 | << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange(); |
| 3936 | return; |
| 3937 | } |
| 3938 | } |
| 3939 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3940 | D->addAttr(::new (S.Context) |
| 3941 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 3942 | MaxThreads.getZExtValue(), |
| 3943 | MinBlocks.getZExtValue(), |
| 3944 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3945 | } else { |
| 3946 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 3947 | } |
| 3948 | } |
| 3949 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3950 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 3951 | const AttributeList &Attr) { |
| 3952 | StringRef AttrName = Attr.getName()->getName(); |
| 3953 | if (!Attr.getParameterName()) { |
| 3954 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier) |
| 3955 | << Attr.getName() << /* arg num = */ 1; |
| 3956 | return; |
| 3957 | } |
| 3958 | |
| 3959 | if (Attr.getNumArgs() != 2) { |
| 3960 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3961 | << /* required args = */ 3; |
| 3962 | return; |
| 3963 | } |
| 3964 | |
| 3965 | IdentifierInfo *ArgumentKind = Attr.getParameterName(); |
| 3966 | |
| 3967 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 3968 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3969 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 3970 | return; |
| 3971 | } |
| 3972 | |
| 3973 | uint64_t ArgumentIdx; |
| 3974 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 3975 | Attr.getLoc(), 2, |
| 3976 | Attr.getArg(0), ArgumentIdx)) |
| 3977 | return; |
| 3978 | |
| 3979 | uint64_t TypeTagIdx; |
| 3980 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 3981 | Attr.getLoc(), 3, |
| 3982 | Attr.getArg(1), TypeTagIdx)) |
| 3983 | return; |
| 3984 | |
| 3985 | bool IsPointer = (AttrName == "pointer_with_type_tag"); |
| 3986 | if (IsPointer) { |
| 3987 | // Ensure that buffer has a pointer type. |
| 3988 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 3989 | if (!BufferTy->isPointerType()) { |
| 3990 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
| 3991 | << AttrName; |
| 3992 | } |
| 3993 | } |
| 3994 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3995 | D->addAttr(::new (S.Context) |
| 3996 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 3997 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 3998 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3999 | } |
| 4000 | |
| 4001 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 4002 | const AttributeList &Attr) { |
| 4003 | IdentifierInfo *PointerKind = Attr.getParameterName(); |
| 4004 | if (!PointerKind) { |
| 4005 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier) |
| 4006 | << "type_tag_for_datatype" << 1; |
| 4007 | return; |
| 4008 | } |
| 4009 | |
| 4010 | QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL); |
| 4011 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4012 | D->addAttr(::new (S.Context) |
| 4013 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
| 4014 | MatchingCType, |
| 4015 | Attr.getLayoutCompatible(), |
| 4016 | Attr.getMustBeNull(), |
| 4017 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4018 | } |
| 4019 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4020 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4021 | // Checker-specific attribute handlers. |
| 4022 | //===----------------------------------------------------------------------===// |
| 4023 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4024 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4025 | return type->isDependentType() || |
| 4026 | type->isObjCObjectPointerType() || |
| 4027 | S.Context.isObjCNSObjectType(type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4028 | } |
| 4029 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4030 | return type->isDependentType() || |
| 4031 | type->isPointerType() || |
| 4032 | isValidSubjectOfNSAttribute(S, type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4033 | } |
| 4034 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4035 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4036 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4037 | if (!param) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4038 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4039 | << Attr.getRange() << Attr.getName() << ExpectedParameter; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4040 | return; |
| 4041 | } |
| 4042 | |
| 4043 | bool typeOK, cf; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4044 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4045 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 4046 | cf = false; |
| 4047 | } else { |
| 4048 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 4049 | cf = true; |
| 4050 | } |
| 4051 | |
| 4052 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4053 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4054 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4055 | return; |
| 4056 | } |
| 4057 | |
| 4058 | if (cf) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4059 | param->addAttr(::new (S.Context) |
| 4060 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 4061 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4062 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4063 | param->addAttr(::new (S.Context) |
| 4064 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 4065 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4066 | } |
| 4067 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4068 | static void handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 4069 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4070 | if (!isa<ObjCMethodDecl>(D)) { |
| 4071 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4072 | << Attr.getRange() << Attr.getName() << ExpectedMethod; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4073 | return; |
| 4074 | } |
| 4075 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4076 | D->addAttr(::new (S.Context) |
| 4077 | NSConsumesSelfAttr(Attr.getRange(), S.Context, |
| 4078 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4079 | } |
| 4080 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4081 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 4082 | const AttributeList &Attr) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4083 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4084 | QualType returnType; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4085 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4086 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4087 | returnType = MD->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4088 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4089 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4090 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | a23bd4c | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 4091 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4092 | returnType = PD->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4093 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4094 | returnType = FD->getResultType(); |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4095 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4096 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4097 | << Attr.getRange() << Attr.getName() |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4098 | << ExpectedFunctionOrMethod; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4099 | return; |
| 4100 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4101 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4102 | bool typeOK; |
| 4103 | bool cf; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4104 | switch (Attr.getKind()) { |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4105 | default: llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4106 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4107 | case AttributeList::AT_NSReturnsRetained: |
| 4108 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4109 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 4110 | cf = false; |
| 4111 | break; |
| 4112 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4113 | case AttributeList::AT_CFReturnsRetained: |
| 4114 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4115 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 4116 | cf = true; |
| 4117 | break; |
| 4118 | } |
| 4119 | |
| 4120 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4121 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4122 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4123 | return; |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4124 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4125 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4126 | switch (Attr.getKind()) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4127 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4128 | llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4129 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4130 | D->addAttr(::new (S.Context) |
| 4131 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 4132 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4133 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4134 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4135 | D->addAttr(::new (S.Context) |
| 4136 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4137 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4138 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4139 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4140 | D->addAttr(::new (S.Context) |
| 4141 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4142 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4143 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4144 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4145 | D->addAttr(::new (S.Context) |
| 4146 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4147 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4148 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4149 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4150 | D->addAttr(::new (S.Context) |
| 4151 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4152 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4153 | return; |
| 4154 | }; |
| 4155 | } |
| 4156 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4157 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4158 | const AttributeList &attr) { |
| 4159 | SourceLocation loc = attr.getLoc(); |
| 4160 | |
| 4161 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4162 | |
Fariborz Jahanian | 94d55d7 | 2012-04-21 17:51:44 +0000 | [diff] [blame] | 4163 | if (!method) { |
Fariborz Jahanian | 0e78afb | 2012-04-20 22:00:46 +0000 | [diff] [blame] | 4164 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4165 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4166 | return; |
| 4167 | } |
| 4168 | |
| 4169 | // Check that the method returns a normal pointer. |
| 4170 | QualType resultType = method->getResultType(); |
Fariborz Jahanian | f2e5945 | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4171 | |
| 4172 | if (!resultType->isReferenceType() && |
| 4173 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4174 | S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 4175 | << SourceRange(loc) |
| 4176 | << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2; |
| 4177 | |
| 4178 | // Drop the attribute. |
| 4179 | return; |
| 4180 | } |
| 4181 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4182 | method->addAttr(::new (S.Context) |
| 4183 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 4184 | attr.getAttributeSpellingListIndex())); |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4187 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4188 | const AttributeList &attr) { |
| 4189 | SourceLocation loc = attr.getLoc(); |
| 4190 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4191 | |
| 4192 | if (!method) { |
| 4193 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 4194 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
| 4195 | return; |
| 4196 | } |
| 4197 | DeclContext *DC = method->getDeclContext(); |
| 4198 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4199 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4200 | << attr.getName() << 0; |
| 4201 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4202 | return; |
| 4203 | } |
| 4204 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4205 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4206 | << attr.getName() << 1; |
| 4207 | return; |
| 4208 | } |
| 4209 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4210 | method->addAttr(::new (S.Context) |
| 4211 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 4212 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4213 | } |
| 4214 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4215 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 4216 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
| 4217 | if (!isa<FunctionDecl>(D)) { |
| 4218 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4219 | << A.getRange() << A.getName() << ExpectedFunction; |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4220 | return; |
| 4221 | } |
| 4222 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4223 | bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4224 | |
| 4225 | // Check whether there's a conflicting attribute already present. |
| 4226 | Attr *Existing; |
| 4227 | if (IsAudited) { |
| 4228 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 4229 | } else { |
| 4230 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 4231 | } |
| 4232 | if (Existing) { |
| 4233 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 4234 | << A.getName() |
| 4235 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 4236 | << A.getRange() << Existing->getRange(); |
| 4237 | return; |
| 4238 | } |
| 4239 | |
| 4240 | // All clear; add the attribute. |
| 4241 | if (IsAudited) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4242 | D->addAttr(::new (S.Context) |
| 4243 | CFAuditedTransferAttr(A.getRange(), S.Context, |
| 4244 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4245 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4246 | D->addAttr(::new (S.Context) |
| 4247 | CFUnknownTransferAttr(A.getRange(), S.Context, |
| 4248 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4249 | } |
| 4250 | } |
| 4251 | |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4252 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4253 | const AttributeList &Attr) { |
| 4254 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4255 | if (!RD || RD->isUnion()) { |
| 4256 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4257 | << Attr.getRange() << Attr.getName() << ExpectedStruct; |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4258 | } |
| 4259 | |
| 4260 | IdentifierInfo *ParmName = Attr.getParameterName(); |
| 4261 | |
| 4262 | // In Objective-C, verify that the type names an Objective-C type. |
| 4263 | // We don't want to check this outside of ObjC because people sometimes |
| 4264 | // do crazy C declarations of Objective-C types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4265 | if (ParmName && S.getLangOpts().ObjC1) { |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4266 | // Check for an existing type with this name. |
| 4267 | LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(), |
| 4268 | Sema::LookupOrdinaryName); |
| 4269 | if (S.LookupName(R, Sc)) { |
| 4270 | NamedDecl *Target = R.getFoundDecl(); |
| 4271 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 4272 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 4273 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 4274 | } |
| 4275 | } |
| 4276 | } |
| 4277 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4278 | D->addAttr(::new (S.Context) |
| 4279 | NSBridgedAttr(Attr.getRange(), S.Context, ParmName, |
| 4280 | Attr.getAttributeSpellingListIndex())); |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4281 | } |
| 4282 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4283 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4284 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4285 | if (hasDeclarator(D)) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4286 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4287 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4288 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4289 | } |
| 4290 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4291 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4292 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4293 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4294 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4295 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4296 | return; |
| 4297 | } |
| 4298 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4299 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4300 | QualType type = vd->getType(); |
| 4301 | |
| 4302 | if (!type->isDependentType() && |
| 4303 | !type->isObjCLifetimeType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4304 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4305 | << type; |
| 4306 | return; |
| 4307 | } |
| 4308 | |
| 4309 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4310 | |
| 4311 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4312 | // going to infer. |
| 4313 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4314 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4315 | |
| 4316 | switch (lifetime) { |
| 4317 | case Qualifiers::OCL_None: |
| 4318 | assert(type->isDependentType() && |
| 4319 | "didn't infer lifetime for non-dependent type?"); |
| 4320 | break; |
| 4321 | |
| 4322 | case Qualifiers::OCL_Weak: // meaningful |
| 4323 | case Qualifiers::OCL_Strong: // meaningful |
| 4324 | break; |
| 4325 | |
| 4326 | case Qualifiers::OCL_ExplicitNone: |
| 4327 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4328 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4329 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4330 | break; |
| 4331 | } |
| 4332 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4333 | D->addAttr(::new (S.Context) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4334 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 4335 | Attr.getAttributeSpellingListIndex())); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4336 | } |
| 4337 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4338 | //===----------------------------------------------------------------------===// |
| 4339 | // Microsoft specific attribute handlers. |
| 4340 | //===----------------------------------------------------------------------===// |
| 4341 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4342 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 4343 | if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) { |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4344 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4345 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4346 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4347 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4348 | Expr *Arg = Attr.getArg(0); |
| 4349 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4350 | if (!Str || !Str->isAscii()) { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4351 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 4352 | << "uuid" << 1; |
| 4353 | return; |
| 4354 | } |
| 4355 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4356 | StringRef StrRef = Str->getString(); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4357 | |
| 4358 | bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' && |
| 4359 | StrRef.back() == '}'; |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4360 | |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4361 | // Validate GUID length. |
| 4362 | if (IsCurly && StrRef.size() != 38) { |
| 4363 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4364 | return; |
| 4365 | } |
| 4366 | if (!IsCurly && StrRef.size() != 36) { |
| 4367 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4368 | return; |
| 4369 | } |
| 4370 | |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4371 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4372 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4373 | StringRef::iterator I = StrRef.begin(); |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4374 | if (IsCurly) // Skip the optional '{' |
| 4375 | ++I; |
| 4376 | |
| 4377 | for (int i = 0; i < 36; ++i) { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4378 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 4379 | if (*I != '-') { |
| 4380 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4381 | return; |
| 4382 | } |
| 4383 | } else if (!isxdigit(*I)) { |
| 4384 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4385 | return; |
| 4386 | } |
| 4387 | I++; |
| 4388 | } |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4389 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4390 | D->addAttr(::new (S.Context) |
| 4391 | UuidAttr(Attr.getRange(), S.Context, Str->getString(), |
| 4392 | Attr.getAttributeSpellingListIndex())); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4393 | } else |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4394 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid"; |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4395 | } |
| 4396 | |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4397 | static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4398 | if (!S.LangOpts.MicrosoftExt) { |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4399 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4400 | return; |
| 4401 | } |
| 4402 | |
| 4403 | AttributeList::Kind Kind = Attr.getKind(); |
| 4404 | if (Kind == AttributeList::AT_SingleInheritance) |
| 4405 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4406 | ::new (S.Context) |
| 4407 | SingleInheritanceAttr(Attr.getRange(), S.Context, |
| 4408 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4409 | else if (Kind == AttributeList::AT_MultipleInheritance) |
| 4410 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4411 | ::new (S.Context) |
| 4412 | MultipleInheritanceAttr(Attr.getRange(), S.Context, |
| 4413 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4414 | else if (Kind == AttributeList::AT_VirtualInheritance) |
| 4415 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4416 | ::new (S.Context) |
| 4417 | VirtualInheritanceAttr(Attr.getRange(), S.Context, |
| 4418 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4419 | } |
| 4420 | |
| 4421 | static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4422 | if (S.LangOpts.MicrosoftExt) { |
| 4423 | AttributeList::Kind Kind = Attr.getKind(); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4424 | if (Kind == AttributeList::AT_Ptr32) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4425 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4426 | ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context, |
| 4427 | Attr.getAttributeSpellingListIndex())); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4428 | else if (Kind == AttributeList::AT_Ptr64) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4429 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4430 | ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context, |
| 4431 | Attr.getAttributeSpellingListIndex())); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4432 | else if (Kind == AttributeList::AT_Win64) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4433 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4434 | ::new (S.Context) Win64Attr(Attr.getRange(), S.Context, |
| 4435 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4436 | } else |
| 4437 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4438 | } |
| 4439 | |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4440 | static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4441 | if (S.LangOpts.MicrosoftExt) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4442 | D->addAttr(::new (S.Context) |
| 4443 | ForceInlineAttr(Attr.getRange(), S.Context, |
| 4444 | Attr.getAttributeSpellingListIndex())); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4445 | else |
| 4446 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4447 | } |
| 4448 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4449 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4450 | // Top Level Sema Entry Points |
| 4451 | //===----------------------------------------------------------------------===// |
| 4452 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4453 | static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4454 | const AttributeList &Attr) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4455 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4456 | case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break; |
| 4457 | case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break; |
| 4458 | case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break; |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4459 | default: |
| 4460 | break; |
| 4461 | } |
| 4462 | } |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4463 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4464 | static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4465 | const AttributeList &Attr) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4466 | switch (Attr.getKind()) { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4467 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 4468 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 4469 | case AttributeList::AT_IBOutletCollection: |
| 4470 | handleIBOutletCollection(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4471 | case AttributeList::AT_AddressSpace: |
| 4472 | case AttributeList::AT_OpenCLImageAccess: |
| 4473 | case AttributeList::AT_ObjCGC: |
| 4474 | case AttributeList::AT_VectorSize: |
| 4475 | case AttributeList::AT_NeonVectorType: |
| 4476 | case AttributeList::AT_NeonPolyVectorType: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4477 | // Ignore these, these are type attributes, handled by |
| 4478 | // ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4479 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4480 | case AttributeList::AT_CUDADevice: |
| 4481 | case AttributeList::AT_CUDAHost: |
| 4482 | case AttributeList::AT_Overloadable: |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4483 | // Ignore, this is a non-inheritable attribute, handled |
| 4484 | // by ProcessNonInheritableDeclAttr. |
| 4485 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4486 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 4487 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 4488 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 4489 | case AttributeList::AT_AlwaysInline: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4490 | handleAlwaysInlineAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4491 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4492 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 4493 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4494 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 4495 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 4496 | case AttributeList::AT_CarriesDependency: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4497 | handleDependencyAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4498 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
| 4499 | case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break; |
| 4500 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4501 | case AttributeList::AT_CXX11NoReturn: |
| 4502 | handleCXX11NoReturnAttr(S, D, Attr); |
| 4503 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4504 | case AttributeList::AT_Deprecated: |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4505 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated"); |
| 4506 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4507 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 4508 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4509 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4510 | break; |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4511 | case AttributeList::AT_MinSize: |
| 4512 | handleMinSizeAttr(S, D, Attr); |
| 4513 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4514 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4515 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4516 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
| 4517 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4518 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4519 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4520 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4521 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
| 4522 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
| 4523 | case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break; |
| 4524 | case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break; |
| 4525 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4526 | case AttributeList::AT_ownership_returns: |
| 4527 | case AttributeList::AT_ownership_takes: |
| 4528 | case AttributeList::AT_ownership_holds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4529 | handleOwnershipAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4530 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4531 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
| 4532 | case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break; |
| 4533 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
| 4534 | case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break; |
| 4535 | case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break; |
| 4536 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4537 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4538 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4539 | handleObjCOwnershipAttr(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4540 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4541 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4542 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4543 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4544 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4545 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4546 | case AttributeList::AT_ObjCRequiresSuper: |
| 4547 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4548 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4549 | case AttributeList::AT_NSBridged: |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4550 | handleNSBridgedAttr(S, scope, D, Attr); break; |
| 4551 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4552 | case AttributeList::AT_CFAuditedTransfer: |
| 4553 | case AttributeList::AT_CFUnknownTransfer: |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4554 | handleCFTransferAttr(S, D, Attr); break; |
| 4555 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4556 | // Checker-specific. |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4557 | case AttributeList::AT_CFConsumed: |
| 4558 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4559 | case AttributeList::AT_NSConsumesSelf: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4560 | handleNSConsumesSelfAttr(S, D, Attr); break; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4561 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4562 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4563 | case AttributeList::AT_NSReturnsNotRetained: |
| 4564 | case AttributeList::AT_CFReturnsNotRetained: |
| 4565 | case AttributeList::AT_NSReturnsRetained: |
| 4566 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4567 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4568 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4569 | case AttributeList::AT_WorkGroupSizeHint: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4570 | case AttributeList::AT_ReqdWorkGroupSize: |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4571 | handleWorkGroupSize(S, D, Attr); break; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 4572 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4573 | case AttributeList::AT_InitPriority: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4574 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4575 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4576 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4577 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4578 | case AttributeList::AT_Unavailable: |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4579 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable"); |
| 4580 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4581 | case AttributeList::AT_ArcWeakrefUnavailable: |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 4582 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 4583 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4584 | case AttributeList::AT_ObjCRootClass: |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4585 | handleObjCRootClassAttr(S, D, Attr); |
| 4586 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4587 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 4588 | handleObjCRequiresPropertyDefsAttr (S, D, Attr); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 4589 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4590 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4591 | case AttributeList::AT_ReturnsTwice: |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 4592 | handleReturnsTwiceAttr(S, D, Attr); |
| 4593 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4594 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
| 4595 | case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break; |
| 4596 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4597 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4598 | case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; |
| 4599 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4600 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4601 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4602 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4603 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4604 | case AttributeList::AT_ObjCException: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4605 | handleObjCExceptionAttr(S, D, Attr); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 4606 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4607 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4608 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4609 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4610 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4611 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4612 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
| 4613 | case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break; |
| 4614 | case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break; |
| 4615 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4616 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
| 4617 | case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break; |
| 4618 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4619 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4620 | // Just ignore |
| 4621 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4622 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4623 | handleNoInstrumentFunctionAttr(S, D, Attr); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 4624 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4625 | case AttributeList::AT_StdCall: |
| 4626 | case AttributeList::AT_CDecl: |
| 4627 | case AttributeList::AT_FastCall: |
| 4628 | case AttributeList::AT_ThisCall: |
| 4629 | case AttributeList::AT_Pascal: |
| 4630 | case AttributeList::AT_Pcs: |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4631 | case AttributeList::AT_PnaclCall: |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4632 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4633 | handleCallConvAttr(S, D, Attr); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4634 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4635 | case AttributeList::AT_OpenCLKernel: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4636 | handleOpenCLKernelAttr(S, D, Attr); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4637 | break; |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4638 | |
| 4639 | // Microsoft attributes: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4640 | case AttributeList::AT_MsStruct: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4641 | handleMsStructAttr(S, D, Attr); |
| 4642 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4643 | case AttributeList::AT_Uuid: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4644 | handleUuidAttr(S, D, Attr); |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4645 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4646 | case AttributeList::AT_SingleInheritance: |
| 4647 | case AttributeList::AT_MultipleInheritance: |
| 4648 | case AttributeList::AT_VirtualInheritance: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4649 | handleInheritanceAttr(S, D, Attr); |
| 4650 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4651 | case AttributeList::AT_Win64: |
| 4652 | case AttributeList::AT_Ptr32: |
| 4653 | case AttributeList::AT_Ptr64: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4654 | handlePortabilityAttr(S, D, Attr); |
| 4655 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4656 | case AttributeList::AT_ForceInline: |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4657 | handleForceInlineAttr(S, D, Attr); |
| 4658 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4659 | |
| 4660 | // Thread safety attributes: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4661 | case AttributeList::AT_GuardedVar: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4662 | handleGuardedVarAttr(S, D, Attr); |
| 4663 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4664 | case AttributeList::AT_PtGuardedVar: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4665 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4666 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4667 | case AttributeList::AT_ScopedLockable: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4668 | handleScopedLockableAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4669 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4670 | case AttributeList::AT_NoAddressSafetyAnalysis: |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4671 | handleNoAddressSafetyAttr(S, D, Attr); |
| 4672 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4673 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4674 | handleNoThreadSafetyAttr(S, D, Attr); |
| 4675 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4676 | case AttributeList::AT_Lockable: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4677 | handleLockableAttr(S, D, Attr); |
| 4678 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4679 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4680 | handleGuardedByAttr(S, D, Attr); |
| 4681 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4682 | case AttributeList::AT_PtGuardedBy: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4683 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4684 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4685 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4686 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4687 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4688 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4689 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4690 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4691 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4692 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4693 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4694 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4695 | handleLockReturnedAttr(S, D, Attr); |
| 4696 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4697 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4698 | handleLocksExcludedAttr(S, D, Attr); |
| 4699 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4700 | case AttributeList::AT_SharedLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4701 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4702 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4703 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4704 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4705 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4706 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4707 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4708 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4709 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4710 | handleUnlockFunAttr(S, D, Attr); |
| 4711 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4712 | case AttributeList::AT_AcquiredBefore: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4713 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4714 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4715 | case AttributeList::AT_AcquiredAfter: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4716 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4717 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4718 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4719 | // Type safety attributes. |
| 4720 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4721 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4722 | break; |
| 4723 | case AttributeList::AT_TypeTagForDatatype: |
| 4724 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4725 | break; |
| 4726 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4727 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4728 | // Ask target about the attribute. |
| 4729 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 4730 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4731 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 4732 | diag::warn_unhandled_ms_attribute_ignored : |
| 4733 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4734 | break; |
| 4735 | } |
| 4736 | } |
| 4737 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4738 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4739 | /// the attribute applies to decls. If the attribute is a type attribute, just |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4740 | /// silently ignore it if a GNU attribute. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4741 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4742 | const AttributeList &Attr, |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4743 | bool NonInheritable, bool Inheritable, |
| 4744 | bool IncludeCXX11Attributes) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4745 | if (Attr.isInvalid()) |
| 4746 | return; |
| 4747 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4748 | // Type attributes are still treated as declaration attributes by |
| 4749 | // ParseMicrosoftTypeAttributes and ParseBorlandTypeAttributes. We don't |
| 4750 | // want to process them, however, because we will simply warn about ignoring |
| 4751 | // them. So instead, we will bail out early. |
| 4752 | if (Attr.isMSTypespecAttribute()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4753 | return; |
| 4754 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4755 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 4756 | // instead. |
| 4757 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 4758 | return; |
| 4759 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4760 | if (NonInheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4761 | ProcessNonInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4762 | |
| 4763 | if (Inheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4764 | ProcessInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4765 | } |
| 4766 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4767 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4768 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4769 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4770 | const AttributeList *AttrList, |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4771 | bool NonInheritable, bool Inheritable, |
| 4772 | bool IncludeCXX11Attributes) { |
| 4773 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
| 4774 | ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable, |
| 4775 | IncludeCXX11Attributes); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4776 | |
| 4777 | // GCC accepts |
| 4778 | // static int a9 __attribute__((weakref)); |
| 4779 | // but that looks really pointless. We reject it. |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4780 | if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4781 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Rafael Espindola | 4d8a33b | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4782 | cast<NamedDecl>(D)->getNameAsString(); |
| 4783 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4784 | return; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4785 | } |
| 4786 | } |
| 4787 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4788 | // Annotation attributes are the only attributes allowed after an access |
| 4789 | // specifier. |
| 4790 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4791 | const AttributeList *AttrList) { |
| 4792 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4793 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4794 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4795 | } else { |
| 4796 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4797 | return true; |
| 4798 | } |
| 4799 | } |
| 4800 | |
| 4801 | return false; |
| 4802 | } |
| 4803 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4804 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 4805 | /// contains any decl attributes that we should warn about. |
| 4806 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 4807 | for ( ; A; A = A->getNext()) { |
| 4808 | // Only warn if the attribute is an unignored, non-type attribute. |
| 4809 | if (A->isUsedAsTypeAttr()) continue; |
| 4810 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 4811 | |
| 4812 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 4813 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 4814 | << A->getName() << A->getRange(); |
| 4815 | } else { |
| 4816 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 4817 | << A->getName() << A->getRange(); |
| 4818 | } |
| 4819 | } |
| 4820 | } |
| 4821 | |
| 4822 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 4823 | /// used to build a declaration, complain about any decl attributes |
| 4824 | /// which might be lying around on it. |
| 4825 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 4826 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 4827 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 4828 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 4829 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 4830 | } |
| 4831 | |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4832 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4833 | /// \#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] | 4834 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 4835 | SourceLocation Loc) { |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4836 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4837 | NamedDecl *NewD = 0; |
| 4838 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4839 | FunctionDecl *NewFD; |
| 4840 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 4841 | // FIXME: Mangling? |
| 4842 | // FIXME: Is the qualifier info correct? |
| 4843 | // FIXME: Is the DeclContext correct? |
| 4844 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 4845 | Loc, Loc, DeclarationName(II), |
| 4846 | FD->getType(), FD->getTypeSourceInfo(), |
| 4847 | SC_None, SC_None, |
| 4848 | false/*isInlineSpecified*/, |
| 4849 | FD->hasPrototype(), |
| 4850 | false/*isConstexprSpecified*/); |
| 4851 | NewD = NewFD; |
| 4852 | |
| 4853 | if (FD->getQualifier()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4854 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4855 | |
| 4856 | // Fake up parameter variables; they are declared as if this were |
| 4857 | // a typedef. |
| 4858 | QualType FDTy = FD->getType(); |
| 4859 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 4860 | SmallVector<ParmVarDecl*, 16> Params; |
| 4861 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 4862 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 4863 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 4864 | Param->setScopeInfo(0, Params.size()); |
| 4865 | Params.push_back(Param); |
| 4866 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 4867 | NewFD->setParams(Params); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4868 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4869 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 4870 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4871 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4872 | VD->getType(), VD->getTypeSourceInfo(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 4873 | VD->getStorageClass(), |
| 4874 | VD->getStorageClassAsWritten()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4875 | if (VD->getQualifier()) { |
| 4876 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4877 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4878 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4879 | } |
| 4880 | return NewD; |
| 4881 | } |
| 4882 | |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4883 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4884 | /// applied to it, possibly with an alias. |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4885 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4886 | if (W.getUsed()) return; // only do this once |
| 4887 | W.setUsed(true); |
| 4888 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 4889 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4890 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4891 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 4892 | NDId->getName())); |
| 4893 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4894 | WeakTopLevelDecl.push_back(NewD); |
| 4895 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 4896 | // to insert Decl at TU scope, sorry. |
| 4897 | DeclContext *SavedContext = CurContext; |
| 4898 | CurContext = Context.getTranslationUnitDecl(); |
| 4899 | PushOnScopeChains(NewD, S); |
| 4900 | CurContext = SavedContext; |
| 4901 | } else { // just add weak to existing |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4902 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4903 | } |
| 4904 | } |
| 4905 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4906 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 4907 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 4908 | /// 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] | 4909 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD, |
| 4910 | bool NonInheritable, bool Inheritable) { |
John McCall | d4aff0e | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 4911 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 4912 | // have to do this. |
Douglas Gregor | 31e37b2 | 2011-07-28 18:09:57 +0000 | [diff] [blame] | 4913 | if (Inheritable) { |
| 4914 | LoadExternalWeakUndeclaredIdentifiers(); |
| 4915 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 4916 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
| 4917 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 4918 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 4919 | = WeakUndeclaredIdentifiers.find(Id); |
| 4920 | if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) { |
| 4921 | WeakInfo W = I->second; |
| 4922 | DeclApplyPragmaWeak(S, ND, W); |
| 4923 | WeakUndeclaredIdentifiers[Id] = W; |
| 4924 | } |
John McCall | d4aff0e | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 4925 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4926 | } |
| 4927 | } |
| 4928 | } |
| 4929 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4930 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4931 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4932 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4933 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4934 | // Walk the declarator structure, applying decl attributes that were in a type |
| 4935 | // position to the decl itself. This handles cases like: |
| 4936 | // int *__attr__(x)** D; |
| 4937 | // when X is a decl attribute. |
| 4938 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 4939 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4940 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable, |
| 4941 | /*IncludeCXX11Attributes=*/false); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4942 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4943 | // Finally, apply any attributes on the decl itself. |
| 4944 | if (const AttributeList *Attrs = PD.getAttributes()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4945 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4946 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4947 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4948 | /// Is the given declaration allowed to use a forbidden type? |
| 4949 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 4950 | // Private ivars are always okay. Unfortunately, people don't |
| 4951 | // always properly make their ivars private, even in system headers. |
| 4952 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | a6b3380 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 4953 | // Function declarations in sys headers will be marked unavailable. |
| 4954 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 4955 | !isa<FunctionDecl>(decl)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4956 | return false; |
| 4957 | |
| 4958 | // Require it to be declared in a system header. |
| 4959 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 4960 | } |
| 4961 | |
| 4962 | /// Handle a delayed forbidden-type diagnostic. |
| 4963 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 4964 | Decl *decl) { |
| 4965 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 4966 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 4967 | "this system declaration uses an unsupported type")); |
| 4968 | return; |
| 4969 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4970 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4971 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 4972 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4973 | // kind of forbidden type messages on unavailable functions. |
| 4974 | if (FD->hasAttr<UnavailableAttr>() && |
| 4975 | diag.getForbiddenTypeDiagnostic() == |
| 4976 | diag::err_arc_array_param_no_ownership) { |
| 4977 | diag.Triggered = true; |
| 4978 | return; |
| 4979 | } |
| 4980 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4981 | |
| 4982 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 4983 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 4984 | diag.Triggered = true; |
| 4985 | } |
| 4986 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4987 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 4988 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4989 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4990 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4991 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4992 | // When delaying diagnostics to run in the context of a parsed |
| 4993 | // declaration, we only want to actually emit anything if parsing |
| 4994 | // succeeds. |
| 4995 | if (!decl) return; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4996 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4997 | // We emit all the active diagnostics in this pool or any of its |
| 4998 | // parents. In general, we'll get one pool for the decl spec |
| 4999 | // and a child pool for each declarator; in a decl group like: |
| 5000 | // deprecated_typedef foo, *bar, baz(); |
| 5001 | // only the declarator pops will be passed decls. This is correct; |
| 5002 | // we really do need to consider delayed diagnostics from the decl spec |
| 5003 | // for each of the different declarations. |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5004 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5005 | do { |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5006 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5007 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 5008 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 5009 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5010 | if (diag.Triggered) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5011 | continue; |
| 5012 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5013 | switch (diag.Kind) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5014 | case DelayedDiagnostic::Deprecation: |
John McCall | e8c904f | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 5015 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 5016 | if (!decl->isInvalidDecl()) |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5017 | HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5018 | break; |
| 5019 | |
| 5020 | case DelayedDiagnostic::Access: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5021 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5022 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5023 | |
| 5024 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5025 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5026 | break; |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5027 | } |
| 5028 | } |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5029 | } while ((pool = pool->getParent())); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5030 | } |
| 5031 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5032 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 5033 | /// been delayed in the current context instead of in the given pool. |
| 5034 | /// Essentially, this just moves them to the current pool. |
| 5035 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 5036 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 5037 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 5038 | curPool->steal(pool); |
| 5039 | } |
| 5040 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5041 | static bool isDeclDeprecated(Decl *D) { |
| 5042 | do { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5043 | if (D->isDeprecated()) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5044 | return true; |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 5045 | // A category implicitly has the availability of the interface. |
| 5046 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 5047 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5048 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 5049 | return false; |
| 5050 | } |
| 5051 | |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5052 | static void |
| 5053 | DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message, |
| 5054 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5055 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5056 | const ObjCPropertyDecl *ObjCPropery) { |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5057 | DeclarationName Name = D->getDeclName(); |
| 5058 | if (!Message.empty()) { |
| 5059 | S.Diag(Loc, diag::warn_deprecated_message) << Name << Message; |
| 5060 | S.Diag(D->getLocation(), |
| 5061 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5062 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5063 | if (ObjCPropery) |
| 5064 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5065 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5066 | } else if (!UnknownObjCClass) { |
| 5067 | S.Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 5068 | S.Diag(D->getLocation(), |
| 5069 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5070 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5071 | if (ObjCPropery) |
| 5072 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5073 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5074 | } else { |
| 5075 | S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name; |
| 5076 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 5077 | } |
| 5078 | } |
| 5079 | |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 5080 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5081 | Decl *Ctx) { |
| 5082 | if (isDeclDeprecated(Ctx)) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5083 | return; |
| 5084 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5085 | DD.Triggered = true; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5086 | DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(), |
| 5087 | DD.getDeprecationMessage(), DD.Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5088 | DD.getUnknownObjCClass(), |
| 5089 | DD.getObjCProperty()); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5090 | } |
| 5091 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5092 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 5093 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5094 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5095 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5096 | // Delay if we're currently parsing a declaration. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5097 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5098 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 5099 | UnknownObjCClass, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5100 | ObjCProperty, |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5101 | Message)); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5102 | return; |
| 5103 | } |
| 5104 | |
| 5105 | // Otherwise, don't warn if our current context is deprecated. |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 5106 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5107 | return; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5108 | DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5109 | } |