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" |
Rafael Espindola | d7a60ad | 2013-02-26 19:13:56 +0000 | [diff] [blame] | 22 | #include "clang/AST/Mangle.h" |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 23 | #include "clang/Basic/CharInfo.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 26 | #include "clang/Sema/DeclSpec.h" |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 27 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 28 | #include "clang/Sema/Lookup.h" |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 29 | #include "clang/Sema/Scope.h" |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 31 | using namespace clang; |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 32 | using namespace sema; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 33 | |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 34 | /// These constants match the enumerated choices of |
| 35 | /// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 36 | enum AttributeDeclKind { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 37 | ExpectedFunction, |
| 38 | ExpectedUnion, |
| 39 | ExpectedVariableOrFunction, |
| 40 | ExpectedFunctionOrMethod, |
| 41 | ExpectedParameter, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 42 | ExpectedFunctionMethodOrBlock, |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 43 | ExpectedFunctionMethodOrClass, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 44 | ExpectedFunctionMethodOrParameter, |
| 45 | ExpectedClass, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 46 | ExpectedVariable, |
| 47 | ExpectedMethod, |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 48 | ExpectedVariableFunctionOrLabel, |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 49 | ExpectedFieldOrGlobalVar, |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 50 | ExpectedStruct, |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 51 | ExpectedVariableFunctionOrTag, |
Richard Smith | 5f838aa | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 52 | ExpectedTLSVar, |
| 53 | ExpectedVariableOrField, |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 54 | ExpectedVariableFieldOrTag, |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 55 | ExpectedTypeOrNamespace, |
| 56 | ExpectedObjectiveCInterface |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 59 | /// These constants match the enumerated choices of |
| 60 | /// err_attribute_argument_n_type. |
| 61 | enum AttributeArgumentNType { |
| 62 | ArgumentIntOrBool, |
| 63 | ArgumentIntegerConstant, |
| 64 | ArgumentString, |
| 65 | ArgumentIdentifier |
| 66 | }; |
| 67 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 68 | //===----------------------------------------------------------------------===// |
| 69 | // Helper functions |
| 70 | //===----------------------------------------------------------------------===// |
| 71 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 72 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 73 | bool blocksToo = true) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 74 | QualType Ty; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 75 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 76 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 77 | else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 78 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 79 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 80 | Ty = decl->getUnderlyingType(); |
| 81 | else |
| 82 | return 0; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 83 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 84 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 85 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 755f9d2 | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 86 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 87 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 88 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 89 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 92 | // FIXME: We should provide an abstraction around a method or function |
| 93 | // to provide the following bits of information. |
| 94 | |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 95 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 96 | /// type (function or function-typed variable). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 97 | static bool isFunction(const Decl *D) { |
| 98 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 102 | /// type (function or function-typed variable) or an Objective-C |
| 103 | /// method. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 104 | static bool isFunctionOrMethod(const Decl *D) { |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 105 | return isFunction(D) || isa<ObjCMethodDecl>(D); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 108 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 109 | /// type (function or function-typed variable) or an Objective-C |
| 110 | /// method or a block. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 111 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 112 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 113 | return true; |
| 114 | // check for block is more involved. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 115 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 116 | QualType Ty = V->getType(); |
| 117 | return Ty->isBlockPointerType(); |
| 118 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 119 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 120 | } |
| 121 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 122 | /// Return true if the given decl has a declarator that should have |
| 123 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 124 | static bool hasDeclarator(const Decl *D) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 125 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 126 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 127 | isa<ObjCPropertyDecl>(D); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 130 | /// hasFunctionProto - Return true if the given decl has a argument |
| 131 | /// information. This decl should have already passed |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 132 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 133 | static bool hasFunctionProto(const Decl *D) { |
| 134 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 135 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 136 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 137 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 138 | return true; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 143 | /// arguments. It is an error to call this on a K&R function (use |
| 144 | /// hasFunctionProto first). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 145 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 146 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 147 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 148 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 149 | return BD->getNumParams(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 150 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 153 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 154 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 155 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 156 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 157 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 158 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 159 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 162 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 163 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 164 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 165 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 168 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 169 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 170 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 171 | return proto->isVariadic(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 172 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | db9a0ae | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 173 | return BD->isVariadic(); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 174 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 175 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 179 | static bool isInstanceMethod(const Decl *D) { |
| 180 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 181 | return MethodDecl->isInstance(); |
| 182 | return false; |
| 183 | } |
| 184 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 185 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 186 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 187 | if (!PT) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 188 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 189 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 190 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 191 | if (!Cls) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 192 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 193 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 194 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 195 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 196 | // FIXME: Should we walk the chain of classes? |
| 197 | return ClsName == &Ctx.Idents.get("NSString") || |
| 198 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 199 | } |
| 200 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 201 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 202 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 203 | if (!PT) |
| 204 | return false; |
| 205 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 206 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 207 | if (!RT) |
| 208 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 209 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 210 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 211 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 212 | return false; |
| 213 | |
| 214 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 215 | } |
| 216 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 217 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 218 | /// output an error. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 219 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 220 | unsigned int Num) { |
| 221 | if (Attr.getNumArgs() != Num) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 222 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 223 | << Attr.getName() << Num; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 224 | return false; |
| 225 | } |
| 226 | |
| 227 | return true; |
| 228 | } |
| 229 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 230 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 231 | /// \brief Check if the attribute has at least as many args as Num. May |
| 232 | /// output an error. |
| 233 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
| 234 | unsigned int Num) { |
| 235 | if (Attr.getNumArgs() < Num) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 236 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| 237 | return false; |
| 238 | } |
| 239 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 240 | return true; |
| 241 | } |
| 242 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 243 | /// \brief Check if IdxExpr is a valid argument index for a function or |
| 244 | /// instance method D. May output an error. |
| 245 | /// |
| 246 | /// \returns true if IdxExpr is a valid index. |
| 247 | static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D, |
| 248 | StringRef AttrName, |
| 249 | SourceLocation AttrLoc, |
| 250 | unsigned AttrArgNum, |
| 251 | const Expr *IdxExpr, |
| 252 | uint64_t &Idx) |
| 253 | { |
| 254 | assert(isFunctionOrMethod(D) && hasFunctionProto(D)); |
| 255 | |
| 256 | // In C++ the implicit 'this' function parameter also counts. |
| 257 | // Parameters are counted from one. |
| 258 | const bool HasImplicitThisParam = isInstanceMethod(D); |
| 259 | const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| 260 | const unsigned FirstIdx = 1; |
| 261 | |
| 262 | llvm::APSInt IdxInt; |
| 263 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 264 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 265 | std::string Name = std::string("'") + AttrName.str() + std::string("'"); |
| 266 | S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str() |
| 267 | << AttrArgNum << ArgumentIntegerConstant << IdxExpr->getSourceRange(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 268 | return false; |
| 269 | } |
| 270 | |
| 271 | Idx = IdxInt.getLimitedValue(); |
| 272 | if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) { |
| 273 | S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds) |
| 274 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 275 | return false; |
| 276 | } |
| 277 | Idx--; // Convert to zero-based. |
| 278 | if (HasImplicitThisParam) { |
| 279 | if (Idx == 0) { |
| 280 | S.Diag(AttrLoc, |
| 281 | diag::err_attribute_invalid_implicit_this_argument) |
| 282 | << AttrName << IdxExpr->getSourceRange(); |
| 283 | return false; |
| 284 | } |
| 285 | --Idx; |
| 286 | } |
| 287 | |
| 288 | return true; |
| 289 | } |
| 290 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 291 | /// |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 292 | /// \brief Check if passed in Decl is a field or potentially shared global var |
| 293 | /// \return true if the Decl is a field or potentially shared global variable |
| 294 | /// |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 295 | static bool mayBeSharedVariable(const Decl *D) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 296 | if (isa<FieldDecl>(D)) |
| 297 | return true; |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 298 | if (const VarDecl *vd = dyn_cast<VarDecl>(D)) |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 299 | return vd->hasGlobalStorage() && !vd->getTLSKind(); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 300 | |
| 301 | return false; |
| 302 | } |
| 303 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 304 | /// \brief Check if the passed-in expression is of type int or bool. |
| 305 | static bool isIntOrBool(Expr *Exp) { |
| 306 | QualType QT = Exp->getType(); |
| 307 | return QT->isBooleanType() || QT->isIntegerType(); |
| 308 | } |
| 309 | |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 310 | |
| 311 | // Check to see if the type is a smart pointer of some kind. We assume |
| 312 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 313 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 314 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 315 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 316 | if (Res1.empty()) |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 317 | return false; |
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 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 320 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 321 | if (Res2.empty()) |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 322 | return false; |
| 323 | |
| 324 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 327 | /// \brief Check if passed in Decl is a pointer type. |
| 328 | /// Note that this function may produce an error message. |
| 329 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 330 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 331 | const AttributeList &Attr) { |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 332 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 333 | QualType QT = vd->getType(); |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 334 | if (QT->isAnyPointerType()) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 335 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 336 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 337 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 338 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 339 | // (We don't want to force template instantiation if we can avoid it, |
| 340 | // since that would alter the order in which templates are instantiated.) |
| 341 | if (RT->isIncompleteType()) |
| 342 | return true; |
| 343 | |
| 344 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 345 | return true; |
| 346 | } |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 347 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 348 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 349 | << Attr.getName()->getName() << QT; |
| 350 | } else { |
| 351 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 352 | << Attr.getName(); |
| 353 | } |
| 354 | return false; |
| 355 | } |
| 356 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 357 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 358 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 359 | static const RecordType *getRecordType(QualType QT) { |
| 360 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 361 | return RT; |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 362 | |
| 363 | // Now check if we point to record type. |
| 364 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 365 | return PT->getPointeeType()->getAs<RecordType>(); |
| 366 | |
| 367 | return 0; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 368 | } |
| 369 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 370 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 371 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 372 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 373 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
| 374 | if (RT->getDecl()->getAttr<LockableAttr>()) |
| 375 | return true; |
| 376 | return false; |
| 377 | } |
| 378 | |
| 379 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 380 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 381 | /// resolves to a lockable object. |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 382 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 383 | QualType Ty) { |
| 384 | const RecordType *RT = getRecordType(Ty); |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 385 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 386 | // Warn if could not get record type for this argument. |
Benjamin Kramer | d77ba89 | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 387 | if (!RT) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 388 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 389 | << Attr.getName() << Ty.getAsString(); |
| 390 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 391 | } |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 392 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 393 | // 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] | 394 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 395 | return; |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 396 | |
| 397 | // Allow smart pointers to be used as lockable objects. |
| 398 | // FIXME -- Check the type that the smart pointer points to. |
| 399 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 400 | return; |
| 401 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 402 | // Check if the type is lockable. |
| 403 | RecordDecl *RD = RT->getDecl(); |
| 404 | if (RD->getAttr<LockableAttr>()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 405 | return; |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 406 | |
| 407 | // Else check if any base classes are lockable. |
| 408 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 409 | CXXBasePaths BPaths(false, false); |
| 410 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 411 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 412 | } |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 413 | |
| 414 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 415 | << Attr.getName() << Ty.getAsString(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 418 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 419 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 420 | /// \param Sidx The attribute argument index to start checking with. |
| 421 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 422 | /// parameter list. |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 423 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 424 | const AttributeList &Attr, |
| 425 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 426 | int Sidx = 0, |
| 427 | bool ParamIdxOk = false) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 428 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 429 | Expr *ArgExp = Attr.getArg(Idx); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 430 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 431 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 432 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 433 | Args.push_back(ArgExp); |
| 434 | continue; |
| 435 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 436 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 437 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 438 | if (StrLit->getLength() == 0 || |
| 439 | StrLit->getString() == StringRef("*")) { |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 440 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 441 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 442 | Args.push_back(ArgExp); |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 443 | continue; |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 444 | } |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 445 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 446 | // We allow constant strings to be used as a placeholder for expressions |
| 447 | // that are not valid C++ syntax, but warn that they are ignored. |
| 448 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 449 | Attr.getName(); |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 450 | Args.push_back(ArgExp); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 451 | continue; |
| 452 | } |
| 453 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 454 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 455 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 456 | // A pointer to member expression of the form &MyClass::mu is treated |
| 457 | // specially -- we need to look at the type of the member. |
| 458 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 459 | if (UOp->getOpcode() == UO_AddrOf) |
| 460 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 461 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 462 | ArgTy = DRE->getDecl()->getType(); |
| 463 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 464 | // First see if we can just cast to record type, or point to record type. |
| 465 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 466 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 467 | // Now check if we index into a record type function param. |
| 468 | if(!RT && ParamIdxOk) { |
| 469 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 470 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 471 | if(FD && IL) { |
| 472 | unsigned int NumParams = FD->getNumParams(); |
| 473 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 474 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 475 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 476 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 477 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 478 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 479 | continue; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 480 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 481 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 485 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 486 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 487 | Args.push_back(ArgExp); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 488 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 491 | //===----------------------------------------------------------------------===// |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 492 | // Attribute Implementations |
| 493 | //===----------------------------------------------------------------------===// |
| 494 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 495 | // FIXME: All this manual attribute parsing code is gross. At the |
| 496 | // least add some helper functions to check most argument patterns (# |
| 497 | // and types of args). |
| 498 | |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 499 | enum ThreadAttributeDeclKind { |
| 500 | ThreadExpectedFieldOrGlobalVar, |
| 501 | ThreadExpectedFunctionOrMethod, |
| 502 | ThreadExpectedClassOrStruct |
| 503 | }; |
| 504 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 505 | static bool checkGuardedVarAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 506 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 507 | assert(!Attr.isInvalid()); |
| 508 | |
| 509 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 510 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 511 | |
| 512 | // D must be either a member field or global (potentially shared) variable. |
| 513 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 514 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 515 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 516 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 519 | return true; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 522 | static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 523 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 524 | return; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 525 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 526 | D->addAttr(::new (S.Context) |
| 527 | GuardedVarAttr(Attr.getRange(), S.Context, |
| 528 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 531 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 532 | const AttributeList &Attr) { |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 533 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 534 | return; |
| 535 | |
| 536 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 537 | return; |
| 538 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 539 | D->addAttr(::new (S.Context) |
| 540 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 541 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 544 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 545 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 546 | Expr* &Arg) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 547 | assert(!Attr.isInvalid()); |
| 548 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 549 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 550 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 551 | |
| 552 | // D must be either a member field or global (potentially shared) variable. |
| 553 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 554 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 555 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 556 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 557 | } |
| 558 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 559 | SmallVector<Expr*, 1> Args; |
| 560 | // check that all arguments are lockable objects |
| 561 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 562 | unsigned Size = Args.size(); |
| 563 | if (Size != 1) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 564 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 565 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 566 | Arg = Args[0]; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 567 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 568 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 571 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 572 | Expr *Arg = 0; |
| 573 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 574 | return; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 575 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 576 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| 577 | } |
| 578 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 579 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 580 | const AttributeList &Attr) { |
| 581 | Expr *Arg = 0; |
| 582 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 583 | return; |
| 584 | |
| 585 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 586 | return; |
| 587 | |
| 588 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| 589 | S.Context, Arg)); |
| 590 | } |
| 591 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 592 | static bool checkLockableAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 593 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 594 | assert(!Attr.isInvalid()); |
| 595 | |
| 596 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 597 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 598 | |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 599 | // FIXME: Lockable structs for C code. |
David Blaikie | 88c4b5e | 2013-07-29 18:24:03 +0000 | [diff] [blame^] | 600 | if (!isa<RecordDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 601 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 602 | << Attr.getName() << ThreadExpectedClassOrStruct; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 603 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 606 | return true; |
| 607 | } |
| 608 | |
| 609 | static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 610 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 611 | return; |
| 612 | |
| 613 | D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); |
| 614 | } |
| 615 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 616 | static void handleScopedLockableAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 617 | const AttributeList &Attr) { |
| 618 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 619 | return; |
| 620 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 621 | D->addAttr(::new (S.Context) |
| 622 | ScopedLockableAttr(Attr.getRange(), S.Context, |
| 623 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 626 | static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D, |
| 627 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 628 | assert(!Attr.isInvalid()); |
| 629 | |
| 630 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 631 | return; |
| 632 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 633 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 634 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 635 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 636 | return; |
| 637 | } |
| 638 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 639 | D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(), |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 640 | S.Context)); |
| 641 | } |
| 642 | |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 643 | static void handleNoSanitizeAddressAttr(Sema &S, Decl *D, |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 644 | const AttributeList &Attr) { |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 645 | assert(!Attr.isInvalid()); |
| 646 | |
| 647 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 648 | return; |
| 649 | |
| 650 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 651 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 652 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 653 | return; |
| 654 | } |
| 655 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 656 | D->addAttr(::new (S.Context) |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 657 | NoSanitizeAddressAttr(Attr.getRange(), S.Context, |
| 658 | Attr.getAttributeSpellingListIndex())); |
| 659 | } |
| 660 | |
| 661 | static void handleNoSanitizeMemory(Sema &S, Decl *D, |
| 662 | const AttributeList &Attr) { |
| 663 | assert(!Attr.isInvalid()); |
| 664 | |
| 665 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 666 | return; |
| 667 | |
| 668 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 669 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 670 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 671 | return; |
| 672 | } |
| 673 | |
| 674 | D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(), |
| 675 | S.Context)); |
| 676 | } |
| 677 | |
| 678 | static void handleNoSanitizeThread(Sema &S, Decl *D, |
| 679 | const AttributeList &Attr) { |
| 680 | assert(!Attr.isInvalid()); |
| 681 | |
| 682 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 683 | return; |
| 684 | |
| 685 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 686 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 687 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 688 | return; |
| 689 | } |
| 690 | |
| 691 | D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(), |
| 692 | S.Context)); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 695 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 696 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 697 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 698 | assert(!Attr.isInvalid()); |
| 699 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 700 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 701 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 702 | |
| 703 | // D must be either a member field or global (potentially shared) variable. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 704 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 705 | if (!VD || !mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 706 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 707 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 708 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 709 | } |
| 710 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 711 | // Check that this attribute only applies to lockable types. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 712 | QualType QT = VD->getType(); |
| 713 | if (!QT->isDependentType()) { |
| 714 | const RecordType *RT = getRecordType(QT); |
| 715 | if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 716 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 717 | << Attr.getName(); |
| 718 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 722 | // Check that all arguments are lockable objects. |
| 723 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 724 | if (Args.empty()) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 725 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 726 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 727 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 730 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 731 | const AttributeList &Attr) { |
| 732 | SmallVector<Expr*, 1> Args; |
| 733 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 734 | return; |
| 735 | |
| 736 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 737 | D->addAttr(::new (S.Context) |
| 738 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 739 | StartArg, Args.size(), |
| 740 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 743 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 744 | const AttributeList &Attr) { |
| 745 | SmallVector<Expr*, 1> Args; |
| 746 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 747 | return; |
| 748 | |
| 749 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 750 | D->addAttr(::new (S.Context) |
| 751 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 752 | StartArg, Args.size(), |
| 753 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 756 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 757 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 758 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 759 | assert(!Attr.isInvalid()); |
| 760 | |
| 761 | // zero or more arguments ok |
| 762 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 763 | // check that the attribute is applied to a function |
| 764 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 765 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 766 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 767 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 770 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 771 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 772 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 773 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 776 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 777 | const AttributeList &Attr) { |
| 778 | SmallVector<Expr*, 1> Args; |
| 779 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 780 | return; |
| 781 | |
| 782 | unsigned Size = Args.size(); |
| 783 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 784 | D->addAttr(::new (S.Context) |
| 785 | SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 786 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 789 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 790 | const AttributeList &Attr) { |
| 791 | SmallVector<Expr*, 1> Args; |
| 792 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 793 | return; |
| 794 | |
| 795 | unsigned Size = Args.size(); |
| 796 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 797 | D->addAttr(::new (S.Context) |
| 798 | ExclusiveLockFunctionAttr(Attr.getRange(), S.Context, |
| 799 | StartArg, Size, |
| 800 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 801 | } |
| 802 | |
DeLesley Hutchins | 5c6134f | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 803 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 804 | const AttributeList &Attr) { |
| 805 | SmallVector<Expr*, 1> Args; |
| 806 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 807 | return; |
| 808 | |
| 809 | unsigned Size = Args.size(); |
| 810 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 811 | D->addAttr(::new (S.Context) |
| 812 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 813 | Attr.getAttributeSpellingListIndex())); |
| 814 | } |
| 815 | |
| 816 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 817 | const AttributeList &Attr) { |
| 818 | SmallVector<Expr*, 1> Args; |
| 819 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 820 | return; |
| 821 | |
| 822 | unsigned Size = Args.size(); |
| 823 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 824 | D->addAttr(::new (S.Context) |
| 825 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 826 | StartArg, Size, |
| 827 | Attr.getAttributeSpellingListIndex())); |
| 828 | } |
| 829 | |
| 830 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 831 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 832 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 833 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 834 | assert(!Attr.isInvalid()); |
| 835 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 836 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 837 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 838 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 839 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 840 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 841 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 842 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 845 | if (!isIntOrBool(Attr.getArg(0))) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 846 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 847 | << Attr.getName() << 1 << ArgumentIntOrBool; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 848 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 852 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 853 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 854 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 857 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 858 | const AttributeList &Attr) { |
| 859 | SmallVector<Expr*, 2> Args; |
| 860 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 861 | return; |
| 862 | |
| 863 | unsigned Size = Args.size(); |
| 864 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 865 | D->addAttr(::new (S.Context) |
| 866 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
| 867 | Attr.getArg(0), StartArg, Size, |
| 868 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 871 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 872 | const AttributeList &Attr) { |
| 873 | SmallVector<Expr*, 2> Args; |
| 874 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 875 | return; |
| 876 | |
| 877 | unsigned Size = Args.size(); |
| 878 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 879 | D->addAttr(::new (S.Context) |
| 880 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
| 881 | Attr.getArg(0), StartArg, Size, |
| 882 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 885 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 886 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 887 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 888 | assert(!Attr.isInvalid()); |
| 889 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 890 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 891 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 892 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 893 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 894 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 895 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 896 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 899 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 900 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 901 | if (Args.empty()) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 902 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 903 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 904 | return true; |
| 905 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 906 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 907 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 908 | const AttributeList &Attr) { |
| 909 | SmallVector<Expr*, 1> Args; |
| 910 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 911 | return; |
| 912 | |
| 913 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 914 | D->addAttr(::new (S.Context) |
| 915 | ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context, |
| 916 | StartArg, Args.size(), |
| 917 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 920 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 921 | const AttributeList &Attr) { |
| 922 | SmallVector<Expr*, 1> Args; |
| 923 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 924 | return; |
| 925 | |
| 926 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 927 | D->addAttr(::new (S.Context) |
| 928 | SharedLocksRequiredAttr(Attr.getRange(), S.Context, |
| 929 | StartArg, Args.size(), |
| 930 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 933 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 934 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 935 | assert(!Attr.isInvalid()); |
| 936 | |
| 937 | // zero or more arguments ok |
| 938 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 939 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 940 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 941 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 942 | return; |
| 943 | } |
| 944 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 945 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 946 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 947 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 948 | unsigned Size = Args.size(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 949 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 950 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 951 | D->addAttr(::new (S.Context) |
| 952 | UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 953 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 957 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 958 | assert(!Attr.isInvalid()); |
| 959 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 960 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 961 | return; |
| 962 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 963 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 964 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 965 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 966 | return; |
| 967 | } |
| 968 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 969 | // check that the argument is lockable object |
DeLesley Hutchins | f26efd7 | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 970 | SmallVector<Expr*, 1> Args; |
| 971 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 972 | unsigned Size = Args.size(); |
| 973 | if (Size == 0) |
| 974 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 975 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 976 | D->addAttr(::new (S.Context) |
| 977 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 978 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 982 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 983 | assert(!Attr.isInvalid()); |
| 984 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 985 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 986 | return; |
| 987 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 988 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 989 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 990 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 991 | return; |
| 992 | } |
| 993 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 994 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 995 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 996 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 997 | unsigned Size = Args.size(); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 998 | if (Size == 0) |
| 999 | return; |
| 1000 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1001 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1002 | D->addAttr(::new (S.Context) |
| 1003 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 1004 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1008 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1009 | const AttributeList &Attr) { |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1010 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 1011 | if (TD == 0) { |
| 1012 | // __attribute__((ext_vector_type(N))) can only be applied to typedefs |
| 1013 | // and type-ids. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1014 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1015 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1016 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1017 | |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1018 | // Remember this typedef decl, we will need it later for diagnostics. |
| 1019 | S.ExtVectorDecls.push_back(TD); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1022 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1023 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1024 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1025 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1026 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1027 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1028 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1029 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1030 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1031 | // has no effect. |
Eli Friedman | b68ec6b | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1032 | if (!FD->getType()->isDependentType() && |
| 1033 | !FD->getType()->isIncompleteType() && |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1034 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1035 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1036 | << Attr.getName() << FD->getType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1037 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1038 | FD->addAttr(::new (S.Context) |
| 1039 | PackedAttr(Attr.getRange(), S.Context, |
| 1040 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1041 | } else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1042 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1045 | static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 5f608ae | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 1046 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1047 | RD->addAttr(::new (S.Context) |
| 1048 | MsStructAttr(Attr.getRange(), S.Context, |
| 1049 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | c1a0a73 | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 1050 | else |
| 1051 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1052 | } |
| 1053 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1054 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1055 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1056 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1057 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1058 | |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1059 | // The IBAction attributes only apply to instance methods. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1060 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1061 | if (MD->isInstanceMethod()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1062 | D->addAttr(::new (S.Context) |
| 1063 | IBActionAttr(Attr.getRange(), S.Context, |
| 1064 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1065 | return; |
| 1066 | } |
| 1067 | |
Ted Kremenek | 4ee2bb1 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 1068 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1071 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1072 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1073 | // variables or properties of Objective-C classes. The outlet must also |
| 1074 | // have an object reference type. |
| 1075 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1076 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 0bfaf06 | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1077 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1078 | << Attr.getName() << VD->getType() << 0; |
| 1079 | return false; |
| 1080 | } |
| 1081 | } |
| 1082 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1083 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1084 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1085 | << Attr.getName() << PD->getType() << 1; |
| 1086 | return false; |
| 1087 | } |
| 1088 | } |
| 1089 | else { |
| 1090 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1091 | return false; |
| 1092 | } |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1093 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1094 | return true; |
| 1095 | } |
| 1096 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1097 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1098 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1099 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1100 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1101 | |
| 1102 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1103 | return; |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1104 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1105 | D->addAttr(::new (S.Context) |
| 1106 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1107 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1110 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1111 | const AttributeList &Attr) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1112 | |
| 1113 | // The iboutletcollection attribute can have zero or one arguments. |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1114 | if (Attr.getParameterName() && Attr.getNumArgs() > 0) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1115 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1116 | << Attr.getName() << 1; |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1117 | return; |
| 1118 | } |
| 1119 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1120 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1121 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1122 | |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1123 | IdentifierInfo *II = Attr.getParameterName(); |
| 1124 | if (!II) |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1125 | II = &S.Context.Idents.get("NSObject"); |
Fariborz Jahanian | 3a3400b | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 1126 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1127 | ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1128 | S.getScopeForContext(D->getDeclContext()->getParent())); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1129 | if (!TypeRep) { |
| 1130 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1131 | return; |
| 1132 | } |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1133 | QualType QT = TypeRep.get(); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1134 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1135 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1136 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1137 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1138 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1139 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1140 | return; |
| 1141 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1142 | D->addAttr(::new (S.Context) |
| 1143 | IBOutletCollectionAttr(Attr.getRange(),S.Context, |
| 1144 | QT, Attr.getParameterLoc(), |
| 1145 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1148 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1149 | if (const RecordType *UT = T->getAsUnionType()) |
| 1150 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1151 | RecordDecl *UD = UT->getDecl(); |
| 1152 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1153 | itend = UD->field_end(); it != itend; ++it) { |
| 1154 | QualType QT = it->getType(); |
| 1155 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1156 | T = QT; |
| 1157 | return; |
| 1158 | } |
| 1159 | } |
| 1160 | } |
| 1161 | } |
| 1162 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1163 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nuno Lopes | 174930d | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1164 | if (!isFunctionOrMethod(D)) { |
| 1165 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1166 | << "alloc_size" << ExpectedFunctionOrMethod; |
| 1167 | return; |
| 1168 | } |
| 1169 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1170 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1171 | return; |
| 1172 | |
| 1173 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1174 | // counted from one. |
| 1175 | bool HasImplicitThisParam = isInstanceMethod(D); |
Argyrios Kyrtzidis | 28965bf | 2013-02-22 06:58:28 +0000 | [diff] [blame] | 1176 | unsigned NumArgs; |
| 1177 | if (hasFunctionProto(D)) |
| 1178 | NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| 1179 | else |
| 1180 | NumArgs = 0; |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1181 | |
| 1182 | SmallVector<unsigned, 8> SizeArgs; |
| 1183 | |
| 1184 | for (AttributeList::arg_iterator I = Attr.arg_begin(), |
| 1185 | E = Attr.arg_end(); I!=E; ++I) { |
| 1186 | // The argument must be an integer constant expression. |
| 1187 | Expr *Ex = *I; |
| 1188 | llvm::APSInt ArgNum; |
| 1189 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 1190 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1191 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1192 | << "alloc_size" << Ex->getSourceRange(); |
| 1193 | return; |
| 1194 | } |
| 1195 | |
| 1196 | uint64_t x = ArgNum.getZExtValue(); |
| 1197 | |
| 1198 | if (x < 1 || x > NumArgs) { |
| 1199 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1200 | << "alloc_size" << I.getArgNum() << Ex->getSourceRange(); |
| 1201 | return; |
| 1202 | } |
| 1203 | |
| 1204 | --x; |
| 1205 | if (HasImplicitThisParam) { |
| 1206 | if (x == 0) { |
| 1207 | S.Diag(Attr.getLoc(), |
| 1208 | diag::err_attribute_invalid_implicit_this_argument) |
| 1209 | << "alloc_size" << Ex->getSourceRange(); |
| 1210 | return; |
| 1211 | } |
| 1212 | --x; |
| 1213 | } |
| 1214 | |
| 1215 | // check if the function argument is of an integer type |
| 1216 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
| 1217 | if (!T->isIntegerType()) { |
| 1218 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1219 | << "alloc_size" << Ex->getSourceRange(); |
| 1220 | return; |
| 1221 | } |
| 1222 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1223 | SizeArgs.push_back(x); |
| 1224 | } |
| 1225 | |
| 1226 | // check if the function returns a pointer |
| 1227 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 1228 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
| 1229 | << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
| 1230 | } |
| 1231 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1232 | D->addAttr(::new (S.Context) |
| 1233 | AllocSizeAttr(Attr.getRange(), S.Context, |
| 1234 | SizeArgs.data(), SizeArgs.size(), |
| 1235 | Attr.getAttributeSpellingListIndex())); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1238 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1239 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1240 | // ignore it as well |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1241 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1242 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1243 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1244 | return; |
| 1245 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1246 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1247 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1248 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1249 | bool HasImplicitThisParam = isInstanceMethod(D); |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1250 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1251 | |
| 1252 | // The nonnull attribute only applies to pointers. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1253 | SmallVector<unsigned, 10> NonNullArgs; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1254 | |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1255 | for (AttributeList::arg_iterator I = Attr.arg_begin(), |
| 1256 | E = Attr.arg_end(); I != E; ++I) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1257 | // The argument must be an integer constant expression. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1258 | Expr *Ex = *I; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1259 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1260 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 1261 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1262 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1263 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1264 | return; |
| 1265 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1266 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1267 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1268 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1269 | if (x < 1 || x > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1270 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 1271 | << "nonnull" << I.getArgNum() << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1272 | return; |
| 1273 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1274 | |
Ted Kremenek | 465172f | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 1275 | --x; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1276 | if (HasImplicitThisParam) { |
| 1277 | if (x == 0) { |
| 1278 | S.Diag(Attr.getLoc(), |
| 1279 | diag::err_attribute_invalid_implicit_this_argument) |
| 1280 | << "nonnull" << Ex->getSourceRange(); |
| 1281 | return; |
| 1282 | } |
| 1283 | --x; |
| 1284 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1285 | |
| 1286 | // Is the function argument a pointer type? |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1287 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1288 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1289 | |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1290 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1291 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | c9ef405 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1292 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1293 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1294 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1295 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1296 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1297 | NonNullArgs.push_back(x); |
| 1298 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1299 | |
| 1300 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1301 | // arguments have a nonnull attribute. |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1302 | if (NonNullArgs.empty()) { |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1303 | for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) { |
| 1304 | QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1305 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1306 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1307 | NonNullArgs.push_back(i); |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1308 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1309 | |
Ted Kremenek | ee1c08c | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1310 | // No pointer arguments? |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1311 | if (NonNullArgs.empty()) { |
| 1312 | // Warn the trivial case only if attribute is not coming from a |
| 1313 | // macro instantiation. |
| 1314 | if (Attr.getLoc().isFileID()) |
| 1315 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1316 | return; |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1317 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1318 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1319 | |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1320 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1321 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1322 | llvm::array_pod_sort(start, start + size); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1323 | D->addAttr(::new (S.Context) |
| 1324 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1325 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1328 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1329 | // This attribute must be applied to a function declaration. |
| 1330 | // The first argument to the attribute must be a string, |
| 1331 | // the name of the resource, for example "malloc". |
| 1332 | // The following arguments must be argument indexes, the arguments must be |
| 1333 | // of integer type for Returns, otherwise of pointer type. |
| 1334 | // 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] | 1335 | // after being held. free() should be __attribute((ownership_takes)), whereas |
| 1336 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1337 | |
| 1338 | if (!AL.getParameterName()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1339 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
| 1340 | << AL.getName()->getName() << 1 << ArgumentString; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1341 | return; |
| 1342 | } |
| 1343 | // Figure out our Kind, and check arguments while we're at it. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1344 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1345 | switch (AL.getKind()) { |
| 1346 | case AttributeList::AT_ownership_takes: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1347 | K = OwnershipAttr::Takes; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1348 | if (AL.getNumArgs() < 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1349 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1350 | << AL.getName() << 2; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1351 | return; |
| 1352 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1353 | break; |
| 1354 | case AttributeList::AT_ownership_holds: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1355 | K = OwnershipAttr::Holds; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1356 | if (AL.getNumArgs() < 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1357 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1358 | << AL.getName() << 2; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1359 | return; |
| 1360 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1361 | break; |
| 1362 | case AttributeList::AT_ownership_returns: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1363 | K = OwnershipAttr::Returns; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1364 | if (AL.getNumArgs() > 1) { |
| 1365 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1366 | << AL.getName() << AL.getNumArgs() + 1; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1367 | return; |
| 1368 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1369 | break; |
| 1370 | default: |
| 1371 | // This should never happen given how we are called. |
| 1372 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1375 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1376 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1377 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1378 | return; |
| 1379 | } |
| 1380 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1381 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1382 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1383 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1384 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1385 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1386 | StringRef Module = AL.getParameterName()->getName(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1387 | |
| 1388 | // Normalize the argument, __foo__ becomes foo. |
| 1389 | if (Module.startswith("__") && Module.endswith("__")) |
| 1390 | Module = Module.substr(2, Module.size() - 4); |
| 1391 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1392 | SmallVector<unsigned, 10> OwnershipArgs; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1393 | |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1394 | for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E; |
| 1395 | ++I) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1396 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1397 | Expr *IdxExpr = *I; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1398 | llvm::APSInt ArgNum(32); |
| 1399 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1400 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1401 | S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int) |
| 1402 | << AL.getName()->getName() << IdxExpr->getSourceRange(); |
| 1403 | continue; |
| 1404 | } |
| 1405 | |
| 1406 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
| 1407 | |
| 1408 | if (x > NumArgs || x < 1) { |
| 1409 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1410 | << AL.getName()->getName() << x << IdxExpr->getSourceRange(); |
| 1411 | continue; |
| 1412 | } |
| 1413 | --x; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1414 | if (HasImplicitThisParam) { |
| 1415 | if (x == 0) { |
| 1416 | S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 1417 | << "ownership" << IdxExpr->getSourceRange(); |
| 1418 | return; |
| 1419 | } |
| 1420 | --x; |
| 1421 | } |
| 1422 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1423 | switch (K) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1424 | case OwnershipAttr::Takes: |
| 1425 | case OwnershipAttr::Holds: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1426 | // Is the function argument a pointer type? |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1427 | QualType T = getFunctionOrMethodArgType(D, x); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1428 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| 1429 | // FIXME: Should also highlight argument in decl. |
| 1430 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1431 | << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1432 | << "pointer" |
| 1433 | << IdxExpr->getSourceRange(); |
| 1434 | continue; |
| 1435 | } |
| 1436 | break; |
| 1437 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1438 | case OwnershipAttr::Returns: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1439 | if (AL.getNumArgs() > 1) { |
| 1440 | // Is the function argument an integer type? |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1441 | Expr *IdxExpr = AL.getArg(0); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1442 | llvm::APSInt ArgNum(32); |
| 1443 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1444 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1445 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
| 1446 | << "ownership_returns" << "integer" |
| 1447 | << IdxExpr->getSourceRange(); |
| 1448 | return; |
| 1449 | } |
| 1450 | } |
| 1451 | break; |
| 1452 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1453 | } // switch |
| 1454 | |
| 1455 | // Check we don't have a conflict with another ownership attribute. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1456 | for (specific_attr_iterator<OwnershipAttr> |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1457 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1458 | e = D->specific_attr_end<OwnershipAttr>(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1459 | i != e; ++i) { |
| 1460 | if ((*i)->getOwnKind() != K) { |
| 1461 | for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); |
| 1462 | I!=E; ++I) { |
| 1463 | if (x == *I) { |
| 1464 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1465 | << AL.getName()->getName() << "ownership_*"; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1466 | } |
| 1467 | } |
| 1468 | } |
| 1469 | } |
| 1470 | OwnershipArgs.push_back(x); |
| 1471 | } |
| 1472 | |
| 1473 | unsigned* start = OwnershipArgs.data(); |
| 1474 | unsigned size = OwnershipArgs.size(); |
| 1475 | llvm::array_pod_sort(start, start + size); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1476 | |
| 1477 | if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1478 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1479 | << AL.getName() << 2; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1480 | return; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1481 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1482 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1483 | D->addAttr(::new (S.Context) |
| 1484 | OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size, |
| 1485 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1488 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1489 | // Check the attribute arguments. |
| 1490 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1491 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1492 | << Attr.getName() << 1; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1493 | return; |
| 1494 | } |
| 1495 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1496 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1497 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1498 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1499 | return; |
| 1500 | } |
| 1501 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1502 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1503 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1504 | // gcc rejects |
| 1505 | // class c { |
| 1506 | // static int a __attribute__((weakref ("v2"))); |
| 1507 | // static int b() __attribute__((weakref ("f3"))); |
| 1508 | // }; |
| 1509 | // and ignores the attributes of |
| 1510 | // void f(void) { |
| 1511 | // static int a __attribute__((weakref ("v2"))); |
| 1512 | // } |
| 1513 | // we reject them |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1514 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1515 | if (!Ctx->isFileContext()) { |
| 1516 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1517 | nd->getNameAsString(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1518 | return; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | // The GCC manual says |
| 1522 | // |
| 1523 | // At present, a declaration to which `weakref' is attached can only |
| 1524 | // be `static'. |
| 1525 | // |
| 1526 | // It also says |
| 1527 | // |
| 1528 | // Without a TARGET, |
| 1529 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1530 | // equivalent to `weak'. |
| 1531 | // |
| 1532 | // gcc 4.4.1 will accept |
| 1533 | // int a7 __attribute__((weakref)); |
| 1534 | // as |
| 1535 | // int a7 __attribute__((weak)); |
| 1536 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1537 | // it if this behaviour is actually used. |
| 1538 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1539 | // GCC rejects |
| 1540 | // static ((alias ("y"), weakref)). |
| 1541 | // Should we? How to check that weakref is before or after alias? |
| 1542 | |
| 1543 | if (Attr.getNumArgs() == 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1544 | Expr *Arg = Attr.getArg(0); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1545 | Arg = Arg->IgnoreParenCasts(); |
| 1546 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1547 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1548 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1549 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 1550 | << Attr.getName() << 1 << ArgumentString; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1551 | return; |
| 1552 | } |
| 1553 | // GCC will accept anything as the argument of weakref. Should we |
| 1554 | // check for an existing decl? |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1555 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1556 | Str->getString())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1559 | D->addAttr(::new (S.Context) |
| 1560 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1561 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1564 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1565 | // check the attribute arguments. |
Aaron Ballman | ffa9d57 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 1566 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1567 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1568 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1569 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1570 | Arg = Arg->IgnoreParenCasts(); |
| 1571 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1572 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1573 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1574 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 1575 | << Attr.getName() << 1 << ArgumentString; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1576 | return; |
| 1577 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1578 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1579 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | f5fe292 | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1580 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1581 | return; |
| 1582 | } |
| 1583 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1584 | // FIXME: check if target symbol exists in current file |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1585 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1586 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1587 | Str->getString(), |
| 1588 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1591 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1592 | // Check the attribute arguments. |
| 1593 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1594 | return; |
| 1595 | |
| 1596 | if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) { |
| 1597 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1598 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1599 | return; |
| 1600 | } |
| 1601 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1602 | D->addAttr(::new (S.Context) |
| 1603 | MinSizeAttr(Attr.getRange(), S.Context, |
| 1604 | Attr.getAttributeSpellingListIndex())); |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1607 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1608 | // Check the attribute arguments. |
| 1609 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1610 | return; |
| 1611 | |
| 1612 | if (!isa<FunctionDecl>(D)) { |
| 1613 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1614 | << Attr.getName() << ExpectedFunction; |
| 1615 | return; |
| 1616 | } |
| 1617 | |
| 1618 | if (D->hasAttr<HotAttr>()) { |
| 1619 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1620 | << Attr.getName() << "hot"; |
| 1621 | return; |
| 1622 | } |
| 1623 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1624 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1625 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1629 | // Check the attribute arguments. |
| 1630 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1631 | return; |
| 1632 | |
| 1633 | if (!isa<FunctionDecl>(D)) { |
| 1634 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1635 | << Attr.getName() << ExpectedFunction; |
| 1636 | return; |
| 1637 | } |
| 1638 | |
| 1639 | if (D->hasAttr<ColdAttr>()) { |
| 1640 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1641 | << Attr.getName() << "cold"; |
| 1642 | return; |
| 1643 | } |
| 1644 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1645 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1646 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1649 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1650 | // Check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1651 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1652 | return; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1653 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1654 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1655 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1656 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1657 | return; |
| 1658 | } |
| 1659 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1660 | D->addAttr(::new (S.Context) |
| 1661 | NakedAttr(Attr.getRange(), S.Context, |
| 1662 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1665 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 1666 | const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1667 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1668 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1669 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1670 | << Attr.getName() << 0; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1671 | return; |
| 1672 | } |
| 1673 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1674 | if (!isa<FunctionDecl>(D)) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1675 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1676 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1677 | return; |
| 1678 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1679 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1680 | D->addAttr(::new (S.Context) |
| 1681 | AlwaysInlineAttr(Attr.getRange(), S.Context, |
| 1682 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1685 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1686 | const AttributeList &Attr) { |
| 1687 | // Check the attribute arguments. |
Aaron Ballman | ffa9d57 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 1688 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1689 | return; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1690 | |
| 1691 | Expr *Arg = Attr.getArg(0); |
| 1692 | Arg = Arg->IgnoreParenCasts(); |
| 1693 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1694 | |
| 1695 | // Check that it is a string. |
| 1696 | if (!Str) { |
| 1697 | S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model"; |
| 1698 | return; |
| 1699 | } |
| 1700 | |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 1701 | if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) { |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1702 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1703 | << Attr.getName() << ExpectedTLSVar; |
| 1704 | return; |
| 1705 | } |
| 1706 | |
| 1707 | // Check that the value. |
| 1708 | StringRef Model = Str->getString(); |
| 1709 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1710 | && Model != "initial-exec" && Model != "local-exec") { |
| 1711 | S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg); |
| 1712 | return; |
| 1713 | } |
| 1714 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1715 | D->addAttr(::new (S.Context) |
| 1716 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1717 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1720 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1721 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1722 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1723 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1724 | << Attr.getName() << 0; |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1725 | return; |
| 1726 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1727 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1728 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1729 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1730 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1731 | D->addAttr(::new (S.Context) |
| 1732 | MallocAttr(Attr.getRange(), S.Context, |
| 1733 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1734 | return; |
| 1735 | } |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1738 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1741 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1742 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1743 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1744 | return; |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1745 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1746 | D->addAttr(::new (S.Context) |
| 1747 | MayAliasAttr(Attr.getRange(), S.Context, |
| 1748 | Attr.getAttributeSpellingListIndex())); |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1751 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1752 | assert(!Attr.isInvalid()); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1753 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1754 | D->addAttr(::new (S.Context) |
| 1755 | NoCommonAttr(Attr.getRange(), S.Context, |
| 1756 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1757 | else |
| 1758 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1759 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1762 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1763 | assert(!Attr.isInvalid()); |
Eli Friedman | 3e1aca2 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1764 | |
| 1765 | if (S.LangOpts.CPlusPlus) { |
| 1766 | S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus); |
| 1767 | return; |
| 1768 | } |
| 1769 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1770 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1771 | D->addAttr(::new (S.Context) |
| 1772 | CommonAttr(Attr.getRange(), S.Context, |
| 1773 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1774 | else |
| 1775 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1776 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1779 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1780 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1781 | |
| 1782 | if (S.CheckNoReturnAttr(attr)) return; |
| 1783 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1784 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1785 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1786 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1787 | return; |
| 1788 | } |
| 1789 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1790 | D->addAttr(::new (S.Context) |
| 1791 | NoReturnAttr(attr.getRange(), S.Context, |
| 1792 | attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1796 | if (attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1797 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1798 | << attr.getName() << 0; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1799 | attr.setInvalid(); |
| 1800 | return true; |
| 1801 | } |
| 1802 | |
| 1803 | return false; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1806 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1807 | const AttributeList &Attr) { |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1808 | |
| 1809 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1810 | // because 'analyzer_noreturn' does not impact the type. |
| 1811 | |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1812 | if(!checkAttributeNumArgs(S, Attr, 0)) |
| 1813 | return; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1814 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1815 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1816 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1817 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1818 | && !VD->getType()->isFunctionPointerType())) { |
| 1819 | S.Diag(Attr.getLoc(), |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1820 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1821 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1822 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1823 | return; |
| 1824 | } |
| 1825 | } |
| 1826 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1827 | D->addAttr(::new (S.Context) |
| 1828 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1829 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1832 | static void handleCXX11NoReturnAttr(Sema &S, Decl *D, |
| 1833 | const AttributeList &Attr) { |
| 1834 | // C++11 [dcl.attr.noreturn]p1: |
| 1835 | // The attribute may be applied to the declarator-id in a function |
| 1836 | // declaration. |
| 1837 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 1838 | if (!FD) { |
| 1839 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1840 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1841 | return; |
| 1842 | } |
| 1843 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1844 | D->addAttr(::new (S.Context) |
| 1845 | CXX11NoReturnAttr(Attr.getRange(), S.Context, |
| 1846 | Attr.getAttributeSpellingListIndex())); |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1849 | // PS3 PPU-specific. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1850 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1851 | /* |
| 1852 | Returning a Vector Class in Registers |
| 1853 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1854 | According to the PPU ABI specifications, a class with a single member of |
| 1855 | vector type is returned in memory when used as the return value of a function. |
| 1856 | This results in inefficient code when implementing vector classes. To return |
| 1857 | the value in a single vector register, add the vecreturn attribute to the |
| 1858 | class definition. This attribute is also applicable to struct types. |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1859 | |
| 1860 | Example: |
| 1861 | |
| 1862 | struct Vector |
| 1863 | { |
| 1864 | __vector float xyzw; |
| 1865 | } __attribute__((vecreturn)); |
| 1866 | |
| 1867 | Vector Add(Vector lhs, Vector rhs) |
| 1868 | { |
| 1869 | Vector result; |
| 1870 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1871 | return result; // This will be returned in a register |
| 1872 | } |
| 1873 | */ |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1874 | if (!isa<RecordDecl>(D)) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1875 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1876 | << Attr.getName() << ExpectedClass; |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1877 | return; |
| 1878 | } |
| 1879 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1880 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1881 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1882 | return; |
| 1883 | } |
| 1884 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1885 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1886 | int count = 0; |
| 1887 | |
| 1888 | if (!isa<CXXRecordDecl>(record)) { |
| 1889 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1890 | return; |
| 1891 | } |
| 1892 | |
| 1893 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1894 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1895 | return; |
| 1896 | } |
| 1897 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1898 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1899 | iter != record->field_end(); iter++) { |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1900 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1901 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1902 | return; |
| 1903 | } |
| 1904 | count++; |
| 1905 | } |
| 1906 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1907 | D->addAttr(::new (S.Context) |
| 1908 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1909 | Attr.getAttributeSpellingListIndex())); |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1912 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1913 | const AttributeList &Attr) { |
| 1914 | if (isa<ParmVarDecl>(D)) { |
| 1915 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1916 | // parameter of a function declaration or lambda. |
| 1917 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1918 | S.Diag(Attr.getLoc(), |
| 1919 | diag::err_carries_dependency_param_not_function_decl); |
| 1920 | return; |
| 1921 | } |
| 1922 | } else if (!isa<FunctionDecl>(D)) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1923 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1924 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1925 | return; |
| 1926 | } |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1927 | |
| 1928 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1929 | Attr.getRange(), S.Context, |
| 1930 | Attr.getAttributeSpellingListIndex())); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1933 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1934 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1935 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1936 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1937 | << Attr.getName() << 0; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1938 | return; |
| 1939 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1940 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1941 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 568eae4 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1942 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1943 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1944 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1945 | return; |
| 1946 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1947 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1948 | D->addAttr(::new (S.Context) |
| 1949 | UnusedAttr(Attr.getRange(), S.Context, |
| 1950 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1953 | static void handleReturnsTwiceAttr(Sema &S, Decl *D, |
| 1954 | const AttributeList &Attr) { |
| 1955 | // check the attribute arguments. |
| 1956 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1957 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1958 | << Attr.getName() << 0; |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1959 | return; |
| 1960 | } |
| 1961 | |
| 1962 | if (!isa<FunctionDecl>(D)) { |
| 1963 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1964 | << Attr.getName() << ExpectedFunction; |
| 1965 | return; |
| 1966 | } |
| 1967 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1968 | D->addAttr(::new (S.Context) |
| 1969 | ReturnsTwiceAttr(Attr.getRange(), S.Context, |
| 1970 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1973 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1974 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1975 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1976 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1977 | << Attr.getName() << 0; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1978 | return; |
| 1979 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1980 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1981 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Daniel Dunbar | 186204b | 2009-02-13 22:48:56 +0000 | [diff] [blame] | 1982 | if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1983 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 1984 | return; |
| 1985 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1986 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1987 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1988 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1989 | return; |
| 1990 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1991 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1992 | D->addAttr(::new (S.Context) |
| 1993 | UsedAttr(Attr.getRange(), S.Context, |
| 1994 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1997 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1998 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1999 | if (Attr.getNumArgs() > 1) { |
| 2000 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2001 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2002 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2003 | |
| 2004 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 2005 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2006 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2007 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2008 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2009 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2010 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2011 | << Attr.getName() << 1 << ArgumentIntegerConstant |
| 2012 | << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2013 | return; |
| 2014 | } |
| 2015 | priority = Idx.getZExtValue(); |
| 2016 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2017 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2018 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2019 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2020 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2021 | return; |
| 2022 | } |
| 2023 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2024 | D->addAttr(::new (S.Context) |
| 2025 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 2026 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2029 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2030 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2031 | if (Attr.getNumArgs() > 1) { |
| 2032 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2033 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2034 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2035 | |
| 2036 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 2037 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2038 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2039 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2040 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2041 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2042 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2043 | << Attr.getName() << 1 << ArgumentIntegerConstant |
| 2044 | << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2045 | return; |
| 2046 | } |
| 2047 | priority = Idx.getZExtValue(); |
| 2048 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2049 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2050 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2051 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2052 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2053 | return; |
| 2054 | } |
| 2055 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2056 | D->addAttr(::new (S.Context) |
| 2057 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 2058 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2061 | template <typename AttrTy> |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 2062 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 2063 | const AttributeList &Attr) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2064 | unsigned NumArgs = Attr.getNumArgs(); |
| 2065 | if (NumArgs > 1) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2066 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2067 | return; |
| 2068 | } |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2069 | |
| 2070 | // Handle the case where the attribute has a text message. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2071 | StringRef Str; |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2072 | if (NumArgs == 1) { |
| 2073 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 2074 | if (!SE) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2075 | S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string) |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 2076 | << Attr.getName(); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 2077 | return; |
| 2078 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2079 | Str = SE->getString(); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 2080 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2081 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2082 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 2083 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2086 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 2087 | const AttributeList &Attr) { |
Aaron Ballman | faf71a8 | 2013-07-23 15:16:00 +0000 | [diff] [blame] | 2088 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2089 | return; |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2090 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2091 | D->addAttr(::new (S.Context) |
| 2092 | ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context, |
| 2093 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2096 | static void handleObjCRootClassAttr(Sema &S, Decl *D, |
| 2097 | const AttributeList &Attr) { |
| 2098 | if (!isa<ObjCInterfaceDecl>(D)) { |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 2099 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2100 | << Attr.getName() << ExpectedObjectiveCInterface; |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2101 | return; |
| 2102 | } |
| 2103 | |
Aaron Ballman | faf71a8 | 2013-07-23 15:16:00 +0000 | [diff] [blame] | 2104 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2105 | return; |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2106 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2107 | D->addAttr(::new (S.Context) |
| 2108 | ObjCRootClassAttr(Attr.getRange(), S.Context, |
| 2109 | Attr.getAttributeSpellingListIndex())); |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2112 | static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, |
| 2113 | const AttributeList &Attr) { |
Fariborz Jahanian | 341b8be | 2012-01-03 22:52:32 +0000 | [diff] [blame] | 2114 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 2115 | S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis); |
| 2116 | return; |
| 2117 | } |
| 2118 | |
Aaron Ballman | faf71a8 | 2013-07-23 15:16:00 +0000 | [diff] [blame] | 2119 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2120 | return; |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2121 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2122 | D->addAttr(::new (S.Context) |
| 2123 | ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context, |
| 2124 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2127 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2128 | IdentifierInfo *Platform, |
| 2129 | VersionTuple Introduced, |
| 2130 | VersionTuple Deprecated, |
| 2131 | VersionTuple Obsoleted) { |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2132 | StringRef PlatformName |
| 2133 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2134 | if (PlatformName.empty()) |
| 2135 | PlatformName = Platform->getName(); |
| 2136 | |
| 2137 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2138 | // of these steps are needed). |
| 2139 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2140 | !(Introduced <= Deprecated)) { |
| 2141 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2142 | << 1 << PlatformName << Deprecated.getAsString() |
| 2143 | << 0 << Introduced.getAsString(); |
| 2144 | return true; |
| 2145 | } |
| 2146 | |
| 2147 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2148 | !(Introduced <= Obsoleted)) { |
| 2149 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2150 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2151 | << 0 << Introduced.getAsString(); |
| 2152 | return true; |
| 2153 | } |
| 2154 | |
| 2155 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2156 | !(Deprecated <= Obsoleted)) { |
| 2157 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2158 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2159 | << 1 << Deprecated.getAsString(); |
| 2160 | return true; |
| 2161 | } |
| 2162 | |
| 2163 | return false; |
| 2164 | } |
| 2165 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2166 | /// \brief Check whether the two versions match. |
| 2167 | /// |
| 2168 | /// If either version tuple is empty, then they are assumed to match. If |
| 2169 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2170 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2171 | bool BeforeIsOkay) { |
| 2172 | if (X.empty() || Y.empty()) |
| 2173 | return true; |
| 2174 | |
| 2175 | if (X == Y) |
| 2176 | return true; |
| 2177 | |
| 2178 | if (BeforeIsOkay && X < Y) |
| 2179 | return true; |
| 2180 | |
| 2181 | return false; |
| 2182 | } |
| 2183 | |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2184 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2185 | IdentifierInfo *Platform, |
| 2186 | VersionTuple Introduced, |
| 2187 | VersionTuple Deprecated, |
| 2188 | VersionTuple Obsoleted, |
| 2189 | bool IsUnavailable, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2190 | StringRef Message, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2191 | bool Override, |
| 2192 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2193 | VersionTuple MergedIntroduced = Introduced; |
| 2194 | VersionTuple MergedDeprecated = Deprecated; |
| 2195 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2196 | bool FoundAny = false; |
| 2197 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2198 | if (D->hasAttrs()) { |
| 2199 | AttrVec &Attrs = D->getAttrs(); |
| 2200 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 2201 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 2202 | if (!OldAA) { |
| 2203 | ++i; |
| 2204 | continue; |
| 2205 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2206 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2207 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2208 | if (OldPlatform != Platform) { |
| 2209 | ++i; |
| 2210 | continue; |
| 2211 | } |
| 2212 | |
| 2213 | FoundAny = true; |
| 2214 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2215 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2216 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2217 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2218 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2219 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 2220 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 2221 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 2222 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 72daa3f | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 2223 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2224 | if (Override) { |
| 2225 | int Which = -1; |
| 2226 | VersionTuple FirstVersion; |
| 2227 | VersionTuple SecondVersion; |
| 2228 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 2229 | Which = 0; |
| 2230 | FirstVersion = OldIntroduced; |
| 2231 | SecondVersion = Introduced; |
| 2232 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 2233 | Which = 1; |
| 2234 | FirstVersion = Deprecated; |
| 2235 | SecondVersion = OldDeprecated; |
| 2236 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 2237 | Which = 2; |
| 2238 | FirstVersion = Obsoleted; |
| 2239 | SecondVersion = OldObsoleted; |
| 2240 | } |
| 2241 | |
| 2242 | if (Which == -1) { |
| 2243 | Diag(OldAA->getLocation(), |
| 2244 | diag::warn_mismatched_availability_override_unavail) |
| 2245 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2246 | } else { |
| 2247 | Diag(OldAA->getLocation(), |
| 2248 | diag::warn_mismatched_availability_override) |
| 2249 | << Which |
| 2250 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2251 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 2252 | } |
| 2253 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2254 | } else { |
| 2255 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2256 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2257 | } |
| 2258 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2259 | Attrs.erase(Attrs.begin() + i); |
| 2260 | --e; |
| 2261 | continue; |
| 2262 | } |
| 2263 | |
| 2264 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2265 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2266 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2267 | |
| 2268 | if (MergedIntroduced2.empty()) |
| 2269 | MergedIntroduced2 = OldIntroduced; |
| 2270 | if (MergedDeprecated2.empty()) |
| 2271 | MergedDeprecated2 = OldDeprecated; |
| 2272 | if (MergedObsoleted2.empty()) |
| 2273 | MergedObsoleted2 = OldObsoleted; |
| 2274 | |
| 2275 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2276 | MergedIntroduced2, MergedDeprecated2, |
| 2277 | MergedObsoleted2)) { |
| 2278 | Attrs.erase(Attrs.begin() + i); |
| 2279 | --e; |
| 2280 | continue; |
| 2281 | } |
| 2282 | |
| 2283 | MergedIntroduced = MergedIntroduced2; |
| 2284 | MergedDeprecated = MergedDeprecated2; |
| 2285 | MergedObsoleted = MergedObsoleted2; |
| 2286 | ++i; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2287 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | if (FoundAny && |
| 2291 | MergedIntroduced == Introduced && |
| 2292 | MergedDeprecated == Deprecated && |
| 2293 | MergedObsoleted == Obsoleted) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2294 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2295 | |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2296 | // Only create a new attribute if !Override, but we want to do |
| 2297 | // the checking. |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2298 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2299 | MergedDeprecated, MergedObsoleted) && |
| 2300 | !Override) { |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2301 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 2302 | Introduced, Deprecated, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2303 | Obsoleted, IsUnavailable, Message, |
| 2304 | AttrSpellingListIndex); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2305 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2306 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2309 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2310 | const AttributeList &Attr) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2311 | IdentifierInfo *Platform = Attr.getParameterName(); |
| 2312 | SourceLocation PlatformLoc = Attr.getParameterLoc(); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2313 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2314 | |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2315 | if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2316 | S.Diag(PlatformLoc, diag::warn_availability_unknown_platform) |
| 2317 | << Platform; |
| 2318 | |
Rafael Espindola | 8c4222a | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2319 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 2320 | if (!ND) { |
| 2321 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2322 | return; |
| 2323 | } |
| 2324 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2325 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2326 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2327 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2328 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2329 | StringRef Str; |
| 2330 | const StringLiteral *SE = |
| 2331 | dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr()); |
| 2332 | if (SE) |
| 2333 | Str = SE->getString(); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2334 | |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2335 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2336 | Platform, |
| 2337 | Introduced.Version, |
| 2338 | Deprecated.Version, |
| 2339 | Obsoleted.Version, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2340 | IsUnavailable, Str, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2341 | /*Override=*/false, |
| 2342 | Index); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2343 | if (NewAttr) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2344 | D->addAttr(NewAttr); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2347 | template <class T> |
| 2348 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2349 | typename T::VisibilityType value, |
| 2350 | unsigned attrSpellingListIndex) { |
| 2351 | T *existingAttr = D->getAttr<T>(); |
| 2352 | if (existingAttr) { |
| 2353 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2354 | if (existingValue == value) |
| 2355 | return NULL; |
| 2356 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2357 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2358 | D->dropAttr<T>(); |
| 2359 | } |
| 2360 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2361 | } |
| 2362 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2363 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2364 | VisibilityAttr::VisibilityType Vis, |
| 2365 | unsigned AttrSpellingListIndex) { |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2366 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2367 | AttrSpellingListIndex); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2370 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2371 | TypeVisibilityAttr::VisibilityType Vis, |
| 2372 | unsigned AttrSpellingListIndex) { |
| 2373 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2374 | AttrSpellingListIndex); |
| 2375 | } |
| 2376 | |
| 2377 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2378 | bool isTypeVisibility) { |
| 2379 | // Visibility attributes don't mean anything on a typedef. |
| 2380 | if (isa<TypedefNameDecl>(D)) { |
| 2381 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2382 | << Attr.getName(); |
| 2383 | return; |
| 2384 | } |
| 2385 | |
| 2386 | // 'type_visibility' can only go on a type or namespace. |
| 2387 | if (isTypeVisibility && |
| 2388 | !(isa<TagDecl>(D) || |
| 2389 | isa<ObjCInterfaceDecl>(D) || |
| 2390 | isa<NamespaceDecl>(D))) { |
| 2391 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2392 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2393 | return; |
| 2394 | } |
| 2395 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2396 | // check the attribute arguments. |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2397 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2398 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2399 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2400 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2401 | Arg = Arg->IgnoreParenCasts(); |
| 2402 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2403 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2404 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2405 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2406 | << Attr.getName() << 1 << ArgumentString; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2407 | return; |
| 2408 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2409 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2410 | StringRef TypeStr = Str->getString(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2411 | VisibilityAttr::VisibilityType type; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2412 | |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2413 | if (TypeStr == "default") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2414 | type = VisibilityAttr::Default; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2415 | else if (TypeStr == "hidden") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2416 | type = VisibilityAttr::Hidden; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2417 | else if (TypeStr == "internal") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2418 | type = VisibilityAttr::Hidden; // FIXME |
John McCall | 4188760 | 2012-01-29 01:20:30 +0000 | [diff] [blame] | 2419 | else if (TypeStr == "protected") { |
| 2420 | // Complain about attempts to use protected visibility on targets |
| 2421 | // (like Darwin) that don't support it. |
| 2422 | if (!S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2423 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2424 | type = VisibilityAttr::Default; |
| 2425 | } else { |
| 2426 | type = VisibilityAttr::Protected; |
| 2427 | } |
| 2428 | } else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2429 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2430 | return; |
| 2431 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2432 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2433 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2434 | clang::Attr *newAttr; |
| 2435 | if (isTypeVisibility) { |
| 2436 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2437 | (TypeVisibilityAttr::VisibilityType) type, |
| 2438 | Index); |
| 2439 | } else { |
| 2440 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2441 | } |
| 2442 | if (newAttr) |
| 2443 | D->addAttr(newAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2446 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2447 | const AttributeList &Attr) { |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2448 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 2449 | if (!method) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2450 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2451 | << ExpectedMethod; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2452 | return; |
| 2453 | } |
| 2454 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2455 | if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) { |
| 2456 | if (!Attr.getParameterName() && Attr.getNumArgs() == 1) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2457 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2458 | << Attr.getName() << 1 << ArgumentString; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2459 | } else { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2460 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2461 | << Attr.getName() << 0; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2462 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2463 | Attr.setInvalid(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2464 | return; |
| 2465 | } |
| 2466 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2467 | StringRef param = Attr.getParameterName()->getName(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2468 | ObjCMethodFamilyAttr::FamilyKind family; |
| 2469 | if (param == "none") |
| 2470 | family = ObjCMethodFamilyAttr::OMF_None; |
| 2471 | else if (param == "alloc") |
| 2472 | family = ObjCMethodFamilyAttr::OMF_alloc; |
| 2473 | else if (param == "copy") |
| 2474 | family = ObjCMethodFamilyAttr::OMF_copy; |
| 2475 | else if (param == "init") |
| 2476 | family = ObjCMethodFamilyAttr::OMF_init; |
| 2477 | else if (param == "mutableCopy") |
| 2478 | family = ObjCMethodFamilyAttr::OMF_mutableCopy; |
| 2479 | else if (param == "new") |
| 2480 | family = ObjCMethodFamilyAttr::OMF_new; |
| 2481 | else { |
| 2482 | // Just warn and ignore it. This is future-proof against new |
| 2483 | // families being used in system headers. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2484 | S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2485 | return; |
| 2486 | } |
| 2487 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2488 | if (family == ObjCMethodFamilyAttr::OMF_init && |
| 2489 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2490 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2491 | << method->getResultType(); |
| 2492 | // Ignore the attribute. |
| 2493 | return; |
| 2494 | } |
| 2495 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2496 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2497 | S.Context, family)); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2498 | } |
| 2499 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2500 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 2501 | const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2502 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2503 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2504 | |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2505 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 2506 | if (OCI == 0) { |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 2507 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2508 | << Attr.getName() << ExpectedObjectiveCInterface; |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2509 | return; |
| 2510 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2511 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2512 | D->addAttr(::new (S.Context) |
| 2513 | ObjCExceptionAttr(Attr.getRange(), S.Context, |
| 2514 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2517 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 081c883 | 2013-07-23 12:13:14 +0000 | [diff] [blame] | 2518 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2519 | return; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2520 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2521 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2522 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2523 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2524 | return; |
| 2525 | } |
| 2526 | } |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2527 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2528 | QualType T = PD->getType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2529 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2530 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2531 | return; |
| 2532 | } |
| 2533 | } |
| 2534 | else { |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2535 | // It is okay to include this attribute on properties, e.g.: |
| 2536 | // |
| 2537 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2538 | // |
| 2539 | // In this case it follows tradition and suppresses an error in the above |
| 2540 | // case. |
Fariborz Jahanian | 9b2eb7b | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2541 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2542 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2543 | D->addAttr(::new (S.Context) |
| 2544 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2545 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2548 | static void |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2549 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 081c883 | 2013-07-23 12:13:14 +0000 | [diff] [blame] | 2550 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2551 | return; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2552 | |
| 2553 | if (!isa<FunctionDecl>(D)) { |
| 2554 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 2555 | return; |
| 2556 | } |
| 2557 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2558 | D->addAttr(::new (S.Context) |
| 2559 | OverloadableAttr(Attr.getRange(), S.Context, |
| 2560 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2563 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2564 | if (!Attr.getParameterName()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2565 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2566 | << Attr.getName() << 1 << ArgumentString; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2567 | return; |
| 2568 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2569 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2570 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2571 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2572 | << Attr.getName() << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2573 | return; |
| 2574 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2575 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2576 | BlocksAttr::BlockType type; |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 2577 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2578 | type = BlocksAttr::ByRef; |
| 2579 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2580 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2581 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2582 | return; |
| 2583 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2584 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2585 | D->addAttr(::new (S.Context) |
| 2586 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2587 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2590 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2591 | // check the attribute arguments. |
| 2592 | if (Attr.getNumArgs() > 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2593 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2594 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2595 | } |
| 2596 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2597 | unsigned sentinel = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2598 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2599 | Expr *E = Attr.getArg(0); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2600 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2601 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2602 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2603 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | d732928 | 2013-07-23 17:35:26 +0000 | [diff] [blame] | 2604 | << Attr.getName() << 1 << ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2605 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2606 | return; |
| 2607 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2608 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2609 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2610 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2611 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2612 | return; |
| 2613 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2614 | |
| 2615 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2618 | unsigned nullPos = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2619 | if (Attr.getNumArgs() > 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2620 | Expr *E = Attr.getArg(1); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2621 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2622 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2623 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2624 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | d732928 | 2013-07-23 17:35:26 +0000 | [diff] [blame] | 2625 | << Attr.getName() << 2 << ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2626 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2627 | return; |
| 2628 | } |
| 2629 | nullPos = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2630 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2631 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2632 | // FIXME: This error message could be improved, it would be nice |
| 2633 | // to say what the bounds actually are. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2634 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2635 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2636 | return; |
| 2637 | } |
| 2638 | } |
| 2639 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2640 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2641 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2642 | if (isa<FunctionNoProtoType>(FT)) { |
| 2643 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2644 | return; |
| 2645 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2646 | |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2647 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2648 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2649 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2650 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2651 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2652 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2653 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2654 | return; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2655 | } |
Eli Friedman | a0b2ba1 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2656 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2657 | if (!BD->isVariadic()) { |
| 2658 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2659 | return; |
| 2660 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2661 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2662 | QualType Ty = V->getType(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2663 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2664 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2665 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2666 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2667 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2668 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2669 | return; |
| 2670 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2671 | } else { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2672 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2673 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2674 | return; |
| 2675 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2676 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2677 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2678 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2679 | return; |
| 2680 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2681 | D->addAttr(::new (S.Context) |
| 2682 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2683 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 2686 | static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2687 | // Check the attribute arguments. |
| 2688 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 2689 | return; |
| 2690 | |
| 2691 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
| 2692 | RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context)); |
| 2693 | else |
| 2694 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2695 | } |
| 2696 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2697 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2698 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2699 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2700 | return; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2701 | |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 2702 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2703 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Kaelyn Uhrain | d449c79 | 2012-11-13 00:18:47 +0000 | [diff] [blame] | 2704 | << Attr.getName() << ExpectedFunctionMethodOrClass; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2705 | return; |
| 2706 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2707 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2708 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2709 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2710 | << Attr.getName() << 0; |
Nuno Lopes | f857798 | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2711 | return; |
| 2712 | } |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2713 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2714 | if (MD->getResultType()->isVoidType()) { |
| 2715 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2716 | << Attr.getName() << 1; |
| 2717 | return; |
| 2718 | } |
| 2719 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2720 | D->addAttr(::new (S.Context) |
| 2721 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2722 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2723 | } |
| 2724 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2725 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2726 | // check the attribute arguments. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2727 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2728 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2729 | << Attr.getName() << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2730 | return; |
| 2731 | } |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2732 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2733 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
Fariborz Jahanian | 13c7fcc | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 2734 | if (isa<CXXRecordDecl>(D)) { |
| 2735 | D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| 2736 | return; |
| 2737 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2738 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2739 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | f23ecd9 | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 2740 | return; |
| 2741 | } |
| 2742 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2743 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 2744 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2745 | nd->addAttr(::new (S.Context) |
| 2746 | WeakAttr(Attr.getRange(), S.Context, |
| 2747 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2748 | } |
| 2749 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2750 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2751 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2752 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2753 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2754 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2755 | |
| 2756 | // weak_import only applies to variable & function declarations. |
| 2757 | bool isDef = false; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2758 | if (!D->canBeWeakImported(isDef)) { |
| 2759 | if (isDef) |
Reid Kleckner | beba3e8 | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2760 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2761 | << "weak_import"; |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2762 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2763 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 90eed21 | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2764 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2765 | // Nothing to warn about here. |
| 2766 | } else |
Fariborz Jahanian | c034974 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2767 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2768 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2769 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2770 | return; |
| 2771 | } |
| 2772 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2773 | D->addAttr(::new (S.Context) |
| 2774 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2775 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2778 | // Handles reqd_work_group_size and work_group_size_hint. |
| 2779 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2780 | const AttributeList &Attr) { |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2781 | assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2782 | || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint); |
| 2783 | |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2784 | // Attribute has 3 arguments. |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2785 | if (!checkAttributeNumArgs(S, Attr, 3)) return; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2786 | |
| 2787 | unsigned WGSize[3]; |
| 2788 | for (unsigned i = 0; i < 3; ++i) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2789 | Expr *E = Attr.getArg(i); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2790 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2791 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2792 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2793 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2794 | << Attr.getName()->getName() << E->getSourceRange(); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2795 | return; |
| 2796 | } |
| 2797 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 2798 | } |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2799 | |
| 2800 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2801 | && D->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 2802 | ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>(); |
| 2803 | if (!(A->getXDim() == WGSize[0] && |
| 2804 | A->getYDim() == WGSize[1] && |
| 2805 | A->getZDim() == WGSize[2])) { |
| 2806 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2807 | Attr.getName(); |
| 2808 | } |
| 2809 | } |
| 2810 | |
| 2811 | if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint |
| 2812 | && D->hasAttr<WorkGroupSizeHintAttr>()) { |
| 2813 | WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>(); |
| 2814 | if (!(A->getXDim() == WGSize[0] && |
| 2815 | A->getYDim() == WGSize[1] && |
| 2816 | A->getZDim() == WGSize[2])) { |
| 2817 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2818 | Attr.getName(); |
| 2819 | } |
| 2820 | } |
| 2821 | |
| 2822 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize) |
| 2823 | D->addAttr(::new (S.Context) |
| 2824 | ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2825 | WGSize[0], WGSize[1], WGSize[2], |
| 2826 | Attr.getAttributeSpellingListIndex())); |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2827 | else |
| 2828 | D->addAttr(::new (S.Context) |
| 2829 | WorkGroupSizeHintAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2830 | WGSize[0], WGSize[1], WGSize[2], |
| 2831 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2832 | } |
| 2833 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2834 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2835 | assert(Attr.getKind() == AttributeList::AT_VecTypeHint); |
| 2836 | |
| 2837 | // Attribute has 1 argument. |
| 2838 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 2839 | return; |
| 2840 | |
| 2841 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg()); |
| 2842 | |
| 2843 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2844 | (ParmType->isBooleanType() || |
| 2845 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2846 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2847 | << ParmType; |
| 2848 | return; |
| 2849 | } |
| 2850 | |
| 2851 | if (Attr.getKind() == AttributeList::AT_VecTypeHint && |
| 2852 | D->hasAttr<VecTypeHintAttr>()) { |
| 2853 | VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>(); |
| 2854 | if (A->getTypeHint() != ParmType) { |
| 2855 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2856 | return; |
| 2857 | } |
| 2858 | } |
| 2859 | |
| 2860 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
| 2861 | ParmType, Attr.getLoc())); |
| 2862 | } |
| 2863 | |
Joey Gouly | 96cead5 | 2013-03-14 09:54:43 +0000 | [diff] [blame] | 2864 | static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2865 | if (!dyn_cast<VarDecl>(D)) |
| 2866 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian" |
| 2867 | << 9; |
| 2868 | StringRef EndianType = Attr.getParameterName()->getName(); |
| 2869 | if (EndianType != "host" && EndianType != "device") |
| 2870 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType; |
| 2871 | } |
| 2872 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2873 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2874 | StringRef Name, |
| 2875 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2876 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2877 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2878 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2879 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2880 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2881 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2882 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2883 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2884 | AttrSpellingListIndex); |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2887 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2888 | // Attribute has no arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2889 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2890 | return; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2891 | |
| 2892 | // Make sure that there is a string literal as the sections's single |
| 2893 | // argument. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2894 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2895 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2896 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2897 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section"; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2898 | return; |
| 2899 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2900 | |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2901 | // If the target wants to validate the section specifier, make it happen. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2902 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString()); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2903 | if (!Error.empty()) { |
| 2904 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 2905 | << Error; |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2906 | return; |
| 2907 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2908 | |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2909 | // This attribute cannot be applied to local variables. |
| 2910 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 2911 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 2912 | return; |
| 2913 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2914 | |
| 2915 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2916 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2917 | SE->getString(), Index); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2918 | if (NewAttr) |
| 2919 | D->addAttr(NewAttr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2920 | } |
| 2921 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2922 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2923 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2924 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2925 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2926 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2927 | << Attr.getName() << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2928 | return; |
| 2929 | } |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2930 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2931 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2932 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2933 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2934 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2935 | D->addAttr(::new (S.Context) |
| 2936 | NoThrowAttr(Attr.getRange(), S.Context, |
| 2937 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2938 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2941 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2942 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2943 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2944 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2945 | << Attr.getName() << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2946 | return; |
| 2947 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2948 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2949 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2950 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2951 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2952 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2953 | D->addAttr(::new (S.Context) |
| 2954 | ConstAttr(Attr.getRange(), S.Context, |
| 2955 | Attr.getAttributeSpellingListIndex() )); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2956 | } |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2959 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2960 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2961 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2962 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2963 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2964 | D->addAttr(::new (S.Context) |
| 2965 | PureAttr(Attr.getRange(), S.Context, |
| 2966 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2967 | } |
| 2968 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2969 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2970 | if (!Attr.getParameterName()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2971 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2972 | << Attr.getName() << 1; |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2973 | return; |
| 2974 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2975 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2976 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2977 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2978 | << Attr.getName() << 1; |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2979 | return; |
| 2980 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2981 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2982 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2983 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2984 | if (!VD || !VD->hasLocalStorage()) { |
| 2985 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 2986 | return; |
| 2987 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2988 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2989 | // Look up the function |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2990 | // FIXME: Lookup probably isn't looking in the right place |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2991 | NamedDecl *CleanupDecl |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2992 | = S.LookupSingleName(S.TUScope, Attr.getParameterName(), |
| 2993 | Attr.getParameterLoc(), Sema::LookupOrdinaryName); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2994 | if (!CleanupDecl) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2995 | S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2996 | Attr.getParameterName(); |
| 2997 | return; |
| 2998 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2999 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3000 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 3001 | if (!FD) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 3002 | S.Diag(Attr.getParameterLoc(), |
| 3003 | diag::err_attribute_cleanup_arg_not_function) |
| 3004 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3005 | return; |
| 3006 | } |
| 3007 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3008 | if (FD->getNumParams() != 1) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 3009 | S.Diag(Attr.getParameterLoc(), |
| 3010 | diag::err_attribute_cleanup_func_must_take_one_arg) |
| 3011 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3012 | return; |
| 3013 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3014 | |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 3015 | // We're currently more strict than GCC about what function types we accept. |
| 3016 | // If this ever proves to be a problem it should be easy to fix. |
| 3017 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 3018 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 3019 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 3020 | ParamTy, Ty) != Sema::Compatible) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 3021 | S.Diag(Attr.getParameterLoc(), |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 3022 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 3023 | Attr.getParameterName() << ParamTy << Ty; |
| 3024 | return; |
| 3025 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3026 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3027 | D->addAttr(::new (S.Context) |
| 3028 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 3029 | Attr.getAttributeSpellingListIndex())); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3030 | S.MarkFunctionReferenced(Attr.getParameterLoc(), FD); |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 3031 | S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc()); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3032 | } |
| 3033 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3034 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3035 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3036 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3037 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3038 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3039 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3040 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3041 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3042 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3043 | return; |
| 3044 | } |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3045 | |
| 3046 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3047 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3048 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 3049 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3050 | unsigned FirstIdx = 1; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3051 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3052 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3053 | Expr *IdxExpr = Attr.getArg(0); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3054 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3055 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 3056 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3057 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 3058 | << Attr.getName() << 2 << ArgumentIntegerConstant |
| 3059 | << IdxExpr->getSourceRange(); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3060 | return; |
| 3061 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3062 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3063 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
| 3064 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 3065 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 3066 | return; |
| 3067 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3068 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3069 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3070 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3071 | if (HasImplicitThisParam) { |
| 3072 | if (ArgIdx == 0) { |
| 3073 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 3074 | << "format_arg" << IdxExpr->getSourceRange(); |
| 3075 | return; |
| 3076 | } |
| 3077 | ArgIdx--; |
| 3078 | } |
| 3079 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3080 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3081 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3082 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3083 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 3084 | if (not_nsstring_type && |
| 3085 | !isCFStringType(Ty, S.Context) && |
| 3086 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3087 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3088 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 3089 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3090 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3091 | << IdxExpr->getSourceRange(); |
| 3092 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3093 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3094 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3095 | if (!isNSStringType(Ty, S.Context) && |
| 3096 | !isCFStringType(Ty, S.Context) && |
| 3097 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3098 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3099 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 3100 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3101 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3102 | << IdxExpr->getSourceRange(); |
| 3103 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3106 | D->addAttr(::new (S.Context) |
| 3107 | FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(), |
| 3108 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3111 | enum FormatAttrKind { |
| 3112 | CFStringFormat, |
| 3113 | NSStringFormat, |
| 3114 | StrftimeFormat, |
| 3115 | SupportedFormat, |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3116 | IgnoredFormat, |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3117 | InvalidFormat |
| 3118 | }; |
| 3119 | |
| 3120 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 3121 | /// types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3122 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3123 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 3124 | // Check for formats that get handled specially. |
| 3125 | .Case("NSString", NSStringFormat) |
| 3126 | .Case("CFString", CFStringFormat) |
| 3127 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3128 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3129 | // Otherwise, check for supported formats. |
| 3130 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 3131 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 3132 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3133 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3134 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 3135 | .Default(InvalidFormat); |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3138 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3139 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3140 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 3141 | const AttributeList &Attr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3142 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3143 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3144 | return; |
| 3145 | } |
| 3146 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3147 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3148 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3149 | Attr.setInvalid(); |
| 3150 | return; |
| 3151 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3152 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3153 | if (S.Context.getAsArrayType(T)) |
| 3154 | T = S.Context.getBaseElementType(T); |
| 3155 | if (!T->getAs<RecordType>()) { |
| 3156 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3157 | Attr.setInvalid(); |
| 3158 | return; |
| 3159 | } |
| 3160 | |
Aaron Ballman | ffa9d57 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3161 | if (!checkAttributeNumArgs(S, Attr, 1)) { |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3162 | Attr.setInvalid(); |
| 3163 | return; |
| 3164 | } |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3165 | Expr *priorityExpr = Attr.getArg(0); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3166 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3167 | llvm::APSInt priority(32); |
| 3168 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 3169 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
| 3170 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 3171 | << "init_priority" << priorityExpr->getSourceRange(); |
| 3172 | Attr.setInvalid(); |
| 3173 | return; |
| 3174 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 3175 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3176 | if (prioritynum < 101 || prioritynum > 65535) { |
| 3177 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 3178 | << priorityExpr->getSourceRange(); |
| 3179 | Attr.setInvalid(); |
| 3180 | return; |
| 3181 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3182 | D->addAttr(::new (S.Context) |
| 3183 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 3184 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3187 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3188 | int FormatIdx, int FirstArg, |
| 3189 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3190 | // Check whether we already have an equivalent format attribute. |
| 3191 | for (specific_attr_iterator<FormatAttr> |
| 3192 | i = D->specific_attr_begin<FormatAttr>(), |
| 3193 | e = D->specific_attr_end<FormatAttr>(); |
| 3194 | i != e ; ++i) { |
| 3195 | FormatAttr *f = *i; |
| 3196 | if (f->getType() == Format && |
| 3197 | f->getFormatIdx() == FormatIdx && |
| 3198 | f->getFirstArg() == FirstArg) { |
| 3199 | // If we don't have a valid location for this attribute, adopt the |
| 3200 | // location. |
| 3201 | if (f->getLocation().isInvalid()) |
| 3202 | f->setRange(Range); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3203 | return NULL; |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3204 | } |
| 3205 | } |
| 3206 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3207 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg, |
| 3208 | AttrSpellingListIndex); |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3209 | } |
| 3210 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3211 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3212 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3213 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3214 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3215 | if (!Attr.getParameterName()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3216 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 3217 | << Attr.getName() << 1 << ArgumentString; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3218 | return; |
| 3219 | } |
| 3220 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3221 | if (Attr.getNumArgs() != 2) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3222 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3223 | << Attr.getName() << 3; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3224 | return; |
| 3225 | } |
| 3226 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3227 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3228 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3229 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3230 | return; |
| 3231 | } |
| 3232 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3233 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3234 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3235 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 3236 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3237 | unsigned FirstIdx = 1; |
| 3238 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3239 | StringRef Format = Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3240 | |
| 3241 | // Normalize the argument, __foo__ becomes foo. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3242 | if (Format.startswith("__") && Format.endswith("__")) |
| 3243 | Format = Format.substr(2, Format.size() - 4); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3244 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3245 | // Check for supported formats. |
| 3246 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3247 | |
| 3248 | if (Kind == IgnoredFormat) |
| 3249 | return; |
| 3250 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3251 | if (Kind == InvalidFormat) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3252 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3253 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3254 | return; |
| 3255 | } |
| 3256 | |
| 3257 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3258 | Expr *IdxExpr = Attr.getArg(0); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3259 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3260 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 3261 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3262 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 3263 | << Attr.getName() << 2 << ArgumentIntegerConstant |
| 3264 | << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3265 | return; |
| 3266 | } |
| 3267 | |
| 3268 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3269 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3270 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3271 | return; |
| 3272 | } |
| 3273 | |
| 3274 | // FIXME: Do we need to bounds check? |
| 3275 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3276 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3277 | if (HasImplicitThisParam) { |
| 3278 | if (ArgIdx == 0) { |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3279 | S.Diag(Attr.getLoc(), |
| 3280 | diag::err_format_attribute_implicit_this_format_string) |
| 3281 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3282 | return; |
| 3283 | } |
| 3284 | ArgIdx--; |
| 3285 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3286 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3287 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3288 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3289 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3290 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3291 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3292 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3293 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3294 | return; |
| 3295 | } |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3296 | } else if (Kind == NSStringFormat) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3297 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3298 | // semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3299 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3300 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3301 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3302 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3303 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3304 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3305 | } else if (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3306 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3307 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3308 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3309 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3310 | return; |
| 3311 | } |
| 3312 | |
| 3313 | // check the 3rd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3314 | Expr *FirstArgExpr = Attr.getArg(1); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3315 | llvm::APSInt FirstArg(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3316 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 3317 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3318 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 3319 | << Attr.getName() << 3 << ArgumentIntegerConstant |
| 3320 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3321 | return; |
| 3322 | } |
| 3323 | |
| 3324 | // check if the function is variadic if the 3rd argument non-zero |
| 3325 | if (FirstArg != 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3326 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3327 | ++NumArgs; // +1 for ... |
| 3328 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3329 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3330 | return; |
| 3331 | } |
| 3332 | } |
| 3333 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3334 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3335 | // variable the input is just the current time + the format string. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3336 | if (Kind == StrftimeFormat) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3337 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3338 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 3339 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3340 | return; |
| 3341 | } |
| 3342 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3343 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3344 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3345 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3346 | return; |
| 3347 | } |
| 3348 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3349 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format, |
| 3350 | Idx.getZExtValue(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3351 | FirstArg.getZExtValue(), |
| 3352 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3353 | if (NewAttr) |
| 3354 | D->addAttr(NewAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3357 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 3358 | const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3359 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3360 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3361 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3362 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3363 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3364 | // Try to find the underlying union declaration. |
| 3365 | RecordDecl *RD = 0; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3366 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3367 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3368 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3369 | else |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3370 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3371 | |
| 3372 | if (!RD || !RD->isUnion()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3373 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3374 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3375 | return; |
| 3376 | } |
| 3377 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3378 | if (!RD->isCompleteDefinition()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3379 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3380 | diag::warn_transparent_union_attribute_not_definition); |
| 3381 | return; |
| 3382 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3383 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3384 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3385 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3386 | if (Field == FieldEnd) { |
| 3387 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3388 | return; |
| 3389 | } |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3390 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3391 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3392 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3393 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3394 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3395 | diag::warn_transparent_union_attribute_floating) |
| 3396 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3397 | return; |
| 3398 | } |
| 3399 | |
| 3400 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3401 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3402 | for (; Field != FieldEnd; ++Field) { |
| 3403 | QualType FieldType = Field->getType(); |
| 3404 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 3405 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 3406 | // Warn if we drop the attribute. |
| 3407 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3408 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3409 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3410 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3411 | diag::warn_transparent_union_attribute_field_size_align) |
| 3412 | << isSize << Field->getDeclName() << FieldBits; |
| 3413 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3414 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3415 | diag::note_transparent_union_first_field_size_align) |
| 3416 | << isSize << FirstBits; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3417 | return; |
| 3418 | } |
| 3419 | } |
| 3420 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3421 | RD->addAttr(::new (S.Context) |
| 3422 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 3423 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3426 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3427 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3428 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3429 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3430 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3431 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3432 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3433 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3434 | // Make sure that there is a string literal as the annotation's single |
| 3435 | // argument. |
| 3436 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3437 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate"; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3438 | return; |
| 3439 | } |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3440 | |
| 3441 | // Don't duplicate annotations that are already set. |
| 3442 | for (specific_attr_iterator<AnnotateAttr> |
| 3443 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 3444 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
| 3445 | if ((*i)->getAnnotation() == SE->getString()) |
| 3446 | return; |
| 3447 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3448 | |
| 3449 | D->addAttr(::new (S.Context) |
| 3450 | AnnotateAttr(Attr.getRange(), S.Context, SE->getString(), |
| 3451 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3452 | } |
| 3453 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3454 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3455 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3456 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3457 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3458 | << Attr.getName() << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3459 | return; |
| 3460 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3461 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3462 | if (Attr.getNumArgs() == 0) { |
| 3463 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 3464 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 3465 | return; |
| 3466 | } |
| 3467 | |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3468 | Expr *E = Attr.getArg(0); |
| 3469 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3470 | S.Diag(Attr.getEllipsisLoc(), |
| 3471 | diag::err_pack_expansion_without_parameter_packs); |
| 3472 | return; |
| 3473 | } |
| 3474 | |
| 3475 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 3476 | return; |
| 3477 | |
| 3478 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 3479 | Attr.isPackExpansion()); |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3480 | } |
| 3481 | |
| 3482 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3483 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3484 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3485 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3486 | |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3487 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3488 | if (TmpAttr.isAlignas()) { |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3489 | // C++11 [dcl.align]p1: |
| 3490 | // An alignment-specifier may be applied to a variable or to a class |
| 3491 | // data member, but it shall not be applied to a bit-field, a function |
| 3492 | // parameter, the formal parameter of a catch clause, or a variable |
| 3493 | // declared with the register storage class specifier. An |
| 3494 | // alignment-specifier may also be applied to the declaration of a class |
| 3495 | // or enumeration type. |
| 3496 | // C11 6.7.5/2: |
| 3497 | // An alignment attribute shall not be specified in a declaration of |
| 3498 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3499 | // object declared with the register storage-class specifier. |
| 3500 | int DiagKind = -1; |
| 3501 | if (isa<ParmVarDecl>(D)) { |
| 3502 | DiagKind = 0; |
| 3503 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3504 | if (VD->getStorageClass() == SC_Register) |
| 3505 | DiagKind = 1; |
| 3506 | if (VD->isExceptionVariable()) |
| 3507 | DiagKind = 2; |
| 3508 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 3509 | if (FD->isBitField()) |
| 3510 | DiagKind = 3; |
| 3511 | } else if (!isa<TagDecl>(D)) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3512 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) |
| 3513 | << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'") |
Richard Smith | 5f838aa | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3514 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3515 | : ExpectedVariableFieldOrTag); |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3516 | return; |
| 3517 | } |
| 3518 | if (DiagKind != -1) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3519 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 3520 | << TmpAttr.isC11() << DiagKind; |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3521 | return; |
| 3522 | } |
| 3523 | } |
| 3524 | |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3525 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3526 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3527 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3528 | AA->setPackExpansion(IsPackExpansion); |
| 3529 | D->addAttr(AA); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3530 | return; |
| 3531 | } |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3532 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3533 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3534 | llvm::APSInt Alignment(32); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3535 | ExprResult ICE |
| 3536 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3537 | diag::err_aligned_attribute_argument_not_int, |
| 3538 | /*AllowFold*/ false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3539 | if (ICE.isInvalid()) |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3540 | return; |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3541 | |
| 3542 | // C++11 [dcl.align]p2: |
| 3543 | // -- if the constant expression evaluates to zero, the alignment |
| 3544 | // specifier shall have no effect |
| 3545 | // C11 6.7.5p6: |
| 3546 | // An alignment specification of zero has no effect. |
| 3547 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 3548 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3549 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 3550 | << E->getSourceRange(); |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3551 | return; |
| 3552 | } |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3553 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3554 | if (TmpAttr.isDeclspec()) { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3555 | // We've already verified it's a power of 2, now let's make sure it's |
| 3556 | // 8192 or less. |
| 3557 | if (Alignment.getZExtValue() > 8192) { |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3558 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3559 | << E->getSourceRange(); |
| 3560 | return; |
| 3561 | } |
| 3562 | } |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3563 | |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3564 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 3565 | ICE.take(), SpellingListIndex); |
| 3566 | AA->setPackExpansion(IsPackExpansion); |
| 3567 | D->addAttr(AA); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3568 | } |
| 3569 | |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3570 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3571 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3572 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3573 | // FIXME: Perform checking of type validity |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3574 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3575 | SpellingListIndex); |
| 3576 | AA->setPackExpansion(IsPackExpansion); |
| 3577 | D->addAttr(AA); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3578 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3579 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3580 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3581 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3582 | |
| 3583 | QualType Ty; |
| 3584 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3585 | Ty = VD->getType(); |
| 3586 | else |
| 3587 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
Richard Smith | 4da0903 | 2013-02-22 09:21:42 +0000 | [diff] [blame] | 3588 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3589 | return; |
| 3590 | |
| 3591 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3592 | // The combined effect of all alignment attributes in a declaration shall |
| 3593 | // not specify an alignment that is less strict than the alignment that |
| 3594 | // would otherwise be required for the entity being declared. |
| 3595 | AlignedAttr *AlignasAttr = 0; |
| 3596 | unsigned Align = 0; |
| 3597 | for (specific_attr_iterator<AlignedAttr> |
| 3598 | I = D->specific_attr_begin<AlignedAttr>(), |
| 3599 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 3600 | if (I->isAlignmentDependent()) |
| 3601 | return; |
| 3602 | if (I->isAlignas()) |
| 3603 | AlignasAttr = *I; |
| 3604 | Align = std::max(Align, I->getAlignment(Context)); |
| 3605 | } |
| 3606 | |
| 3607 | if (AlignasAttr && Align) { |
| 3608 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 3609 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 3610 | if (NaturalAlign > RequestedAlign) |
| 3611 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 3612 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 3613 | } |
| 3614 | } |
| 3615 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3616 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3617 | /// type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3618 | /// |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3619 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3620 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3621 | /// HImode, not an intermediate pointer. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3622 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3623 | // This attribute isn't documented, but glibc uses it. It changes |
| 3624 | // the width of an int or unsigned int to the specified size. |
| 3625 | |
| 3626 | // Check that there aren't any arguments |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3627 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3628 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3629 | |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3630 | |
| 3631 | IdentifierInfo *Name = Attr.getParameterName(); |
| 3632 | if (!Name) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3633 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3634 | return; |
| 3635 | } |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3636 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3637 | StringRef Str = Attr.getParameterName()->getName(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3638 | |
| 3639 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3640 | if (Str.startswith("__") && Str.endswith("__")) |
| 3641 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3642 | |
| 3643 | unsigned DestWidth = 0; |
| 3644 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3645 | bool ComplexMode = false; |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3646 | switch (Str.size()) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3647 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3648 | switch (Str[0]) { |
| 3649 | case 'Q': DestWidth = 8; break; |
| 3650 | case 'H': DestWidth = 16; break; |
| 3651 | case 'S': DestWidth = 32; break; |
| 3652 | case 'D': DestWidth = 64; break; |
| 3653 | case 'X': DestWidth = 96; break; |
| 3654 | case 'T': DestWidth = 128; break; |
| 3655 | } |
| 3656 | if (Str[1] == 'F') { |
| 3657 | IntegerMode = false; |
| 3658 | } else if (Str[1] == 'C') { |
| 3659 | IntegerMode = false; |
| 3660 | ComplexMode = true; |
| 3661 | } else if (Str[1] != 'I') { |
| 3662 | DestWidth = 0; |
| 3663 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3664 | break; |
| 3665 | case 4: |
| 3666 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3667 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3668 | if (Str == "word") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3669 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3670 | else if (Str == "byte") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3671 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3672 | break; |
| 3673 | case 7: |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3674 | if (Str == "pointer") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3675 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3676 | break; |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3677 | case 11: |
| 3678 | if (Str == "unwind_word") |
Rafael Espindola | 0b1de54 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3679 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3680 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3681 | } |
| 3682 | |
| 3683 | QualType OldTy; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3684 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3685 | OldTy = TD->getUnderlyingType(); |
| 3686 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3687 | OldTy = VD->getType(); |
| 3688 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3689 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3690 | << "mode" << Attr.getRange(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3691 | return; |
| 3692 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3693 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3694 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3695 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3696 | else if (IntegerMode) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3697 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3698 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3699 | } else if (ComplexMode) { |
| 3700 | if (!OldTy->isComplexType()) |
| 3701 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3702 | } else { |
| 3703 | if (!OldTy->isFloatingType()) |
| 3704 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3705 | } |
| 3706 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3707 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3708 | // and friends, at least with glibc. |
| 3709 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 3710 | // width on unusual platforms. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3711 | // FIXME: Make sure floating-point mappings are accurate |
| 3712 | // FIXME: Support XF and TF types |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3713 | QualType NewTy; |
| 3714 | switch (DestWidth) { |
| 3715 | case 0: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3716 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3717 | return; |
| 3718 | default: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3719 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3720 | return; |
| 3721 | case 8: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3722 | if (!IntegerMode) { |
| 3723 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3724 | return; |
| 3725 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3726 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3727 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3728 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3729 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3730 | break; |
| 3731 | case 16: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3732 | if (!IntegerMode) { |
| 3733 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3734 | return; |
| 3735 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3736 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3737 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3738 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3739 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3740 | break; |
| 3741 | case 32: |
| 3742 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3743 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3744 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3745 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3746 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3747 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3748 | break; |
| 3749 | case 64: |
| 3750 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3751 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3752 | else if (OldTy->isSignedIntegerType()) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3753 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3754 | NewTy = S.Context.LongTy; |
| 3755 | else |
| 3756 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3757 | else |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3758 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3759 | NewTy = S.Context.UnsignedLongTy; |
| 3760 | else |
| 3761 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3762 | break; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3763 | case 96: |
| 3764 | NewTy = S.Context.LongDoubleTy; |
| 3765 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3766 | case 128: |
Roman Divacky | 1964554 | 2013-07-03 21:08:41 +0000 | [diff] [blame] | 3767 | if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() != |
| 3768 | &llvm::APFloat::PPCDoubleDouble) { |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3769 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3770 | return; |
| 3771 | } |
Roman Divacky | f0d14cb | 2013-07-03 20:48:06 +0000 | [diff] [blame] | 3772 | if (IntegerMode) { |
| 3773 | if (OldTy->isSignedIntegerType()) |
| 3774 | NewTy = S.Context.Int128Ty; |
| 3775 | else |
| 3776 | NewTy = S.Context.UnsignedInt128Ty; |
| 3777 | } else |
| 3778 | NewTy = S.Context.LongDoubleTy; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3779 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3780 | } |
| 3781 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3782 | if (ComplexMode) { |
| 3783 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
| 3786 | // Install the new type. |
Enea Zaffanella | c2fa6b6 | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3787 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3788 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 3789 | else |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3790 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | c2fa6b6 | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3791 | |
| 3792 | D->addAttr(::new (S.Context) |
| 3793 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 3794 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3795 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3796 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3797 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3798 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3799 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3800 | return; |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 3801 | |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3802 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3803 | if (!VD->hasGlobalStorage()) |
| 3804 | S.Diag(Attr.getLoc(), |
| 3805 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3806 | << Attr.getName(); |
| 3807 | } else if (!isFunctionOrMethod(D)) { |
| 3808 | S.Diag(Attr.getLoc(), |
| 3809 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3810 | << Attr.getName(); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3811 | return; |
| 3812 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3813 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3814 | D->addAttr(::new (S.Context) |
| 3815 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3816 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3817 | } |
| 3818 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3819 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3820 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3821 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3822 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3823 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3824 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3825 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3826 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3827 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3828 | return; |
| 3829 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3830 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3831 | D->addAttr(::new (S.Context) |
| 3832 | NoInlineAttr(Attr.getRange(), S.Context, |
| 3833 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3834 | } |
| 3835 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3836 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 3837 | const AttributeList &Attr) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3838 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3839 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3840 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3841 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3842 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3843 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3844 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3845 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3846 | return; |
| 3847 | } |
| 3848 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3849 | D->addAttr(::new (S.Context) |
| 3850 | NoInstrumentFunctionAttr(Attr.getRange(), S.Context, |
| 3851 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3852 | } |
| 3853 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3854 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3855 | if (S.LangOpts.CUDA) { |
| 3856 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 3857 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3858 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3859 | << Attr.getName() << 0; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3860 | return; |
| 3861 | } |
| 3862 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3863 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3864 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3865 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3866 | return; |
| 3867 | } |
| 3868 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3869 | D->addAttr(::new (S.Context) |
| 3870 | CUDAConstantAttr(Attr.getRange(), S.Context, |
| 3871 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3872 | } else { |
| 3873 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 3874 | } |
| 3875 | } |
| 3876 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3877 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3878 | if (S.LangOpts.CUDA) { |
| 3879 | // check the attribute arguments. |
| 3880 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3881 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3882 | << Attr.getName() << 0; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3883 | return; |
| 3884 | } |
| 3885 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3886 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3887 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3888 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3889 | return; |
| 3890 | } |
| 3891 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3892 | D->addAttr(::new (S.Context) |
| 3893 | CUDADeviceAttr(Attr.getRange(), S.Context, |
| 3894 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3895 | } else { |
| 3896 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 3897 | } |
| 3898 | } |
| 3899 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3900 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3901 | if (S.LangOpts.CUDA) { |
| 3902 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3903 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3904 | return; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3905 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3906 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3907 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3908 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3909 | return; |
| 3910 | } |
| 3911 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3912 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3913 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 3914 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3915 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3916 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3917 | << FD->getType() |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3918 | << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(), |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3919 | "void"); |
| 3920 | } else { |
| 3921 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3922 | << FD->getType(); |
| 3923 | } |
| 3924 | return; |
| 3925 | } |
| 3926 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3927 | D->addAttr(::new (S.Context) |
| 3928 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
| 3929 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3930 | } else { |
| 3931 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 3932 | } |
| 3933 | } |
| 3934 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3935 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3936 | if (S.LangOpts.CUDA) { |
| 3937 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3938 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3939 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3940 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3941 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3942 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3943 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3944 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3945 | return; |
| 3946 | } |
| 3947 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3948 | D->addAttr(::new (S.Context) |
| 3949 | CUDAHostAttr(Attr.getRange(), S.Context, |
| 3950 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3951 | } else { |
| 3952 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 3953 | } |
| 3954 | } |
| 3955 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3956 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3957 | if (S.LangOpts.CUDA) { |
| 3958 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3959 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3960 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3961 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3962 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3963 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3964 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3965 | return; |
| 3966 | } |
| 3967 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3968 | D->addAttr(::new (S.Context) |
| 3969 | CUDASharedAttr(Attr.getRange(), S.Context, |
| 3970 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3971 | } else { |
| 3972 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 3973 | } |
| 3974 | } |
| 3975 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3976 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3977 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3978 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3979 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3980 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3981 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3982 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3983 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3984 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3985 | return; |
| 3986 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3987 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3988 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3989 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3990 | return; |
| 3991 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3992 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3993 | D->addAttr(::new (S.Context) |
| 3994 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3995 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3996 | } |
| 3997 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3998 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3999 | if (hasDeclarator(D)) return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4000 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4001 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4002 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4003 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 4004 | CallingConv CC; |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4005 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4006 | return; |
| 4007 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4008 | if (!isa<ObjCMethodDecl>(D)) { |
| 4009 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4010 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4011 | return; |
| 4012 | } |
| 4013 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4014 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4015 | case AttributeList::AT_FastCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4016 | D->addAttr(::new (S.Context) |
| 4017 | FastCallAttr(Attr.getRange(), S.Context, |
| 4018 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4019 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4020 | case AttributeList::AT_StdCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4021 | D->addAttr(::new (S.Context) |
| 4022 | StdCallAttr(Attr.getRange(), S.Context, |
| 4023 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4024 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4025 | case AttributeList::AT_ThisCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4026 | D->addAttr(::new (S.Context) |
| 4027 | ThisCallAttr(Attr.getRange(), S.Context, |
| 4028 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 04633eb | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 4029 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4030 | case AttributeList::AT_CDecl: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4031 | D->addAttr(::new (S.Context) |
| 4032 | CDeclAttr(Attr.getRange(), S.Context, |
| 4033 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4034 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4035 | case AttributeList::AT_Pascal: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4036 | D->addAttr(::new (S.Context) |
| 4037 | PascalAttr(Attr.getRange(), S.Context, |
| 4038 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4039 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4040 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4041 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4042 | switch (CC) { |
| 4043 | case CC_AAPCS: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4044 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4045 | break; |
| 4046 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4047 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4048 | break; |
| 4049 | default: |
| 4050 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4051 | } |
| 4052 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4053 | D->addAttr(::new (S.Context) |
| 4054 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 4055 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4056 | return; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4057 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4058 | case AttributeList::AT_PnaclCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4059 | D->addAttr(::new (S.Context) |
| 4060 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 4061 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4062 | return; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4063 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4064 | D->addAttr(::new (S.Context) |
| 4065 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 4066 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4067 | return; |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4068 | |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4069 | default: |
| 4070 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4071 | } |
| 4072 | } |
| 4073 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4074 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 4075 | assert(!Attr.isInvalid()); |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4076 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4077 | } |
| 4078 | |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4079 | static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
| 4080 | assert(!Attr.isInvalid()); |
| 4081 | |
| 4082 | Expr *E = Attr.getArg(0); |
| 4083 | llvm::APSInt ArgNum(32); |
| 4084 | if (E->isTypeDependent() || E->isValueDependent() || |
| 4085 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 4086 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 4087 | << Attr.getName()->getName() << E->getSourceRange(); |
| 4088 | return; |
| 4089 | } |
| 4090 | |
| 4091 | D->addAttr(::new (S.Context) OpenCLImageAccessAttr( |
| 4092 | Attr.getRange(), S.Context, ArgNum.getZExtValue())); |
| 4093 | } |
| 4094 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4095 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 4096 | const FunctionDecl *FD) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4097 | if (attr.isInvalid()) |
| 4098 | return true; |
| 4099 | |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4100 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
| 4101 | if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 4102 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 4103 | << attr.getName() << ReqArgs; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4104 | attr.setInvalid(); |
| 4105 | return true; |
| 4106 | } |
| 4107 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4108 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 4109 | // move to TargetAttributesSema one day. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4110 | switch (attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4111 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 4112 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 4113 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 4114 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 4115 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
| 4116 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4117 | Expr *Arg = attr.getArg(0); |
| 4118 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4119 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4120 | Diag(attr.getLoc(), diag::err_attribute_argument_n_type) |
| 4121 | << attr.getName() << 1 << ArgumentString; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4122 | attr.setInvalid(); |
| 4123 | return true; |
| 4124 | } |
| 4125 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4126 | StringRef StrRef = Str->getString(); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4127 | if (StrRef == "aapcs") { |
| 4128 | CC = CC_AAPCS; |
| 4129 | break; |
| 4130 | } else if (StrRef == "aapcs-vfp") { |
| 4131 | CC = CC_AAPCS_VFP; |
| 4132 | break; |
| 4133 | } |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4134 | |
| 4135 | attr.setInvalid(); |
| 4136 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 4137 | return true; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4138 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4139 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4140 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4141 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4142 | } |
| 4143 | |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4144 | const TargetInfo &TI = Context.getTargetInfo(); |
| 4145 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 4146 | if (A == TargetInfo::CCCR_Warning) { |
| 4147 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4148 | |
| 4149 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 4150 | if (FD) |
| 4151 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 4152 | TargetInfo::CCMT_NonMember; |
| 4153 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4154 | } |
| 4155 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4156 | return false; |
| 4157 | } |
| 4158 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4159 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4160 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4161 | |
| 4162 | unsigned numParams; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4163 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4164 | return; |
| 4165 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4166 | if (!isa<ObjCMethodDecl>(D)) { |
| 4167 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4168 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4169 | return; |
| 4170 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4171 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4172 | D->addAttr(::new (S.Context) |
| 4173 | RegparmAttr(Attr.getRange(), S.Context, numParams, |
| 4174 | Attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
| 4177 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 4178 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4179 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 4180 | if (Attr.isInvalid()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4181 | return true; |
| 4182 | |
Aaron Ballman | ffa9d57 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 4183 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4184 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4185 | return true; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4186 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4187 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4188 | Expr *NumParamsExpr = Attr.getArg(0); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4189 | llvm::APSInt NumParams(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 4190 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4191 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4192 | Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4193 | << "regparm" << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4194 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4195 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4196 | } |
| 4197 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4198 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4199 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4200 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4201 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4202 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4205 | numParams = NumParams.getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4206 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4207 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4208 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4209 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4210 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4211 | } |
| 4212 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4213 | return false; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4216 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4217 | if (S.LangOpts.CUDA) { |
| 4218 | // check the attribute arguments. |
| 4219 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 4220 | // FIXME: 0 is not okay. |
| 4221 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4222 | return; |
| 4223 | } |
| 4224 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4225 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4226 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4227 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4228 | return; |
| 4229 | } |
| 4230 | |
| 4231 | Expr *MaxThreadsExpr = Attr.getArg(0); |
| 4232 | llvm::APSInt MaxThreads(32); |
| 4233 | if (MaxThreadsExpr->isTypeDependent() || |
| 4234 | MaxThreadsExpr->isValueDependent() || |
| 4235 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4236 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 4237 | << Attr.getName() << 1 << ArgumentIntegerConstant |
| 4238 | << MaxThreadsExpr->getSourceRange(); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4239 | return; |
| 4240 | } |
| 4241 | |
| 4242 | llvm::APSInt MinBlocks(32); |
| 4243 | if (Attr.getNumArgs() > 1) { |
| 4244 | Expr *MinBlocksExpr = Attr.getArg(1); |
| 4245 | if (MinBlocksExpr->isTypeDependent() || |
| 4246 | MinBlocksExpr->isValueDependent() || |
| 4247 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4248 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 4249 | << Attr.getName() << 2 << ArgumentIntegerConstant |
| 4250 | << MinBlocksExpr->getSourceRange(); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4251 | return; |
| 4252 | } |
| 4253 | } |
| 4254 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4255 | D->addAttr(::new (S.Context) |
| 4256 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 4257 | MaxThreads.getZExtValue(), |
| 4258 | MinBlocks.getZExtValue(), |
| 4259 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4260 | } else { |
| 4261 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 4262 | } |
| 4263 | } |
| 4264 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4265 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 4266 | const AttributeList &Attr) { |
| 4267 | StringRef AttrName = Attr.getName()->getName(); |
| 4268 | if (!Attr.getParameterName()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4269 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 4270 | << Attr.getName() << /* arg num = */ 1 << ArgumentIdentifier; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4271 | return; |
| 4272 | } |
| 4273 | |
| 4274 | if (Attr.getNumArgs() != 2) { |
| 4275 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 4276 | << Attr.getName() << /* required args = */ 3; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4277 | return; |
| 4278 | } |
| 4279 | |
| 4280 | IdentifierInfo *ArgumentKind = Attr.getParameterName(); |
| 4281 | |
| 4282 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 4283 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4284 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 4285 | return; |
| 4286 | } |
| 4287 | |
| 4288 | uint64_t ArgumentIdx; |
| 4289 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4290 | Attr.getLoc(), 2, |
| 4291 | Attr.getArg(0), ArgumentIdx)) |
| 4292 | return; |
| 4293 | |
| 4294 | uint64_t TypeTagIdx; |
| 4295 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4296 | Attr.getLoc(), 3, |
| 4297 | Attr.getArg(1), TypeTagIdx)) |
| 4298 | return; |
| 4299 | |
| 4300 | bool IsPointer = (AttrName == "pointer_with_type_tag"); |
| 4301 | if (IsPointer) { |
| 4302 | // Ensure that buffer has a pointer type. |
| 4303 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 4304 | if (!BufferTy->isPointerType()) { |
| 4305 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4306 | << Attr.getName(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4307 | } |
| 4308 | } |
| 4309 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4310 | D->addAttr(::new (S.Context) |
| 4311 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 4312 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 4313 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4314 | } |
| 4315 | |
| 4316 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 4317 | const AttributeList &Attr) { |
| 4318 | IdentifierInfo *PointerKind = Attr.getParameterName(); |
| 4319 | if (!PointerKind) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4320 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 4321 | << Attr.getName() << 1 << ArgumentIdentifier; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4322 | return; |
| 4323 | } |
| 4324 | |
| 4325 | QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL); |
| 4326 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4327 | D->addAttr(::new (S.Context) |
| 4328 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
| 4329 | MatchingCType, |
| 4330 | Attr.getLayoutCompatible(), |
| 4331 | Attr.getMustBeNull(), |
| 4332 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4333 | } |
| 4334 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4335 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4336 | // Checker-specific attribute handlers. |
| 4337 | //===----------------------------------------------------------------------===// |
| 4338 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4339 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4340 | return type->isDependentType() || |
| 4341 | type->isObjCObjectPointerType() || |
| 4342 | S.Context.isObjCNSObjectType(type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4343 | } |
| 4344 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4345 | return type->isDependentType() || |
| 4346 | type->isPointerType() || |
| 4347 | isValidSubjectOfNSAttribute(S, type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4348 | } |
| 4349 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4350 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4351 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4352 | if (!param) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4353 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4354 | << Attr.getRange() << Attr.getName() << ExpectedParameter; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4355 | return; |
| 4356 | } |
| 4357 | |
| 4358 | bool typeOK, cf; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4359 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4360 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 4361 | cf = false; |
| 4362 | } else { |
| 4363 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 4364 | cf = true; |
| 4365 | } |
| 4366 | |
| 4367 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4368 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4369 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4370 | return; |
| 4371 | } |
| 4372 | |
| 4373 | if (cf) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4374 | param->addAttr(::new (S.Context) |
| 4375 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 4376 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4377 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4378 | param->addAttr(::new (S.Context) |
| 4379 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 4380 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4381 | } |
| 4382 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4383 | static void handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 4384 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4385 | if (!isa<ObjCMethodDecl>(D)) { |
| 4386 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4387 | << Attr.getRange() << Attr.getName() << ExpectedMethod; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4388 | return; |
| 4389 | } |
| 4390 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4391 | D->addAttr(::new (S.Context) |
| 4392 | NSConsumesSelfAttr(Attr.getRange(), S.Context, |
| 4393 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4394 | } |
| 4395 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4396 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 4397 | const AttributeList &Attr) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4398 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4399 | QualType returnType; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4400 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4401 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4402 | returnType = MD->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4403 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4404 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4405 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | a23bd4c | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 4406 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4407 | returnType = PD->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4408 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4409 | returnType = FD->getResultType(); |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4410 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4411 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4412 | << Attr.getRange() << Attr.getName() |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4413 | << ExpectedFunctionOrMethod; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4414 | return; |
| 4415 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4416 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4417 | bool typeOK; |
| 4418 | bool cf; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4419 | switch (Attr.getKind()) { |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4420 | default: llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4421 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4422 | case AttributeList::AT_NSReturnsRetained: |
| 4423 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4424 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 4425 | cf = false; |
| 4426 | break; |
| 4427 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4428 | case AttributeList::AT_CFReturnsRetained: |
| 4429 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4430 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 4431 | cf = true; |
| 4432 | break; |
| 4433 | } |
| 4434 | |
| 4435 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4436 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4437 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4438 | return; |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4439 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4440 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4441 | switch (Attr.getKind()) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4442 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4443 | llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4444 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4445 | D->addAttr(::new (S.Context) |
| 4446 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 4447 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4448 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4449 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4450 | D->addAttr(::new (S.Context) |
| 4451 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4452 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4453 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4454 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4455 | D->addAttr(::new (S.Context) |
| 4456 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4457 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4458 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4459 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4460 | D->addAttr(::new (S.Context) |
| 4461 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4462 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4463 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4464 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4465 | D->addAttr(::new (S.Context) |
| 4466 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4467 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4468 | return; |
| 4469 | }; |
| 4470 | } |
| 4471 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4472 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4473 | const AttributeList &attr) { |
| 4474 | SourceLocation loc = attr.getLoc(); |
| 4475 | |
| 4476 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4477 | |
Fariborz Jahanian | 94d55d7 | 2012-04-21 17:51:44 +0000 | [diff] [blame] | 4478 | if (!method) { |
Fariborz Jahanian | 0e78afb | 2012-04-20 22:00:46 +0000 | [diff] [blame] | 4479 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4480 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4481 | return; |
| 4482 | } |
| 4483 | |
| 4484 | // Check that the method returns a normal pointer. |
| 4485 | QualType resultType = method->getResultType(); |
Fariborz Jahanian | f2e5945 | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4486 | |
| 4487 | if (!resultType->isReferenceType() && |
| 4488 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4489 | S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 4490 | << SourceRange(loc) |
| 4491 | << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2; |
| 4492 | |
| 4493 | // Drop the attribute. |
| 4494 | return; |
| 4495 | } |
| 4496 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4497 | method->addAttr(::new (S.Context) |
| 4498 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 4499 | attr.getAttributeSpellingListIndex())); |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4500 | } |
| 4501 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4502 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4503 | const AttributeList &attr) { |
| 4504 | SourceLocation loc = attr.getLoc(); |
| 4505 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4506 | |
| 4507 | if (!method) { |
| 4508 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 4509 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
| 4510 | return; |
| 4511 | } |
| 4512 | DeclContext *DC = method->getDeclContext(); |
| 4513 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4514 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4515 | << attr.getName() << 0; |
| 4516 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4517 | return; |
| 4518 | } |
| 4519 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4520 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4521 | << attr.getName() << 1; |
| 4522 | return; |
| 4523 | } |
| 4524 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4525 | method->addAttr(::new (S.Context) |
| 4526 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 4527 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4528 | } |
| 4529 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4530 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 4531 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
| 4532 | if (!isa<FunctionDecl>(D)) { |
| 4533 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4534 | << A.getRange() << A.getName() << ExpectedFunction; |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4535 | return; |
| 4536 | } |
| 4537 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4538 | bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4539 | |
| 4540 | // Check whether there's a conflicting attribute already present. |
| 4541 | Attr *Existing; |
| 4542 | if (IsAudited) { |
| 4543 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 4544 | } else { |
| 4545 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 4546 | } |
| 4547 | if (Existing) { |
| 4548 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 4549 | << A.getName() |
| 4550 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 4551 | << A.getRange() << Existing->getRange(); |
| 4552 | return; |
| 4553 | } |
| 4554 | |
| 4555 | // All clear; add the attribute. |
| 4556 | if (IsAudited) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4557 | D->addAttr(::new (S.Context) |
| 4558 | CFAuditedTransferAttr(A.getRange(), S.Context, |
| 4559 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4560 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4561 | D->addAttr(::new (S.Context) |
| 4562 | CFUnknownTransferAttr(A.getRange(), S.Context, |
| 4563 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4564 | } |
| 4565 | } |
| 4566 | |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4567 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4568 | const AttributeList &Attr) { |
| 4569 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4570 | if (!RD || RD->isUnion()) { |
| 4571 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4572 | << Attr.getRange() << Attr.getName() << ExpectedStruct; |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4573 | } |
| 4574 | |
| 4575 | IdentifierInfo *ParmName = Attr.getParameterName(); |
| 4576 | |
| 4577 | // In Objective-C, verify that the type names an Objective-C type. |
| 4578 | // We don't want to check this outside of ObjC because people sometimes |
| 4579 | // do crazy C declarations of Objective-C types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4580 | if (ParmName && S.getLangOpts().ObjC1) { |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4581 | // Check for an existing type with this name. |
| 4582 | LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(), |
| 4583 | Sema::LookupOrdinaryName); |
| 4584 | if (S.LookupName(R, Sc)) { |
| 4585 | NamedDecl *Target = R.getFoundDecl(); |
| 4586 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 4587 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 4588 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 4589 | } |
| 4590 | } |
| 4591 | } |
| 4592 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4593 | D->addAttr(::new (S.Context) |
| 4594 | NSBridgedAttr(Attr.getRange(), S.Context, ParmName, |
| 4595 | Attr.getAttributeSpellingListIndex())); |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4596 | } |
| 4597 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4598 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4599 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4600 | if (hasDeclarator(D)) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4601 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4602 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4603 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4604 | } |
| 4605 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4606 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4607 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4608 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4609 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4610 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4611 | return; |
| 4612 | } |
| 4613 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4614 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4615 | QualType type = vd->getType(); |
| 4616 | |
| 4617 | if (!type->isDependentType() && |
| 4618 | !type->isObjCLifetimeType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4619 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4620 | << type; |
| 4621 | return; |
| 4622 | } |
| 4623 | |
| 4624 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4625 | |
| 4626 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4627 | // going to infer. |
| 4628 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4629 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4630 | |
| 4631 | switch (lifetime) { |
| 4632 | case Qualifiers::OCL_None: |
| 4633 | assert(type->isDependentType() && |
| 4634 | "didn't infer lifetime for non-dependent type?"); |
| 4635 | break; |
| 4636 | |
| 4637 | case Qualifiers::OCL_Weak: // meaningful |
| 4638 | case Qualifiers::OCL_Strong: // meaningful |
| 4639 | break; |
| 4640 | |
| 4641 | case Qualifiers::OCL_ExplicitNone: |
| 4642 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4643 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4644 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4645 | break; |
| 4646 | } |
| 4647 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4648 | D->addAttr(::new (S.Context) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4649 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 4650 | Attr.getAttributeSpellingListIndex())); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4651 | } |
| 4652 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4653 | //===----------------------------------------------------------------------===// |
| 4654 | // Microsoft specific attribute handlers. |
| 4655 | //===----------------------------------------------------------------------===// |
| 4656 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4657 | // Check if MS extensions or some other language extensions are enabled. If |
| 4658 | // not, issue a diagnostic that the given attribute is unused. |
| 4659 | static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr, |
| 4660 | bool OtherExtension = false) { |
| 4661 | if (S.LangOpts.MicrosoftExt || OtherExtension) |
| 4662 | return true; |
| 4663 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4664 | return false; |
| 4665 | } |
| 4666 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4667 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4668 | if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland)) |
| 4669 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4670 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4671 | // check the attribute arguments. |
| 4672 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4673 | return; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4674 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4675 | Expr *Arg = Attr.getArg(0); |
| 4676 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 4677 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4678 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 4679 | << Attr.getName() << 1 << ArgumentString; |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4680 | return; |
| 4681 | } |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4682 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4683 | StringRef StrRef = Str->getString(); |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4684 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4685 | bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' && |
| 4686 | StrRef.back() == '}'; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4687 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4688 | // Validate GUID length. |
| 4689 | if (IsCurly && StrRef.size() != 38) { |
| 4690 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4691 | return; |
| 4692 | } |
| 4693 | if (!IsCurly && StrRef.size() != 36) { |
| 4694 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4695 | return; |
| 4696 | } |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4697 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4698 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 4699 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" |
| 4700 | StringRef::iterator I = StrRef.begin(); |
| 4701 | if (IsCurly) // Skip the optional '{' |
| 4702 | ++I; |
| 4703 | |
| 4704 | for (int i = 0; i < 36; ++i) { |
| 4705 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 4706 | if (*I != '-') { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4707 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4708 | return; |
| 4709 | } |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4710 | } else if (!isHexDigit(*I)) { |
| 4711 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4712 | return; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4713 | } |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4714 | I++; |
| 4715 | } |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4716 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4717 | D->addAttr(::new (S.Context) |
| 4718 | UuidAttr(Attr.getRange(), S.Context, Str->getString(), |
| 4719 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4720 | } |
| 4721 | |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4722 | static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4723 | if (!checkMicrosoftExt(S, Attr)) |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4724 | return; |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4725 | |
| 4726 | AttributeList::Kind Kind = Attr.getKind(); |
| 4727 | if (Kind == AttributeList::AT_SingleInheritance) |
| 4728 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4729 | ::new (S.Context) |
| 4730 | SingleInheritanceAttr(Attr.getRange(), S.Context, |
| 4731 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4732 | else if (Kind == AttributeList::AT_MultipleInheritance) |
| 4733 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4734 | ::new (S.Context) |
| 4735 | MultipleInheritanceAttr(Attr.getRange(), S.Context, |
| 4736 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4737 | else if (Kind == AttributeList::AT_VirtualInheritance) |
| 4738 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4739 | ::new (S.Context) |
| 4740 | VirtualInheritanceAttr(Attr.getRange(), S.Context, |
| 4741 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4742 | } |
| 4743 | |
| 4744 | static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4745 | if (!checkMicrosoftExt(S, Attr)) |
| 4746 | return; |
| 4747 | |
| 4748 | AttributeList::Kind Kind = Attr.getKind(); |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4749 | if (Kind == AttributeList::AT_Win64) |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4750 | D->addAttr( |
| 4751 | ::new (S.Context) Win64Attr(Attr.getRange(), S.Context, |
| 4752 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4753 | } |
| 4754 | |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4755 | static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4756 | if (!checkMicrosoftExt(S, Attr)) |
| 4757 | return; |
| 4758 | D->addAttr(::new (S.Context) |
| 4759 | ForceInlineAttr(Attr.getRange(), S.Context, |
| 4760 | Attr.getAttributeSpellingListIndex())); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4761 | } |
| 4762 | |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4763 | static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4764 | if (!checkMicrosoftExt(S, Attr)) |
| 4765 | return; |
| 4766 | // Check linkage after possibly merging declaratinos. See |
| 4767 | // checkAttributesAfterMerging(). |
| 4768 | D->addAttr(::new (S.Context) |
| 4769 | SelectAnyAttr(Attr.getRange(), S.Context, |
| 4770 | Attr.getAttributeSpellingListIndex())); |
| 4771 | } |
| 4772 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4773 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4774 | // Top Level Sema Entry Points |
| 4775 | //===----------------------------------------------------------------------===// |
| 4776 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4777 | static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4778 | const AttributeList &Attr) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4779 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4780 | case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break; |
| 4781 | case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break; |
| 4782 | case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break; |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4783 | default: |
| 4784 | break; |
| 4785 | } |
| 4786 | } |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4787 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4788 | static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4789 | const AttributeList &Attr) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4790 | switch (Attr.getKind()) { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4791 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 4792 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 4793 | case AttributeList::AT_IBOutletCollection: |
| 4794 | handleIBOutletCollection(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4795 | case AttributeList::AT_AddressSpace: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4796 | case AttributeList::AT_ObjCGC: |
| 4797 | case AttributeList::AT_VectorSize: |
| 4798 | case AttributeList::AT_NeonVectorType: |
| 4799 | case AttributeList::AT_NeonPolyVectorType: |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4800 | case AttributeList::AT_Ptr32: |
| 4801 | case AttributeList::AT_Ptr64: |
| 4802 | case AttributeList::AT_SPtr: |
| 4803 | case AttributeList::AT_UPtr: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4804 | // Ignore these, these are type attributes, handled by |
| 4805 | // ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4806 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4807 | case AttributeList::AT_CUDADevice: |
| 4808 | case AttributeList::AT_CUDAHost: |
| 4809 | case AttributeList::AT_Overloadable: |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4810 | // Ignore, this is a non-inheritable attribute, handled |
| 4811 | // by ProcessNonInheritableDeclAttr. |
| 4812 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4813 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 4814 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 4815 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 4816 | case AttributeList::AT_AlwaysInline: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4817 | handleAlwaysInlineAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4818 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4819 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 4820 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4821 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 4822 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 4823 | case AttributeList::AT_CarriesDependency: |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4824 | handleDependencyAttr(S, scope, D, Attr); |
| 4825 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4826 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
| 4827 | case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break; |
| 4828 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4829 | case AttributeList::AT_CXX11NoReturn: |
| 4830 | handleCXX11NoReturnAttr(S, D, Attr); |
| 4831 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4832 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4833 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4834 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4835 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 4836 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4837 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4838 | break; |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4839 | case AttributeList::AT_MinSize: |
| 4840 | handleMinSizeAttr(S, D, Attr); |
| 4841 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4842 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4843 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4844 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
| 4845 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4846 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4847 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4848 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4849 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
| 4850 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
| 4851 | case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break; |
| 4852 | case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break; |
| 4853 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4854 | case AttributeList::AT_ownership_returns: |
| 4855 | case AttributeList::AT_ownership_takes: |
| 4856 | case AttributeList::AT_ownership_holds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4857 | handleOwnershipAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4858 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4859 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
| 4860 | case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break; |
| 4861 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
| 4862 | case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break; |
| 4863 | case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break; |
| 4864 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4865 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4866 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4867 | handleObjCOwnershipAttr(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4868 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4869 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4870 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4871 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4872 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4873 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4874 | case AttributeList::AT_ObjCRequiresSuper: |
| 4875 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4876 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4877 | case AttributeList::AT_NSBridged: |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4878 | handleNSBridgedAttr(S, scope, D, Attr); break; |
| 4879 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4880 | case AttributeList::AT_CFAuditedTransfer: |
| 4881 | case AttributeList::AT_CFUnknownTransfer: |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4882 | handleCFTransferAttr(S, D, Attr); break; |
| 4883 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4884 | // Checker-specific. |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4885 | case AttributeList::AT_CFConsumed: |
| 4886 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4887 | case AttributeList::AT_NSConsumesSelf: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4888 | handleNSConsumesSelfAttr(S, D, Attr); break; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4889 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4890 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4891 | case AttributeList::AT_NSReturnsNotRetained: |
| 4892 | case AttributeList::AT_CFReturnsNotRetained: |
| 4893 | case AttributeList::AT_NSReturnsRetained: |
| 4894 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4895 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4896 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4897 | case AttributeList::AT_WorkGroupSizeHint: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4898 | case AttributeList::AT_ReqdWorkGroupSize: |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4899 | handleWorkGroupSize(S, D, Attr); break; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 4900 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4901 | case AttributeList::AT_VecTypeHint: |
| 4902 | handleVecTypeHint(S, D, Attr); break; |
| 4903 | |
Joey Gouly | 96cead5 | 2013-03-14 09:54:43 +0000 | [diff] [blame] | 4904 | case AttributeList::AT_Endian: |
| 4905 | handleEndianAttr(S, D, Attr); |
| 4906 | break; |
| 4907 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4908 | case AttributeList::AT_InitPriority: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4909 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4910 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4911 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4912 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4913 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4914 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4915 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4916 | case AttributeList::AT_ArcWeakrefUnavailable: |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 4917 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 4918 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4919 | case AttributeList::AT_ObjCRootClass: |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4920 | handleObjCRootClassAttr(S, D, Attr); |
| 4921 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4922 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 4923 | handleObjCRequiresPropertyDefsAttr (S, D, Attr); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 4924 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4925 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4926 | case AttributeList::AT_ReturnsTwice: |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 4927 | handleReturnsTwiceAttr(S, D, Attr); |
| 4928 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4929 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4930 | case AttributeList::AT_Visibility: |
| 4931 | handleVisibilityAttr(S, D, Attr, false); |
| 4932 | break; |
| 4933 | case AttributeList::AT_TypeVisibility: |
| 4934 | handleVisibilityAttr(S, D, Attr, true); |
| 4935 | break; |
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 4936 | case AttributeList::AT_WarnUnused: |
| 4937 | handleWarnUnusedAttr(S, D, Attr); |
| 4938 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4939 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4940 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4941 | case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; |
| 4942 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4943 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4944 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4945 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4946 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4947 | case AttributeList::AT_ObjCException: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4948 | handleObjCExceptionAttr(S, D, Attr); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 4949 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4950 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4951 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4952 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4953 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4954 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4955 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
| 4956 | case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break; |
| 4957 | case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break; |
| 4958 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4959 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
| 4960 | case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break; |
| 4961 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4962 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4963 | // Just ignore |
| 4964 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4965 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4966 | handleNoInstrumentFunctionAttr(S, D, Attr); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 4967 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4968 | case AttributeList::AT_StdCall: |
| 4969 | case AttributeList::AT_CDecl: |
| 4970 | case AttributeList::AT_FastCall: |
| 4971 | case AttributeList::AT_ThisCall: |
| 4972 | case AttributeList::AT_Pascal: |
| 4973 | case AttributeList::AT_Pcs: |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4974 | case AttributeList::AT_PnaclCall: |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4975 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4976 | handleCallConvAttr(S, D, Attr); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4977 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4978 | case AttributeList::AT_OpenCLKernel: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4979 | handleOpenCLKernelAttr(S, D, Attr); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4980 | break; |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4981 | case AttributeList::AT_OpenCLImageAccess: |
| 4982 | handleOpenCLImageAccessAttr(S, D, Attr); |
| 4983 | break; |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4984 | |
| 4985 | // Microsoft attributes: |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4986 | case AttributeList::AT_MsProperty: break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4987 | case AttributeList::AT_MsStruct: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4988 | handleMsStructAttr(S, D, Attr); |
| 4989 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4990 | case AttributeList::AT_Uuid: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4991 | handleUuidAttr(S, D, Attr); |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4992 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4993 | case AttributeList::AT_SingleInheritance: |
| 4994 | case AttributeList::AT_MultipleInheritance: |
| 4995 | case AttributeList::AT_VirtualInheritance: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4996 | handleInheritanceAttr(S, D, Attr); |
| 4997 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4998 | case AttributeList::AT_Win64: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4999 | handlePortabilityAttr(S, D, Attr); |
| 5000 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5001 | case AttributeList::AT_ForceInline: |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 5002 | handleForceInlineAttr(S, D, Attr); |
| 5003 | break; |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 5004 | case AttributeList::AT_SelectAny: |
| 5005 | handleSelectAnyAttr(S, D, Attr); |
| 5006 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5007 | |
| 5008 | // Thread safety attributes: |
DeLesley Hutchins | 5c6134f | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 5009 | case AttributeList::AT_AssertExclusiveLock: |
| 5010 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 5011 | break; |
| 5012 | case AttributeList::AT_AssertSharedLock: |
| 5013 | handleAssertSharedLockAttr(S, D, Attr); |
| 5014 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5015 | case AttributeList::AT_GuardedVar: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5016 | handleGuardedVarAttr(S, D, Attr); |
| 5017 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5018 | case AttributeList::AT_PtGuardedVar: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5019 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5020 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5021 | case AttributeList::AT_ScopedLockable: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5022 | handleScopedLockableAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5023 | break; |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 5024 | case AttributeList::AT_NoSanitizeAddress: |
| 5025 | handleNoSanitizeAddressAttr(S, D, Attr); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 5026 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5027 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 5028 | handleNoThreadSafetyAnalysis(S, D, Attr); |
| 5029 | break; |
| 5030 | case AttributeList::AT_NoSanitizeThread: |
| 5031 | handleNoSanitizeThread(S, D, Attr); |
| 5032 | break; |
| 5033 | case AttributeList::AT_NoSanitizeMemory: |
| 5034 | handleNoSanitizeMemory(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5035 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5036 | case AttributeList::AT_Lockable: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5037 | handleLockableAttr(S, D, Attr); |
| 5038 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5039 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5040 | handleGuardedByAttr(S, D, Attr); |
| 5041 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5042 | case AttributeList::AT_PtGuardedBy: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5043 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5044 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5045 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5046 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5047 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5048 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5049 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5050 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5051 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5052 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5053 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5054 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5055 | handleLockReturnedAttr(S, D, Attr); |
| 5056 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5057 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5058 | handleLocksExcludedAttr(S, D, Attr); |
| 5059 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5060 | case AttributeList::AT_SharedLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5061 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5062 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5063 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5064 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5065 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5066 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5067 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5068 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5069 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5070 | handleUnlockFunAttr(S, D, Attr); |
| 5071 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5072 | case AttributeList::AT_AcquiredBefore: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5073 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5074 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5075 | case AttributeList::AT_AcquiredAfter: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5076 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5077 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5078 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 5079 | // Type safety attributes. |
| 5080 | case AttributeList::AT_ArgumentWithTypeTag: |
| 5081 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 5082 | break; |
| 5083 | case AttributeList::AT_TypeTagForDatatype: |
| 5084 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 5085 | break; |
| 5086 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5087 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5088 | // Ask target about the attribute. |
| 5089 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 5090 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 5091 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 5092 | diag::warn_unhandled_ms_attribute_ignored : |
| 5093 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5094 | break; |
| 5095 | } |
| 5096 | } |
| 5097 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5098 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 5099 | /// the attribute applies to decls. If the attribute is a type attribute, just |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5100 | /// silently ignore it if a GNU attribute. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5101 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 5102 | const AttributeList &Attr, |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5103 | bool NonInheritable, bool Inheritable, |
| 5104 | bool IncludeCXX11Attributes) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5105 | if (Attr.isInvalid()) |
| 5106 | return; |
| 5107 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5108 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 5109 | // instead. |
| 5110 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 5111 | return; |
| 5112 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5113 | if (NonInheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5114 | ProcessNonInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5115 | |
| 5116 | if (Inheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5117 | ProcessInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5118 | } |
| 5119 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5120 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 5121 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 5122 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5123 | const AttributeList *AttrList, |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5124 | bool NonInheritable, bool Inheritable, |
| 5125 | bool IncludeCXX11Attributes) { |
| 5126 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
| 5127 | ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable, |
| 5128 | IncludeCXX11Attributes); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5129 | |
| 5130 | // GCC accepts |
| 5131 | // static int a9 __attribute__((weakref)); |
| 5132 | // but that looks really pointless. We reject it. |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5133 | if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5134 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Rafael Espindola | 4d8a33b | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 5135 | cast<NamedDecl>(D)->getNameAsString(); |
| 5136 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5137 | return; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5138 | } |
| 5139 | } |
| 5140 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 5141 | // Annotation attributes are the only attributes allowed after an access |
| 5142 | // specifier. |
| 5143 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 5144 | const AttributeList *AttrList) { |
| 5145 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5146 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 5147 | handleAnnotateAttr(*this, ASDecl, *l); |
| 5148 | } else { |
| 5149 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 5150 | return true; |
| 5151 | } |
| 5152 | } |
| 5153 | |
| 5154 | return false; |
| 5155 | } |
| 5156 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5157 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 5158 | /// contains any decl attributes that we should warn about. |
| 5159 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 5160 | for ( ; A; A = A->getNext()) { |
| 5161 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 5162 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5163 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 5164 | |
| 5165 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 5166 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 5167 | << A->getName() << A->getRange(); |
| 5168 | } else { |
| 5169 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 5170 | << A->getName() << A->getRange(); |
| 5171 | } |
| 5172 | } |
| 5173 | } |
| 5174 | |
| 5175 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 5176 | /// used to build a declaration, complain about any decl attributes |
| 5177 | /// which might be lying around on it. |
| 5178 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 5179 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 5180 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 5181 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 5182 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 5183 | } |
| 5184 | |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5185 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5186 | /// \#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] | 5187 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 5188 | SourceLocation Loc) { |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5189 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5190 | NamedDecl *NewD = 0; |
| 5191 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5192 | FunctionDecl *NewFD; |
| 5193 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 5194 | // FIXME: Mangling? |
| 5195 | // FIXME: Is the qualifier info correct? |
| 5196 | // FIXME: Is the DeclContext correct? |
| 5197 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 5198 | Loc, Loc, DeclarationName(II), |
| 5199 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5200 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5201 | FD->hasPrototype(), |
| 5202 | false/*isConstexprSpecified*/); |
| 5203 | NewD = NewFD; |
| 5204 | |
| 5205 | if (FD->getQualifier()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5206 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5207 | |
| 5208 | // Fake up parameter variables; they are declared as if this were |
| 5209 | // a typedef. |
| 5210 | QualType FDTy = FD->getType(); |
| 5211 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 5212 | SmallVector<ParmVarDecl*, 16> Params; |
| 5213 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 5214 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 5215 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 5216 | Param->setScopeInfo(0, Params.size()); |
| 5217 | Params.push_back(Param); |
| 5218 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 5219 | NewFD->setParams(Params); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5220 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5221 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 5222 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5223 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5224 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5225 | VD->getStorageClass()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5226 | if (VD->getQualifier()) { |
| 5227 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5228 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5229 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5230 | } |
| 5231 | return NewD; |
| 5232 | } |
| 5233 | |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5234 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5235 | /// applied to it, possibly with an alias. |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5236 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5237 | if (W.getUsed()) return; // only do this once |
| 5238 | W.setUsed(true); |
| 5239 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 5240 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5241 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5242 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 5243 | NDId->getName())); |
| 5244 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5245 | WeakTopLevelDecl.push_back(NewD); |
| 5246 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 5247 | // to insert Decl at TU scope, sorry. |
| 5248 | DeclContext *SavedContext = CurContext; |
| 5249 | CurContext = Context.getTranslationUnitDecl(); |
| 5250 | PushOnScopeChains(NewD, S); |
| 5251 | CurContext = SavedContext; |
| 5252 | } else { // just add weak to existing |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5253 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5254 | } |
| 5255 | } |
| 5256 | |
Rafael Espindola | 65611bf | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5257 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 5258 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 5259 | // have to do this. |
| 5260 | LoadExternalWeakUndeclaredIdentifiers(); |
| 5261 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 5262 | NamedDecl *ND = NULL; |
| 5263 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 5264 | if (VD->isExternC()) |
| 5265 | ND = VD; |
| 5266 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5267 | if (FD->isExternC()) |
| 5268 | ND = FD; |
| 5269 | if (ND) { |
| 5270 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 5271 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 5272 | = WeakUndeclaredIdentifiers.find(Id); |
| 5273 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 5274 | WeakInfo W = I->second; |
| 5275 | DeclApplyPragmaWeak(S, ND, W); |
| 5276 | WeakUndeclaredIdentifiers[Id] = W; |
| 5277 | } |
| 5278 | } |
| 5279 | } |
| 5280 | } |
| 5281 | } |
| 5282 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5283 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 5284 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 5285 | /// 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] | 5286 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD, |
| 5287 | bool NonInheritable, bool Inheritable) { |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5288 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5289 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5290 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5291 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5292 | // Walk the declarator structure, applying decl attributes that were in a type |
| 5293 | // position to the decl itself. This handles cases like: |
| 5294 | // int *__attr__(x)** D; |
| 5295 | // when X is a decl attribute. |
| 5296 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 5297 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5298 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable, |
| 5299 | /*IncludeCXX11Attributes=*/false); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5300 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5301 | // Finally, apply any attributes on the decl itself. |
| 5302 | if (const AttributeList *Attrs = PD.getAttributes()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5303 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5304 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5305 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5306 | /// Is the given declaration allowed to use a forbidden type? |
| 5307 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 5308 | // Private ivars are always okay. Unfortunately, people don't |
| 5309 | // always properly make their ivars private, even in system headers. |
| 5310 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | a6b3380 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 5311 | // Function declarations in sys headers will be marked unavailable. |
| 5312 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 5313 | !isa<FunctionDecl>(decl)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5314 | return false; |
| 5315 | |
| 5316 | // Require it to be declared in a system header. |
| 5317 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 5318 | } |
| 5319 | |
| 5320 | /// Handle a delayed forbidden-type diagnostic. |
| 5321 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 5322 | Decl *decl) { |
| 5323 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 5324 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 5325 | "this system declaration uses an unsupported type")); |
| 5326 | return; |
| 5327 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5328 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5329 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 5330 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5331 | // kind of forbidden type messages on unavailable functions. |
| 5332 | if (FD->hasAttr<UnavailableAttr>() && |
| 5333 | diag.getForbiddenTypeDiagnostic() == |
| 5334 | diag::err_arc_array_param_no_ownership) { |
| 5335 | diag.Triggered = true; |
| 5336 | return; |
| 5337 | } |
| 5338 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5339 | |
| 5340 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 5341 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 5342 | diag.Triggered = true; |
| 5343 | } |
| 5344 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5345 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 5346 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5347 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5348 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5349 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5350 | // When delaying diagnostics to run in the context of a parsed |
| 5351 | // declaration, we only want to actually emit anything if parsing |
| 5352 | // succeeds. |
| 5353 | if (!decl) return; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5354 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5355 | // We emit all the active diagnostics in this pool or any of its |
| 5356 | // parents. In general, we'll get one pool for the decl spec |
| 5357 | // and a child pool for each declarator; in a decl group like: |
| 5358 | // deprecated_typedef foo, *bar, baz(); |
| 5359 | // only the declarator pops will be passed decls. This is correct; |
| 5360 | // we really do need to consider delayed diagnostics from the decl spec |
| 5361 | // for each of the different declarations. |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5362 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5363 | do { |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5364 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5365 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 5366 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 5367 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5368 | if (diag.Triggered) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5369 | continue; |
| 5370 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5371 | switch (diag.Kind) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5372 | case DelayedDiagnostic::Deprecation: |
John McCall | e8c904f | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 5373 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 5374 | if (!decl->isInvalidDecl()) |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5375 | HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5376 | break; |
| 5377 | |
| 5378 | case DelayedDiagnostic::Access: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5379 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5380 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5381 | |
| 5382 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5383 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5384 | break; |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5385 | } |
| 5386 | } |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5387 | } while ((pool = pool->getParent())); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5388 | } |
| 5389 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5390 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 5391 | /// been delayed in the current context instead of in the given pool. |
| 5392 | /// Essentially, this just moves them to the current pool. |
| 5393 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 5394 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 5395 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 5396 | curPool->steal(pool); |
| 5397 | } |
| 5398 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5399 | static bool isDeclDeprecated(Decl *D) { |
| 5400 | do { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5401 | if (D->isDeprecated()) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5402 | return true; |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 5403 | // A category implicitly has the availability of the interface. |
| 5404 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 5405 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5406 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 5407 | return false; |
| 5408 | } |
| 5409 | |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5410 | static void |
| 5411 | DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message, |
| 5412 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5413 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5414 | const ObjCPropertyDecl *ObjCPropery) { |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5415 | DeclarationName Name = D->getDeclName(); |
| 5416 | if (!Message.empty()) { |
| 5417 | S.Diag(Loc, diag::warn_deprecated_message) << Name << Message; |
| 5418 | S.Diag(D->getLocation(), |
| 5419 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5420 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5421 | if (ObjCPropery) |
| 5422 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5423 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5424 | } else if (!UnknownObjCClass) { |
| 5425 | S.Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 5426 | S.Diag(D->getLocation(), |
| 5427 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5428 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5429 | if (ObjCPropery) |
| 5430 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5431 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5432 | } else { |
| 5433 | S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name; |
| 5434 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 5435 | } |
| 5436 | } |
| 5437 | |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 5438 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5439 | Decl *Ctx) { |
| 5440 | if (isDeclDeprecated(Ctx)) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5441 | return; |
| 5442 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5443 | DD.Triggered = true; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5444 | DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(), |
| 5445 | DD.getDeprecationMessage(), DD.Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5446 | DD.getUnknownObjCClass(), |
| 5447 | DD.getObjCProperty()); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5448 | } |
| 5449 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5450 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 5451 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5452 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5453 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5454 | // Delay if we're currently parsing a declaration. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5455 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5456 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 5457 | UnknownObjCClass, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5458 | ObjCProperty, |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5459 | Message)); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5460 | return; |
| 5461 | } |
| 5462 | |
| 5463 | // Otherwise, don't warn if our current context is deprecated. |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 5464 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5465 | return; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5466 | DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5467 | } |