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 | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 508 | D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context)); |
| 509 | } |
| 510 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 511 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 512 | const AttributeList &Attr) { |
| 513 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 514 | return; |
| 515 | |
| 516 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 517 | return; |
| 518 | |
| 519 | D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context)); |
| 520 | } |
| 521 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 522 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 523 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 524 | Expr* &Arg) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 525 | assert(!Attr.isInvalid()); |
| 526 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 527 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 528 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 529 | |
| 530 | // D must be either a member field or global (potentially shared) variable. |
| 531 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 532 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 533 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 534 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 535 | } |
| 536 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 537 | SmallVector<Expr*, 1> Args; |
| 538 | // check that all arguments are lockable objects |
| 539 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 540 | unsigned Size = Args.size(); |
| 541 | if (Size != 1) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 542 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 543 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 544 | Arg = Args[0]; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 545 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 546 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 549 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 550 | Expr *Arg = 0; |
| 551 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 552 | return; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 553 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 554 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| 555 | } |
| 556 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 557 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 558 | const AttributeList &Attr) { |
| 559 | Expr *Arg = 0; |
| 560 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 561 | return; |
| 562 | |
| 563 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 564 | return; |
| 565 | |
| 566 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| 567 | S.Context, Arg)); |
| 568 | } |
| 569 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 570 | static bool checkLockableAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 571 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 572 | assert(!Attr.isInvalid()); |
| 573 | |
| 574 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 575 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 576 | |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 577 | // FIXME: Lockable structs for C code. |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 578 | if (!isa<CXXRecordDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 579 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 580 | << Attr.getName() << ThreadExpectedClassOrStruct; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 581 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 584 | return true; |
| 585 | } |
| 586 | |
| 587 | static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 588 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 589 | return; |
| 590 | |
| 591 | D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); |
| 592 | } |
| 593 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 594 | static void handleScopedLockableAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 595 | const AttributeList &Attr) { |
| 596 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 597 | return; |
| 598 | |
| 599 | D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context)); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | static void handleNoThreadSafetyAttr(Sema &S, Decl *D, |
| 603 | const AttributeList &Attr) { |
| 604 | assert(!Attr.isInvalid()); |
| 605 | |
| 606 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 607 | return; |
| 608 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 609 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 610 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 611 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 612 | return; |
| 613 | } |
| 614 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 615 | D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(), |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 616 | S.Context)); |
| 617 | } |
| 618 | |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 619 | static void handleNoAddressSafetyAttr(Sema &S, Decl *D, |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 620 | const AttributeList &Attr) { |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 621 | assert(!Attr.isInvalid()); |
| 622 | |
| 623 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 624 | return; |
| 625 | |
| 626 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 627 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 628 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 629 | return; |
| 630 | } |
| 631 | |
| 632 | D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(), |
Nick Lewycky | f50b6fe | 2012-07-24 01:37:23 +0000 | [diff] [blame] | 633 | S.Context)); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 636 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 637 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 638 | SmallVector<Expr*, 1> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 639 | assert(!Attr.isInvalid()); |
| 640 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 641 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 642 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 643 | |
| 644 | // D must be either a member field or global (potentially shared) variable. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 645 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 646 | if (!VD || !mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 647 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 648 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
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 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 652 | // Check that this attribute only applies to lockable types. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 653 | QualType QT = VD->getType(); |
| 654 | if (!QT->isDependentType()) { |
| 655 | const RecordType *RT = getRecordType(QT); |
| 656 | if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 657 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 658 | << Attr.getName(); |
| 659 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 663 | // Check that all arguments are lockable objects. |
| 664 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 665 | if (Args.size() == 0) |
| 666 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 667 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 668 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 671 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 672 | const AttributeList &Attr) { |
| 673 | SmallVector<Expr*, 1> Args; |
| 674 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 675 | return; |
| 676 | |
| 677 | Expr **StartArg = &Args[0]; |
| 678 | D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context, |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 679 | StartArg, Args.size())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 682 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 683 | const AttributeList &Attr) { |
| 684 | SmallVector<Expr*, 1> Args; |
| 685 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 686 | return; |
| 687 | |
| 688 | Expr **StartArg = &Args[0]; |
| 689 | D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context, |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 690 | StartArg, Args.size())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 693 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 694 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 695 | SmallVector<Expr*, 1> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 696 | assert(!Attr.isInvalid()); |
| 697 | |
| 698 | // zero or more arguments ok |
| 699 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 700 | // check that the attribute is applied to a function |
| 701 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 702 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 703 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 704 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 707 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 708 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 709 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 710 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 713 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 714 | const AttributeList &Attr) { |
| 715 | SmallVector<Expr*, 1> Args; |
| 716 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 717 | return; |
| 718 | |
| 719 | unsigned Size = Args.size(); |
| 720 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 721 | D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(), |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 722 | S.Context, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 723 | StartArg, Size)); |
| 724 | } |
| 725 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 726 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 727 | const AttributeList &Attr) { |
| 728 | SmallVector<Expr*, 1> Args; |
| 729 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 730 | return; |
| 731 | |
| 732 | unsigned Size = Args.size(); |
| 733 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 734 | D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(), |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 735 | S.Context, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 736 | StartArg, Size)); |
| 737 | } |
| 738 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 739 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 740 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 741 | SmallVector<Expr*, 2> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 742 | assert(!Attr.isInvalid()); |
| 743 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 744 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 745 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 746 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 747 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 748 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 749 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 750 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 753 | if (!isIntOrBool(Attr.getArg(0))) { |
| 754 | 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] | 755 | << Attr.getName(); |
| 756 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 760 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 761 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 762 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 765 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 766 | const AttributeList &Attr) { |
| 767 | SmallVector<Expr*, 2> Args; |
| 768 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 769 | return; |
| 770 | |
| 771 | unsigned Size = Args.size(); |
| 772 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 773 | D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(), |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 774 | S.Context, |
| 775 | Attr.getArg(0), |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 776 | StartArg, Size)); |
| 777 | } |
| 778 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 779 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 780 | const AttributeList &Attr) { |
| 781 | SmallVector<Expr*, 2> Args; |
| 782 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 783 | return; |
| 784 | |
| 785 | unsigned Size = Args.size(); |
| 786 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 787 | D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(), |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 788 | S.Context, |
| 789 | Attr.getArg(0), |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 790 | StartArg, Size)); |
| 791 | } |
| 792 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 793 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 794 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 795 | SmallVector<Expr*, 1> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 796 | assert(!Attr.isInvalid()); |
| 797 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 798 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 799 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 800 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 801 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 802 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 803 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 804 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 807 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 808 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 809 | if (Args.size() == 0) |
| 810 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 811 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 812 | return true; |
| 813 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 814 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 815 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 816 | const AttributeList &Attr) { |
| 817 | SmallVector<Expr*, 1> Args; |
| 818 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 819 | return; |
| 820 | |
| 821 | Expr **StartArg = &Args[0]; |
| 822 | D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(), |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 823 | S.Context, |
| 824 | StartArg, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 825 | Args.size())); |
| 826 | } |
| 827 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 828 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 829 | const AttributeList &Attr) { |
| 830 | SmallVector<Expr*, 1> Args; |
| 831 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 832 | return; |
| 833 | |
| 834 | Expr **StartArg = &Args[0]; |
| 835 | D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(), |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 836 | S.Context, |
| 837 | StartArg, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 838 | Args.size())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 841 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 842 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 843 | assert(!Attr.isInvalid()); |
| 844 | |
| 845 | // zero or more arguments ok |
| 846 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 847 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 848 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 849 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 850 | return; |
| 851 | } |
| 852 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 853 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 854 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 855 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 856 | unsigned Size = Args.size(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 857 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 858 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 859 | D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 860 | StartArg, Size)); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 864 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 865 | assert(!Attr.isInvalid()); |
| 866 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 867 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 868 | return; |
| 869 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 870 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 871 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 872 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 873 | return; |
| 874 | } |
| 875 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 876 | // check that the argument is lockable object |
DeLesley Hutchins | f26efd7 | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 877 | SmallVector<Expr*, 1> Args; |
| 878 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 879 | unsigned Size = Args.size(); |
| 880 | if (Size == 0) |
| 881 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 882 | |
DeLesley Hutchins | f26efd7 | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 883 | D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context, |
| 884 | Args[0])); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 888 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 889 | assert(!Attr.isInvalid()); |
| 890 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 891 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 892 | return; |
| 893 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 894 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 895 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 896 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 897 | return; |
| 898 | } |
| 899 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 900 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 901 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 902 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 903 | unsigned Size = Args.size(); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 904 | if (Size == 0) |
| 905 | return; |
| 906 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 907 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 908 | D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 909 | StartArg, Size)); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 913 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 914 | const AttributeList &Attr) { |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 915 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 916 | if (TD == 0) { |
| 917 | // __attribute__((ext_vector_type(N))) can only be applied to typedefs |
| 918 | // and type-ids. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 919 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 920 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 921 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 922 | |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 923 | // Remember this typedef decl, we will need it later for diagnostics. |
| 924 | S.ExtVectorDecls.push_back(TD); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 927 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 928 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 929 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 930 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 931 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 932 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 933 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 934 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 935 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 936 | // has no effect. |
Eli Friedman | b68ec6b | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 937 | if (!FD->getType()->isDependentType() && |
| 938 | !FD->getType()->isIncompleteType() && |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 939 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 940 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 941 | << Attr.getName() << FD->getType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 942 | else |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 943 | FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 944 | } else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 945 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 948 | static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 5f608ae | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 949 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
| 950 | RD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context)); |
Fariborz Jahanian | c1a0a73 | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 951 | else |
| 952 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 953 | } |
| 954 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 955 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 956 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 957 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 958 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 959 | |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 960 | // The IBAction attributes only apply to instance methods. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 961 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 962 | if (MD->isInstanceMethod()) { |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 963 | D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context)); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 964 | return; |
| 965 | } |
| 966 | |
Ted Kremenek | 4ee2bb1 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 967 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 970 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 971 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 972 | // variables or properties of Objective-C classes. The outlet must also |
| 973 | // have an object reference type. |
| 974 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 975 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 0bfaf06 | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 976 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 977 | << Attr.getName() << VD->getType() << 0; |
| 978 | return false; |
| 979 | } |
| 980 | } |
| 981 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 982 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 983 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 984 | << Attr.getName() << PD->getType() << 1; |
| 985 | return false; |
| 986 | } |
| 987 | } |
| 988 | else { |
| 989 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 990 | return false; |
| 991 | } |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 992 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 993 | return true; |
| 994 | } |
| 995 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 996 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 997 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 998 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 999 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1000 | |
| 1001 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1002 | return; |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1003 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1004 | D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context)); |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1007 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1008 | const AttributeList &Attr) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1009 | |
| 1010 | // The iboutletcollection attribute can have zero or one arguments. |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1011 | if (Attr.getParameterName() && Attr.getNumArgs() > 0) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1012 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1013 | return; |
| 1014 | } |
| 1015 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1016 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1017 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1018 | |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1019 | IdentifierInfo *II = Attr.getParameterName(); |
| 1020 | if (!II) |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1021 | II = &S.Context.Idents.get("NSObject"); |
Fariborz Jahanian | 3a3400b | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 1022 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1023 | ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1024 | S.getScopeForContext(D->getDeclContext()->getParent())); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1025 | if (!TypeRep) { |
| 1026 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1027 | return; |
| 1028 | } |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1029 | QualType QT = TypeRep.get(); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1030 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1031 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1032 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1033 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1034 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1035 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1036 | return; |
| 1037 | } |
Argyrios Kyrtzidis | f1e7af3 | 2011-09-13 18:41:59 +0000 | [diff] [blame] | 1038 | D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context, |
| 1039 | QT, Attr.getParameterLoc())); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1042 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1043 | if (const RecordType *UT = T->getAsUnionType()) |
| 1044 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1045 | RecordDecl *UD = UT->getDecl(); |
| 1046 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1047 | itend = UD->field_end(); it != itend; ++it) { |
| 1048 | QualType QT = it->getType(); |
| 1049 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1050 | T = QT; |
| 1051 | return; |
| 1052 | } |
| 1053 | } |
| 1054 | } |
| 1055 | } |
| 1056 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1057 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nuno Lopes | 174930d | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1058 | if (!isFunctionOrMethod(D)) { |
| 1059 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1060 | << "alloc_size" << ExpectedFunctionOrMethod; |
| 1061 | return; |
| 1062 | } |
| 1063 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1064 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1065 | return; |
| 1066 | |
| 1067 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1068 | // counted from one. |
| 1069 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1070 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| 1071 | |
| 1072 | SmallVector<unsigned, 8> SizeArgs; |
| 1073 | |
| 1074 | for (AttributeList::arg_iterator I = Attr.arg_begin(), |
| 1075 | E = Attr.arg_end(); I!=E; ++I) { |
| 1076 | // The argument must be an integer constant expression. |
| 1077 | Expr *Ex = *I; |
| 1078 | llvm::APSInt ArgNum; |
| 1079 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 1080 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1081 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1082 | << "alloc_size" << Ex->getSourceRange(); |
| 1083 | return; |
| 1084 | } |
| 1085 | |
| 1086 | uint64_t x = ArgNum.getZExtValue(); |
| 1087 | |
| 1088 | if (x < 1 || x > NumArgs) { |
| 1089 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1090 | << "alloc_size" << I.getArgNum() << Ex->getSourceRange(); |
| 1091 | return; |
| 1092 | } |
| 1093 | |
| 1094 | --x; |
| 1095 | if (HasImplicitThisParam) { |
| 1096 | if (x == 0) { |
| 1097 | S.Diag(Attr.getLoc(), |
| 1098 | diag::err_attribute_invalid_implicit_this_argument) |
| 1099 | << "alloc_size" << Ex->getSourceRange(); |
| 1100 | return; |
| 1101 | } |
| 1102 | --x; |
| 1103 | } |
| 1104 | |
| 1105 | // check if the function argument is of an integer type |
| 1106 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
| 1107 | if (!T->isIntegerType()) { |
| 1108 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1109 | << "alloc_size" << Ex->getSourceRange(); |
| 1110 | return; |
| 1111 | } |
| 1112 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1113 | SizeArgs.push_back(x); |
| 1114 | } |
| 1115 | |
| 1116 | // check if the function returns a pointer |
| 1117 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 1118 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
| 1119 | << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
| 1120 | } |
| 1121 | |
Nuno Lopes | 96c67d1 | 2012-06-18 16:27:56 +0000 | [diff] [blame] | 1122 | D->addAttr(::new (S.Context) AllocSizeAttr(Attr.getRange(), S.Context, |
| 1123 | SizeArgs.data(), SizeArgs.size())); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1126 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1127 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1128 | // ignore it as well |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1129 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1130 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1131 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1132 | return; |
| 1133 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1134 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1135 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1136 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1137 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1138 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1139 | |
| 1140 | // The nonnull attribute only applies to pointers. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1141 | SmallVector<unsigned, 10> NonNullArgs; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1142 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1143 | for (AttributeList::arg_iterator I=Attr.arg_begin(), |
| 1144 | E=Attr.arg_end(); I!=E; ++I) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1145 | |
| 1146 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1147 | // The argument must be an integer constant expression. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1148 | Expr *Ex = *I; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1149 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1150 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 1151 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1152 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1153 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1154 | return; |
| 1155 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1156 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1157 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1158 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1159 | if (x < 1 || x > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1160 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 1161 | << "nonnull" << I.getArgNum() << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1162 | return; |
| 1163 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1164 | |
Ted Kremenek | 465172f | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 1165 | --x; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1166 | if (HasImplicitThisParam) { |
| 1167 | if (x == 0) { |
| 1168 | S.Diag(Attr.getLoc(), |
| 1169 | diag::err_attribute_invalid_implicit_this_argument) |
| 1170 | << "nonnull" << Ex->getSourceRange(); |
| 1171 | return; |
| 1172 | } |
| 1173 | --x; |
| 1174 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1175 | |
| 1176 | // Is the function argument a pointer type? |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1177 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1178 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1179 | |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1180 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1181 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | c9ef405 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1182 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1183 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1184 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1185 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1186 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1187 | NonNullArgs.push_back(x); |
| 1188 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1189 | |
| 1190 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1191 | // arguments have a nonnull attribute. |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1192 | if (NonNullArgs.empty()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1193 | for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) { |
| 1194 | QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1195 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1196 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 1197 | NonNullArgs.push_back(I); |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1198 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1199 | |
Ted Kremenek | ee1c08c | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1200 | // No pointer arguments? |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1201 | if (NonNullArgs.empty()) { |
| 1202 | // Warn the trivial case only if attribute is not coming from a |
| 1203 | // macro instantiation. |
| 1204 | if (Attr.getLoc().isFileID()) |
| 1205 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1206 | return; |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1207 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1208 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1209 | |
| 1210 | unsigned* start = &NonNullArgs[0]; |
| 1211 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1212 | llvm::array_pod_sort(start, start + size); |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1213 | D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start, |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1214 | size)); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1217 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1218 | // This attribute must be applied to a function declaration. |
| 1219 | // The first argument to the attribute must be a string, |
| 1220 | // the name of the resource, for example "malloc". |
| 1221 | // The following arguments must be argument indexes, the arguments must be |
| 1222 | // of integer type for Returns, otherwise of pointer type. |
| 1223 | // 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] | 1224 | // after being held. free() should be __attribute((ownership_takes)), whereas |
| 1225 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1226 | |
| 1227 | if (!AL.getParameterName()) { |
| 1228 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1229 | << AL.getName()->getName() << 1; |
| 1230 | return; |
| 1231 | } |
| 1232 | // Figure out our Kind, and check arguments while we're at it. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1233 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1234 | switch (AL.getKind()) { |
| 1235 | case AttributeList::AT_ownership_takes: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1236 | K = OwnershipAttr::Takes; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1237 | if (AL.getNumArgs() < 1) { |
| 1238 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1239 | return; |
| 1240 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1241 | break; |
| 1242 | case AttributeList::AT_ownership_holds: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1243 | K = OwnershipAttr::Holds; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1244 | if (AL.getNumArgs() < 1) { |
| 1245 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1246 | return; |
| 1247 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1248 | break; |
| 1249 | case AttributeList::AT_ownership_returns: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1250 | K = OwnershipAttr::Returns; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1251 | if (AL.getNumArgs() > 1) { |
| 1252 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1253 | << AL.getNumArgs() + 1; |
| 1254 | return; |
| 1255 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1256 | break; |
| 1257 | default: |
| 1258 | // This should never happen given how we are called. |
| 1259 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1262 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1263 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1264 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1265 | return; |
| 1266 | } |
| 1267 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1268 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1269 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1270 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1271 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1272 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1273 | StringRef Module = AL.getParameterName()->getName(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1274 | |
| 1275 | // Normalize the argument, __foo__ becomes foo. |
| 1276 | if (Module.startswith("__") && Module.endswith("__")) |
| 1277 | Module = Module.substr(2, Module.size() - 4); |
| 1278 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1279 | SmallVector<unsigned, 10> OwnershipArgs; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1280 | |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1281 | for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E; |
| 1282 | ++I) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1283 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1284 | Expr *IdxExpr = *I; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1285 | llvm::APSInt ArgNum(32); |
| 1286 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1287 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1288 | S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int) |
| 1289 | << AL.getName()->getName() << IdxExpr->getSourceRange(); |
| 1290 | continue; |
| 1291 | } |
| 1292 | |
| 1293 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
| 1294 | |
| 1295 | if (x > NumArgs || x < 1) { |
| 1296 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1297 | << AL.getName()->getName() << x << IdxExpr->getSourceRange(); |
| 1298 | continue; |
| 1299 | } |
| 1300 | --x; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1301 | if (HasImplicitThisParam) { |
| 1302 | if (x == 0) { |
| 1303 | S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 1304 | << "ownership" << IdxExpr->getSourceRange(); |
| 1305 | return; |
| 1306 | } |
| 1307 | --x; |
| 1308 | } |
| 1309 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1310 | switch (K) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1311 | case OwnershipAttr::Takes: |
| 1312 | case OwnershipAttr::Holds: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1313 | // Is the function argument a pointer type? |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1314 | QualType T = getFunctionOrMethodArgType(D, x); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1315 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| 1316 | // FIXME: Should also highlight argument in decl. |
| 1317 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1318 | << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1319 | << "pointer" |
| 1320 | << IdxExpr->getSourceRange(); |
| 1321 | continue; |
| 1322 | } |
| 1323 | break; |
| 1324 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1325 | case OwnershipAttr::Returns: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1326 | if (AL.getNumArgs() > 1) { |
| 1327 | // Is the function argument an integer type? |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1328 | Expr *IdxExpr = AL.getArg(0); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1329 | llvm::APSInt ArgNum(32); |
| 1330 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1331 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1332 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
| 1333 | << "ownership_returns" << "integer" |
| 1334 | << IdxExpr->getSourceRange(); |
| 1335 | return; |
| 1336 | } |
| 1337 | } |
| 1338 | break; |
| 1339 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1340 | } // switch |
| 1341 | |
| 1342 | // Check we don't have a conflict with another ownership attribute. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1343 | for (specific_attr_iterator<OwnershipAttr> |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1344 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1345 | e = D->specific_attr_end<OwnershipAttr>(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1346 | i != e; ++i) { |
| 1347 | if ((*i)->getOwnKind() != K) { |
| 1348 | for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); |
| 1349 | I!=E; ++I) { |
| 1350 | if (x == *I) { |
| 1351 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1352 | << AL.getName()->getName() << "ownership_*"; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1353 | } |
| 1354 | } |
| 1355 | } |
| 1356 | } |
| 1357 | OwnershipArgs.push_back(x); |
| 1358 | } |
| 1359 | |
| 1360 | unsigned* start = OwnershipArgs.data(); |
| 1361 | unsigned size = OwnershipArgs.size(); |
| 1362 | llvm::array_pod_sort(start, start + size); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1363 | |
| 1364 | if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { |
| 1365 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1366 | return; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1367 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1368 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1369 | D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module, |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1370 | start, size)); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1373 | /// Whether this declaration has internal linkage for the purposes of |
| 1374 | /// things that want to complain about things not have internal linkage. |
| 1375 | static bool hasEffectivelyInternalLinkage(NamedDecl *D) { |
| 1376 | switch (D->getLinkage()) { |
| 1377 | case NoLinkage: |
| 1378 | case InternalLinkage: |
| 1379 | return true; |
| 1380 | |
| 1381 | // Template instantiations that go from external to unique-external |
| 1382 | // shouldn't get diagnosed. |
| 1383 | case UniqueExternalLinkage: |
| 1384 | return true; |
| 1385 | |
| 1386 | case ExternalLinkage: |
| 1387 | return false; |
| 1388 | } |
| 1389 | llvm_unreachable("unknown linkage kind!"); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1392 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1393 | // Check the attribute arguments. |
| 1394 | if (Attr.getNumArgs() > 1) { |
| 1395 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1396 | return; |
| 1397 | } |
| 1398 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1399 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1400 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1401 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1402 | return; |
| 1403 | } |
| 1404 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1405 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1406 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1407 | // gcc rejects |
| 1408 | // class c { |
| 1409 | // static int a __attribute__((weakref ("v2"))); |
| 1410 | // static int b() __attribute__((weakref ("f3"))); |
| 1411 | // }; |
| 1412 | // and ignores the attributes of |
| 1413 | // void f(void) { |
| 1414 | // static int a __attribute__((weakref ("v2"))); |
| 1415 | // } |
| 1416 | // we reject them |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1417 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1418 | if (!Ctx->isFileContext()) { |
| 1419 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1420 | nd->getNameAsString(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1421 | return; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | // The GCC manual says |
| 1425 | // |
| 1426 | // At present, a declaration to which `weakref' is attached can only |
| 1427 | // be `static'. |
| 1428 | // |
| 1429 | // It also says |
| 1430 | // |
| 1431 | // Without a TARGET, |
| 1432 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1433 | // equivalent to `weak'. |
| 1434 | // |
| 1435 | // gcc 4.4.1 will accept |
| 1436 | // int a7 __attribute__((weakref)); |
| 1437 | // as |
| 1438 | // int a7 __attribute__((weak)); |
| 1439 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1440 | // it if this behaviour is actually used. |
| 1441 | |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1442 | if (!hasEffectivelyInternalLinkage(nd)) { |
| 1443 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1444 | return; |
| 1445 | } |
| 1446 | |
| 1447 | // GCC rejects |
| 1448 | // static ((alias ("y"), weakref)). |
| 1449 | // Should we? How to check that weakref is before or after alias? |
| 1450 | |
| 1451 | if (Attr.getNumArgs() == 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1452 | Expr *Arg = Attr.getArg(0); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1453 | Arg = Arg->IgnoreParenCasts(); |
| 1454 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1455 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1456 | if (!Str || !Str->isAscii()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1457 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1458 | << "weakref" << 1; |
| 1459 | return; |
| 1460 | } |
| 1461 | // GCC will accept anything as the argument of weakref. Should we |
| 1462 | // check for an existing decl? |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1463 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1464 | Str->getString())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1467 | D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context)); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1470 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1471 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1472 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1473 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1474 | return; |
| 1475 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1476 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1477 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1478 | Arg = Arg->IgnoreParenCasts(); |
| 1479 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1480 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1481 | if (!Str || !Str->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1482 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1483 | << "alias" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1484 | return; |
| 1485 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1486 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1487 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | f5fe292 | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1488 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1489 | return; |
| 1490 | } |
| 1491 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1492 | // FIXME: check if target symbol exists in current file |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1493 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1494 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1495 | Str->getString())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1498 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1499 | // Check the attribute arguments. |
| 1500 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1501 | return; |
| 1502 | |
| 1503 | if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) { |
| 1504 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1505 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1506 | return; |
| 1507 | } |
| 1508 | |
| 1509 | D->addAttr(::new (S.Context) MinSizeAttr(Attr.getRange(), S.Context)); |
| 1510 | } |
| 1511 | |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1512 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1513 | // Check the attribute arguments. |
| 1514 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1515 | return; |
| 1516 | |
| 1517 | if (!isa<FunctionDecl>(D)) { |
| 1518 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1519 | << Attr.getName() << ExpectedFunction; |
| 1520 | return; |
| 1521 | } |
| 1522 | |
| 1523 | if (D->hasAttr<HotAttr>()) { |
| 1524 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1525 | << Attr.getName() << "hot"; |
| 1526 | return; |
| 1527 | } |
| 1528 | |
| 1529 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context)); |
| 1530 | } |
| 1531 | |
| 1532 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1533 | // Check the attribute arguments. |
| 1534 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1535 | return; |
| 1536 | |
| 1537 | if (!isa<FunctionDecl>(D)) { |
| 1538 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1539 | << Attr.getName() << ExpectedFunction; |
| 1540 | return; |
| 1541 | } |
| 1542 | |
| 1543 | if (D->hasAttr<ColdAttr>()) { |
| 1544 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1545 | << Attr.getName() << "cold"; |
| 1546 | return; |
| 1547 | } |
| 1548 | |
| 1549 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context)); |
| 1550 | } |
| 1551 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1552 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1553 | // Check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1554 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1555 | return; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1556 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1557 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1558 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1559 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1560 | return; |
| 1561 | } |
| 1562 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1563 | D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context)); |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1566 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 1567 | const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1568 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1569 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1570 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1571 | return; |
| 1572 | } |
| 1573 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1574 | if (!isa<FunctionDecl>(D)) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1575 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1576 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1577 | return; |
| 1578 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1579 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1580 | D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context)); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1583 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1584 | const AttributeList &Attr) { |
| 1585 | // Check the attribute arguments. |
| 1586 | if (Attr.getNumArgs() != 1) { |
| 1587 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1588 | return; |
| 1589 | } |
| 1590 | |
| 1591 | Expr *Arg = Attr.getArg(0); |
| 1592 | Arg = Arg->IgnoreParenCasts(); |
| 1593 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1594 | |
| 1595 | // Check that it is a string. |
| 1596 | if (!Str) { |
| 1597 | S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model"; |
| 1598 | return; |
| 1599 | } |
| 1600 | |
| 1601 | if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) { |
| 1602 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1603 | << Attr.getName() << ExpectedTLSVar; |
| 1604 | return; |
| 1605 | } |
| 1606 | |
| 1607 | // Check that the value. |
| 1608 | StringRef Model = Str->getString(); |
| 1609 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1610 | && Model != "initial-exec" && Model != "local-exec") { |
| 1611 | S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg); |
| 1612 | return; |
| 1613 | } |
| 1614 | |
| 1615 | D->addAttr(::new (S.Context) TLSModelAttr(Attr.getRange(), S.Context, |
| 1616 | Model)); |
| 1617 | } |
| 1618 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1619 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1620 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1621 | if (Attr.hasParameterOrArguments()) { |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1622 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1623 | return; |
| 1624 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1625 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1626 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1627 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1628 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1629 | D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context)); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1630 | return; |
| 1631 | } |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1634 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1637 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1638 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1639 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1640 | return; |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1641 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1642 | D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context)); |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1645 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1646 | assert(!Attr.isInvalid()); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1647 | if (isa<VarDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1648 | D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context)); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1649 | else |
| 1650 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1651 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1654 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1655 | assert(!Attr.isInvalid()); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1656 | if (isa<VarDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1657 | D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context)); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1658 | else |
| 1659 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1660 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1663 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1664 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1665 | |
| 1666 | if (S.CheckNoReturnAttr(attr)) return; |
| 1667 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1668 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1669 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1670 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1671 | return; |
| 1672 | } |
| 1673 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1674 | D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context)); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1678 | if (attr.hasParameterOrArguments()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1679 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1680 | attr.setInvalid(); |
| 1681 | return true; |
| 1682 | } |
| 1683 | |
| 1684 | return false; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1687 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1688 | const AttributeList &Attr) { |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1689 | |
| 1690 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1691 | // because 'analyzer_noreturn' does not impact the type. |
| 1692 | |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1693 | if(!checkAttributeNumArgs(S, Attr, 0)) |
| 1694 | return; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1695 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1696 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1697 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1698 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1699 | && !VD->getType()->isFunctionPointerType())) { |
| 1700 | S.Diag(Attr.getLoc(), |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1701 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1702 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1703 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1704 | return; |
| 1705 | } |
| 1706 | } |
| 1707 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1708 | D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1711 | // PS3 PPU-specific. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1712 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1713 | /* |
| 1714 | Returning a Vector Class in Registers |
| 1715 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1716 | According to the PPU ABI specifications, a class with a single member of |
| 1717 | vector type is returned in memory when used as the return value of a function. |
| 1718 | This results in inefficient code when implementing vector classes. To return |
| 1719 | the value in a single vector register, add the vecreturn attribute to the |
| 1720 | class definition. This attribute is also applicable to struct types. |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1721 | |
| 1722 | Example: |
| 1723 | |
| 1724 | struct Vector |
| 1725 | { |
| 1726 | __vector float xyzw; |
| 1727 | } __attribute__((vecreturn)); |
| 1728 | |
| 1729 | Vector Add(Vector lhs, Vector rhs) |
| 1730 | { |
| 1731 | Vector result; |
| 1732 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1733 | return result; // This will be returned in a register |
| 1734 | } |
| 1735 | */ |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1736 | if (!isa<RecordDecl>(D)) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1737 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1738 | << Attr.getName() << ExpectedClass; |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1739 | return; |
| 1740 | } |
| 1741 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1742 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1743 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1744 | return; |
| 1745 | } |
| 1746 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1747 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1748 | int count = 0; |
| 1749 | |
| 1750 | if (!isa<CXXRecordDecl>(record)) { |
| 1751 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1752 | return; |
| 1753 | } |
| 1754 | |
| 1755 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1756 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1757 | return; |
| 1758 | } |
| 1759 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1760 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1761 | iter != record->field_end(); iter++) { |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1762 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1763 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1764 | return; |
| 1765 | } |
| 1766 | count++; |
| 1767 | } |
| 1768 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1769 | D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context)); |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1772 | static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1773 | if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1774 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1775 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1776 | return; |
| 1777 | } |
| 1778 | // FIXME: Actually store the attribute on the declaration |
| 1779 | } |
| 1780 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1781 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1782 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1783 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1784 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1785 | return; |
| 1786 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1787 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1788 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 568eae4 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1789 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1790 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1791 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1792 | return; |
| 1793 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1794 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1795 | D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context)); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1798 | static void handleReturnsTwiceAttr(Sema &S, Decl *D, |
| 1799 | const AttributeList &Attr) { |
| 1800 | // check the attribute arguments. |
| 1801 | if (Attr.hasParameterOrArguments()) { |
| 1802 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1803 | return; |
| 1804 | } |
| 1805 | |
| 1806 | if (!isa<FunctionDecl>(D)) { |
| 1807 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1808 | << Attr.getName() << ExpectedFunction; |
| 1809 | return; |
| 1810 | } |
| 1811 | |
| 1812 | D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context)); |
| 1813 | } |
| 1814 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1815 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1816 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1817 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1818 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1819 | return; |
| 1820 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1821 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1822 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Daniel Dunbar | 186204b | 2009-02-13 22:48:56 +0000 | [diff] [blame] | 1823 | if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1824 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 1825 | return; |
| 1826 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1827 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1828 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1829 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1830 | return; |
| 1831 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1832 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1833 | D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context)); |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1836 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1837 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1838 | if (Attr.getNumArgs() > 1) { |
| 1839 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1840 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1841 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1842 | |
| 1843 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1844 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1845 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1846 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1847 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1848 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1849 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1850 | << "constructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1851 | return; |
| 1852 | } |
| 1853 | priority = Idx.getZExtValue(); |
| 1854 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1855 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1856 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1857 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1858 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1859 | return; |
| 1860 | } |
| 1861 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1862 | D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1863 | priority)); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1866 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1867 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1868 | if (Attr.getNumArgs() > 1) { |
| 1869 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1870 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1871 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1872 | |
| 1873 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1874 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1875 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1876 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1877 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1878 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1879 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1880 | << "destructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1881 | return; |
| 1882 | } |
| 1883 | priority = Idx.getZExtValue(); |
| 1884 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1885 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1886 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1887 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1888 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1889 | return; |
| 1890 | } |
| 1891 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1892 | D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1893 | priority)); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1896 | template <typename AttrTy> |
| 1897 | static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr, |
| 1898 | const char *Name) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1899 | unsigned NumArgs = Attr.getNumArgs(); |
| 1900 | if (NumArgs > 1) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1901 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1902 | return; |
| 1903 | } |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1904 | |
| 1905 | // Handle the case where the attribute has a text message. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1906 | StringRef Str; |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1907 | if (NumArgs == 1) { |
| 1908 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1909 | if (!SE) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1910 | S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string) |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1911 | << Name; |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1912 | return; |
| 1913 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1914 | Str = SE->getString(); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1915 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1916 | |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1917 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str)); |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1920 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 1921 | const AttributeList &Attr) { |
| 1922 | unsigned NumArgs = Attr.getNumArgs(); |
| 1923 | if (NumArgs > 0) { |
| 1924 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 1925 | return; |
| 1926 | } |
| 1927 | |
| 1928 | D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr( |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1929 | Attr.getRange(), S.Context)); |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 1932 | static void handleObjCRootClassAttr(Sema &S, Decl *D, |
| 1933 | const AttributeList &Attr) { |
| 1934 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 1935 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 1936 | return; |
| 1937 | } |
| 1938 | |
| 1939 | unsigned NumArgs = Attr.getNumArgs(); |
| 1940 | if (NumArgs > 0) { |
| 1941 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 1942 | return; |
| 1943 | } |
| 1944 | |
| 1945 | D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context)); |
| 1946 | } |
| 1947 | |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1948 | static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 1949 | const AttributeList &Attr) { |
Fariborz Jahanian | 341b8be | 2012-01-03 22:52:32 +0000 | [diff] [blame] | 1950 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 1951 | S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis); |
| 1952 | return; |
| 1953 | } |
| 1954 | |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 1955 | unsigned NumArgs = Attr.getNumArgs(); |
| 1956 | if (NumArgs > 0) { |
| 1957 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 1958 | return; |
| 1959 | } |
| 1960 | |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1961 | D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr( |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 1962 | Attr.getRange(), S.Context)); |
| 1963 | } |
| 1964 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 1965 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 1966 | IdentifierInfo *Platform, |
| 1967 | VersionTuple Introduced, |
| 1968 | VersionTuple Deprecated, |
| 1969 | VersionTuple Obsoleted) { |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1970 | StringRef PlatformName |
| 1971 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1972 | if (PlatformName.empty()) |
| 1973 | PlatformName = Platform->getName(); |
| 1974 | |
| 1975 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 1976 | // of these steps are needed). |
| 1977 | if (!Introduced.empty() && !Deprecated.empty() && |
| 1978 | !(Introduced <= Deprecated)) { |
| 1979 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1980 | << 1 << PlatformName << Deprecated.getAsString() |
| 1981 | << 0 << Introduced.getAsString(); |
| 1982 | return true; |
| 1983 | } |
| 1984 | |
| 1985 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 1986 | !(Introduced <= Obsoleted)) { |
| 1987 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1988 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1989 | << 0 << Introduced.getAsString(); |
| 1990 | return true; |
| 1991 | } |
| 1992 | |
| 1993 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 1994 | !(Deprecated <= Obsoleted)) { |
| 1995 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1996 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1997 | << 1 << Deprecated.getAsString(); |
| 1998 | return true; |
| 1999 | } |
| 2000 | |
| 2001 | return false; |
| 2002 | } |
| 2003 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2004 | /// \brief Check whether the two versions match. |
| 2005 | /// |
| 2006 | /// If either version tuple is empty, then they are assumed to match. If |
| 2007 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2008 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2009 | bool BeforeIsOkay) { |
| 2010 | if (X.empty() || Y.empty()) |
| 2011 | return true; |
| 2012 | |
| 2013 | if (X == Y) |
| 2014 | return true; |
| 2015 | |
| 2016 | if (BeforeIsOkay && X < Y) |
| 2017 | return true; |
| 2018 | |
| 2019 | return false; |
| 2020 | } |
| 2021 | |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2022 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2023 | IdentifierInfo *Platform, |
| 2024 | VersionTuple Introduced, |
| 2025 | VersionTuple Deprecated, |
| 2026 | VersionTuple Obsoleted, |
| 2027 | bool IsUnavailable, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2028 | StringRef Message, |
| 2029 | bool Override) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2030 | VersionTuple MergedIntroduced = Introduced; |
| 2031 | VersionTuple MergedDeprecated = Deprecated; |
| 2032 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2033 | bool FoundAny = false; |
| 2034 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2035 | if (D->hasAttrs()) { |
| 2036 | AttrVec &Attrs = D->getAttrs(); |
| 2037 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 2038 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 2039 | if (!OldAA) { |
| 2040 | ++i; |
| 2041 | continue; |
| 2042 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2043 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2044 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2045 | if (OldPlatform != Platform) { |
| 2046 | ++i; |
| 2047 | continue; |
| 2048 | } |
| 2049 | |
| 2050 | FoundAny = true; |
| 2051 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2052 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2053 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2054 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2055 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2056 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 2057 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 2058 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 2059 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 72daa3f | 2013-01-16 00:54:48 +0000 | [diff] [blame^] | 2060 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2061 | if (Override) { |
| 2062 | int Which = -1; |
| 2063 | VersionTuple FirstVersion; |
| 2064 | VersionTuple SecondVersion; |
| 2065 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 2066 | Which = 0; |
| 2067 | FirstVersion = OldIntroduced; |
| 2068 | SecondVersion = Introduced; |
| 2069 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 2070 | Which = 1; |
| 2071 | FirstVersion = Deprecated; |
| 2072 | SecondVersion = OldDeprecated; |
| 2073 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 2074 | Which = 2; |
| 2075 | FirstVersion = Obsoleted; |
| 2076 | SecondVersion = OldObsoleted; |
| 2077 | } |
| 2078 | |
| 2079 | if (Which == -1) { |
| 2080 | Diag(OldAA->getLocation(), |
| 2081 | diag::warn_mismatched_availability_override_unavail) |
| 2082 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2083 | } else { |
| 2084 | Diag(OldAA->getLocation(), |
| 2085 | diag::warn_mismatched_availability_override) |
| 2086 | << Which |
| 2087 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2088 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 2089 | } |
| 2090 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2091 | } else { |
| 2092 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2093 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2094 | } |
| 2095 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2096 | Attrs.erase(Attrs.begin() + i); |
| 2097 | --e; |
| 2098 | continue; |
| 2099 | } |
| 2100 | |
| 2101 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2102 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2103 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2104 | |
| 2105 | if (MergedIntroduced2.empty()) |
| 2106 | MergedIntroduced2 = OldIntroduced; |
| 2107 | if (MergedDeprecated2.empty()) |
| 2108 | MergedDeprecated2 = OldDeprecated; |
| 2109 | if (MergedObsoleted2.empty()) |
| 2110 | MergedObsoleted2 = OldObsoleted; |
| 2111 | |
| 2112 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2113 | MergedIntroduced2, MergedDeprecated2, |
| 2114 | MergedObsoleted2)) { |
| 2115 | Attrs.erase(Attrs.begin() + i); |
| 2116 | --e; |
| 2117 | continue; |
| 2118 | } |
| 2119 | |
| 2120 | MergedIntroduced = MergedIntroduced2; |
| 2121 | MergedDeprecated = MergedDeprecated2; |
| 2122 | MergedObsoleted = MergedObsoleted2; |
| 2123 | ++i; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2124 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | if (FoundAny && |
| 2128 | MergedIntroduced == Introduced && |
| 2129 | MergedDeprecated == Deprecated && |
| 2130 | MergedObsoleted == Obsoleted) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2131 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2132 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2133 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2134 | MergedDeprecated, MergedObsoleted)) { |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2135 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 2136 | Introduced, Deprecated, |
| 2137 | Obsoleted, IsUnavailable, Message); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2138 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2139 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2142 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2143 | const AttributeList &Attr) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2144 | IdentifierInfo *Platform = Attr.getParameterName(); |
| 2145 | SourceLocation PlatformLoc = Attr.getParameterLoc(); |
| 2146 | |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2147 | if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2148 | S.Diag(PlatformLoc, diag::warn_availability_unknown_platform) |
| 2149 | << Platform; |
| 2150 | |
Rafael Espindola | 8c4222a | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2151 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 2152 | if (!ND) { |
| 2153 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2154 | return; |
| 2155 | } |
| 2156 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2157 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2158 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2159 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2160 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2161 | StringRef Str; |
| 2162 | const StringLiteral *SE = |
| 2163 | dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr()); |
| 2164 | if (SE) |
| 2165 | Str = SE->getString(); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2166 | |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2167 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2168 | Platform, |
| 2169 | Introduced.Version, |
| 2170 | Deprecated.Version, |
| 2171 | Obsoleted.Version, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2172 | IsUnavailable, Str, |
| 2173 | /*Override=*/false); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2174 | if (NewAttr) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2175 | D->addAttr(NewAttr); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2176 | } |
| 2177 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2178 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
| 2179 | VisibilityAttr::VisibilityType Vis) { |
Rafael Espindola | dd44f34 | 2012-05-10 03:01:34 +0000 | [diff] [blame] | 2180 | if (isa<TypedefNameDecl>(D)) { |
| 2181 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility"; |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2182 | return NULL; |
Rafael Espindola | dd44f34 | 2012-05-10 03:01:34 +0000 | [diff] [blame] | 2183 | } |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2184 | VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>(); |
| 2185 | if (ExistingAttr) { |
| 2186 | VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility(); |
| 2187 | if (ExistingVis == Vis) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2188 | return NULL; |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2189 | Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2190 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2191 | D->dropAttr<VisibilityAttr>(); |
| 2192 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2193 | return ::new (Context) VisibilityAttr(Range, Context, Vis); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2196 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2197 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2198 | if(!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2199 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2200 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2201 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2202 | Arg = Arg->IgnoreParenCasts(); |
| 2203 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2204 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2205 | if (!Str || !Str->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2206 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2207 | << "visibility" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2208 | return; |
| 2209 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2210 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2211 | StringRef TypeStr = Str->getString(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2212 | VisibilityAttr::VisibilityType type; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2213 | |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2214 | if (TypeStr == "default") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2215 | type = VisibilityAttr::Default; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2216 | else if (TypeStr == "hidden") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2217 | type = VisibilityAttr::Hidden; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2218 | else if (TypeStr == "internal") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2219 | type = VisibilityAttr::Hidden; // FIXME |
John McCall | 4188760 | 2012-01-29 01:20:30 +0000 | [diff] [blame] | 2220 | else if (TypeStr == "protected") { |
| 2221 | // Complain about attempts to use protected visibility on targets |
| 2222 | // (like Darwin) that don't support it. |
| 2223 | if (!S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2224 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2225 | type = VisibilityAttr::Default; |
| 2226 | } else { |
| 2227 | type = VisibilityAttr::Protected; |
| 2228 | } |
| 2229 | } else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2230 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2231 | return; |
| 2232 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2233 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2234 | VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2235 | if (NewAttr) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2236 | D->addAttr(NewAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2239 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2240 | const AttributeList &Attr) { |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2241 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 2242 | if (!method) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2243 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2244 | << ExpectedMethod; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2245 | return; |
| 2246 | } |
| 2247 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2248 | if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) { |
| 2249 | if (!Attr.getParameterName() && Attr.getNumArgs() == 1) { |
| 2250 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2251 | << "objc_method_family" << 1; |
| 2252 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2253 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2254 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2255 | Attr.setInvalid(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2256 | return; |
| 2257 | } |
| 2258 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2259 | StringRef param = Attr.getParameterName()->getName(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2260 | ObjCMethodFamilyAttr::FamilyKind family; |
| 2261 | if (param == "none") |
| 2262 | family = ObjCMethodFamilyAttr::OMF_None; |
| 2263 | else if (param == "alloc") |
| 2264 | family = ObjCMethodFamilyAttr::OMF_alloc; |
| 2265 | else if (param == "copy") |
| 2266 | family = ObjCMethodFamilyAttr::OMF_copy; |
| 2267 | else if (param == "init") |
| 2268 | family = ObjCMethodFamilyAttr::OMF_init; |
| 2269 | else if (param == "mutableCopy") |
| 2270 | family = ObjCMethodFamilyAttr::OMF_mutableCopy; |
| 2271 | else if (param == "new") |
| 2272 | family = ObjCMethodFamilyAttr::OMF_new; |
| 2273 | else { |
| 2274 | // Just warn and ignore it. This is future-proof against new |
| 2275 | // families being used in system headers. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2276 | S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2277 | return; |
| 2278 | } |
| 2279 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2280 | if (family == ObjCMethodFamilyAttr::OMF_init && |
| 2281 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2282 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2283 | << method->getResultType(); |
| 2284 | // Ignore the attribute. |
| 2285 | return; |
| 2286 | } |
| 2287 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2288 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2289 | S.Context, family)); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2292 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 2293 | const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2294 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2295 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2296 | |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2297 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 2298 | if (OCI == 0) { |
| 2299 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 2300 | return; |
| 2301 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2302 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2303 | D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2306 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2307 | if (Attr.getNumArgs() != 0) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 2308 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2309 | return; |
| 2310 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2311 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2312 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2313 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2314 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2315 | return; |
| 2316 | } |
| 2317 | } |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2318 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2319 | QualType T = PD->getType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2320 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2321 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2322 | return; |
| 2323 | } |
| 2324 | } |
| 2325 | else { |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2326 | // It is okay to include this attribute on properties, e.g.: |
| 2327 | // |
| 2328 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2329 | // |
| 2330 | // In this case it follows tradition and suppresses an error in the above |
| 2331 | // case. |
Fariborz Jahanian | 9b2eb7b | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2332 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2333 | } |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2334 | D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context)); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2337 | static void |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2338 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2339 | if (Attr.getNumArgs() != 0) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 2340 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2341 | return; |
| 2342 | } |
| 2343 | |
| 2344 | if (!isa<FunctionDecl>(D)) { |
| 2345 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 2346 | return; |
| 2347 | } |
| 2348 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2349 | D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context)); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2352 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2353 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2354 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2355 | << "blocks" << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2356 | return; |
| 2357 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2358 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2359 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2360 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2361 | return; |
| 2362 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2363 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2364 | BlocksAttr::BlockType type; |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 2365 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2366 | type = BlocksAttr::ByRef; |
| 2367 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2368 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2369 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2370 | return; |
| 2371 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2372 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2373 | D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type)); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2376 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2377 | // check the attribute arguments. |
| 2378 | if (Attr.getNumArgs() > 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2379 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2380 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2383 | unsigned sentinel = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2384 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2385 | Expr *E = Attr.getArg(0); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2386 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2387 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2388 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2389 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2390 | << "sentinel" << 1 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2391 | return; |
| 2392 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2393 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2394 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2395 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2396 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2397 | return; |
| 2398 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2399 | |
| 2400 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2403 | unsigned nullPos = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2404 | if (Attr.getNumArgs() > 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2405 | Expr *E = Attr.getArg(1); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2406 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2407 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2408 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2409 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2410 | << "sentinel" << 2 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2411 | return; |
| 2412 | } |
| 2413 | nullPos = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2414 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2415 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2416 | // FIXME: This error message could be improved, it would be nice |
| 2417 | // to say what the bounds actually are. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2418 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2419 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2420 | return; |
| 2421 | } |
| 2422 | } |
| 2423 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2424 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2425 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2426 | if (isa<FunctionNoProtoType>(FT)) { |
| 2427 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2428 | return; |
| 2429 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2430 | |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2431 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2432 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2433 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2434 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2435 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2436 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2437 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2438 | return; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2439 | } |
Eli Friedman | a0b2ba1 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2440 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2441 | if (!BD->isVariadic()) { |
| 2442 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2443 | return; |
| 2444 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2445 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2446 | QualType Ty = V->getType(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2447 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2448 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2449 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2450 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2451 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2452 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2453 | return; |
| 2454 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2455 | } else { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2456 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2457 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2458 | return; |
| 2459 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2460 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2461 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2462 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2463 | return; |
| 2464 | } |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2465 | D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2466 | nullPos)); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2469 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2470 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2471 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2472 | return; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2473 | |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 2474 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2475 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Kaelyn Uhrain | d449c79 | 2012-11-13 00:18:47 +0000 | [diff] [blame] | 2476 | << Attr.getName() << ExpectedFunctionMethodOrClass; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2477 | return; |
| 2478 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2479 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2480 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2481 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2482 | << Attr.getName() << 0; |
Nuno Lopes | f857798 | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2483 | return; |
| 2484 | } |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2485 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2486 | if (MD->getResultType()->isVoidType()) { |
| 2487 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2488 | << Attr.getName() << 1; |
| 2489 | return; |
| 2490 | } |
| 2491 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2492 | D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2495 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2496 | // check the attribute arguments. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2497 | if (Attr.hasParameterOrArguments()) { |
| 2498 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2499 | return; |
| 2500 | } |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2501 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2502 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
Fariborz Jahanian | 13c7fcc | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 2503 | if (isa<CXXRecordDecl>(D)) { |
| 2504 | D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| 2505 | return; |
| 2506 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2507 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2508 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | f23ecd9 | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 2509 | return; |
| 2510 | } |
| 2511 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2512 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 2513 | |
| 2514 | // 'weak' only applies to declarations with external linkage. |
| 2515 | if (hasEffectivelyInternalLinkage(nd)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2516 | S.Diag(Attr.getLoc(), diag::err_attribute_weak_static); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2517 | return; |
| 2518 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2519 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2520 | nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2521 | } |
| 2522 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2523 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2524 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2525 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2526 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2527 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2528 | |
| 2529 | // weak_import only applies to variable & function declarations. |
| 2530 | bool isDef = false; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2531 | if (!D->canBeWeakImported(isDef)) { |
| 2532 | if (isDef) |
| 2533 | S.Diag(Attr.getLoc(), |
| 2534 | diag::warn_attribute_weak_import_invalid_on_definition) |
| 2535 | << "weak_import" << 2 /*variable and function*/; |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2536 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2537 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 90eed21 | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2538 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2539 | // Nothing to warn about here. |
| 2540 | } else |
Fariborz Jahanian | c034974 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2541 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2542 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2543 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2544 | return; |
| 2545 | } |
| 2546 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2547 | D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context)); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2550 | // Handles reqd_work_group_size and work_group_size_hint. |
| 2551 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2552 | const AttributeList &Attr) { |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2553 | assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2554 | || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint); |
| 2555 | |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2556 | // Attribute has 3 arguments. |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2557 | if (!checkAttributeNumArgs(S, Attr, 3)) return; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2558 | |
| 2559 | unsigned WGSize[3]; |
| 2560 | for (unsigned i = 0; i < 3; ++i) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2561 | Expr *E = Attr.getArg(i); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2562 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2563 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2564 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2565 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2566 | << Attr.getName()->getName() << E->getSourceRange(); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2567 | return; |
| 2568 | } |
| 2569 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 2570 | } |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2571 | |
| 2572 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2573 | && D->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 2574 | ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>(); |
| 2575 | if (!(A->getXDim() == WGSize[0] && |
| 2576 | A->getYDim() == WGSize[1] && |
| 2577 | A->getZDim() == WGSize[2])) { |
| 2578 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2579 | Attr.getName(); |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint |
| 2584 | && D->hasAttr<WorkGroupSizeHintAttr>()) { |
| 2585 | WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>(); |
| 2586 | if (!(A->getXDim() == WGSize[0] && |
| 2587 | A->getYDim() == WGSize[1] && |
| 2588 | A->getZDim() == WGSize[2])) { |
| 2589 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2590 | Attr.getName(); |
| 2591 | } |
| 2592 | } |
| 2593 | |
| 2594 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize) |
| 2595 | D->addAttr(::new (S.Context) |
| 2596 | ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, |
| 2597 | WGSize[0], WGSize[1], WGSize[2])); |
| 2598 | else |
| 2599 | D->addAttr(::new (S.Context) |
| 2600 | WorkGroupSizeHintAttr(Attr.getRange(), S.Context, |
| 2601 | WGSize[0], WGSize[1], WGSize[2])); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2602 | } |
| 2603 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2604 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
| 2605 | StringRef Name) { |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2606 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2607 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2608 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2609 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2610 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2611 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2612 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2613 | return ::new (Context) SectionAttr(Range, Context, Name); |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2616 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2617 | // Attribute has no arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2618 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2619 | return; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2620 | |
| 2621 | // Make sure that there is a string literal as the sections's single |
| 2622 | // argument. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2623 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2624 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2625 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2626 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section"; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2627 | return; |
| 2628 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2629 | |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2630 | // If the target wants to validate the section specifier, make it happen. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2631 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString()); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2632 | if (!Error.empty()) { |
| 2633 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 2634 | << Error; |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2635 | return; |
| 2636 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2637 | |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2638 | // This attribute cannot be applied to local variables. |
| 2639 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 2640 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 2641 | return; |
| 2642 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2643 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), |
| 2644 | SE->getString()); |
| 2645 | if (NewAttr) |
| 2646 | D->addAttr(NewAttr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2647 | } |
| 2648 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2649 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2650 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2651 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2652 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2653 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2654 | return; |
| 2655 | } |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2656 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2657 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2658 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2659 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2660 | } else { |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2661 | D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context)); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2662 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2663 | } |
| 2664 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2665 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2666 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2667 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2668 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2669 | return; |
| 2670 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2671 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2672 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2673 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2674 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2675 | } else { |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2676 | D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context)); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2677 | } |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2678 | } |
| 2679 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2680 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2681 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2682 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2683 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2684 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2685 | D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context)); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2686 | } |
| 2687 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2688 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2689 | if (!Attr.getParameterName()) { |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2690 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2691 | return; |
| 2692 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2693 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2694 | if (Attr.getNumArgs() != 0) { |
| 2695 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2696 | return; |
| 2697 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2698 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2699 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2700 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2701 | if (!VD || !VD->hasLocalStorage()) { |
| 2702 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 2703 | return; |
| 2704 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2705 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2706 | // Look up the function |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2707 | // FIXME: Lookup probably isn't looking in the right place |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2708 | NamedDecl *CleanupDecl |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2709 | = S.LookupSingleName(S.TUScope, Attr.getParameterName(), |
| 2710 | Attr.getParameterLoc(), Sema::LookupOrdinaryName); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2711 | if (!CleanupDecl) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2712 | S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2713 | Attr.getParameterName(); |
| 2714 | return; |
| 2715 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2716 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2717 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 2718 | if (!FD) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2719 | S.Diag(Attr.getParameterLoc(), |
| 2720 | diag::err_attribute_cleanup_arg_not_function) |
| 2721 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2722 | return; |
| 2723 | } |
| 2724 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2725 | if (FD->getNumParams() != 1) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2726 | S.Diag(Attr.getParameterLoc(), |
| 2727 | diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2728 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2729 | return; |
| 2730 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2731 | |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2732 | // We're currently more strict than GCC about what function types we accept. |
| 2733 | // If this ever proves to be a problem it should be easy to fix. |
| 2734 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2735 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2736 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2737 | ParamTy, Ty) != Sema::Compatible) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2738 | S.Diag(Attr.getParameterLoc(), |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2739 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 2740 | Attr.getParameterName() << ParamTy << Ty; |
| 2741 | return; |
| 2742 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2743 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2744 | D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD)); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2745 | S.MarkFunctionReferenced(Attr.getParameterLoc(), FD); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2748 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2749 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2750 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2751 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2752 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2753 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2754 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2755 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2756 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2757 | return; |
| 2758 | } |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2759 | |
| 2760 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2761 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2762 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 2763 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2764 | unsigned FirstIdx = 1; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2765 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2766 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2767 | Expr *IdxExpr = Attr.getArg(0); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2768 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2769 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 2770 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2771 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 2772 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 2773 | return; |
| 2774 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2775 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2776 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
| 2777 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 2778 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 2779 | return; |
| 2780 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2781 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2782 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2783 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2784 | if (HasImplicitThisParam) { |
| 2785 | if (ArgIdx == 0) { |
| 2786 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 2787 | << "format_arg" << IdxExpr->getSourceRange(); |
| 2788 | return; |
| 2789 | } |
| 2790 | ArgIdx--; |
| 2791 | } |
| 2792 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2793 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2794 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2795 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2796 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2797 | if (not_nsstring_type && |
| 2798 | !isCFStringType(Ty, S.Context) && |
| 2799 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2800 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2801 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2802 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2803 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2804 | << IdxExpr->getSourceRange(); |
| 2805 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2806 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2807 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2808 | if (!isNSStringType(Ty, S.Context) && |
| 2809 | !isCFStringType(Ty, S.Context) && |
| 2810 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2811 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2812 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2813 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2814 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2815 | << IdxExpr->getSourceRange(); |
| 2816 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2817 | } |
| 2818 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2819 | D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context, |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2820 | Idx.getZExtValue())); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2823 | enum FormatAttrKind { |
| 2824 | CFStringFormat, |
| 2825 | NSStringFormat, |
| 2826 | StrftimeFormat, |
| 2827 | SupportedFormat, |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2828 | IgnoredFormat, |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2829 | InvalidFormat |
| 2830 | }; |
| 2831 | |
| 2832 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2833 | /// types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2834 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2835 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2836 | // Check for formats that get handled specially. |
| 2837 | .Case("NSString", NSStringFormat) |
| 2838 | .Case("CFString", CFStringFormat) |
| 2839 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2840 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2841 | // Otherwise, check for supported formats. |
| 2842 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2843 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2844 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2845 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2846 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2847 | .Default(InvalidFormat); |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2850 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2851 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2852 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2853 | const AttributeList &Attr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2854 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2855 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2856 | return; |
| 2857 | } |
| 2858 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2859 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2860 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2861 | Attr.setInvalid(); |
| 2862 | return; |
| 2863 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2864 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2865 | if (S.Context.getAsArrayType(T)) |
| 2866 | T = S.Context.getBaseElementType(T); |
| 2867 | if (!T->getAs<RecordType>()) { |
| 2868 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2869 | Attr.setInvalid(); |
| 2870 | return; |
| 2871 | } |
| 2872 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2873 | if (Attr.getNumArgs() != 1) { |
| 2874 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2875 | Attr.setInvalid(); |
| 2876 | return; |
| 2877 | } |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2878 | Expr *priorityExpr = Attr.getArg(0); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2879 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2880 | llvm::APSInt priority(32); |
| 2881 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 2882 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
| 2883 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 2884 | << "init_priority" << priorityExpr->getSourceRange(); |
| 2885 | Attr.setInvalid(); |
| 2886 | return; |
| 2887 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 2888 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2889 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2890 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 2891 | << priorityExpr->getSourceRange(); |
| 2892 | Attr.setInvalid(); |
| 2893 | return; |
| 2894 | } |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2895 | D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2896 | prioritynum)); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2899 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format, |
| 2900 | int FormatIdx, int FirstArg) { |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2901 | // Check whether we already have an equivalent format attribute. |
| 2902 | for (specific_attr_iterator<FormatAttr> |
| 2903 | i = D->specific_attr_begin<FormatAttr>(), |
| 2904 | e = D->specific_attr_end<FormatAttr>(); |
| 2905 | i != e ; ++i) { |
| 2906 | FormatAttr *f = *i; |
| 2907 | if (f->getType() == Format && |
| 2908 | f->getFormatIdx() == FormatIdx && |
| 2909 | f->getFirstArg() == FirstArg) { |
| 2910 | // If we don't have a valid location for this attribute, adopt the |
| 2911 | // location. |
| 2912 | if (f->getLocation().isInvalid()) |
| 2913 | f->setRange(Range); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2914 | return NULL; |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2915 | } |
| 2916 | } |
| 2917 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2918 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 2919 | FirstArg); |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2920 | } |
| 2921 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2922 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2923 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2924 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2925 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2926 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2927 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2928 | << "format" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2929 | return; |
| 2930 | } |
| 2931 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2932 | if (Attr.getNumArgs() != 2) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2933 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2934 | return; |
| 2935 | } |
| 2936 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2937 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2938 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2939 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2940 | return; |
| 2941 | } |
| 2942 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2943 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2944 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2945 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 2946 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2947 | unsigned FirstIdx = 1; |
| 2948 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2949 | StringRef Format = Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2950 | |
| 2951 | // Normalize the argument, __foo__ becomes foo. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2952 | if (Format.startswith("__") && Format.endswith("__")) |
| 2953 | Format = Format.substr(2, Format.size() - 4); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2954 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2955 | // Check for supported formats. |
| 2956 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2957 | |
| 2958 | if (Kind == IgnoredFormat) |
| 2959 | return; |
| 2960 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2961 | if (Kind == InvalidFormat) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2962 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 2963 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2964 | return; |
| 2965 | } |
| 2966 | |
| 2967 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2968 | Expr *IdxExpr = Attr.getArg(0); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2969 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2970 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 2971 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2972 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2973 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2974 | return; |
| 2975 | } |
| 2976 | |
| 2977 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2978 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2979 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2980 | return; |
| 2981 | } |
| 2982 | |
| 2983 | // FIXME: Do we need to bounds check? |
| 2984 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2985 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2986 | if (HasImplicitThisParam) { |
| 2987 | if (ArgIdx == 0) { |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2988 | S.Diag(Attr.getLoc(), |
| 2989 | diag::err_format_attribute_implicit_this_format_string) |
| 2990 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2991 | return; |
| 2992 | } |
| 2993 | ArgIdx--; |
| 2994 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2995 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2996 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2997 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2998 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2999 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3000 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3001 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3002 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3003 | return; |
| 3004 | } |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3005 | } else if (Kind == NSStringFormat) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3006 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3007 | // semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3008 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3009 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3010 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3011 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3012 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3013 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3014 | } else if (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3015 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3016 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3017 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3018 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3019 | return; |
| 3020 | } |
| 3021 | |
| 3022 | // check the 3rd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3023 | Expr *FirstArgExpr = Attr.getArg(1); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3024 | llvm::APSInt FirstArg(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3025 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 3026 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3027 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3028 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3029 | return; |
| 3030 | } |
| 3031 | |
| 3032 | // check if the function is variadic if the 3rd argument non-zero |
| 3033 | if (FirstArg != 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3034 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3035 | ++NumArgs; // +1 for ... |
| 3036 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3037 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3038 | return; |
| 3039 | } |
| 3040 | } |
| 3041 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3042 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3043 | // variable the input is just the current time + the format string. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3044 | if (Kind == StrftimeFormat) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3045 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3046 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 3047 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3048 | return; |
| 3049 | } |
| 3050 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3051 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3052 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3053 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3054 | return; |
| 3055 | } |
| 3056 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3057 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format, |
| 3058 | Idx.getZExtValue(), |
| 3059 | FirstArg.getZExtValue()); |
| 3060 | if (NewAttr) |
| 3061 | D->addAttr(NewAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3062 | } |
| 3063 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3064 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 3065 | const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3066 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3067 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3068 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3069 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3070 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3071 | // Try to find the underlying union declaration. |
| 3072 | RecordDecl *RD = 0; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3073 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3074 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3075 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3076 | else |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3077 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3078 | |
| 3079 | if (!RD || !RD->isUnion()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3080 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3081 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3082 | return; |
| 3083 | } |
| 3084 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3085 | if (!RD->isCompleteDefinition()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3086 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3087 | diag::warn_transparent_union_attribute_not_definition); |
| 3088 | return; |
| 3089 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3090 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3091 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3092 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3093 | if (Field == FieldEnd) { |
| 3094 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3095 | return; |
| 3096 | } |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3097 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3098 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3099 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3100 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3101 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3102 | diag::warn_transparent_union_attribute_floating) |
| 3103 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3104 | return; |
| 3105 | } |
| 3106 | |
| 3107 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3108 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3109 | for (; Field != FieldEnd; ++Field) { |
| 3110 | QualType FieldType = Field->getType(); |
| 3111 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 3112 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 3113 | // Warn if we drop the attribute. |
| 3114 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3115 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3116 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3117 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3118 | diag::warn_transparent_union_attribute_field_size_align) |
| 3119 | << isSize << Field->getDeclName() << FieldBits; |
| 3120 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3121 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3122 | diag::note_transparent_union_first_field_size_align) |
| 3123 | << isSize << FirstBits; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3124 | return; |
| 3125 | } |
| 3126 | } |
| 3127 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3128 | RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3129 | } |
| 3130 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3131 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3132 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3133 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3134 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3135 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3136 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3137 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3138 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3139 | // Make sure that there is a string literal as the annotation's single |
| 3140 | // argument. |
| 3141 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3142 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate"; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3143 | return; |
| 3144 | } |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3145 | |
| 3146 | // Don't duplicate annotations that are already set. |
| 3147 | for (specific_attr_iterator<AnnotateAttr> |
| 3148 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 3149 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
| 3150 | if ((*i)->getAnnotation() == SE->getString()) |
| 3151 | return; |
| 3152 | } |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3153 | D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3154 | SE->getString())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3157 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3158 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3159 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3160 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3161 | return; |
| 3162 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3163 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3164 | //FIXME: The C++0x version of this attribute has more limited applicabilty |
| 3165 | // than GNU's, and should error out when it is used to specify a |
| 3166 | // weaker alignment, rather than being silently ignored. |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3167 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3168 | if (Attr.getNumArgs() == 0) { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3169 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 3170 | true, 0, Attr.isDeclspecAttribute())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3171 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3172 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3173 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3174 | S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0), |
| 3175 | Attr.isDeclspecAttribute()); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3178 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 3179 | bool isDeclSpec) { |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 3180 | // FIXME: Handle pack-expansions here. |
| 3181 | if (DiagnoseUnexpandedParameterPack(E)) |
| 3182 | return; |
| 3183 | |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3184 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3185 | // Save dependent expressions in the AST to be instantiated. |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3186 | D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E, |
| 3187 | isDeclSpec)); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3188 | return; |
| 3189 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3190 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3191 | SourceLocation AttrLoc = AttrRange.getBegin(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3192 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3193 | llvm::APSInt Alignment(32); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3194 | ExprResult ICE |
| 3195 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3196 | diag::err_aligned_attribute_argument_not_int, |
| 3197 | /*AllowFold*/ false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3198 | if (ICE.isInvalid()) |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3199 | return; |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3200 | if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3201 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 3202 | << E->getSourceRange(); |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3203 | return; |
| 3204 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3205 | if (isDeclSpec) { |
| 3206 | // We've already verified it's a power of 2, now let's make sure it's |
| 3207 | // 8192 or less. |
| 3208 | if (Alignment.getZExtValue() > 8192) { |
| 3209 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
| 3210 | << E->getSourceRange(); |
| 3211 | return; |
| 3212 | } |
| 3213 | } |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3214 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3215 | D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take(), |
| 3216 | isDeclSpec)); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3219 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
| 3220 | bool isDeclSpec) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3221 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3222 | // FIXME: Perform checking of type validity |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3223 | D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3224 | isDeclSpec)); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3225 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3226 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3227 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3228 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3229 | /// type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3230 | /// |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3231 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3232 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3233 | /// HImode, not an intermediate pointer. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3234 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3235 | // This attribute isn't documented, but glibc uses it. It changes |
| 3236 | // the width of an int or unsigned int to the specified size. |
| 3237 | |
| 3238 | // Check that there aren't any arguments |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3239 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3240 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3241 | |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3242 | |
| 3243 | IdentifierInfo *Name = Attr.getParameterName(); |
| 3244 | if (!Name) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3245 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3246 | return; |
| 3247 | } |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3248 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3249 | StringRef Str = Attr.getParameterName()->getName(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3250 | |
| 3251 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3252 | if (Str.startswith("__") && Str.endswith("__")) |
| 3253 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3254 | |
| 3255 | unsigned DestWidth = 0; |
| 3256 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3257 | bool ComplexMode = false; |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3258 | switch (Str.size()) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3259 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3260 | switch (Str[0]) { |
| 3261 | case 'Q': DestWidth = 8; break; |
| 3262 | case 'H': DestWidth = 16; break; |
| 3263 | case 'S': DestWidth = 32; break; |
| 3264 | case 'D': DestWidth = 64; break; |
| 3265 | case 'X': DestWidth = 96; break; |
| 3266 | case 'T': DestWidth = 128; break; |
| 3267 | } |
| 3268 | if (Str[1] == 'F') { |
| 3269 | IntegerMode = false; |
| 3270 | } else if (Str[1] == 'C') { |
| 3271 | IntegerMode = false; |
| 3272 | ComplexMode = true; |
| 3273 | } else if (Str[1] != 'I') { |
| 3274 | DestWidth = 0; |
| 3275 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3276 | break; |
| 3277 | case 4: |
| 3278 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3279 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3280 | if (Str == "word") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3281 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3282 | else if (Str == "byte") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3283 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3284 | break; |
| 3285 | case 7: |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3286 | if (Str == "pointer") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3287 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3288 | break; |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3289 | case 11: |
| 3290 | if (Str == "unwind_word") |
Rafael Espindola | 0b1de54 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3291 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3292 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | QualType OldTy; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3296 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3297 | OldTy = TD->getUnderlyingType(); |
| 3298 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3299 | OldTy = VD->getType(); |
| 3300 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3301 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3302 | << "mode" << Attr.getRange(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3303 | return; |
| 3304 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3305 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3306 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3307 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3308 | else if (IntegerMode) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3309 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3310 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3311 | } else if (ComplexMode) { |
| 3312 | if (!OldTy->isComplexType()) |
| 3313 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3314 | } else { |
| 3315 | if (!OldTy->isFloatingType()) |
| 3316 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3317 | } |
| 3318 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3319 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3320 | // and friends, at least with glibc. |
| 3321 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 3322 | // width on unusual platforms. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3323 | // FIXME: Make sure floating-point mappings are accurate |
| 3324 | // FIXME: Support XF and TF types |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3325 | QualType NewTy; |
| 3326 | switch (DestWidth) { |
| 3327 | case 0: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3328 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3329 | return; |
| 3330 | default: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3331 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3332 | return; |
| 3333 | case 8: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3334 | if (!IntegerMode) { |
| 3335 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3336 | return; |
| 3337 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3338 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3339 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3340 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3341 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3342 | break; |
| 3343 | case 16: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3344 | if (!IntegerMode) { |
| 3345 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3346 | return; |
| 3347 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3348 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3349 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3350 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3351 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3352 | break; |
| 3353 | case 32: |
| 3354 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3355 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3356 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3357 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3358 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3359 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3360 | break; |
| 3361 | case 64: |
| 3362 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3363 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3364 | else if (OldTy->isSignedIntegerType()) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3365 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3366 | NewTy = S.Context.LongTy; |
| 3367 | else |
| 3368 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3369 | else |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3370 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3371 | NewTy = S.Context.UnsignedLongTy; |
| 3372 | else |
| 3373 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3374 | break; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3375 | case 96: |
| 3376 | NewTy = S.Context.LongDoubleTy; |
| 3377 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3378 | case 128: |
| 3379 | if (!IntegerMode) { |
| 3380 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3381 | return; |
| 3382 | } |
Anders Carlsson | f5f7d86 | 2009-12-29 07:07:36 +0000 | [diff] [blame] | 3383 | if (OldTy->isSignedIntegerType()) |
| 3384 | NewTy = S.Context.Int128Ty; |
| 3385 | else |
| 3386 | NewTy = S.Context.UnsignedInt128Ty; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3387 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3390 | if (ComplexMode) { |
| 3391 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3392 | } |
| 3393 | |
| 3394 | // Install the new type. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3395 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 3396 | // FIXME: preserve existing source info. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3397 | TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy)); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 3398 | } else |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3399 | cast<ValueDecl>(D)->setType(NewTy); |
| 3400 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3401 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3402 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3403 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3404 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3405 | return; |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 3406 | |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3407 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3408 | if (!VD->hasGlobalStorage()) |
| 3409 | S.Diag(Attr.getLoc(), |
| 3410 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3411 | << Attr.getName(); |
| 3412 | } else if (!isFunctionOrMethod(D)) { |
| 3413 | S.Diag(Attr.getLoc(), |
| 3414 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3415 | << Attr.getName(); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3416 | return; |
| 3417 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3418 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3419 | D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context)); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3420 | } |
| 3421 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3422 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3423 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3424 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3425 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3426 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3427 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3428 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3429 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3430 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3431 | return; |
| 3432 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3433 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3434 | D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context)); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3437 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 3438 | const AttributeList &Attr) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3439 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3440 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3441 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3442 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3443 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3444 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3445 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3446 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3447 | return; |
| 3448 | } |
| 3449 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3450 | D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3451 | S.Context)); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3452 | } |
| 3453 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3454 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3455 | if (S.LangOpts.CUDA) { |
| 3456 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 3457 | if (Attr.hasParameterOrArguments()) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3458 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 3459 | return; |
| 3460 | } |
| 3461 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3462 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3463 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3464 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3465 | return; |
| 3466 | } |
| 3467 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3468 | D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3469 | } else { |
| 3470 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 3471 | } |
| 3472 | } |
| 3473 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3474 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3475 | if (S.LangOpts.CUDA) { |
| 3476 | // check the attribute arguments. |
| 3477 | if (Attr.getNumArgs() != 0) { |
| 3478 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 3479 | return; |
| 3480 | } |
| 3481 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3482 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3483 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3484 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3485 | return; |
| 3486 | } |
| 3487 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3488 | D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3489 | } else { |
| 3490 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 3491 | } |
| 3492 | } |
| 3493 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3494 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3495 | if (S.LangOpts.CUDA) { |
| 3496 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3497 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3498 | return; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3499 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3500 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3501 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3502 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3503 | return; |
| 3504 | } |
| 3505 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3506 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3507 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 3508 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3509 | if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) { |
| 3510 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3511 | << FD->getType() |
| 3512 | << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(), |
| 3513 | "void"); |
| 3514 | } else { |
| 3515 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3516 | << FD->getType(); |
| 3517 | } |
| 3518 | return; |
| 3519 | } |
| 3520 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3521 | D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3522 | } else { |
| 3523 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 3524 | } |
| 3525 | } |
| 3526 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3527 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3528 | if (S.LangOpts.CUDA) { |
| 3529 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3530 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3531 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3532 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3533 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3534 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3535 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3536 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3537 | return; |
| 3538 | } |
| 3539 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3540 | D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3541 | } else { |
| 3542 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 3543 | } |
| 3544 | } |
| 3545 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3546 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3547 | if (S.LangOpts.CUDA) { |
| 3548 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3549 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3550 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3551 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3552 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3553 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3554 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3555 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3556 | return; |
| 3557 | } |
| 3558 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3559 | D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3560 | } else { |
| 3561 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 3562 | } |
| 3563 | } |
| 3564 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3565 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3566 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3567 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3568 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3569 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3570 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3571 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3572 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3573 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3574 | return; |
| 3575 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3576 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3577 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3578 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3579 | return; |
| 3580 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3581 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3582 | D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context)); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3583 | } |
| 3584 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3585 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3586 | if (hasDeclarator(D)) return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3587 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3588 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3589 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3590 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3591 | CallingConv CC; |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3592 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3593 | return; |
| 3594 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3595 | if (!isa<ObjCMethodDecl>(D)) { |
| 3596 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3597 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3598 | return; |
| 3599 | } |
| 3600 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3601 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3602 | case AttributeList::AT_FastCall: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3603 | D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3604 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3605 | case AttributeList::AT_StdCall: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3606 | D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3607 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3608 | case AttributeList::AT_ThisCall: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3609 | D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context)); |
Douglas Gregor | 04633eb | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3610 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3611 | case AttributeList::AT_CDecl: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3612 | D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3613 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3614 | case AttributeList::AT_Pascal: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3615 | D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context)); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3616 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3617 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3618 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3619 | switch (CC) { |
| 3620 | case CC_AAPCS: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3621 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3622 | break; |
| 3623 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3624 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3625 | break; |
| 3626 | default: |
| 3627 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3628 | } |
| 3629 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3630 | D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS)); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3631 | return; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3632 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3633 | case AttributeList::AT_PnaclCall: |
| 3634 | D->addAttr(::new (S.Context) PnaclCallAttr(Attr.getRange(), S.Context)); |
| 3635 | return; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3636 | case AttributeList::AT_IntelOclBicc: |
| 3637 | D->addAttr(::new (S.Context) IntelOclBiccAttr(Attr.getRange(), S.Context)); |
| 3638 | return; |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3639 | |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3640 | default: |
| 3641 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3642 | } |
| 3643 | } |
| 3644 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3645 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 3646 | assert(!Attr.isInvalid()); |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3647 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3648 | } |
| 3649 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3650 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3651 | const FunctionDecl *FD) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3652 | if (attr.isInvalid()) |
| 3653 | return true; |
| 3654 | |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3655 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
| 3656 | if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) { |
| 3657 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3658 | attr.setInvalid(); |
| 3659 | return true; |
| 3660 | } |
| 3661 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3662 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 3663 | // move to TargetAttributesSema one day. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3664 | switch (attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3665 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3666 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3667 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3668 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3669 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
| 3670 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3671 | Expr *Arg = attr.getArg(0); |
| 3672 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3673 | if (!Str || !Str->isAscii()) { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3674 | Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 3675 | << "pcs" << 1; |
| 3676 | attr.setInvalid(); |
| 3677 | return true; |
| 3678 | } |
| 3679 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3680 | StringRef StrRef = Str->getString(); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3681 | if (StrRef == "aapcs") { |
| 3682 | CC = CC_AAPCS; |
| 3683 | break; |
| 3684 | } else if (StrRef == "aapcs-vfp") { |
| 3685 | CC = CC_AAPCS_VFP; |
| 3686 | break; |
| 3687 | } |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3688 | |
| 3689 | attr.setInvalid(); |
| 3690 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3691 | return true; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3692 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3693 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3694 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3695 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3696 | } |
| 3697 | |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3698 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3699 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 3700 | if (A == TargetInfo::CCCR_Warning) { |
| 3701 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3702 | |
| 3703 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3704 | if (FD) |
| 3705 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3706 | TargetInfo::CCMT_NonMember; |
| 3707 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3710 | return false; |
| 3711 | } |
| 3712 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3713 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3714 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3715 | |
| 3716 | unsigned numParams; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3717 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3718 | return; |
| 3719 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3720 | if (!isa<ObjCMethodDecl>(D)) { |
| 3721 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3722 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3723 | return; |
| 3724 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3725 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3726 | D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams)); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
| 3729 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3730 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3731 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3732 | if (Attr.isInvalid()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3733 | return true; |
| 3734 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3735 | if (Attr.getNumArgs() != 1) { |
| 3736 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 3737 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3738 | return true; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3739 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3740 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3741 | Expr *NumParamsExpr = Attr.getArg(0); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3742 | llvm::APSInt NumParams(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3743 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3744 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3745 | Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3746 | << "regparm" << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3747 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3748 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3749 | } |
| 3750 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3751 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3752 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3753 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3754 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3755 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3756 | } |
| 3757 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3758 | numParams = NumParams.getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3759 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3760 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3761 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3762 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3763 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3764 | } |
| 3765 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3766 | return false; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3767 | } |
| 3768 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3769 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3770 | if (S.LangOpts.CUDA) { |
| 3771 | // check the attribute arguments. |
| 3772 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 3773 | // FIXME: 0 is not okay. |
| 3774 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3775 | return; |
| 3776 | } |
| 3777 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3778 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3779 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3780 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3781 | return; |
| 3782 | } |
| 3783 | |
| 3784 | Expr *MaxThreadsExpr = Attr.getArg(0); |
| 3785 | llvm::APSInt MaxThreads(32); |
| 3786 | if (MaxThreadsExpr->isTypeDependent() || |
| 3787 | MaxThreadsExpr->isValueDependent() || |
| 3788 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
| 3789 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 3790 | << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange(); |
| 3791 | return; |
| 3792 | } |
| 3793 | |
| 3794 | llvm::APSInt MinBlocks(32); |
| 3795 | if (Attr.getNumArgs() > 1) { |
| 3796 | Expr *MinBlocksExpr = Attr.getArg(1); |
| 3797 | if (MinBlocksExpr->isTypeDependent() || |
| 3798 | MinBlocksExpr->isValueDependent() || |
| 3799 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
| 3800 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 3801 | << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange(); |
| 3802 | return; |
| 3803 | } |
| 3804 | } |
| 3805 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3806 | D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3807 | MaxThreads.getZExtValue(), |
| 3808 | MinBlocks.getZExtValue())); |
| 3809 | } else { |
| 3810 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 3811 | } |
| 3812 | } |
| 3813 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3814 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 3815 | const AttributeList &Attr) { |
| 3816 | StringRef AttrName = Attr.getName()->getName(); |
| 3817 | if (!Attr.getParameterName()) { |
| 3818 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier) |
| 3819 | << Attr.getName() << /* arg num = */ 1; |
| 3820 | return; |
| 3821 | } |
| 3822 | |
| 3823 | if (Attr.getNumArgs() != 2) { |
| 3824 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3825 | << /* required args = */ 3; |
| 3826 | return; |
| 3827 | } |
| 3828 | |
| 3829 | IdentifierInfo *ArgumentKind = Attr.getParameterName(); |
| 3830 | |
| 3831 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 3832 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3833 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 3834 | return; |
| 3835 | } |
| 3836 | |
| 3837 | uint64_t ArgumentIdx; |
| 3838 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 3839 | Attr.getLoc(), 2, |
| 3840 | Attr.getArg(0), ArgumentIdx)) |
| 3841 | return; |
| 3842 | |
| 3843 | uint64_t TypeTagIdx; |
| 3844 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 3845 | Attr.getLoc(), 3, |
| 3846 | Attr.getArg(1), TypeTagIdx)) |
| 3847 | return; |
| 3848 | |
| 3849 | bool IsPointer = (AttrName == "pointer_with_type_tag"); |
| 3850 | if (IsPointer) { |
| 3851 | // Ensure that buffer has a pointer type. |
| 3852 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 3853 | if (!BufferTy->isPointerType()) { |
| 3854 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
| 3855 | << AttrName; |
| 3856 | } |
| 3857 | } |
| 3858 | |
| 3859 | D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(Attr.getRange(), |
| 3860 | S.Context, |
| 3861 | ArgumentKind, |
| 3862 | ArgumentIdx, |
| 3863 | TypeTagIdx, |
| 3864 | IsPointer)); |
| 3865 | } |
| 3866 | |
| 3867 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 3868 | const AttributeList &Attr) { |
| 3869 | IdentifierInfo *PointerKind = Attr.getParameterName(); |
| 3870 | if (!PointerKind) { |
| 3871 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier) |
| 3872 | << "type_tag_for_datatype" << 1; |
| 3873 | return; |
| 3874 | } |
| 3875 | |
| 3876 | QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL); |
| 3877 | |
| 3878 | D->addAttr(::new (S.Context) TypeTagForDatatypeAttr( |
| 3879 | Attr.getRange(), |
| 3880 | S.Context, |
| 3881 | PointerKind, |
| 3882 | MatchingCType, |
| 3883 | Attr.getLayoutCompatible(), |
| 3884 | Attr.getMustBeNull())); |
| 3885 | } |
| 3886 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3887 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3888 | // Checker-specific attribute handlers. |
| 3889 | //===----------------------------------------------------------------------===// |
| 3890 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3891 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3892 | return type->isDependentType() || |
| 3893 | type->isObjCObjectPointerType() || |
| 3894 | S.Context.isObjCNSObjectType(type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3895 | } |
| 3896 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3897 | return type->isDependentType() || |
| 3898 | type->isPointerType() || |
| 3899 | isValidSubjectOfNSAttribute(S, type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3902 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3903 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3904 | if (!param) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3905 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3906 | << Attr.getRange() << Attr.getName() << ExpectedParameter; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3907 | return; |
| 3908 | } |
| 3909 | |
| 3910 | bool typeOK, cf; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3911 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3912 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 3913 | cf = false; |
| 3914 | } else { |
| 3915 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 3916 | cf = true; |
| 3917 | } |
| 3918 | |
| 3919 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3920 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3921 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3922 | return; |
| 3923 | } |
| 3924 | |
| 3925 | if (cf) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3926 | param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context)); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3927 | else |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3928 | param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context)); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3931 | static void handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 3932 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3933 | if (!isa<ObjCMethodDecl>(D)) { |
| 3934 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3935 | << Attr.getRange() << Attr.getName() << ExpectedMethod; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3936 | return; |
| 3937 | } |
| 3938 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3939 | D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context)); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3940 | } |
| 3941 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3942 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 3943 | const AttributeList &Attr) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3944 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3945 | QualType returnType; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3946 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3947 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3948 | returnType = MD->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3949 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3950 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3951 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | a23bd4c | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 3952 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 3953 | returnType = PD->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3954 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3955 | returnType = FD->getResultType(); |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3956 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3957 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3958 | << Attr.getRange() << Attr.getName() |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3959 | << ExpectedFunctionOrMethod; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3960 | return; |
| 3961 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3962 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3963 | bool typeOK; |
| 3964 | bool cf; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3965 | switch (Attr.getKind()) { |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3966 | default: llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3967 | case AttributeList::AT_NSReturnsAutoreleased: |
| 3968 | case AttributeList::AT_NSReturnsRetained: |
| 3969 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3970 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 3971 | cf = false; |
| 3972 | break; |
| 3973 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3974 | case AttributeList::AT_CFReturnsRetained: |
| 3975 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3976 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 3977 | cf = true; |
| 3978 | break; |
| 3979 | } |
| 3980 | |
| 3981 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3982 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3983 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3984 | return; |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3985 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3986 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3987 | switch (Attr.getKind()) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3988 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3989 | llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3990 | case AttributeList::AT_NSReturnsAutoreleased: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3991 | D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(), |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3992 | S.Context)); |
| 3993 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3994 | case AttributeList::AT_CFReturnsNotRetained: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3995 | D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 3996 | S.Context)); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3997 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3998 | case AttributeList::AT_NSReturnsNotRetained: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3999 | D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4000 | S.Context)); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4001 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4002 | case AttributeList::AT_CFReturnsRetained: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4003 | D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4004 | S.Context)); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4005 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4006 | case AttributeList::AT_NSReturnsRetained: |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4007 | D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4008 | S.Context)); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4009 | return; |
| 4010 | }; |
| 4011 | } |
| 4012 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4013 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4014 | const AttributeList &attr) { |
| 4015 | SourceLocation loc = attr.getLoc(); |
| 4016 | |
| 4017 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4018 | |
Fariborz Jahanian | 94d55d7 | 2012-04-21 17:51:44 +0000 | [diff] [blame] | 4019 | if (!method) { |
Fariborz Jahanian | 0e78afb | 2012-04-20 22:00:46 +0000 | [diff] [blame] | 4020 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4021 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4022 | return; |
| 4023 | } |
| 4024 | |
| 4025 | // Check that the method returns a normal pointer. |
| 4026 | QualType resultType = method->getResultType(); |
Fariborz Jahanian | f2e5945 | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4027 | |
| 4028 | if (!resultType->isReferenceType() && |
| 4029 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4030 | S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 4031 | << SourceRange(loc) |
| 4032 | << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2; |
| 4033 | |
| 4034 | // Drop the attribute. |
| 4035 | return; |
| 4036 | } |
| 4037 | |
| 4038 | method->addAttr( |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4039 | ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context)); |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4040 | } |
| 4041 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4042 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4043 | const AttributeList &attr) { |
| 4044 | SourceLocation loc = attr.getLoc(); |
| 4045 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4046 | |
| 4047 | if (!method) { |
| 4048 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 4049 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
| 4050 | return; |
| 4051 | } |
| 4052 | DeclContext *DC = method->getDeclContext(); |
| 4053 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4054 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4055 | << attr.getName() << 0; |
| 4056 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4057 | return; |
| 4058 | } |
| 4059 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4060 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4061 | << attr.getName() << 1; |
| 4062 | return; |
| 4063 | } |
| 4064 | |
| 4065 | method->addAttr( |
| 4066 | ::new (S.Context) ObjCRequiresSuperAttr(attr.getRange(), S.Context)); |
| 4067 | } |
| 4068 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4069 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 4070 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
| 4071 | if (!isa<FunctionDecl>(D)) { |
| 4072 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4073 | << A.getRange() << A.getName() << ExpectedFunction; |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4074 | return; |
| 4075 | } |
| 4076 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4077 | bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4078 | |
| 4079 | // Check whether there's a conflicting attribute already present. |
| 4080 | Attr *Existing; |
| 4081 | if (IsAudited) { |
| 4082 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 4083 | } else { |
| 4084 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 4085 | } |
| 4086 | if (Existing) { |
| 4087 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 4088 | << A.getName() |
| 4089 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 4090 | << A.getRange() << Existing->getRange(); |
| 4091 | return; |
| 4092 | } |
| 4093 | |
| 4094 | // All clear; add the attribute. |
| 4095 | if (IsAudited) { |
| 4096 | D->addAttr( |
| 4097 | ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context)); |
| 4098 | } else { |
| 4099 | D->addAttr( |
| 4100 | ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context)); |
| 4101 | } |
| 4102 | } |
| 4103 | |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4104 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4105 | const AttributeList &Attr) { |
| 4106 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4107 | if (!RD || RD->isUnion()) { |
| 4108 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4109 | << Attr.getRange() << Attr.getName() << ExpectedStruct; |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | IdentifierInfo *ParmName = Attr.getParameterName(); |
| 4113 | |
| 4114 | // In Objective-C, verify that the type names an Objective-C type. |
| 4115 | // We don't want to check this outside of ObjC because people sometimes |
| 4116 | // do crazy C declarations of Objective-C types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4117 | if (ParmName && S.getLangOpts().ObjC1) { |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4118 | // Check for an existing type with this name. |
| 4119 | LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(), |
| 4120 | Sema::LookupOrdinaryName); |
| 4121 | if (S.LookupName(R, Sc)) { |
| 4122 | NamedDecl *Target = R.getFoundDecl(); |
| 4123 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 4124 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 4125 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 4126 | } |
| 4127 | } |
| 4128 | } |
| 4129 | |
| 4130 | D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context, |
| 4131 | ParmName)); |
| 4132 | } |
| 4133 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4134 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4135 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4136 | if (hasDeclarator(D)) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4137 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4138 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4139 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4140 | } |
| 4141 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4142 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4143 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4144 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4145 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4146 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4147 | return; |
| 4148 | } |
| 4149 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4150 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4151 | QualType type = vd->getType(); |
| 4152 | |
| 4153 | if (!type->isDependentType() && |
| 4154 | !type->isObjCLifetimeType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4155 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4156 | << type; |
| 4157 | return; |
| 4158 | } |
| 4159 | |
| 4160 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4161 | |
| 4162 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4163 | // going to infer. |
| 4164 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4165 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4166 | |
| 4167 | switch (lifetime) { |
| 4168 | case Qualifiers::OCL_None: |
| 4169 | assert(type->isDependentType() && |
| 4170 | "didn't infer lifetime for non-dependent type?"); |
| 4171 | break; |
| 4172 | |
| 4173 | case Qualifiers::OCL_Weak: // meaningful |
| 4174 | case Qualifiers::OCL_Strong: // meaningful |
| 4175 | break; |
| 4176 | |
| 4177 | case Qualifiers::OCL_ExplicitNone: |
| 4178 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4179 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4180 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4181 | break; |
| 4182 | } |
| 4183 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4184 | D->addAttr(::new (S.Context) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4185 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4186 | } |
| 4187 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4188 | //===----------------------------------------------------------------------===// |
| 4189 | // Microsoft specific attribute handlers. |
| 4190 | //===----------------------------------------------------------------------===// |
| 4191 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4192 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 4193 | if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) { |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4194 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4195 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4196 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4197 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4198 | Expr *Arg = Attr.getArg(0); |
| 4199 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4200 | if (!Str || !Str->isAscii()) { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4201 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 4202 | << "uuid" << 1; |
| 4203 | return; |
| 4204 | } |
| 4205 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4206 | StringRef StrRef = Str->getString(); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4207 | |
| 4208 | bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' && |
| 4209 | StrRef.back() == '}'; |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4210 | |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4211 | // Validate GUID length. |
| 4212 | if (IsCurly && StrRef.size() != 38) { |
| 4213 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4214 | return; |
| 4215 | } |
| 4216 | if (!IsCurly && StrRef.size() != 36) { |
| 4217 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4218 | return; |
| 4219 | } |
| 4220 | |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4221 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4222 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4223 | StringRef::iterator I = StrRef.begin(); |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4224 | if (IsCurly) // Skip the optional '{' |
| 4225 | ++I; |
| 4226 | |
| 4227 | for (int i = 0; i < 36; ++i) { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4228 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 4229 | if (*I != '-') { |
| 4230 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4231 | return; |
| 4232 | } |
| 4233 | } else if (!isxdigit(*I)) { |
| 4234 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4235 | return; |
| 4236 | } |
| 4237 | I++; |
| 4238 | } |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4239 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4240 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4241 | Str->getString())); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4242 | } else |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4243 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid"; |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4244 | } |
| 4245 | |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4246 | static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4247 | if (!S.LangOpts.MicrosoftExt) { |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4248 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4249 | return; |
| 4250 | } |
| 4251 | |
| 4252 | AttributeList::Kind Kind = Attr.getKind(); |
| 4253 | if (Kind == AttributeList::AT_SingleInheritance) |
| 4254 | D->addAttr( |
| 4255 | ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context)); |
| 4256 | else if (Kind == AttributeList::AT_MultipleInheritance) |
| 4257 | D->addAttr( |
| 4258 | ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context)); |
| 4259 | else if (Kind == AttributeList::AT_VirtualInheritance) |
| 4260 | D->addAttr( |
| 4261 | ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context)); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4262 | } |
| 4263 | |
| 4264 | static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4265 | if (S.LangOpts.MicrosoftExt) { |
| 4266 | AttributeList::Kind Kind = Attr.getKind(); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4267 | if (Kind == AttributeList::AT_Ptr32) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4268 | D->addAttr( |
| 4269 | ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context)); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4270 | else if (Kind == AttributeList::AT_Ptr64) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4271 | D->addAttr( |
| 4272 | ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context)); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4273 | else if (Kind == AttributeList::AT_Win64) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4274 | D->addAttr( |
| 4275 | ::new (S.Context) Win64Attr(Attr.getRange(), S.Context)); |
| 4276 | } else |
| 4277 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4278 | } |
| 4279 | |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4280 | static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4281 | if (S.LangOpts.MicrosoftExt) |
| 4282 | D->addAttr(::new (S.Context) ForceInlineAttr(Attr.getRange(), S.Context)); |
| 4283 | else |
| 4284 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4285 | } |
| 4286 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4287 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4288 | // Top Level Sema Entry Points |
| 4289 | //===----------------------------------------------------------------------===// |
| 4290 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4291 | static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4292 | const AttributeList &Attr) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4293 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4294 | case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break; |
| 4295 | case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break; |
| 4296 | case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break; |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4297 | default: |
| 4298 | break; |
| 4299 | } |
| 4300 | } |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4301 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4302 | static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4303 | const AttributeList &Attr) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4304 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4305 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 4306 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 4307 | case AttributeList::AT_IBOutletCollection: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4308 | handleIBOutletCollection(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4309 | case AttributeList::AT_AddressSpace: |
| 4310 | case AttributeList::AT_OpenCLImageAccess: |
| 4311 | case AttributeList::AT_ObjCGC: |
| 4312 | case AttributeList::AT_VectorSize: |
| 4313 | case AttributeList::AT_NeonVectorType: |
| 4314 | case AttributeList::AT_NeonPolyVectorType: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4315 | // Ignore these, these are type attributes, handled by |
| 4316 | // ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4317 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4318 | case AttributeList::AT_CUDADevice: |
| 4319 | case AttributeList::AT_CUDAHost: |
| 4320 | case AttributeList::AT_Overloadable: |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4321 | // Ignore, this is a non-inheritable attribute, handled |
| 4322 | // by ProcessNonInheritableDeclAttr. |
| 4323 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4324 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 4325 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 4326 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 4327 | case AttributeList::AT_AlwaysInline: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4328 | handleAlwaysInlineAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4329 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4330 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 4331 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4332 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 4333 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 4334 | case AttributeList::AT_CarriesDependency: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4335 | handleDependencyAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4336 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
| 4337 | case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break; |
| 4338 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
| 4339 | case AttributeList::AT_Deprecated: |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4340 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated"); |
| 4341 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4342 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 4343 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4344 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4345 | break; |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4346 | case AttributeList::AT_MinSize: |
| 4347 | handleMinSizeAttr(S, D, Attr); |
| 4348 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4349 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4350 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4351 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
| 4352 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4353 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4354 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4355 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4356 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
| 4357 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
| 4358 | case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break; |
| 4359 | case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break; |
| 4360 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4361 | case AttributeList::AT_ownership_returns: |
| 4362 | case AttributeList::AT_ownership_takes: |
| 4363 | case AttributeList::AT_ownership_holds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4364 | handleOwnershipAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4365 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4366 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
| 4367 | case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break; |
| 4368 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
| 4369 | case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break; |
| 4370 | case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break; |
| 4371 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4372 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4373 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4374 | handleObjCOwnershipAttr(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4375 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4376 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4377 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4378 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4379 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4380 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4381 | case AttributeList::AT_ObjCRequiresSuper: |
| 4382 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4383 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4384 | case AttributeList::AT_NSBridged: |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4385 | handleNSBridgedAttr(S, scope, D, Attr); break; |
| 4386 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4387 | case AttributeList::AT_CFAuditedTransfer: |
| 4388 | case AttributeList::AT_CFUnknownTransfer: |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4389 | handleCFTransferAttr(S, D, Attr); break; |
| 4390 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4391 | // Checker-specific. |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4392 | case AttributeList::AT_CFConsumed: |
| 4393 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4394 | case AttributeList::AT_NSConsumesSelf: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4395 | handleNSConsumesSelfAttr(S, D, Attr); break; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4396 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4397 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4398 | case AttributeList::AT_NSReturnsNotRetained: |
| 4399 | case AttributeList::AT_CFReturnsNotRetained: |
| 4400 | case AttributeList::AT_NSReturnsRetained: |
| 4401 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4402 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4403 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4404 | case AttributeList::AT_WorkGroupSizeHint: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4405 | case AttributeList::AT_ReqdWorkGroupSize: |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4406 | handleWorkGroupSize(S, D, Attr); break; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 4407 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4408 | case AttributeList::AT_InitPriority: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4409 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4410 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4411 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4412 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4413 | case AttributeList::AT_Unavailable: |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4414 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable"); |
| 4415 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4416 | case AttributeList::AT_ArcWeakrefUnavailable: |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 4417 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 4418 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4419 | case AttributeList::AT_ObjCRootClass: |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4420 | handleObjCRootClassAttr(S, D, Attr); |
| 4421 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4422 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 4423 | handleObjCRequiresPropertyDefsAttr (S, D, Attr); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 4424 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4425 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4426 | case AttributeList::AT_ReturnsTwice: |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 4427 | handleReturnsTwiceAttr(S, D, Attr); |
| 4428 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4429 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
| 4430 | case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break; |
| 4431 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4432 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4433 | case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; |
| 4434 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4435 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4436 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4437 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4438 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4439 | case AttributeList::AT_ObjCException: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4440 | handleObjCExceptionAttr(S, D, Attr); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 4441 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4442 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4443 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4444 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4445 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4446 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4447 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
| 4448 | case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break; |
| 4449 | case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break; |
| 4450 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4451 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
| 4452 | case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break; |
| 4453 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4454 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4455 | // Just ignore |
| 4456 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4457 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4458 | handleNoInstrumentFunctionAttr(S, D, Attr); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 4459 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4460 | case AttributeList::AT_StdCall: |
| 4461 | case AttributeList::AT_CDecl: |
| 4462 | case AttributeList::AT_FastCall: |
| 4463 | case AttributeList::AT_ThisCall: |
| 4464 | case AttributeList::AT_Pascal: |
| 4465 | case AttributeList::AT_Pcs: |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4466 | case AttributeList::AT_PnaclCall: |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4467 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4468 | handleCallConvAttr(S, D, Attr); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4469 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4470 | case AttributeList::AT_OpenCLKernel: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4471 | handleOpenCLKernelAttr(S, D, Attr); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4472 | break; |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4473 | |
| 4474 | // Microsoft attributes: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4475 | case AttributeList::AT_MsStruct: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4476 | handleMsStructAttr(S, D, Attr); |
| 4477 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4478 | case AttributeList::AT_Uuid: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4479 | handleUuidAttr(S, D, Attr); |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4480 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4481 | case AttributeList::AT_SingleInheritance: |
| 4482 | case AttributeList::AT_MultipleInheritance: |
| 4483 | case AttributeList::AT_VirtualInheritance: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4484 | handleInheritanceAttr(S, D, Attr); |
| 4485 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4486 | case AttributeList::AT_Win64: |
| 4487 | case AttributeList::AT_Ptr32: |
| 4488 | case AttributeList::AT_Ptr64: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4489 | handlePortabilityAttr(S, D, Attr); |
| 4490 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4491 | case AttributeList::AT_ForceInline: |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4492 | handleForceInlineAttr(S, D, Attr); |
| 4493 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4494 | |
| 4495 | // Thread safety attributes: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4496 | case AttributeList::AT_GuardedVar: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4497 | handleGuardedVarAttr(S, D, Attr); |
| 4498 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4499 | case AttributeList::AT_PtGuardedVar: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4500 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4501 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4502 | case AttributeList::AT_ScopedLockable: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4503 | handleScopedLockableAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4504 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4505 | case AttributeList::AT_NoAddressSafetyAnalysis: |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4506 | handleNoAddressSafetyAttr(S, D, Attr); |
| 4507 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4508 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4509 | handleNoThreadSafetyAttr(S, D, Attr); |
| 4510 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4511 | case AttributeList::AT_Lockable: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4512 | handleLockableAttr(S, D, Attr); |
| 4513 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4514 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4515 | handleGuardedByAttr(S, D, Attr); |
| 4516 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4517 | case AttributeList::AT_PtGuardedBy: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4518 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4519 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4520 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4521 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4522 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4523 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4524 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4525 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4526 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4527 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4528 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4529 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4530 | handleLockReturnedAttr(S, D, Attr); |
| 4531 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4532 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4533 | handleLocksExcludedAttr(S, D, Attr); |
| 4534 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4535 | case AttributeList::AT_SharedLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4536 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4537 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4538 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4539 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4540 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4541 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4542 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4543 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4544 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4545 | handleUnlockFunAttr(S, D, Attr); |
| 4546 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4547 | case AttributeList::AT_AcquiredBefore: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4548 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4549 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4550 | case AttributeList::AT_AcquiredAfter: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4551 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4552 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4553 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4554 | // Type safety attributes. |
| 4555 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4556 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4557 | break; |
| 4558 | case AttributeList::AT_TypeTagForDatatype: |
| 4559 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4560 | break; |
| 4561 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4562 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4563 | // Ask target about the attribute. |
| 4564 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 4565 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4566 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 4567 | diag::warn_unhandled_ms_attribute_ignored : |
| 4568 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4569 | break; |
| 4570 | } |
| 4571 | } |
| 4572 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4573 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4574 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 4575 | /// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to |
| 4576 | /// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4). |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4577 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4578 | const AttributeList &Attr, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4579 | bool NonInheritable, bool Inheritable) { |
| 4580 | if (Attr.isInvalid()) |
| 4581 | return; |
| 4582 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4583 | // Type attributes are still treated as declaration attributes by |
| 4584 | // ParseMicrosoftTypeAttributes and ParseBorlandTypeAttributes. We don't |
| 4585 | // want to process them, however, because we will simply warn about ignoring |
| 4586 | // them. So instead, we will bail out early. |
| 4587 | if (Attr.isMSTypespecAttribute()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4588 | return; |
| 4589 | |
| 4590 | if (NonInheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4591 | ProcessNonInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4592 | |
| 4593 | if (Inheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4594 | ProcessInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4595 | } |
| 4596 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4597 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4598 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4599 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4600 | const AttributeList *AttrList, |
| 4601 | bool NonInheritable, bool Inheritable) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4602 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 4603 | ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable); |
Rafael Espindola | 9b79fc9 | 2012-05-07 23:58:18 +0000 | [diff] [blame] | 4604 | } |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4605 | |
| 4606 | // GCC accepts |
| 4607 | // static int a9 __attribute__((weakref)); |
| 4608 | // but that looks really pointless. We reject it. |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4609 | if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4610 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4611 | dyn_cast<NamedDecl>(D)->getNameAsString(); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4612 | return; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4613 | } |
| 4614 | } |
| 4615 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4616 | // Annotation attributes are the only attributes allowed after an access |
| 4617 | // specifier. |
| 4618 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4619 | const AttributeList *AttrList) { |
| 4620 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4621 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4622 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4623 | } else { |
| 4624 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4625 | return true; |
| 4626 | } |
| 4627 | } |
| 4628 | |
| 4629 | return false; |
| 4630 | } |
| 4631 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4632 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 4633 | /// contains any decl attributes that we should warn about. |
| 4634 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 4635 | for ( ; A; A = A->getNext()) { |
| 4636 | // Only warn if the attribute is an unignored, non-type attribute. |
| 4637 | if (A->isUsedAsTypeAttr()) continue; |
| 4638 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 4639 | |
| 4640 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 4641 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 4642 | << A->getName() << A->getRange(); |
| 4643 | } else { |
| 4644 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 4645 | << A->getName() << A->getRange(); |
| 4646 | } |
| 4647 | } |
| 4648 | } |
| 4649 | |
| 4650 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 4651 | /// used to build a declaration, complain about any decl attributes |
| 4652 | /// which might be lying around on it. |
| 4653 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 4654 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 4655 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 4656 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 4657 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 4658 | } |
| 4659 | |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4660 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4661 | /// \#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] | 4662 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 4663 | SourceLocation Loc) { |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4664 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4665 | NamedDecl *NewD = 0; |
| 4666 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4667 | FunctionDecl *NewFD; |
| 4668 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 4669 | // FIXME: Mangling? |
| 4670 | // FIXME: Is the qualifier info correct? |
| 4671 | // FIXME: Is the DeclContext correct? |
| 4672 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 4673 | Loc, Loc, DeclarationName(II), |
| 4674 | FD->getType(), FD->getTypeSourceInfo(), |
| 4675 | SC_None, SC_None, |
| 4676 | false/*isInlineSpecified*/, |
| 4677 | FD->hasPrototype(), |
| 4678 | false/*isConstexprSpecified*/); |
| 4679 | NewD = NewFD; |
| 4680 | |
| 4681 | if (FD->getQualifier()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4682 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4683 | |
| 4684 | // Fake up parameter variables; they are declared as if this were |
| 4685 | // a typedef. |
| 4686 | QualType FDTy = FD->getType(); |
| 4687 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 4688 | SmallVector<ParmVarDecl*, 16> Params; |
| 4689 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 4690 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 4691 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 4692 | Param->setScopeInfo(0, Params.size()); |
| 4693 | Params.push_back(Param); |
| 4694 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 4695 | NewFD->setParams(Params); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4696 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4697 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 4698 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4699 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4700 | VD->getType(), VD->getTypeSourceInfo(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 4701 | VD->getStorageClass(), |
| 4702 | VD->getStorageClassAsWritten()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4703 | if (VD->getQualifier()) { |
| 4704 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4705 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4706 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4707 | } |
| 4708 | return NewD; |
| 4709 | } |
| 4710 | |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4711 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4712 | /// applied to it, possibly with an alias. |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4713 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4714 | if (W.getUsed()) return; // only do this once |
| 4715 | W.setUsed(true); |
| 4716 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 4717 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4718 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4719 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 4720 | NDId->getName())); |
| 4721 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4722 | WeakTopLevelDecl.push_back(NewD); |
| 4723 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 4724 | // to insert Decl at TU scope, sorry. |
| 4725 | DeclContext *SavedContext = CurContext; |
| 4726 | CurContext = Context.getTranslationUnitDecl(); |
| 4727 | PushOnScopeChains(NewD, S); |
| 4728 | CurContext = SavedContext; |
| 4729 | } else { // just add weak to existing |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4730 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4731 | } |
| 4732 | } |
| 4733 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4734 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 4735 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 4736 | /// 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] | 4737 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD, |
| 4738 | bool NonInheritable, bool Inheritable) { |
John McCall | d4aff0e | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 4739 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 4740 | // have to do this. |
Douglas Gregor | 31e37b2 | 2011-07-28 18:09:57 +0000 | [diff] [blame] | 4741 | if (Inheritable) { |
| 4742 | LoadExternalWeakUndeclaredIdentifiers(); |
| 4743 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 4744 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
| 4745 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 4746 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 4747 | = WeakUndeclaredIdentifiers.find(Id); |
| 4748 | if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) { |
| 4749 | WeakInfo W = I->second; |
| 4750 | DeclApplyPragmaWeak(S, ND, W); |
| 4751 | WeakUndeclaredIdentifiers[Id] = W; |
| 4752 | } |
John McCall | d4aff0e | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 4753 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4754 | } |
| 4755 | } |
| 4756 | } |
| 4757 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4758 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4759 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4760 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4761 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4762 | // Walk the declarator structure, applying decl attributes that were in a type |
| 4763 | // position to the decl itself. This handles cases like: |
| 4764 | // int *__attr__(x)** D; |
| 4765 | // when X is a decl attribute. |
| 4766 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 4767 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4768 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4769 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4770 | // Finally, apply any attributes on the decl itself. |
| 4771 | if (const AttributeList *Attrs = PD.getAttributes()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4772 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4773 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4774 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4775 | /// Is the given declaration allowed to use a forbidden type? |
| 4776 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 4777 | // Private ivars are always okay. Unfortunately, people don't |
| 4778 | // always properly make their ivars private, even in system headers. |
| 4779 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | a6b3380 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 4780 | // Function declarations in sys headers will be marked unavailable. |
| 4781 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 4782 | !isa<FunctionDecl>(decl)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4783 | return false; |
| 4784 | |
| 4785 | // Require it to be declared in a system header. |
| 4786 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 4787 | } |
| 4788 | |
| 4789 | /// Handle a delayed forbidden-type diagnostic. |
| 4790 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 4791 | Decl *decl) { |
| 4792 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 4793 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 4794 | "this system declaration uses an unsupported type")); |
| 4795 | return; |
| 4796 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4797 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4798 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 4799 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4800 | // kind of forbidden type messages on unavailable functions. |
| 4801 | if (FD->hasAttr<UnavailableAttr>() && |
| 4802 | diag.getForbiddenTypeDiagnostic() == |
| 4803 | diag::err_arc_array_param_no_ownership) { |
| 4804 | diag.Triggered = true; |
| 4805 | return; |
| 4806 | } |
| 4807 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4808 | |
| 4809 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 4810 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 4811 | diag.Triggered = true; |
| 4812 | } |
| 4813 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4814 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 4815 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4816 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4817 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4818 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4819 | // When delaying diagnostics to run in the context of a parsed |
| 4820 | // declaration, we only want to actually emit anything if parsing |
| 4821 | // succeeds. |
| 4822 | if (!decl) return; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4823 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4824 | // We emit all the active diagnostics in this pool or any of its |
| 4825 | // parents. In general, we'll get one pool for the decl spec |
| 4826 | // and a child pool for each declarator; in a decl group like: |
| 4827 | // deprecated_typedef foo, *bar, baz(); |
| 4828 | // only the declarator pops will be passed decls. This is correct; |
| 4829 | // we really do need to consider delayed diagnostics from the decl spec |
| 4830 | // for each of the different declarations. |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4831 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4832 | do { |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4833 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4834 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 4835 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 4836 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4837 | if (diag.Triggered) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4838 | continue; |
| 4839 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4840 | switch (diag.Kind) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4841 | case DelayedDiagnostic::Deprecation: |
John McCall | e8c904f | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 4842 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 4843 | if (!decl->isInvalidDecl()) |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4844 | HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4845 | break; |
| 4846 | |
| 4847 | case DelayedDiagnostic::Access: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4848 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4849 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4850 | |
| 4851 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4852 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4853 | break; |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4854 | } |
| 4855 | } |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4856 | } while ((pool = pool->getParent())); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4857 | } |
| 4858 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4859 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 4860 | /// been delayed in the current context instead of in the given pool. |
| 4861 | /// Essentially, this just moves them to the current pool. |
| 4862 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 4863 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 4864 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 4865 | curPool->steal(pool); |
| 4866 | } |
| 4867 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4868 | static bool isDeclDeprecated(Decl *D) { |
| 4869 | do { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 4870 | if (D->isDeprecated()) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4871 | return true; |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 4872 | // A category implicitly has the availability of the interface. |
| 4873 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4874 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4875 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4876 | return false; |
| 4877 | } |
| 4878 | |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4879 | static void |
| 4880 | DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message, |
| 4881 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4882 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 4883 | const ObjCPropertyDecl *ObjCPropery) { |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4884 | DeclarationName Name = D->getDeclName(); |
| 4885 | if (!Message.empty()) { |
| 4886 | S.Diag(Loc, diag::warn_deprecated_message) << Name << Message; |
| 4887 | S.Diag(D->getLocation(), |
| 4888 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 4889 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4890 | if (ObjCPropery) |
| 4891 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 4892 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4893 | } else if (!UnknownObjCClass) { |
| 4894 | S.Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 4895 | S.Diag(D->getLocation(), |
| 4896 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 4897 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4898 | if (ObjCPropery) |
| 4899 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 4900 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4901 | } else { |
| 4902 | S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name; |
| 4903 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 4904 | } |
| 4905 | } |
| 4906 | |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 4907 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4908 | Decl *Ctx) { |
| 4909 | if (isDeclDeprecated(Ctx)) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4910 | return; |
| 4911 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4912 | DD.Triggered = true; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4913 | DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(), |
| 4914 | DD.getDeprecationMessage(), DD.Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4915 | DD.getUnknownObjCClass(), |
| 4916 | DD.getObjCProperty()); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4917 | } |
| 4918 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4919 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 4920 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4921 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 4922 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4923 | // Delay if we're currently parsing a declaration. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4924 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 4925 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 4926 | UnknownObjCClass, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4927 | ObjCProperty, |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 4928 | Message)); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4929 | return; |
| 4930 | } |
| 4931 | |
| 4932 | // Otherwise, don't warn if our current context is deprecated. |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 4933 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4934 | return; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4935 | DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4936 | } |