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 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 59 | //===----------------------------------------------------------------------===// |
| 60 | // Helper functions |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 63 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 64 | bool blocksToo = true) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 65 | QualType Ty; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 66 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 67 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 68 | else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 69 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 70 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 71 | Ty = decl->getUnderlyingType(); |
| 72 | else |
| 73 | return 0; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 75 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 76 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 755f9d2 | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 77 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 78 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 79 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 80 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 83 | // FIXME: We should provide an abstraction around a method or function |
| 84 | // to provide the following bits of information. |
| 85 | |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 86 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 87 | /// type (function or function-typed variable). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 88 | static bool isFunction(const Decl *D) { |
| 89 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 93 | /// type (function or function-typed variable) or an Objective-C |
| 94 | /// method. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 95 | static bool isFunctionOrMethod(const Decl *D) { |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 96 | return isFunction(D) || isa<ObjCMethodDecl>(D); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 99 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 100 | /// type (function or function-typed variable) or an Objective-C |
| 101 | /// method or a block. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 102 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 103 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 104 | return true; |
| 105 | // check for block is more involved. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 106 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 107 | QualType Ty = V->getType(); |
| 108 | return Ty->isBlockPointerType(); |
| 109 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 110 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 111 | } |
| 112 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 113 | /// Return true if the given decl has a declarator that should have |
| 114 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 115 | static bool hasDeclarator(const Decl *D) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 116 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 117 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 118 | isa<ObjCPropertyDecl>(D); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 121 | /// hasFunctionProto - Return true if the given decl has a argument |
| 122 | /// information. This decl should have already passed |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 123 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 124 | static bool hasFunctionProto(const Decl *D) { |
| 125 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 126 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 127 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 128 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 129 | return true; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 134 | /// arguments. It is an error to call this on a K&R function (use |
| 135 | /// hasFunctionProto first). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 136 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 137 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 138 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 139 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 140 | return BD->getNumParams(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 141 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 144 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 145 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 146 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 147 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 148 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 149 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 150 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
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 getFunctionOrMethodResultType(const Decl *D) { |
| 154 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 155 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 156 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 159 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 160 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 161 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 162 | return proto->isVariadic(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 163 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | db9a0ae | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 164 | return BD->isVariadic(); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 165 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 166 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 170 | static bool isInstanceMethod(const Decl *D) { |
| 171 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 172 | return MethodDecl->isInstance(); |
| 173 | return false; |
| 174 | } |
| 175 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 176 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 177 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 178 | if (!PT) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 179 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 180 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 181 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 182 | if (!Cls) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 183 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 184 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 185 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 187 | // FIXME: Should we walk the chain of classes? |
| 188 | return ClsName == &Ctx.Idents.get("NSString") || |
| 189 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 190 | } |
| 191 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 192 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 193 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 194 | if (!PT) |
| 195 | return false; |
| 196 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 197 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 198 | if (!RT) |
| 199 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 200 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 201 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 202 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 203 | return false; |
| 204 | |
| 205 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 206 | } |
| 207 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 208 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 209 | /// output an error. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 210 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 211 | unsigned int Num) { |
| 212 | if (Attr.getNumArgs() != Num) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 213 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 214 | << Attr.getName() << Num; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 215 | return false; |
| 216 | } |
| 217 | |
| 218 | return true; |
| 219 | } |
| 220 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 221 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 222 | /// \brief Check if the attribute has at least as many args as Num. May |
| 223 | /// output an error. |
| 224 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
| 225 | unsigned int Num) { |
| 226 | if (Attr.getNumArgs() < Num) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 227 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| 228 | return false; |
| 229 | } |
| 230 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 231 | return true; |
| 232 | } |
| 233 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 234 | /// \brief Check if IdxExpr is a valid argument index for a function or |
| 235 | /// instance method D. May output an error. |
| 236 | /// |
| 237 | /// \returns true if IdxExpr is a valid index. |
| 238 | static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D, |
| 239 | StringRef AttrName, |
| 240 | SourceLocation AttrLoc, |
| 241 | unsigned AttrArgNum, |
| 242 | const Expr *IdxExpr, |
| 243 | uint64_t &Idx) |
| 244 | { |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 245 | assert(isFunctionOrMethod(D)); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 246 | |
| 247 | // In C++ the implicit 'this' function parameter also counts. |
| 248 | // Parameters are counted from one. |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 249 | bool HP = hasFunctionProto(D); |
| 250 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 251 | bool IV = HP && isFunctionOrMethodVariadic(D); |
| 252 | unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) + |
| 253 | HasImplicitThisParam; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 254 | |
| 255 | llvm::APSInt IdxInt; |
| 256 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 257 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 258 | std::string Name = std::string("'") + AttrName.str() + std::string("'"); |
| 259 | S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str() |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 260 | << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | |
| 264 | Idx = IdxInt.getLimitedValue(); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 265 | if (Idx < 1 || (!IV && Idx > NumArgs)) { |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 266 | S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds) |
| 267 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 268 | return false; |
| 269 | } |
| 270 | Idx--; // Convert to zero-based. |
| 271 | if (HasImplicitThisParam) { |
| 272 | if (Idx == 0) { |
| 273 | S.Diag(AttrLoc, |
| 274 | diag::err_attribute_invalid_implicit_this_argument) |
| 275 | << AttrName << IdxExpr->getSourceRange(); |
| 276 | return false; |
| 277 | } |
| 278 | --Idx; |
| 279 | } |
| 280 | |
| 281 | return true; |
| 282 | } |
| 283 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 284 | /// |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 285 | /// \brief Check if passed in Decl is a field or potentially shared global var |
| 286 | /// \return true if the Decl is a field or potentially shared global variable |
| 287 | /// |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 288 | static bool mayBeSharedVariable(const Decl *D) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 289 | if (isa<FieldDecl>(D)) |
| 290 | return true; |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 291 | if (const VarDecl *vd = dyn_cast<VarDecl>(D)) |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 292 | return vd->hasGlobalStorage() && !vd->getTLSKind(); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 293 | |
| 294 | return false; |
| 295 | } |
| 296 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 297 | /// \brief Check if the passed-in expression is of type int or bool. |
| 298 | static bool isIntOrBool(Expr *Exp) { |
| 299 | QualType QT = Exp->getType(); |
| 300 | return QT->isBooleanType() || QT->isIntegerType(); |
| 301 | } |
| 302 | |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 303 | |
| 304 | // Check to see if the type is a smart pointer of some kind. We assume |
| 305 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 306 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 307 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 308 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 309 | if (Res1.empty()) |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 310 | return false; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 311 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 312 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 313 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 314 | if (Res2.empty()) |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 315 | return false; |
| 316 | |
| 317 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 320 | /// \brief Check if passed in Decl is a pointer type. |
| 321 | /// Note that this function may produce an error message. |
| 322 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 323 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 324 | const AttributeList &Attr) { |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 325 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 326 | QualType QT = vd->getType(); |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 327 | if (QT->isAnyPointerType()) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 328 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 329 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 330 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 331 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 332 | // (We don't want to force template instantiation if we can avoid it, |
| 333 | // since that would alter the order in which templates are instantiated.) |
| 334 | if (RT->isIncompleteType()) |
| 335 | return true; |
| 336 | |
| 337 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 338 | return true; |
| 339 | } |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 340 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 341 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 342 | << Attr.getName()->getName() << QT; |
| 343 | } else { |
| 344 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 345 | << Attr.getName(); |
| 346 | } |
| 347 | return false; |
| 348 | } |
| 349 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 350 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 351 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 352 | static const RecordType *getRecordType(QualType QT) { |
| 353 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 354 | return RT; |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 355 | |
| 356 | // Now check if we point to record type. |
| 357 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 358 | return PT->getPointeeType()->getAs<RecordType>(); |
| 359 | |
| 360 | return 0; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 361 | } |
| 362 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 363 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 364 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 365 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 366 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
| 367 | if (RT->getDecl()->getAttr<LockableAttr>()) |
| 368 | return true; |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 373 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 374 | /// resolves to a lockable object. |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 375 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 376 | QualType Ty) { |
| 377 | const RecordType *RT = getRecordType(Ty); |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 378 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 379 | // Warn if could not get record type for this argument. |
Benjamin Kramer | d77ba89 | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 380 | if (!RT) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 381 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 382 | << Attr.getName() << Ty.getAsString(); |
| 383 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 384 | } |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 385 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 386 | // 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] | 387 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 388 | return; |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 389 | |
| 390 | // Allow smart pointers to be used as lockable objects. |
| 391 | // FIXME -- Check the type that the smart pointer points to. |
| 392 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 393 | return; |
| 394 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 395 | // Check if the type is lockable. |
| 396 | RecordDecl *RD = RT->getDecl(); |
| 397 | if (RD->getAttr<LockableAttr>()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 398 | return; |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 399 | |
| 400 | // Else check if any base classes are lockable. |
| 401 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 402 | CXXBasePaths BPaths(false, false); |
| 403 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 404 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 405 | } |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 406 | |
| 407 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 408 | << Attr.getName() << Ty.getAsString(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 411 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 412 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 413 | /// \param Sidx The attribute argument index to start checking with. |
| 414 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 415 | /// parameter list. |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 416 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 417 | const AttributeList &Attr, |
| 418 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 419 | int Sidx = 0, |
| 420 | bool ParamIdxOk = false) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 421 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 422 | Expr *ArgExp = Attr.getArg(Idx); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 423 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 424 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 425 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 426 | Args.push_back(ArgExp); |
| 427 | continue; |
| 428 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 429 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 430 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 431 | if (StrLit->getLength() == 0 || |
| 432 | StrLit->getString() == StringRef("*")) { |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 433 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 434 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 435 | Args.push_back(ArgExp); |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 436 | continue; |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 437 | } |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 438 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 439 | // We allow constant strings to be used as a placeholder for expressions |
| 440 | // that are not valid C++ syntax, but warn that they are ignored. |
| 441 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 442 | Attr.getName(); |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 443 | Args.push_back(ArgExp); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 444 | continue; |
| 445 | } |
| 446 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 447 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 448 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 449 | // A pointer to member expression of the form &MyClass::mu is treated |
| 450 | // specially -- we need to look at the type of the member. |
| 451 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 452 | if (UOp->getOpcode() == UO_AddrOf) |
| 453 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 454 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 455 | ArgTy = DRE->getDecl()->getType(); |
| 456 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 457 | // First see if we can just cast to record type, or point to record type. |
| 458 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 459 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 460 | // Now check if we index into a record type function param. |
| 461 | if(!RT && ParamIdxOk) { |
| 462 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 463 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 464 | if(FD && IL) { |
| 465 | unsigned int NumParams = FD->getNumParams(); |
| 466 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 467 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 468 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 469 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 470 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 471 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 472 | continue; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 473 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 474 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 478 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 479 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 480 | Args.push_back(ArgExp); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 481 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 484 | //===----------------------------------------------------------------------===// |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 485 | // Attribute Implementations |
| 486 | //===----------------------------------------------------------------------===// |
| 487 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 488 | // FIXME: All this manual attribute parsing code is gross. At the |
| 489 | // least add some helper functions to check most argument patterns (# |
| 490 | // and types of args). |
| 491 | |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 492 | enum ThreadAttributeDeclKind { |
| 493 | ThreadExpectedFieldOrGlobalVar, |
| 494 | ThreadExpectedFunctionOrMethod, |
| 495 | ThreadExpectedClassOrStruct |
| 496 | }; |
| 497 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 498 | static bool checkGuardedVarAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 499 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 500 | assert(!Attr.isInvalid()); |
| 501 | |
| 502 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 503 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 504 | |
| 505 | // D must be either a member field or global (potentially shared) variable. |
| 506 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 507 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 508 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 509 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 512 | return true; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 515 | static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 516 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 517 | return; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 518 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 519 | D->addAttr(::new (S.Context) |
| 520 | GuardedVarAttr(Attr.getRange(), S.Context, |
| 521 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 524 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 525 | const AttributeList &Attr) { |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 526 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 527 | return; |
| 528 | |
| 529 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 530 | return; |
| 531 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 532 | D->addAttr(::new (S.Context) |
| 533 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 534 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 537 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 538 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 539 | Expr* &Arg) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 540 | assert(!Attr.isInvalid()); |
| 541 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 542 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 543 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 544 | |
| 545 | // D must be either a member field or global (potentially shared) variable. |
| 546 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 547 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 548 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 549 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 550 | } |
| 551 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 552 | SmallVector<Expr*, 1> Args; |
| 553 | // check that all arguments are lockable objects |
| 554 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 555 | unsigned Size = Args.size(); |
| 556 | if (Size != 1) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 557 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 558 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 559 | Arg = Args[0]; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 560 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 561 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 564 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 565 | Expr *Arg = 0; |
| 566 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 567 | return; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 568 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 569 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| 570 | } |
| 571 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 572 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 573 | const AttributeList &Attr) { |
| 574 | Expr *Arg = 0; |
| 575 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 576 | return; |
| 577 | |
| 578 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 579 | return; |
| 580 | |
| 581 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| 582 | S.Context, Arg)); |
| 583 | } |
| 584 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 585 | static bool checkLockableAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 586 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 587 | assert(!Attr.isInvalid()); |
| 588 | |
| 589 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 590 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 591 | |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 592 | // FIXME: Lockable structs for C code. |
David Blaikie | 88c4b5e | 2013-07-29 18:24:03 +0000 | [diff] [blame] | 593 | if (!isa<RecordDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 594 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 595 | << Attr.getName() << ThreadExpectedClassOrStruct; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 596 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 599 | return true; |
| 600 | } |
| 601 | |
| 602 | static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 603 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 604 | return; |
| 605 | |
| 606 | D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); |
| 607 | } |
| 608 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 609 | static void handleScopedLockableAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 610 | const AttributeList &Attr) { |
| 611 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 612 | return; |
| 613 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 614 | D->addAttr(::new (S.Context) |
| 615 | ScopedLockableAttr(Attr.getRange(), S.Context, |
| 616 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 619 | static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D, |
| 620 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 621 | assert(!Attr.isInvalid()); |
| 622 | |
| 623 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 624 | return; |
| 625 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 626 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 627 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 628 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 629 | return; |
| 630 | } |
| 631 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 632 | D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(), |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 633 | S.Context)); |
| 634 | } |
| 635 | |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 636 | static void handleNoSanitizeAddressAttr(Sema &S, Decl *D, |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 637 | const AttributeList &Attr) { |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 638 | assert(!Attr.isInvalid()); |
| 639 | |
| 640 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 641 | return; |
| 642 | |
| 643 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 644 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 645 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 646 | return; |
| 647 | } |
| 648 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 649 | D->addAttr(::new (S.Context) |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 650 | NoSanitizeAddressAttr(Attr.getRange(), S.Context, |
| 651 | Attr.getAttributeSpellingListIndex())); |
| 652 | } |
| 653 | |
| 654 | static void handleNoSanitizeMemory(Sema &S, Decl *D, |
| 655 | const AttributeList &Attr) { |
| 656 | assert(!Attr.isInvalid()); |
| 657 | |
| 658 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 659 | return; |
| 660 | |
| 661 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 662 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 663 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 664 | return; |
| 665 | } |
| 666 | |
| 667 | D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(), |
| 668 | S.Context)); |
| 669 | } |
| 670 | |
| 671 | static void handleNoSanitizeThread(Sema &S, Decl *D, |
| 672 | const AttributeList &Attr) { |
| 673 | assert(!Attr.isInvalid()); |
| 674 | |
| 675 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 676 | return; |
| 677 | |
| 678 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 679 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 680 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 681 | return; |
| 682 | } |
| 683 | |
| 684 | D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(), |
| 685 | S.Context)); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 688 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 689 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 690 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 691 | assert(!Attr.isInvalid()); |
| 692 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 693 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 694 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 695 | |
| 696 | // D must be either a member field or global (potentially shared) variable. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 697 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 698 | if (!VD || !mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 699 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 700 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
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 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 704 | // Check that this attribute only applies to lockable types. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 705 | QualType QT = VD->getType(); |
| 706 | if (!QT->isDependentType()) { |
| 707 | const RecordType *RT = getRecordType(QT); |
| 708 | if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 709 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 710 | << Attr.getName(); |
| 711 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 715 | // Check that all arguments are lockable objects. |
| 716 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 717 | if (Args.empty()) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 718 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 719 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 720 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 723 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 724 | const AttributeList &Attr) { |
| 725 | SmallVector<Expr*, 1> Args; |
| 726 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 727 | return; |
| 728 | |
| 729 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 730 | D->addAttr(::new (S.Context) |
| 731 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 732 | StartArg, Args.size(), |
| 733 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 736 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 737 | const AttributeList &Attr) { |
| 738 | SmallVector<Expr*, 1> Args; |
| 739 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 740 | return; |
| 741 | |
| 742 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 743 | D->addAttr(::new (S.Context) |
| 744 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 745 | StartArg, Args.size(), |
| 746 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 749 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 750 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 751 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 752 | assert(!Attr.isInvalid()); |
| 753 | |
| 754 | // zero or more arguments ok |
| 755 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 756 | // check that the attribute is applied to a function |
| 757 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 758 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 759 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 760 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 763 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 764 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 765 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 766 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 769 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 770 | const AttributeList &Attr) { |
| 771 | SmallVector<Expr*, 1> Args; |
| 772 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 773 | return; |
| 774 | |
| 775 | unsigned Size = Args.size(); |
| 776 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 777 | D->addAttr(::new (S.Context) |
| 778 | SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 779 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 782 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 783 | const AttributeList &Attr) { |
| 784 | SmallVector<Expr*, 1> Args; |
| 785 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 786 | return; |
| 787 | |
| 788 | unsigned Size = Args.size(); |
| 789 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 790 | D->addAttr(::new (S.Context) |
| 791 | ExclusiveLockFunctionAttr(Attr.getRange(), S.Context, |
| 792 | StartArg, Size, |
| 793 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 794 | } |
| 795 | |
DeLesley Hutchins | 5c6134f | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 796 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 797 | const AttributeList &Attr) { |
| 798 | SmallVector<Expr*, 1> Args; |
| 799 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 800 | return; |
| 801 | |
| 802 | unsigned Size = Args.size(); |
| 803 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 804 | D->addAttr(::new (S.Context) |
| 805 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 806 | Attr.getAttributeSpellingListIndex())); |
| 807 | } |
| 808 | |
| 809 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 810 | const AttributeList &Attr) { |
| 811 | SmallVector<Expr*, 1> Args; |
| 812 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 813 | return; |
| 814 | |
| 815 | unsigned Size = Args.size(); |
| 816 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 817 | D->addAttr(::new (S.Context) |
| 818 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 819 | StartArg, Size, |
| 820 | Attr.getAttributeSpellingListIndex())); |
| 821 | } |
| 822 | |
| 823 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 824 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 825 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 826 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 827 | assert(!Attr.isInvalid()); |
| 828 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 829 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 830 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 831 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 832 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 833 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 834 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 835 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 838 | if (!isIntOrBool(Attr.getArg(0))) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 839 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 840 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 841 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 845 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 846 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 847 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 850 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 851 | const AttributeList &Attr) { |
| 852 | SmallVector<Expr*, 2> Args; |
| 853 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 854 | return; |
| 855 | |
| 856 | unsigned Size = Args.size(); |
| 857 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 858 | D->addAttr(::new (S.Context) |
| 859 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
| 860 | Attr.getArg(0), StartArg, Size, |
| 861 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 864 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 865 | const AttributeList &Attr) { |
| 866 | SmallVector<Expr*, 2> Args; |
| 867 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 868 | return; |
| 869 | |
| 870 | unsigned Size = Args.size(); |
| 871 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 872 | D->addAttr(::new (S.Context) |
| 873 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
| 874 | Attr.getArg(0), StartArg, Size, |
| 875 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 878 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 879 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 880 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 881 | assert(!Attr.isInvalid()); |
| 882 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 883 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 884 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 885 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 886 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 887 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 888 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 889 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 892 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 893 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 894 | if (Args.empty()) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 895 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 896 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 897 | return true; |
| 898 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 899 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 900 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 901 | const AttributeList &Attr) { |
| 902 | SmallVector<Expr*, 1> Args; |
| 903 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 904 | return; |
| 905 | |
| 906 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 907 | D->addAttr(::new (S.Context) |
| 908 | ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context, |
| 909 | StartArg, Args.size(), |
| 910 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 913 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 914 | const AttributeList &Attr) { |
| 915 | SmallVector<Expr*, 1> Args; |
| 916 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 917 | return; |
| 918 | |
| 919 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 920 | D->addAttr(::new (S.Context) |
| 921 | SharedLocksRequiredAttr(Attr.getRange(), S.Context, |
| 922 | StartArg, Args.size(), |
| 923 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 926 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 927 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 928 | assert(!Attr.isInvalid()); |
| 929 | |
| 930 | // zero or more arguments ok |
| 931 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 932 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 933 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 934 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 935 | return; |
| 936 | } |
| 937 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 938 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 939 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 940 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 941 | unsigned Size = Args.size(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 942 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 943 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 944 | D->addAttr(::new (S.Context) |
| 945 | UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 946 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 950 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 951 | assert(!Attr.isInvalid()); |
| 952 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 953 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 954 | return; |
| 955 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 956 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 957 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 958 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 959 | return; |
| 960 | } |
| 961 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 962 | // check that the argument is lockable object |
DeLesley Hutchins | f26efd7 | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 963 | SmallVector<Expr*, 1> Args; |
| 964 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 965 | unsigned Size = Args.size(); |
| 966 | if (Size == 0) |
| 967 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 968 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 969 | D->addAttr(::new (S.Context) |
| 970 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 971 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 975 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 976 | assert(!Attr.isInvalid()); |
| 977 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 978 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 979 | return; |
| 980 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 981 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 982 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 983 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 984 | return; |
| 985 | } |
| 986 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 987 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 988 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 989 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 990 | unsigned Size = Args.size(); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 991 | if (Size == 0) |
| 992 | return; |
| 993 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 994 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 995 | D->addAttr(::new (S.Context) |
| 996 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 997 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 998 | } |
| 999 | |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1000 | static void handleConsumesAttr(Sema &S, Decl *D, |
| 1001 | const AttributeList &Attr) { |
| 1002 | assert(!Attr.isInvalid()); |
| 1003 | if (!checkAttributeNumArgs(S, Attr, 0)) return; |
| 1004 | |
| 1005 | if (!(isa<CXXMethodDecl>(D) || isa<CXXConstructorDecl>(D))) { |
| 1006 | S.Diag(Attr.getLoc(), diag::warn_uniqueness_attribute_wrong_decl_type) << |
| 1007 | Attr.getName(); |
| 1008 | return; |
| 1009 | } |
| 1010 | |
| 1011 | D->addAttr(::new (S.Context) |
| 1012 | ConsumesAttr(Attr.getRange(), S.Context, |
| 1013 | Attr.getAttributeSpellingListIndex())); |
| 1014 | } |
| 1015 | |
| 1016 | static void handleCallableWhenUnconsumedAttr(Sema &S, Decl *D, |
| 1017 | const AttributeList &Attr) { |
| 1018 | assert(!Attr.isInvalid()); |
| 1019 | if (!checkAttributeNumArgs(S, Attr, 0)) return; |
| 1020 | |
| 1021 | if (!isa<CXXMethodDecl>(D)) { |
| 1022 | S.Diag(Attr.getLoc(), diag::warn_uniqueness_attribute_wrong_decl_type) << |
| 1023 | Attr.getName(); |
| 1024 | return; |
| 1025 | } |
| 1026 | |
| 1027 | D->addAttr(::new (S.Context) |
| 1028 | CallableWhenUnconsumedAttr(Attr.getRange(), S.Context, |
| 1029 | Attr.getAttributeSpellingListIndex())); |
| 1030 | } |
| 1031 | |
| 1032 | static void handleTestsConsumedAttr(Sema &S, Decl *D, |
| 1033 | const AttributeList &Attr) { |
| 1034 | assert(!Attr.isInvalid()); |
| 1035 | if (!checkAttributeNumArgs(S, Attr, 0)) return; |
| 1036 | |
| 1037 | if (!isa<CXXMethodDecl>(D)) { |
| 1038 | S.Diag(Attr.getLoc(), diag::warn_uniqueness_attribute_wrong_decl_type) << |
| 1039 | Attr.getName(); |
| 1040 | return; |
| 1041 | } |
| 1042 | |
| 1043 | D->addAttr(::new (S.Context) |
| 1044 | TestsConsumedAttr(Attr.getRange(), S.Context, |
| 1045 | Attr.getAttributeSpellingListIndex())); |
| 1046 | } |
| 1047 | |
| 1048 | static void handleTestsUnconsumedAttr(Sema &S, Decl *D, |
| 1049 | const AttributeList &Attr) { |
| 1050 | assert(!Attr.isInvalid()); |
| 1051 | if (!checkAttributeNumArgs(S, Attr, 0)) return; |
| 1052 | |
| 1053 | if (!isa<CXXMethodDecl>(D)) { |
| 1054 | S.Diag(Attr.getLoc(), diag::warn_uniqueness_attribute_wrong_decl_type) << |
| 1055 | Attr.getName(); |
| 1056 | return; |
| 1057 | } |
| 1058 | |
| 1059 | D->addAttr(::new (S.Context) |
| 1060 | TestsUnconsumedAttr(Attr.getRange(), S.Context, |
| 1061 | Attr.getAttributeSpellingListIndex())); |
| 1062 | } |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 1063 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1064 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1065 | const AttributeList &Attr) { |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1066 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 1067 | if (TD == 0) { |
| 1068 | // __attribute__((ext_vector_type(N))) can only be applied to typedefs |
| 1069 | // and type-ids. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1070 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1071 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1072 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1073 | |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1074 | // Remember this typedef decl, we will need it later for diagnostics. |
| 1075 | S.ExtVectorDecls.push_back(TD); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1078 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1079 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1080 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1081 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1082 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1083 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1084 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1085 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1086 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1087 | // has no effect. |
Eli Friedman | b68ec6b | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1088 | if (!FD->getType()->isDependentType() && |
| 1089 | !FD->getType()->isIncompleteType() && |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1090 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1091 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1092 | << Attr.getName() << FD->getType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1093 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1094 | FD->addAttr(::new (S.Context) |
| 1095 | PackedAttr(Attr.getRange(), S.Context, |
| 1096 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1097 | } else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1098 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1101 | static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 5f608ae | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 1102 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1103 | RD->addAttr(::new (S.Context) |
| 1104 | MsStructAttr(Attr.getRange(), S.Context, |
| 1105 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | c1a0a73 | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 1106 | else |
| 1107 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1108 | } |
| 1109 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1110 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1111 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1112 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1113 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1114 | |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1115 | // The IBAction attributes only apply to instance methods. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1116 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1117 | if (MD->isInstanceMethod()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1118 | D->addAttr(::new (S.Context) |
| 1119 | IBActionAttr(Attr.getRange(), S.Context, |
| 1120 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1121 | return; |
| 1122 | } |
| 1123 | |
Ted Kremenek | 4ee2bb1 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 1124 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1127 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1128 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1129 | // variables or properties of Objective-C classes. The outlet must also |
| 1130 | // have an object reference type. |
| 1131 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1132 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 0bfaf06 | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1133 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1134 | << Attr.getName() << VD->getType() << 0; |
| 1135 | return false; |
| 1136 | } |
| 1137 | } |
| 1138 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1139 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1140 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1141 | << Attr.getName() << PD->getType() << 1; |
| 1142 | return false; |
| 1143 | } |
| 1144 | } |
| 1145 | else { |
| 1146 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1147 | return false; |
| 1148 | } |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1149 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1150 | return true; |
| 1151 | } |
| 1152 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1153 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1154 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1155 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1156 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1157 | |
| 1158 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1159 | return; |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1160 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1161 | D->addAttr(::new (S.Context) |
| 1162 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1163 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1166 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1167 | const AttributeList &Attr) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1168 | |
| 1169 | // The iboutletcollection attribute can have zero or one arguments. |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1170 | if (Attr.getParameterName() && Attr.getNumArgs() > 0) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1171 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1172 | << Attr.getName() << 1; |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1173 | return; |
| 1174 | } |
| 1175 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1176 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1177 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1178 | |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1179 | IdentifierInfo *II = Attr.getParameterName(); |
| 1180 | if (!II) |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1181 | II = &S.Context.Idents.get("NSObject"); |
Fariborz Jahanian | 3a3400b | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 1182 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1183 | ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1184 | S.getScopeForContext(D->getDeclContext()->getParent())); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1185 | if (!TypeRep) { |
| 1186 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1187 | return; |
| 1188 | } |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1189 | QualType QT = TypeRep.get(); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1190 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1191 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1192 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1193 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1194 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1195 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1196 | return; |
| 1197 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1198 | D->addAttr(::new (S.Context) |
| 1199 | IBOutletCollectionAttr(Attr.getRange(),S.Context, |
| 1200 | QT, Attr.getParameterLoc(), |
| 1201 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1204 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1205 | if (const RecordType *UT = T->getAsUnionType()) |
| 1206 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1207 | RecordDecl *UD = UT->getDecl(); |
| 1208 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1209 | itend = UD->field_end(); it != itend; ++it) { |
| 1210 | QualType QT = it->getType(); |
| 1211 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1212 | T = QT; |
| 1213 | return; |
| 1214 | } |
| 1215 | } |
| 1216 | } |
| 1217 | } |
| 1218 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1219 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nuno Lopes | 174930d | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1220 | if (!isFunctionOrMethod(D)) { |
| 1221 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1222 | << Attr.getName() << ExpectedFunctionOrMethod; |
Nuno Lopes | 174930d | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1223 | return; |
| 1224 | } |
| 1225 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1226 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1227 | return; |
| 1228 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1229 | SmallVector<unsigned, 8> SizeArgs; |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1230 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
| 1231 | Expr *Ex = Attr.getArg(i); |
| 1232 | uint64_t Idx; |
| 1233 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1234 | Attr.getLoc(), i + 1, Ex, Idx)) |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1235 | return; |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1236 | |
| 1237 | // check if the function argument is of an integer type |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1238 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1239 | if (!T->isIntegerType()) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 1240 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1241 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 1242 | << Ex->getSourceRange(); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1243 | return; |
| 1244 | } |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1245 | SizeArgs.push_back(Idx); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | // check if the function returns a pointer |
| 1249 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 1250 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1251 | << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1254 | D->addAttr(::new (S.Context) |
| 1255 | AllocSizeAttr(Attr.getRange(), S.Context, |
| 1256 | SizeArgs.data(), SizeArgs.size(), |
| 1257 | Attr.getAttributeSpellingListIndex())); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1260 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1261 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1262 | // ignore it as well |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1263 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1264 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1265 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1266 | return; |
| 1267 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1268 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1269 | SmallVector<unsigned, 8> NonNullArgs; |
| 1270 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
| 1271 | Expr *Ex = Attr.getArg(i); |
| 1272 | uint64_t Idx; |
| 1273 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1274 | Attr.getLoc(), i + 1, Ex, Idx)) |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1275 | return; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1276 | |
| 1277 | // Is the function argument a pointer type? |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1278 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1279 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1280 | |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1281 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1282 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | c9ef405 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1283 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1284 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1285 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1286 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1287 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1288 | NonNullArgs.push_back(Idx); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1289 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1290 | |
| 1291 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1292 | // arguments have a nonnull attribute. |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1293 | if (NonNullArgs.empty()) { |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1294 | for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) { |
| 1295 | QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1296 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1297 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1298 | NonNullArgs.push_back(i); |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1299 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1300 | |
Ted Kremenek | ee1c08c | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1301 | // No pointer arguments? |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1302 | if (NonNullArgs.empty()) { |
| 1303 | // Warn the trivial case only if attribute is not coming from a |
| 1304 | // macro instantiation. |
| 1305 | if (Attr.getLoc().isFileID()) |
| 1306 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1307 | return; |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1308 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1309 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1310 | |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1311 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1312 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1313 | llvm::array_pod_sort(start, start + size); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1314 | D->addAttr(::new (S.Context) |
| 1315 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1316 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1319 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1320 | // This attribute must be applied to a function declaration. |
| 1321 | // The first argument to the attribute must be a string, |
| 1322 | // the name of the resource, for example "malloc". |
| 1323 | // The following arguments must be argument indexes, the arguments must be |
| 1324 | // of integer type for Returns, otherwise of pointer type. |
| 1325 | // 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] | 1326 | // after being held. free() should be __attribute((ownership_takes)), whereas |
| 1327 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1328 | |
| 1329 | if (!AL.getParameterName()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1330 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1331 | << AL.getName()->getName() << 1 << AANT_ArgumentString; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1332 | return; |
| 1333 | } |
| 1334 | // Figure out our Kind, and check arguments while we're at it. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1335 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1336 | switch (AL.getKind()) { |
| 1337 | case AttributeList::AT_ownership_takes: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1338 | K = OwnershipAttr::Takes; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1339 | if (AL.getNumArgs() < 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1340 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1341 | << AL.getName() << 2; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1342 | return; |
| 1343 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1344 | break; |
| 1345 | case AttributeList::AT_ownership_holds: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1346 | K = OwnershipAttr::Holds; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1347 | if (AL.getNumArgs() < 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1348 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1349 | << AL.getName() << 2; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1350 | return; |
| 1351 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1352 | break; |
| 1353 | case AttributeList::AT_ownership_returns: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1354 | K = OwnershipAttr::Returns; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1355 | if (AL.getNumArgs() > 1) { |
| 1356 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1357 | << AL.getName() << AL.getNumArgs() + 1; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1358 | return; |
| 1359 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1360 | break; |
| 1361 | default: |
| 1362 | // This should never happen given how we are called. |
| 1363 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1366 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1367 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1368 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1369 | return; |
| 1370 | } |
| 1371 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1372 | StringRef Module = AL.getParameterName()->getName(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1373 | |
| 1374 | // Normalize the argument, __foo__ becomes foo. |
| 1375 | if (Module.startswith("__") && Module.endswith("__")) |
| 1376 | Module = Module.substr(2, Module.size() - 4); |
| 1377 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1378 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1379 | SmallVector<unsigned, 8> OwnershipArgs; |
| 1380 | for (unsigned i = 0; i < AL.getNumArgs(); ++i) { |
| 1381 | Expr *Ex = AL.getArg(i); |
| 1382 | uint64_t Idx; |
| 1383 | if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(), |
| 1384 | AL.getLoc(), i + 1, Ex, Idx)) |
| 1385 | return; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1386 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1387 | switch (K) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1388 | case OwnershipAttr::Takes: |
| 1389 | case OwnershipAttr::Holds: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1390 | // Is the function argument a pointer type? |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1391 | QualType T = getFunctionOrMethodArgType(D, Idx); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1392 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| 1393 | // FIXME: Should also highlight argument in decl. |
| 1394 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1395 | << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1396 | << "pointer" |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1397 | << Ex->getSourceRange(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1398 | continue; |
| 1399 | } |
| 1400 | break; |
| 1401 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1402 | case OwnershipAttr::Returns: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1403 | if (AL.getNumArgs() > 1) { |
| 1404 | // Is the function argument an integer type? |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1405 | Expr *IdxExpr = AL.getArg(0); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1406 | llvm::APSInt ArgNum(32); |
| 1407 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1408 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1409 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
| 1410 | << "ownership_returns" << "integer" |
| 1411 | << IdxExpr->getSourceRange(); |
| 1412 | return; |
| 1413 | } |
| 1414 | } |
| 1415 | break; |
| 1416 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1417 | } // switch |
| 1418 | |
| 1419 | // Check we don't have a conflict with another ownership attribute. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1420 | for (specific_attr_iterator<OwnershipAttr> |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1421 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1422 | e = D->specific_attr_end<OwnershipAttr>(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1423 | i != e; ++i) { |
| 1424 | if ((*i)->getOwnKind() != K) { |
| 1425 | for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); |
| 1426 | I!=E; ++I) { |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1427 | if (Idx == *I) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1428 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1429 | << AL.getName()->getName() << "ownership_*"; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | } |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1434 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | unsigned* start = OwnershipArgs.data(); |
| 1438 | unsigned size = OwnershipArgs.size(); |
| 1439 | llvm::array_pod_sort(start, start + size); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1440 | |
| 1441 | if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1442 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1443 | << AL.getName() << 2; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1444 | return; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1445 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1446 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1447 | D->addAttr(::new (S.Context) |
| 1448 | OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size, |
| 1449 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1452 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1453 | // Check the attribute arguments. |
| 1454 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1455 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1456 | << Attr.getName() << 1; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1457 | return; |
| 1458 | } |
| 1459 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1460 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1461 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1462 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1463 | return; |
| 1464 | } |
| 1465 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1466 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1467 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1468 | // gcc rejects |
| 1469 | // class c { |
| 1470 | // static int a __attribute__((weakref ("v2"))); |
| 1471 | // static int b() __attribute__((weakref ("f3"))); |
| 1472 | // }; |
| 1473 | // and ignores the attributes of |
| 1474 | // void f(void) { |
| 1475 | // static int a __attribute__((weakref ("v2"))); |
| 1476 | // } |
| 1477 | // we reject them |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1478 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1479 | if (!Ctx->isFileContext()) { |
| 1480 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1481 | nd->getNameAsString(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1482 | return; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | // The GCC manual says |
| 1486 | // |
| 1487 | // At present, a declaration to which `weakref' is attached can only |
| 1488 | // be `static'. |
| 1489 | // |
| 1490 | // It also says |
| 1491 | // |
| 1492 | // Without a TARGET, |
| 1493 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1494 | // equivalent to `weak'. |
| 1495 | // |
| 1496 | // gcc 4.4.1 will accept |
| 1497 | // int a7 __attribute__((weakref)); |
| 1498 | // as |
| 1499 | // int a7 __attribute__((weak)); |
| 1500 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1501 | // it if this behaviour is actually used. |
| 1502 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1503 | // GCC rejects |
| 1504 | // static ((alias ("y"), weakref)). |
| 1505 | // Should we? How to check that weakref is before or after alias? |
| 1506 | |
| 1507 | if (Attr.getNumArgs() == 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1508 | Expr *Arg = Attr.getArg(0); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1509 | Arg = Arg->IgnoreParenCasts(); |
| 1510 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1511 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1512 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1513 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1514 | << Attr.getName() << 1 << AANT_ArgumentString; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1515 | return; |
| 1516 | } |
| 1517 | // GCC will accept anything as the argument of weakref. Should we |
| 1518 | // check for an existing decl? |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1519 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1520 | Str->getString())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1523 | D->addAttr(::new (S.Context) |
| 1524 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1525 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1528 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1529 | // check the attribute arguments. |
Aaron Ballman | ffa9d57 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 1530 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1531 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1532 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1533 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1534 | Arg = Arg->IgnoreParenCasts(); |
| 1535 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1536 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1537 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1538 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1539 | << Attr.getName() << AANT_ArgumentString; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1540 | return; |
| 1541 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1542 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1543 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | f5fe292 | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1544 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1545 | return; |
| 1546 | } |
| 1547 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1548 | // FIXME: check if target symbol exists in current file |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1549 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1550 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1551 | Str->getString(), |
| 1552 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1555 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1556 | // Check the attribute arguments. |
| 1557 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1558 | return; |
| 1559 | |
| 1560 | if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) { |
| 1561 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1562 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1563 | return; |
| 1564 | } |
| 1565 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1566 | D->addAttr(::new (S.Context) |
| 1567 | MinSizeAttr(Attr.getRange(), S.Context, |
| 1568 | Attr.getAttributeSpellingListIndex())); |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1571 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1572 | // Check the attribute arguments. |
| 1573 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1574 | return; |
| 1575 | |
| 1576 | if (!isa<FunctionDecl>(D)) { |
| 1577 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1578 | << Attr.getName() << ExpectedFunction; |
| 1579 | return; |
| 1580 | } |
| 1581 | |
| 1582 | if (D->hasAttr<HotAttr>()) { |
| 1583 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1584 | << Attr.getName() << "hot"; |
| 1585 | return; |
| 1586 | } |
| 1587 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1588 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1589 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1593 | // Check the attribute arguments. |
| 1594 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1595 | return; |
| 1596 | |
| 1597 | if (!isa<FunctionDecl>(D)) { |
| 1598 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1599 | << Attr.getName() << ExpectedFunction; |
| 1600 | return; |
| 1601 | } |
| 1602 | |
| 1603 | if (D->hasAttr<ColdAttr>()) { |
| 1604 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1605 | << Attr.getName() << "cold"; |
| 1606 | return; |
| 1607 | } |
| 1608 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1609 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1610 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1613 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1614 | // Check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1615 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1616 | return; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1617 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1618 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1619 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1620 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1621 | return; |
| 1622 | } |
| 1623 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1624 | D->addAttr(::new (S.Context) |
| 1625 | NakedAttr(Attr.getRange(), S.Context, |
| 1626 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1629 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 1630 | const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1631 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1632 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1633 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1634 | << Attr.getName() << 0; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1635 | return; |
| 1636 | } |
| 1637 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1638 | if (!isa<FunctionDecl>(D)) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1639 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1640 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1641 | return; |
| 1642 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1643 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1644 | D->addAttr(::new (S.Context) |
| 1645 | AlwaysInlineAttr(Attr.getRange(), S.Context, |
| 1646 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1649 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1650 | const AttributeList &Attr) { |
| 1651 | // Check the attribute arguments. |
Aaron Ballman | ffa9d57 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 1652 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1653 | return; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1654 | |
| 1655 | Expr *Arg = Attr.getArg(0); |
| 1656 | Arg = Arg->IgnoreParenCasts(); |
| 1657 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1658 | |
| 1659 | // Check that it is a string. |
| 1660 | if (!Str) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1661 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1662 | << Attr.getName() << AANT_ArgumentString; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1663 | return; |
| 1664 | } |
| 1665 | |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 1666 | if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) { |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1667 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1668 | << Attr.getName() << ExpectedTLSVar; |
| 1669 | return; |
| 1670 | } |
| 1671 | |
| 1672 | // Check that the value. |
| 1673 | StringRef Model = Str->getString(); |
| 1674 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1675 | && Model != "initial-exec" && Model != "local-exec") { |
| 1676 | S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg); |
| 1677 | return; |
| 1678 | } |
| 1679 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1680 | D->addAttr(::new (S.Context) |
| 1681 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1682 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1685 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1686 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1687 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1688 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1689 | << Attr.getName() << 0; |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1690 | return; |
| 1691 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1692 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1693 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1694 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1695 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1696 | D->addAttr(::new (S.Context) |
| 1697 | MallocAttr(Attr.getRange(), S.Context, |
| 1698 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1699 | return; |
| 1700 | } |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1703 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1706 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1707 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1708 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1709 | return; |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1710 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1711 | D->addAttr(::new (S.Context) |
| 1712 | MayAliasAttr(Attr.getRange(), S.Context, |
| 1713 | Attr.getAttributeSpellingListIndex())); |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1716 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1717 | assert(!Attr.isInvalid()); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1718 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1719 | D->addAttr(::new (S.Context) |
| 1720 | NoCommonAttr(Attr.getRange(), S.Context, |
| 1721 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1722 | else |
| 1723 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1724 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1727 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1728 | assert(!Attr.isInvalid()); |
Eli Friedman | 3e1aca2 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1729 | |
| 1730 | if (S.LangOpts.CPlusPlus) { |
| 1731 | S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus); |
| 1732 | return; |
| 1733 | } |
| 1734 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1735 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1736 | D->addAttr(::new (S.Context) |
| 1737 | CommonAttr(Attr.getRange(), S.Context, |
| 1738 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1739 | else |
| 1740 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1741 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1744 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1745 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1746 | |
| 1747 | if (S.CheckNoReturnAttr(attr)) return; |
| 1748 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1749 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1750 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1751 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1752 | return; |
| 1753 | } |
| 1754 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1755 | D->addAttr(::new (S.Context) |
| 1756 | NoReturnAttr(attr.getRange(), S.Context, |
| 1757 | attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1761 | if (attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1762 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1763 | << attr.getName() << 0; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1764 | attr.setInvalid(); |
| 1765 | return true; |
| 1766 | } |
| 1767 | |
| 1768 | return false; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1771 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1772 | const AttributeList &Attr) { |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1773 | |
| 1774 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1775 | // because 'analyzer_noreturn' does not impact the type. |
| 1776 | |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1777 | if(!checkAttributeNumArgs(S, Attr, 0)) |
| 1778 | return; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1779 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1780 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1781 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1782 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1783 | && !VD->getType()->isFunctionPointerType())) { |
| 1784 | S.Diag(Attr.getLoc(), |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1785 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1786 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1787 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1788 | return; |
| 1789 | } |
| 1790 | } |
| 1791 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1792 | D->addAttr(::new (S.Context) |
| 1793 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1794 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1797 | static void handleCXX11NoReturnAttr(Sema &S, Decl *D, |
| 1798 | const AttributeList &Attr) { |
| 1799 | // C++11 [dcl.attr.noreturn]p1: |
| 1800 | // The attribute may be applied to the declarator-id in a function |
| 1801 | // declaration. |
| 1802 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 1803 | if (!FD) { |
| 1804 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1805 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1806 | return; |
| 1807 | } |
| 1808 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1809 | D->addAttr(::new (S.Context) |
| 1810 | CXX11NoReturnAttr(Attr.getRange(), S.Context, |
| 1811 | Attr.getAttributeSpellingListIndex())); |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1814 | // PS3 PPU-specific. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1815 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1816 | /* |
| 1817 | Returning a Vector Class in Registers |
| 1818 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1819 | According to the PPU ABI specifications, a class with a single member of |
| 1820 | vector type is returned in memory when used as the return value of a function. |
| 1821 | This results in inefficient code when implementing vector classes. To return |
| 1822 | the value in a single vector register, add the vecreturn attribute to the |
| 1823 | class definition. This attribute is also applicable to struct types. |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1824 | |
| 1825 | Example: |
| 1826 | |
| 1827 | struct Vector |
| 1828 | { |
| 1829 | __vector float xyzw; |
| 1830 | } __attribute__((vecreturn)); |
| 1831 | |
| 1832 | Vector Add(Vector lhs, Vector rhs) |
| 1833 | { |
| 1834 | Vector result; |
| 1835 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1836 | return result; // This will be returned in a register |
| 1837 | } |
| 1838 | */ |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1839 | if (!isa<RecordDecl>(D)) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1840 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1841 | << Attr.getName() << ExpectedClass; |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1842 | return; |
| 1843 | } |
| 1844 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1845 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1846 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1847 | return; |
| 1848 | } |
| 1849 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1850 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1851 | int count = 0; |
| 1852 | |
| 1853 | if (!isa<CXXRecordDecl>(record)) { |
| 1854 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1855 | return; |
| 1856 | } |
| 1857 | |
| 1858 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1859 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1860 | return; |
| 1861 | } |
| 1862 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1863 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1864 | iter != record->field_end(); iter++) { |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1865 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1866 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1867 | return; |
| 1868 | } |
| 1869 | count++; |
| 1870 | } |
| 1871 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1872 | D->addAttr(::new (S.Context) |
| 1873 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1874 | Attr.getAttributeSpellingListIndex())); |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1877 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1878 | const AttributeList &Attr) { |
| 1879 | if (isa<ParmVarDecl>(D)) { |
| 1880 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1881 | // parameter of a function declaration or lambda. |
| 1882 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1883 | S.Diag(Attr.getLoc(), |
| 1884 | diag::err_carries_dependency_param_not_function_decl); |
| 1885 | return; |
| 1886 | } |
| 1887 | } else if (!isa<FunctionDecl>(D)) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1888 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1889 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1890 | return; |
| 1891 | } |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1892 | |
| 1893 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1894 | Attr.getRange(), S.Context, |
| 1895 | Attr.getAttributeSpellingListIndex())); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1898 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1899 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1900 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1901 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1902 | << Attr.getName() << 0; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1903 | return; |
| 1904 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1905 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1906 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 568eae4 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1907 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1908 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1909 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1910 | return; |
| 1911 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1912 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1913 | D->addAttr(::new (S.Context) |
| 1914 | UnusedAttr(Attr.getRange(), S.Context, |
| 1915 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1918 | static void handleReturnsTwiceAttr(Sema &S, Decl *D, |
| 1919 | const AttributeList &Attr) { |
| 1920 | // check the attribute arguments. |
| 1921 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1922 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1923 | << Attr.getName() << 0; |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1924 | return; |
| 1925 | } |
| 1926 | |
| 1927 | if (!isa<FunctionDecl>(D)) { |
| 1928 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1929 | << Attr.getName() << ExpectedFunction; |
| 1930 | return; |
| 1931 | } |
| 1932 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1933 | D->addAttr(::new (S.Context) |
| 1934 | ReturnsTwiceAttr(Attr.getRange(), S.Context, |
| 1935 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1938 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1939 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1940 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1941 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1942 | << Attr.getName() << 0; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1943 | return; |
| 1944 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1945 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1946 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 29535ba | 2013-08-16 23:18:50 +0000 | [diff] [blame^] | 1947 | if (VD->hasLocalStorage()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1948 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 1949 | return; |
| 1950 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1951 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1952 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1953 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1954 | return; |
| 1955 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1956 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1957 | D->addAttr(::new (S.Context) |
| 1958 | UsedAttr(Attr.getRange(), S.Context, |
| 1959 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1962 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1963 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1964 | if (Attr.getNumArgs() > 1) { |
| 1965 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1966 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1967 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1968 | |
| 1969 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1970 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1971 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1972 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1973 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1974 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1975 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1976 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1977 | << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1978 | return; |
| 1979 | } |
| 1980 | priority = Idx.getZExtValue(); |
| 1981 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1982 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1983 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1984 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1985 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1986 | return; |
| 1987 | } |
| 1988 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1989 | D->addAttr(::new (S.Context) |
| 1990 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1991 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1994 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1995 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1996 | if (Attr.getNumArgs() > 1) { |
| 1997 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1998 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1999 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2000 | |
| 2001 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 2002 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2003 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2004 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2005 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2006 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2007 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2008 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2009 | << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2010 | return; |
| 2011 | } |
| 2012 | priority = Idx.getZExtValue(); |
| 2013 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2014 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2015 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2016 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2017 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2018 | return; |
| 2019 | } |
| 2020 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2021 | D->addAttr(::new (S.Context) |
| 2022 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 2023 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2026 | template <typename AttrTy> |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 2027 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 2028 | const AttributeList &Attr) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2029 | unsigned NumArgs = Attr.getNumArgs(); |
| 2030 | if (NumArgs > 1) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2031 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2032 | return; |
| 2033 | } |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2034 | |
| 2035 | // Handle the case where the attribute has a text message. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2036 | StringRef Str; |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2037 | if (NumArgs == 1) { |
| 2038 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 2039 | if (!SE) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2040 | S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_argument_type) |
| 2041 | << Attr.getName() << AANT_ArgumentString; |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 2042 | return; |
| 2043 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2044 | Str = SE->getString(); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 2045 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2046 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2047 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 2048 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2051 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 2052 | const AttributeList &Attr) { |
Aaron Ballman | faf71a8 | 2013-07-23 15:16:00 +0000 | [diff] [blame] | 2053 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2054 | return; |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2055 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2056 | D->addAttr(::new (S.Context) |
| 2057 | ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context, |
| 2058 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2061 | static void handleObjCRootClassAttr(Sema &S, Decl *D, |
| 2062 | const AttributeList &Attr) { |
| 2063 | if (!isa<ObjCInterfaceDecl>(D)) { |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 2064 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2065 | << Attr.getName() << ExpectedObjectiveCInterface; |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2066 | return; |
| 2067 | } |
| 2068 | |
Aaron Ballman | faf71a8 | 2013-07-23 15:16:00 +0000 | [diff] [blame] | 2069 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2070 | return; |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2071 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2072 | D->addAttr(::new (S.Context) |
| 2073 | ObjCRootClassAttr(Attr.getRange(), S.Context, |
| 2074 | Attr.getAttributeSpellingListIndex())); |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2077 | static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, |
| 2078 | const AttributeList &Attr) { |
Fariborz Jahanian | 341b8be | 2012-01-03 22:52:32 +0000 | [diff] [blame] | 2079 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 2080 | S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis); |
| 2081 | return; |
| 2082 | } |
| 2083 | |
Aaron Ballman | faf71a8 | 2013-07-23 15:16:00 +0000 | [diff] [blame] | 2084 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2085 | return; |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2086 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2087 | D->addAttr(::new (S.Context) |
| 2088 | ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context, |
| 2089 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2092 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2093 | IdentifierInfo *Platform, |
| 2094 | VersionTuple Introduced, |
| 2095 | VersionTuple Deprecated, |
| 2096 | VersionTuple Obsoleted) { |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2097 | StringRef PlatformName |
| 2098 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2099 | if (PlatformName.empty()) |
| 2100 | PlatformName = Platform->getName(); |
| 2101 | |
| 2102 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2103 | // of these steps are needed). |
| 2104 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2105 | !(Introduced <= Deprecated)) { |
| 2106 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2107 | << 1 << PlatformName << Deprecated.getAsString() |
| 2108 | << 0 << Introduced.getAsString(); |
| 2109 | return true; |
| 2110 | } |
| 2111 | |
| 2112 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2113 | !(Introduced <= Obsoleted)) { |
| 2114 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2115 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2116 | << 0 << Introduced.getAsString(); |
| 2117 | return true; |
| 2118 | } |
| 2119 | |
| 2120 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2121 | !(Deprecated <= Obsoleted)) { |
| 2122 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2123 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2124 | << 1 << Deprecated.getAsString(); |
| 2125 | return true; |
| 2126 | } |
| 2127 | |
| 2128 | return false; |
| 2129 | } |
| 2130 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2131 | /// \brief Check whether the two versions match. |
| 2132 | /// |
| 2133 | /// If either version tuple is empty, then they are assumed to match. If |
| 2134 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2135 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2136 | bool BeforeIsOkay) { |
| 2137 | if (X.empty() || Y.empty()) |
| 2138 | return true; |
| 2139 | |
| 2140 | if (X == Y) |
| 2141 | return true; |
| 2142 | |
| 2143 | if (BeforeIsOkay && X < Y) |
| 2144 | return true; |
| 2145 | |
| 2146 | return false; |
| 2147 | } |
| 2148 | |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2149 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2150 | IdentifierInfo *Platform, |
| 2151 | VersionTuple Introduced, |
| 2152 | VersionTuple Deprecated, |
| 2153 | VersionTuple Obsoleted, |
| 2154 | bool IsUnavailable, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2155 | StringRef Message, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2156 | bool Override, |
| 2157 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2158 | VersionTuple MergedIntroduced = Introduced; |
| 2159 | VersionTuple MergedDeprecated = Deprecated; |
| 2160 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2161 | bool FoundAny = false; |
| 2162 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2163 | if (D->hasAttrs()) { |
| 2164 | AttrVec &Attrs = D->getAttrs(); |
| 2165 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 2166 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 2167 | if (!OldAA) { |
| 2168 | ++i; |
| 2169 | continue; |
| 2170 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2171 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2172 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2173 | if (OldPlatform != Platform) { |
| 2174 | ++i; |
| 2175 | continue; |
| 2176 | } |
| 2177 | |
| 2178 | FoundAny = true; |
| 2179 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2180 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2181 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2182 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2183 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2184 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 2185 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 2186 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 2187 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 72daa3f | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 2188 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2189 | if (Override) { |
| 2190 | int Which = -1; |
| 2191 | VersionTuple FirstVersion; |
| 2192 | VersionTuple SecondVersion; |
| 2193 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 2194 | Which = 0; |
| 2195 | FirstVersion = OldIntroduced; |
| 2196 | SecondVersion = Introduced; |
| 2197 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 2198 | Which = 1; |
| 2199 | FirstVersion = Deprecated; |
| 2200 | SecondVersion = OldDeprecated; |
| 2201 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 2202 | Which = 2; |
| 2203 | FirstVersion = Obsoleted; |
| 2204 | SecondVersion = OldObsoleted; |
| 2205 | } |
| 2206 | |
| 2207 | if (Which == -1) { |
| 2208 | Diag(OldAA->getLocation(), |
| 2209 | diag::warn_mismatched_availability_override_unavail) |
| 2210 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2211 | } else { |
| 2212 | Diag(OldAA->getLocation(), |
| 2213 | diag::warn_mismatched_availability_override) |
| 2214 | << Which |
| 2215 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2216 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 2217 | } |
| 2218 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2219 | } else { |
| 2220 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2221 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2222 | } |
| 2223 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2224 | Attrs.erase(Attrs.begin() + i); |
| 2225 | --e; |
| 2226 | continue; |
| 2227 | } |
| 2228 | |
| 2229 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2230 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2231 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2232 | |
| 2233 | if (MergedIntroduced2.empty()) |
| 2234 | MergedIntroduced2 = OldIntroduced; |
| 2235 | if (MergedDeprecated2.empty()) |
| 2236 | MergedDeprecated2 = OldDeprecated; |
| 2237 | if (MergedObsoleted2.empty()) |
| 2238 | MergedObsoleted2 = OldObsoleted; |
| 2239 | |
| 2240 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2241 | MergedIntroduced2, MergedDeprecated2, |
| 2242 | MergedObsoleted2)) { |
| 2243 | Attrs.erase(Attrs.begin() + i); |
| 2244 | --e; |
| 2245 | continue; |
| 2246 | } |
| 2247 | |
| 2248 | MergedIntroduced = MergedIntroduced2; |
| 2249 | MergedDeprecated = MergedDeprecated2; |
| 2250 | MergedObsoleted = MergedObsoleted2; |
| 2251 | ++i; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2252 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | if (FoundAny && |
| 2256 | MergedIntroduced == Introduced && |
| 2257 | MergedDeprecated == Deprecated && |
| 2258 | MergedObsoleted == Obsoleted) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2259 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2260 | |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2261 | // Only create a new attribute if !Override, but we want to do |
| 2262 | // the checking. |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2263 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2264 | MergedDeprecated, MergedObsoleted) && |
| 2265 | !Override) { |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2266 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 2267 | Introduced, Deprecated, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2268 | Obsoleted, IsUnavailable, Message, |
| 2269 | AttrSpellingListIndex); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2270 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2271 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2274 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2275 | const AttributeList &Attr) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2276 | IdentifierInfo *Platform = Attr.getParameterName(); |
| 2277 | SourceLocation PlatformLoc = Attr.getParameterLoc(); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2278 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2279 | |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2280 | if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2281 | S.Diag(PlatformLoc, diag::warn_availability_unknown_platform) |
| 2282 | << Platform; |
| 2283 | |
Rafael Espindola | 8c4222a | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2284 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 2285 | if (!ND) { |
| 2286 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2287 | return; |
| 2288 | } |
| 2289 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2290 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2291 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2292 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2293 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2294 | StringRef Str; |
| 2295 | const StringLiteral *SE = |
| 2296 | dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr()); |
| 2297 | if (SE) |
| 2298 | Str = SE->getString(); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2299 | |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2300 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2301 | Platform, |
| 2302 | Introduced.Version, |
| 2303 | Deprecated.Version, |
| 2304 | Obsoleted.Version, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2305 | IsUnavailable, Str, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2306 | /*Override=*/false, |
| 2307 | Index); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2308 | if (NewAttr) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2309 | D->addAttr(NewAttr); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2312 | template <class T> |
| 2313 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2314 | typename T::VisibilityType value, |
| 2315 | unsigned attrSpellingListIndex) { |
| 2316 | T *existingAttr = D->getAttr<T>(); |
| 2317 | if (existingAttr) { |
| 2318 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2319 | if (existingValue == value) |
| 2320 | return NULL; |
| 2321 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2322 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2323 | D->dropAttr<T>(); |
| 2324 | } |
| 2325 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2326 | } |
| 2327 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2328 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2329 | VisibilityAttr::VisibilityType Vis, |
| 2330 | unsigned AttrSpellingListIndex) { |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2331 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2332 | AttrSpellingListIndex); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2335 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2336 | TypeVisibilityAttr::VisibilityType Vis, |
| 2337 | unsigned AttrSpellingListIndex) { |
| 2338 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2339 | AttrSpellingListIndex); |
| 2340 | } |
| 2341 | |
| 2342 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2343 | bool isTypeVisibility) { |
| 2344 | // Visibility attributes don't mean anything on a typedef. |
| 2345 | if (isa<TypedefNameDecl>(D)) { |
| 2346 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2347 | << Attr.getName(); |
| 2348 | return; |
| 2349 | } |
| 2350 | |
| 2351 | // 'type_visibility' can only go on a type or namespace. |
| 2352 | if (isTypeVisibility && |
| 2353 | !(isa<TagDecl>(D) || |
| 2354 | isa<ObjCInterfaceDecl>(D) || |
| 2355 | isa<NamespaceDecl>(D))) { |
| 2356 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2357 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2358 | return; |
| 2359 | } |
| 2360 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2361 | // check the attribute arguments. |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2362 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2363 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2364 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2365 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2366 | Arg = Arg->IgnoreParenCasts(); |
| 2367 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2368 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2369 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2370 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 2371 | << Attr.getName() << AANT_ArgumentString; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2372 | return; |
| 2373 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2374 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2375 | StringRef TypeStr = Str->getString(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2376 | VisibilityAttr::VisibilityType type; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2377 | |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2378 | if (TypeStr == "default") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2379 | type = VisibilityAttr::Default; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2380 | else if (TypeStr == "hidden") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2381 | type = VisibilityAttr::Hidden; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2382 | else if (TypeStr == "internal") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2383 | type = VisibilityAttr::Hidden; // FIXME |
John McCall | 4188760 | 2012-01-29 01:20:30 +0000 | [diff] [blame] | 2384 | else if (TypeStr == "protected") { |
| 2385 | // Complain about attempts to use protected visibility on targets |
| 2386 | // (like Darwin) that don't support it. |
| 2387 | if (!S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2388 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2389 | type = VisibilityAttr::Default; |
| 2390 | } else { |
| 2391 | type = VisibilityAttr::Protected; |
| 2392 | } |
| 2393 | } else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2394 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2395 | return; |
| 2396 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2397 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2398 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2399 | clang::Attr *newAttr; |
| 2400 | if (isTypeVisibility) { |
| 2401 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2402 | (TypeVisibilityAttr::VisibilityType) type, |
| 2403 | Index); |
| 2404 | } else { |
| 2405 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2406 | } |
| 2407 | if (newAttr) |
| 2408 | D->addAttr(newAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2409 | } |
| 2410 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2411 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2412 | const AttributeList &Attr) { |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2413 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 2414 | if (!method) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2415 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2416 | << ExpectedMethod; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2417 | return; |
| 2418 | } |
| 2419 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2420 | if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) { |
| 2421 | if (!Attr.getParameterName() && Attr.getNumArgs() == 1) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2422 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2423 | << Attr.getName() << 1 << AANT_ArgumentString; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2424 | } else { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2425 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2426 | << Attr.getName() << 0; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2427 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2428 | Attr.setInvalid(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2429 | return; |
| 2430 | } |
| 2431 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2432 | StringRef param = Attr.getParameterName()->getName(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2433 | ObjCMethodFamilyAttr::FamilyKind family; |
| 2434 | if (param == "none") |
| 2435 | family = ObjCMethodFamilyAttr::OMF_None; |
| 2436 | else if (param == "alloc") |
| 2437 | family = ObjCMethodFamilyAttr::OMF_alloc; |
| 2438 | else if (param == "copy") |
| 2439 | family = ObjCMethodFamilyAttr::OMF_copy; |
| 2440 | else if (param == "init") |
| 2441 | family = ObjCMethodFamilyAttr::OMF_init; |
| 2442 | else if (param == "mutableCopy") |
| 2443 | family = ObjCMethodFamilyAttr::OMF_mutableCopy; |
| 2444 | else if (param == "new") |
| 2445 | family = ObjCMethodFamilyAttr::OMF_new; |
| 2446 | else { |
| 2447 | // Just warn and ignore it. This is future-proof against new |
| 2448 | // families being used in system headers. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2449 | S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2450 | return; |
| 2451 | } |
| 2452 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2453 | if (family == ObjCMethodFamilyAttr::OMF_init && |
| 2454 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2455 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2456 | << method->getResultType(); |
| 2457 | // Ignore the attribute. |
| 2458 | return; |
| 2459 | } |
| 2460 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2461 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2462 | S.Context, family)); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2463 | } |
| 2464 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2465 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 2466 | const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2467 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2468 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2469 | |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2470 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 2471 | if (OCI == 0) { |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 2472 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2473 | << Attr.getName() << ExpectedObjectiveCInterface; |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2474 | return; |
| 2475 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2476 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2477 | D->addAttr(::new (S.Context) |
| 2478 | ObjCExceptionAttr(Attr.getRange(), S.Context, |
| 2479 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2482 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 081c883 | 2013-07-23 12:13:14 +0000 | [diff] [blame] | 2483 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2484 | return; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2485 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2486 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2487 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2488 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2489 | return; |
| 2490 | } |
| 2491 | } |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2492 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2493 | QualType T = PD->getType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2494 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2495 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2496 | return; |
| 2497 | } |
| 2498 | } |
| 2499 | else { |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2500 | // It is okay to include this attribute on properties, e.g.: |
| 2501 | // |
| 2502 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2503 | // |
| 2504 | // In this case it follows tradition and suppresses an error in the above |
| 2505 | // case. |
Fariborz Jahanian | 9b2eb7b | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2506 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2507 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2508 | D->addAttr(::new (S.Context) |
| 2509 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2510 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2511 | } |
| 2512 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2513 | static void |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2514 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 081c883 | 2013-07-23 12:13:14 +0000 | [diff] [blame] | 2515 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2516 | return; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2517 | |
| 2518 | if (!isa<FunctionDecl>(D)) { |
| 2519 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 2520 | return; |
| 2521 | } |
| 2522 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2523 | D->addAttr(::new (S.Context) |
| 2524 | OverloadableAttr(Attr.getRange(), S.Context, |
| 2525 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2526 | } |
| 2527 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2528 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2529 | if (!Attr.getParameterName()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2530 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2531 | << Attr.getName() << 1 << AANT_ArgumentString; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2532 | return; |
| 2533 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2534 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2535 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2536 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2537 | << Attr.getName() << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2538 | return; |
| 2539 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2540 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2541 | BlocksAttr::BlockType type; |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 2542 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2543 | type = BlocksAttr::ByRef; |
| 2544 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2545 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2546 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2547 | return; |
| 2548 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2549 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2550 | D->addAttr(::new (S.Context) |
| 2551 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2552 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2555 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2556 | // check the attribute arguments. |
| 2557 | if (Attr.getNumArgs() > 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2558 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2559 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2562 | unsigned sentinel = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2563 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2564 | Expr *E = Attr.getArg(0); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2565 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2566 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2567 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2568 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2569 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2570 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2571 | return; |
| 2572 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2573 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2574 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2575 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2576 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2577 | return; |
| 2578 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2579 | |
| 2580 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2581 | } |
| 2582 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2583 | unsigned nullPos = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2584 | if (Attr.getNumArgs() > 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2585 | Expr *E = Attr.getArg(1); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2586 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2587 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2588 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2589 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2590 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2591 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2592 | return; |
| 2593 | } |
| 2594 | nullPos = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2595 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2596 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2597 | // FIXME: This error message could be improved, it would be nice |
| 2598 | // to say what the bounds actually are. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2599 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2600 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2601 | return; |
| 2602 | } |
| 2603 | } |
| 2604 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2605 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2606 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2607 | if (isa<FunctionNoProtoType>(FT)) { |
| 2608 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2609 | return; |
| 2610 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2611 | |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2612 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2613 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2614 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2615 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2616 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2617 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2618 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2619 | return; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2620 | } |
Eli Friedman | a0b2ba1 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2621 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2622 | if (!BD->isVariadic()) { |
| 2623 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2624 | return; |
| 2625 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2626 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2627 | QualType Ty = V->getType(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2628 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2629 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2630 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2631 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2632 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2633 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2634 | return; |
| 2635 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2636 | } else { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2637 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2638 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2639 | return; |
| 2640 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2641 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2642 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2643 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2644 | return; |
| 2645 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2646 | D->addAttr(::new (S.Context) |
| 2647 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2648 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 2651 | static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2652 | // Check the attribute arguments. |
| 2653 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 2654 | return; |
| 2655 | |
| 2656 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
| 2657 | RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context)); |
| 2658 | else |
| 2659 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2660 | } |
| 2661 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2662 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2663 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2664 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2665 | return; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2666 | |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 2667 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2668 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Kaelyn Uhrain | d449c79 | 2012-11-13 00:18:47 +0000 | [diff] [blame] | 2669 | << Attr.getName() << ExpectedFunctionMethodOrClass; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2670 | return; |
| 2671 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2672 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2673 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2674 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2675 | << Attr.getName() << 0; |
Nuno Lopes | f857798 | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2676 | return; |
| 2677 | } |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2678 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2679 | if (MD->getResultType()->isVoidType()) { |
| 2680 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2681 | << Attr.getName() << 1; |
| 2682 | return; |
| 2683 | } |
| 2684 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2685 | D->addAttr(::new (S.Context) |
| 2686 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2687 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2690 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2691 | // check the attribute arguments. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2692 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2693 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2694 | << Attr.getName() << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2695 | return; |
| 2696 | } |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2697 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2698 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
Fariborz Jahanian | 13c7fcc | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 2699 | if (isa<CXXRecordDecl>(D)) { |
| 2700 | D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| 2701 | return; |
| 2702 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2703 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2704 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | f23ecd9 | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 2705 | return; |
| 2706 | } |
| 2707 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2708 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 2709 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2710 | nd->addAttr(::new (S.Context) |
| 2711 | WeakAttr(Attr.getRange(), S.Context, |
| 2712 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2713 | } |
| 2714 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2715 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2716 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2717 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2718 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2719 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2720 | |
| 2721 | // weak_import only applies to variable & function declarations. |
| 2722 | bool isDef = false; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2723 | if (!D->canBeWeakImported(isDef)) { |
| 2724 | if (isDef) |
Reid Kleckner | beba3e8 | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2725 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2726 | << "weak_import"; |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2727 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2728 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 90eed21 | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2729 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2730 | // Nothing to warn about here. |
| 2731 | } else |
Fariborz Jahanian | c034974 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2732 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2733 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2734 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2735 | return; |
| 2736 | } |
| 2737 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2738 | D->addAttr(::new (S.Context) |
| 2739 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2740 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2743 | // Handles reqd_work_group_size and work_group_size_hint. |
| 2744 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2745 | const AttributeList &Attr) { |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2746 | assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2747 | || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint); |
| 2748 | |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2749 | // Attribute has 3 arguments. |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2750 | if (!checkAttributeNumArgs(S, Attr, 3)) return; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2751 | |
| 2752 | unsigned WGSize[3]; |
| 2753 | for (unsigned i = 0; i < 3; ++i) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2754 | Expr *E = Attr.getArg(i); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2755 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2756 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2757 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 2758 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 2759 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 2760 | << E->getSourceRange(); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2761 | return; |
| 2762 | } |
| 2763 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 2764 | } |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2765 | |
| 2766 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2767 | && D->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 2768 | ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>(); |
| 2769 | if (!(A->getXDim() == WGSize[0] && |
| 2770 | A->getYDim() == WGSize[1] && |
| 2771 | A->getZDim() == WGSize[2])) { |
| 2772 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2773 | Attr.getName(); |
| 2774 | } |
| 2775 | } |
| 2776 | |
| 2777 | if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint |
| 2778 | && D->hasAttr<WorkGroupSizeHintAttr>()) { |
| 2779 | WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>(); |
| 2780 | if (!(A->getXDim() == WGSize[0] && |
| 2781 | A->getYDim() == WGSize[1] && |
| 2782 | A->getZDim() == WGSize[2])) { |
| 2783 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2784 | Attr.getName(); |
| 2785 | } |
| 2786 | } |
| 2787 | |
| 2788 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize) |
| 2789 | D->addAttr(::new (S.Context) |
| 2790 | ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2791 | WGSize[0], WGSize[1], WGSize[2], |
| 2792 | Attr.getAttributeSpellingListIndex())); |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2793 | else |
| 2794 | D->addAttr(::new (S.Context) |
| 2795 | WorkGroupSizeHintAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2796 | WGSize[0], WGSize[1], WGSize[2], |
| 2797 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2800 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2801 | assert(Attr.getKind() == AttributeList::AT_VecTypeHint); |
| 2802 | |
| 2803 | // Attribute has 1 argument. |
| 2804 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 2805 | return; |
| 2806 | |
| 2807 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg()); |
| 2808 | |
| 2809 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2810 | (ParmType->isBooleanType() || |
| 2811 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2812 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2813 | << ParmType; |
| 2814 | return; |
| 2815 | } |
| 2816 | |
| 2817 | if (Attr.getKind() == AttributeList::AT_VecTypeHint && |
| 2818 | D->hasAttr<VecTypeHintAttr>()) { |
| 2819 | VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>(); |
| 2820 | if (A->getTypeHint() != ParmType) { |
| 2821 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2822 | return; |
| 2823 | } |
| 2824 | } |
| 2825 | |
| 2826 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
| 2827 | ParmType, Attr.getLoc())); |
| 2828 | } |
| 2829 | |
Joey Gouly | 96cead5 | 2013-03-14 09:54:43 +0000 | [diff] [blame] | 2830 | static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2831 | if (!dyn_cast<VarDecl>(D)) |
| 2832 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian" |
| 2833 | << 9; |
| 2834 | StringRef EndianType = Attr.getParameterName()->getName(); |
| 2835 | if (EndianType != "host" && EndianType != "device") |
| 2836 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType; |
| 2837 | } |
| 2838 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2839 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2840 | StringRef Name, |
| 2841 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2842 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2843 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2844 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2845 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2846 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2847 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2848 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2849 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2850 | AttrSpellingListIndex); |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2853 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2854 | // Attribute has no arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2855 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2856 | return; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2857 | |
| 2858 | // Make sure that there is a string literal as the sections's single |
| 2859 | // argument. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2860 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2861 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2862 | if (!SE) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2863 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
| 2864 | << Attr.getName() << AANT_ArgumentString; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2865 | return; |
| 2866 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2867 | |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2868 | // If the target wants to validate the section specifier, make it happen. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2869 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString()); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2870 | if (!Error.empty()) { |
| 2871 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 2872 | << Error; |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2873 | return; |
| 2874 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2875 | |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2876 | // This attribute cannot be applied to local variables. |
| 2877 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 2878 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 2879 | return; |
| 2880 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2881 | |
| 2882 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2883 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2884 | SE->getString(), Index); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2885 | if (NewAttr) |
| 2886 | D->addAttr(NewAttr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2889 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2890 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2891 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2892 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2893 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2894 | << Attr.getName() << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2895 | return; |
| 2896 | } |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2897 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2898 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2899 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2900 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2901 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2902 | D->addAttr(::new (S.Context) |
| 2903 | NoThrowAttr(Attr.getRange(), S.Context, |
| 2904 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2905 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2906 | } |
| 2907 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2908 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2909 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2910 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2911 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2912 | << Attr.getName() << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2913 | return; |
| 2914 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2915 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2916 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2917 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2918 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2919 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2920 | D->addAttr(::new (S.Context) |
| 2921 | ConstAttr(Attr.getRange(), S.Context, |
| 2922 | Attr.getAttributeSpellingListIndex() )); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2923 | } |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2924 | } |
| 2925 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2926 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2927 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2928 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2929 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2930 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2931 | D->addAttr(::new (S.Context) |
| 2932 | PureAttr(Attr.getRange(), S.Context, |
| 2933 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2936 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2937 | if (!Attr.getParameterName()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2938 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2939 | << Attr.getName() << 1; |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2940 | return; |
| 2941 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2942 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2943 | if (Attr.getNumArgs() != 0) { |
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() << 1; |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +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 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2950 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2951 | if (!VD || !VD->hasLocalStorage()) { |
| 2952 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 2953 | return; |
| 2954 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2955 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2956 | // Look up the function |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2957 | // FIXME: Lookup probably isn't looking in the right place |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2958 | NamedDecl *CleanupDecl |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2959 | = S.LookupSingleName(S.TUScope, Attr.getParameterName(), |
| 2960 | Attr.getParameterLoc(), Sema::LookupOrdinaryName); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2961 | if (!CleanupDecl) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2962 | S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2963 | Attr.getParameterName(); |
| 2964 | return; |
| 2965 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2966 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2967 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 2968 | if (!FD) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2969 | S.Diag(Attr.getParameterLoc(), |
| 2970 | diag::err_attribute_cleanup_arg_not_function) |
| 2971 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2972 | return; |
| 2973 | } |
| 2974 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2975 | if (FD->getNumParams() != 1) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2976 | S.Diag(Attr.getParameterLoc(), |
| 2977 | diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2978 | << Attr.getParameterName(); |
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 | |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2982 | // We're currently more strict than GCC about what function types we accept. |
| 2983 | // If this ever proves to be a problem it should be easy to fix. |
| 2984 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2985 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2986 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2987 | ParamTy, Ty) != Sema::Compatible) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2988 | S.Diag(Attr.getParameterLoc(), |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2989 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 2990 | Attr.getParameterName() << ParamTy << Ty; |
| 2991 | return; |
| 2992 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2993 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2994 | D->addAttr(::new (S.Context) |
| 2995 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2996 | Attr.getAttributeSpellingListIndex())); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2997 | S.MarkFunctionReferenced(Attr.getParameterLoc(), FD); |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 2998 | S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc()); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2999 | } |
| 3000 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3001 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3002 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3003 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3004 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3005 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3006 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3007 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3008 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3009 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3010 | return; |
| 3011 | } |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3012 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3013 | Expr *IdxExpr = Attr.getArg(0); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 3014 | uint64_t ArgIdx; |
| 3015 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 3016 | Attr.getLoc(), 1, IdxExpr, ArgIdx)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3017 | return; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3018 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3019 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3020 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3021 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3022 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 3023 | if (not_nsstring_type && |
| 3024 | !isCFStringType(Ty, S.Context) && |
| 3025 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3026 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3027 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 3028 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3029 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3030 | << IdxExpr->getSourceRange(); |
| 3031 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3032 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3033 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3034 | if (!isNSStringType(Ty, S.Context) && |
| 3035 | !isCFStringType(Ty, S.Context) && |
| 3036 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3037 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3038 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 3039 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3040 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3041 | << IdxExpr->getSourceRange(); |
| 3042 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3043 | } |
| 3044 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 3045 | // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex |
| 3046 | // because that has corrected for the implicit this parameter, and is zero- |
| 3047 | // based. The attribute expects what the user wrote explicitly. |
| 3048 | llvm::APSInt Val; |
| 3049 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 3050 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3051 | D->addAttr(::new (S.Context) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 3052 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3053 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3056 | enum FormatAttrKind { |
| 3057 | CFStringFormat, |
| 3058 | NSStringFormat, |
| 3059 | StrftimeFormat, |
| 3060 | SupportedFormat, |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3061 | IgnoredFormat, |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3062 | InvalidFormat |
| 3063 | }; |
| 3064 | |
| 3065 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 3066 | /// types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3067 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3068 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 3069 | // Check for formats that get handled specially. |
| 3070 | .Case("NSString", NSStringFormat) |
| 3071 | .Case("CFString", CFStringFormat) |
| 3072 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3073 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3074 | // Otherwise, check for supported formats. |
| 3075 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 3076 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 3077 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3078 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3079 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 3080 | .Default(InvalidFormat); |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3083 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3084 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3085 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 3086 | const AttributeList &Attr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3087 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3088 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3089 | return; |
| 3090 | } |
| 3091 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3092 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3093 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3094 | Attr.setInvalid(); |
| 3095 | return; |
| 3096 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3097 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3098 | if (S.Context.getAsArrayType(T)) |
| 3099 | T = S.Context.getBaseElementType(T); |
| 3100 | if (!T->getAs<RecordType>()) { |
| 3101 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3102 | Attr.setInvalid(); |
| 3103 | return; |
| 3104 | } |
| 3105 | |
Aaron Ballman | ffa9d57 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3106 | if (!checkAttributeNumArgs(S, Attr, 1)) { |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3107 | Attr.setInvalid(); |
| 3108 | return; |
| 3109 | } |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3110 | Expr *priorityExpr = Attr.getArg(0); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3111 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3112 | llvm::APSInt priority(32); |
| 3113 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 3114 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 3115 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3116 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3117 | << priorityExpr->getSourceRange(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3118 | Attr.setInvalid(); |
| 3119 | return; |
| 3120 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 3121 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3122 | if (prioritynum < 101 || prioritynum > 65535) { |
| 3123 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 3124 | << priorityExpr->getSourceRange(); |
| 3125 | Attr.setInvalid(); |
| 3126 | return; |
| 3127 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3128 | D->addAttr(::new (S.Context) |
| 3129 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 3130 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3133 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3134 | int FormatIdx, int FirstArg, |
| 3135 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3136 | // Check whether we already have an equivalent format attribute. |
| 3137 | for (specific_attr_iterator<FormatAttr> |
| 3138 | i = D->specific_attr_begin<FormatAttr>(), |
| 3139 | e = D->specific_attr_end<FormatAttr>(); |
| 3140 | i != e ; ++i) { |
| 3141 | FormatAttr *f = *i; |
| 3142 | if (f->getType() == Format && |
| 3143 | f->getFormatIdx() == FormatIdx && |
| 3144 | f->getFirstArg() == FirstArg) { |
| 3145 | // If we don't have a valid location for this attribute, adopt the |
| 3146 | // location. |
| 3147 | if (f->getLocation().isInvalid()) |
| 3148 | f->setRange(Range); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3149 | return NULL; |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3150 | } |
| 3151 | } |
| 3152 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3153 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg, |
| 3154 | AttrSpellingListIndex); |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3157 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3158 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3159 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3160 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3161 | if (!Attr.getParameterName()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3162 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3163 | << Attr.getName() << 1 << AANT_ArgumentString; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3164 | return; |
| 3165 | } |
| 3166 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3167 | if (Attr.getNumArgs() != 2) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3168 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3169 | << Attr.getName() << 3; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3170 | return; |
| 3171 | } |
| 3172 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3173 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3174 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3175 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3176 | return; |
| 3177 | } |
| 3178 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3179 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3180 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3181 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 3182 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3183 | unsigned FirstIdx = 1; |
| 3184 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3185 | StringRef Format = Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3186 | |
| 3187 | // Normalize the argument, __foo__ becomes foo. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3188 | if (Format.startswith("__") && Format.endswith("__")) |
| 3189 | Format = Format.substr(2, Format.size() - 4); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3190 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3191 | // Check for supported formats. |
| 3192 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3193 | |
| 3194 | if (Kind == IgnoredFormat) |
| 3195 | return; |
| 3196 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3197 | if (Kind == InvalidFormat) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3198 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3199 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3200 | return; |
| 3201 | } |
| 3202 | |
| 3203 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3204 | Expr *IdxExpr = Attr.getArg(0); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3205 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3206 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 3207 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3208 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3209 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3210 | << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3211 | return; |
| 3212 | } |
| 3213 | |
| 3214 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3215 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3216 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3217 | return; |
| 3218 | } |
| 3219 | |
| 3220 | // FIXME: Do we need to bounds check? |
| 3221 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3222 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3223 | if (HasImplicitThisParam) { |
| 3224 | if (ArgIdx == 0) { |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3225 | S.Diag(Attr.getLoc(), |
| 3226 | diag::err_format_attribute_implicit_this_format_string) |
| 3227 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3228 | return; |
| 3229 | } |
| 3230 | ArgIdx--; |
| 3231 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3232 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3233 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3234 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3235 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3236 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3237 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3238 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3239 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3240 | return; |
| 3241 | } |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3242 | } else if (Kind == NSStringFormat) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3243 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3244 | // semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3245 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3246 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3247 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3248 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3249 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3250 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3251 | } else if (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3252 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3253 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3254 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3255 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3256 | return; |
| 3257 | } |
| 3258 | |
| 3259 | // check the 3rd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3260 | Expr *FirstArgExpr = Attr.getArg(1); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3261 | llvm::APSInt FirstArg(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3262 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 3263 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3264 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3265 | << Attr.getName() << 3 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3266 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3267 | return; |
| 3268 | } |
| 3269 | |
| 3270 | // check if the function is variadic if the 3rd argument non-zero |
| 3271 | if (FirstArg != 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3272 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3273 | ++NumArgs; // +1 for ... |
| 3274 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3275 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3276 | return; |
| 3277 | } |
| 3278 | } |
| 3279 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3280 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3281 | // variable the input is just the current time + the format string. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3282 | if (Kind == StrftimeFormat) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3283 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3284 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 3285 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3286 | return; |
| 3287 | } |
| 3288 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3289 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3290 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3291 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3292 | return; |
| 3293 | } |
| 3294 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3295 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format, |
| 3296 | Idx.getZExtValue(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3297 | FirstArg.getZExtValue(), |
| 3298 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3299 | if (NewAttr) |
| 3300 | D->addAttr(NewAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3301 | } |
| 3302 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3303 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 3304 | const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3305 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3306 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3307 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3308 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3309 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3310 | // Try to find the underlying union declaration. |
| 3311 | RecordDecl *RD = 0; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3312 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3313 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3314 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3315 | else |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3316 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3317 | |
| 3318 | if (!RD || !RD->isUnion()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3319 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3320 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3321 | return; |
| 3322 | } |
| 3323 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3324 | if (!RD->isCompleteDefinition()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3325 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3326 | diag::warn_transparent_union_attribute_not_definition); |
| 3327 | return; |
| 3328 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3329 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3330 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3331 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3332 | if (Field == FieldEnd) { |
| 3333 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3334 | return; |
| 3335 | } |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3336 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3337 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3338 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3339 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3340 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3341 | diag::warn_transparent_union_attribute_floating) |
| 3342 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3343 | return; |
| 3344 | } |
| 3345 | |
| 3346 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3347 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3348 | for (; Field != FieldEnd; ++Field) { |
| 3349 | QualType FieldType = Field->getType(); |
| 3350 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 3351 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 3352 | // Warn if we drop the attribute. |
| 3353 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3354 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3355 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3356 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3357 | diag::warn_transparent_union_attribute_field_size_align) |
| 3358 | << isSize << Field->getDeclName() << FieldBits; |
| 3359 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3360 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3361 | diag::note_transparent_union_first_field_size_align) |
| 3362 | << isSize << FirstBits; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3363 | return; |
| 3364 | } |
| 3365 | } |
| 3366 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3367 | RD->addAttr(::new (S.Context) |
| 3368 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 3369 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3372 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3373 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3374 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3375 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3376 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3377 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3378 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3379 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3380 | // Make sure that there is a string literal as the annotation's single |
| 3381 | // argument. |
| 3382 | if (!SE) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3383 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
| 3384 | << Attr.getName() << AANT_ArgumentString; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3385 | return; |
| 3386 | } |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3387 | |
| 3388 | // Don't duplicate annotations that are already set. |
| 3389 | for (specific_attr_iterator<AnnotateAttr> |
| 3390 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 3391 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
| 3392 | if ((*i)->getAnnotation() == SE->getString()) |
| 3393 | return; |
| 3394 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3395 | |
| 3396 | D->addAttr(::new (S.Context) |
| 3397 | AnnotateAttr(Attr.getRange(), S.Context, SE->getString(), |
| 3398 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3401 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3402 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3403 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3404 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3405 | << Attr.getName() << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3406 | return; |
| 3407 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3408 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3409 | if (Attr.getNumArgs() == 0) { |
| 3410 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 3411 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 3412 | return; |
| 3413 | } |
| 3414 | |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3415 | Expr *E = Attr.getArg(0); |
| 3416 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3417 | S.Diag(Attr.getEllipsisLoc(), |
| 3418 | diag::err_pack_expansion_without_parameter_packs); |
| 3419 | return; |
| 3420 | } |
| 3421 | |
| 3422 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 3423 | return; |
| 3424 | |
| 3425 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 3426 | Attr.isPackExpansion()); |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3427 | } |
| 3428 | |
| 3429 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3430 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3431 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3432 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3433 | |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3434 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3435 | if (TmpAttr.isAlignas()) { |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3436 | // C++11 [dcl.align]p1: |
| 3437 | // An alignment-specifier may be applied to a variable or to a class |
| 3438 | // data member, but it shall not be applied to a bit-field, a function |
| 3439 | // parameter, the formal parameter of a catch clause, or a variable |
| 3440 | // declared with the register storage class specifier. An |
| 3441 | // alignment-specifier may also be applied to the declaration of a class |
| 3442 | // or enumeration type. |
| 3443 | // C11 6.7.5/2: |
| 3444 | // An alignment attribute shall not be specified in a declaration of |
| 3445 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3446 | // object declared with the register storage-class specifier. |
| 3447 | int DiagKind = -1; |
| 3448 | if (isa<ParmVarDecl>(D)) { |
| 3449 | DiagKind = 0; |
| 3450 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3451 | if (VD->getStorageClass() == SC_Register) |
| 3452 | DiagKind = 1; |
| 3453 | if (VD->isExceptionVariable()) |
| 3454 | DiagKind = 2; |
| 3455 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 3456 | if (FD->isBitField()) |
| 3457 | DiagKind = 3; |
| 3458 | } else if (!isa<TagDecl>(D)) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3459 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) |
| 3460 | << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'") |
Richard Smith | 5f838aa | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3461 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3462 | : ExpectedVariableFieldOrTag); |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3463 | return; |
| 3464 | } |
| 3465 | if (DiagKind != -1) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3466 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 3467 | << TmpAttr.isC11() << DiagKind; |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3468 | return; |
| 3469 | } |
| 3470 | } |
| 3471 | |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3472 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3473 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3474 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3475 | AA->setPackExpansion(IsPackExpansion); |
| 3476 | D->addAttr(AA); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3477 | return; |
| 3478 | } |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3479 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3480 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3481 | llvm::APSInt Alignment(32); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3482 | ExprResult ICE |
| 3483 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3484 | diag::err_aligned_attribute_argument_not_int, |
| 3485 | /*AllowFold*/ false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3486 | if (ICE.isInvalid()) |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3487 | return; |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3488 | |
| 3489 | // C++11 [dcl.align]p2: |
| 3490 | // -- if the constant expression evaluates to zero, the alignment |
| 3491 | // specifier shall have no effect |
| 3492 | // C11 6.7.5p6: |
| 3493 | // An alignment specification of zero has no effect. |
| 3494 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 3495 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3496 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 3497 | << E->getSourceRange(); |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3498 | return; |
| 3499 | } |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3500 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3501 | if (TmpAttr.isDeclspec()) { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3502 | // We've already verified it's a power of 2, now let's make sure it's |
| 3503 | // 8192 or less. |
| 3504 | if (Alignment.getZExtValue() > 8192) { |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3505 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3506 | << E->getSourceRange(); |
| 3507 | return; |
| 3508 | } |
| 3509 | } |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3510 | |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3511 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 3512 | ICE.take(), SpellingListIndex); |
| 3513 | AA->setPackExpansion(IsPackExpansion); |
| 3514 | D->addAttr(AA); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3515 | } |
| 3516 | |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3517 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3518 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3519 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3520 | // FIXME: Perform checking of type validity |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3521 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3522 | SpellingListIndex); |
| 3523 | AA->setPackExpansion(IsPackExpansion); |
| 3524 | D->addAttr(AA); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3525 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3526 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3527 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3528 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3529 | |
| 3530 | QualType Ty; |
| 3531 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3532 | Ty = VD->getType(); |
| 3533 | else |
| 3534 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
Richard Smith | 4da0903 | 2013-02-22 09:21:42 +0000 | [diff] [blame] | 3535 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3536 | return; |
| 3537 | |
| 3538 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3539 | // The combined effect of all alignment attributes in a declaration shall |
| 3540 | // not specify an alignment that is less strict than the alignment that |
| 3541 | // would otherwise be required for the entity being declared. |
| 3542 | AlignedAttr *AlignasAttr = 0; |
| 3543 | unsigned Align = 0; |
| 3544 | for (specific_attr_iterator<AlignedAttr> |
| 3545 | I = D->specific_attr_begin<AlignedAttr>(), |
| 3546 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 3547 | if (I->isAlignmentDependent()) |
| 3548 | return; |
| 3549 | if (I->isAlignas()) |
| 3550 | AlignasAttr = *I; |
| 3551 | Align = std::max(Align, I->getAlignment(Context)); |
| 3552 | } |
| 3553 | |
| 3554 | if (AlignasAttr && Align) { |
| 3555 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 3556 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 3557 | if (NaturalAlign > RequestedAlign) |
| 3558 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 3559 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 3560 | } |
| 3561 | } |
| 3562 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3563 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3564 | /// type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3565 | /// |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3566 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3567 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3568 | /// HImode, not an intermediate pointer. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3569 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3570 | // This attribute isn't documented, but glibc uses it. It changes |
| 3571 | // the width of an int or unsigned int to the specified size. |
| 3572 | |
| 3573 | // Check that there aren't any arguments |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3574 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3575 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3576 | |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3577 | |
| 3578 | IdentifierInfo *Name = Attr.getParameterName(); |
| 3579 | if (!Name) { |
Aaron Ballman | 750f73a | 2013-07-30 14:29:12 +0000 | [diff] [blame] | 3580 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3581 | << AANT_ArgumentIdentifier; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3582 | return; |
| 3583 | } |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3584 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3585 | StringRef Str = Attr.getParameterName()->getName(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3586 | |
| 3587 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3588 | if (Str.startswith("__") && Str.endswith("__")) |
| 3589 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3590 | |
| 3591 | unsigned DestWidth = 0; |
| 3592 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3593 | bool ComplexMode = false; |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3594 | switch (Str.size()) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3595 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3596 | switch (Str[0]) { |
| 3597 | case 'Q': DestWidth = 8; break; |
| 3598 | case 'H': DestWidth = 16; break; |
| 3599 | case 'S': DestWidth = 32; break; |
| 3600 | case 'D': DestWidth = 64; break; |
| 3601 | case 'X': DestWidth = 96; break; |
| 3602 | case 'T': DestWidth = 128; break; |
| 3603 | } |
| 3604 | if (Str[1] == 'F') { |
| 3605 | IntegerMode = false; |
| 3606 | } else if (Str[1] == 'C') { |
| 3607 | IntegerMode = false; |
| 3608 | ComplexMode = true; |
| 3609 | } else if (Str[1] != 'I') { |
| 3610 | DestWidth = 0; |
| 3611 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3612 | break; |
| 3613 | case 4: |
| 3614 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3615 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3616 | if (Str == "word") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3617 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3618 | else if (Str == "byte") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3619 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3620 | break; |
| 3621 | case 7: |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3622 | if (Str == "pointer") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3623 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3624 | break; |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3625 | case 11: |
| 3626 | if (Str == "unwind_word") |
Rafael Espindola | 0b1de54 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3627 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3628 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3629 | } |
| 3630 | |
| 3631 | QualType OldTy; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3632 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3633 | OldTy = TD->getUnderlyingType(); |
| 3634 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3635 | OldTy = VD->getType(); |
| 3636 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3637 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3638 | << "mode" << Attr.getRange(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3639 | return; |
| 3640 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3641 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3642 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3643 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3644 | else if (IntegerMode) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3645 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3646 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3647 | } else if (ComplexMode) { |
| 3648 | if (!OldTy->isComplexType()) |
| 3649 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3650 | } else { |
| 3651 | if (!OldTy->isFloatingType()) |
| 3652 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3653 | } |
| 3654 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3655 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3656 | // and friends, at least with glibc. |
| 3657 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 3658 | // width on unusual platforms. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3659 | // FIXME: Make sure floating-point mappings are accurate |
| 3660 | // FIXME: Support XF and TF types |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3661 | QualType NewTy; |
| 3662 | switch (DestWidth) { |
| 3663 | case 0: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3664 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3665 | return; |
| 3666 | default: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3667 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3668 | return; |
| 3669 | case 8: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3670 | if (!IntegerMode) { |
| 3671 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3672 | return; |
| 3673 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3674 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3675 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3676 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3677 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3678 | break; |
| 3679 | case 16: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3680 | if (!IntegerMode) { |
| 3681 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3682 | return; |
| 3683 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3684 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3685 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3686 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3687 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3688 | break; |
| 3689 | case 32: |
| 3690 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3691 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3692 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3693 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3694 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3695 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3696 | break; |
| 3697 | case 64: |
| 3698 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3699 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3700 | else if (OldTy->isSignedIntegerType()) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3701 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3702 | NewTy = S.Context.LongTy; |
| 3703 | else |
| 3704 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3705 | else |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3706 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3707 | NewTy = S.Context.UnsignedLongTy; |
| 3708 | else |
| 3709 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3710 | break; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3711 | case 96: |
| 3712 | NewTy = S.Context.LongDoubleTy; |
| 3713 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3714 | case 128: |
Roman Divacky | 1964554 | 2013-07-03 21:08:41 +0000 | [diff] [blame] | 3715 | if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() != |
| 3716 | &llvm::APFloat::PPCDoubleDouble) { |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3717 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3718 | return; |
| 3719 | } |
Roman Divacky | f0d14cb | 2013-07-03 20:48:06 +0000 | [diff] [blame] | 3720 | if (IntegerMode) { |
| 3721 | if (OldTy->isSignedIntegerType()) |
| 3722 | NewTy = S.Context.Int128Ty; |
| 3723 | else |
| 3724 | NewTy = S.Context.UnsignedInt128Ty; |
| 3725 | } else |
| 3726 | NewTy = S.Context.LongDoubleTy; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3727 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3728 | } |
| 3729 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3730 | if (ComplexMode) { |
| 3731 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3732 | } |
| 3733 | |
| 3734 | // Install the new type. |
Enea Zaffanella | c2fa6b6 | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3735 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3736 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 3737 | else |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3738 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | c2fa6b6 | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3739 | |
| 3740 | D->addAttr(::new (S.Context) |
| 3741 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 3742 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3743 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3744 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3745 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3746 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3747 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3748 | return; |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 3749 | |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3750 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3751 | if (!VD->hasGlobalStorage()) |
| 3752 | S.Diag(Attr.getLoc(), |
| 3753 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3754 | << Attr.getName(); |
| 3755 | } else if (!isFunctionOrMethod(D)) { |
| 3756 | S.Diag(Attr.getLoc(), |
| 3757 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3758 | << Attr.getName(); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3759 | return; |
| 3760 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3761 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3762 | D->addAttr(::new (S.Context) |
| 3763 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3764 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3765 | } |
| 3766 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3767 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3768 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3769 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3770 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3771 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3772 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3773 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3774 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3775 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3776 | return; |
| 3777 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3778 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3779 | D->addAttr(::new (S.Context) |
| 3780 | NoInlineAttr(Attr.getRange(), S.Context, |
| 3781 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3782 | } |
| 3783 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3784 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 3785 | const AttributeList &Attr) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3786 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3787 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3788 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3789 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3790 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3791 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3792 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3793 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3794 | return; |
| 3795 | } |
| 3796 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3797 | D->addAttr(::new (S.Context) |
| 3798 | NoInstrumentFunctionAttr(Attr.getRange(), S.Context, |
| 3799 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3800 | } |
| 3801 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3802 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3803 | if (S.LangOpts.CUDA) { |
| 3804 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 3805 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3806 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3807 | << Attr.getName() << 0; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3808 | return; |
| 3809 | } |
| 3810 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3811 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3812 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3813 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3814 | return; |
| 3815 | } |
| 3816 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3817 | D->addAttr(::new (S.Context) |
| 3818 | CUDAConstantAttr(Attr.getRange(), S.Context, |
| 3819 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3820 | } else { |
| 3821 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 3822 | } |
| 3823 | } |
| 3824 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3825 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3826 | if (S.LangOpts.CUDA) { |
| 3827 | // check the attribute arguments. |
| 3828 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3829 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3830 | << Attr.getName() << 0; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3831 | return; |
| 3832 | } |
| 3833 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3834 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3835 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3836 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3837 | return; |
| 3838 | } |
| 3839 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3840 | D->addAttr(::new (S.Context) |
| 3841 | CUDADeviceAttr(Attr.getRange(), S.Context, |
| 3842 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3843 | } else { |
| 3844 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 3845 | } |
| 3846 | } |
| 3847 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3848 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3849 | if (S.LangOpts.CUDA) { |
| 3850 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3851 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3852 | return; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3853 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3854 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3855 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3856 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3857 | return; |
| 3858 | } |
| 3859 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3860 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3861 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 3862 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3863 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3864 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3865 | << FD->getType() |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3866 | << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(), |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3867 | "void"); |
| 3868 | } else { |
| 3869 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3870 | << FD->getType(); |
| 3871 | } |
| 3872 | return; |
| 3873 | } |
| 3874 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3875 | D->addAttr(::new (S.Context) |
| 3876 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
| 3877 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3878 | } else { |
| 3879 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 3880 | } |
| 3881 | } |
| 3882 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3883 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3884 | if (S.LangOpts.CUDA) { |
| 3885 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3886 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3887 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3888 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3889 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3890 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3891 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3892 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3893 | return; |
| 3894 | } |
| 3895 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3896 | D->addAttr(::new (S.Context) |
| 3897 | CUDAHostAttr(Attr.getRange(), S.Context, |
| 3898 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3899 | } else { |
| 3900 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 3901 | } |
| 3902 | } |
| 3903 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3904 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3905 | if (S.LangOpts.CUDA) { |
| 3906 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3907 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3908 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3909 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3910 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3911 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3912 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3913 | return; |
| 3914 | } |
| 3915 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3916 | D->addAttr(::new (S.Context) |
| 3917 | CUDASharedAttr(Attr.getRange(), S.Context, |
| 3918 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3919 | } else { |
| 3920 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 3921 | } |
| 3922 | } |
| 3923 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3924 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3925 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3926 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3927 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3928 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3929 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3930 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3931 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3932 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3933 | return; |
| 3934 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3935 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3936 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3937 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3938 | return; |
| 3939 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3940 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3941 | D->addAttr(::new (S.Context) |
| 3942 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3943 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3946 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3947 | if (hasDeclarator(D)) return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3948 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3949 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3950 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3951 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3952 | CallingConv CC; |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3953 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3954 | return; |
| 3955 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3956 | if (!isa<ObjCMethodDecl>(D)) { |
| 3957 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3958 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3959 | return; |
| 3960 | } |
| 3961 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3962 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3963 | case AttributeList::AT_FastCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3964 | D->addAttr(::new (S.Context) |
| 3965 | FastCallAttr(Attr.getRange(), S.Context, |
| 3966 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3967 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3968 | case AttributeList::AT_StdCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3969 | D->addAttr(::new (S.Context) |
| 3970 | StdCallAttr(Attr.getRange(), S.Context, |
| 3971 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3972 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3973 | case AttributeList::AT_ThisCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3974 | D->addAttr(::new (S.Context) |
| 3975 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3976 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 04633eb | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3977 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3978 | case AttributeList::AT_CDecl: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3979 | D->addAttr(::new (S.Context) |
| 3980 | CDeclAttr(Attr.getRange(), S.Context, |
| 3981 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3982 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3983 | case AttributeList::AT_Pascal: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3984 | D->addAttr(::new (S.Context) |
| 3985 | PascalAttr(Attr.getRange(), S.Context, |
| 3986 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3987 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3988 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3989 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3990 | switch (CC) { |
| 3991 | case CC_AAPCS: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3992 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3993 | break; |
| 3994 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3995 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3996 | break; |
| 3997 | default: |
| 3998 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3999 | } |
| 4000 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4001 | D->addAttr(::new (S.Context) |
| 4002 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 4003 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4004 | return; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4005 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4006 | case AttributeList::AT_PnaclCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4007 | D->addAttr(::new (S.Context) |
| 4008 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 4009 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4010 | return; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4011 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4012 | D->addAttr(::new (S.Context) |
| 4013 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 4014 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4015 | return; |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4016 | |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4017 | default: |
| 4018 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4019 | } |
| 4020 | } |
| 4021 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4022 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 4023 | assert(!Attr.isInvalid()); |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4024 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4025 | } |
| 4026 | |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4027 | static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
| 4028 | assert(!Attr.isInvalid()); |
| 4029 | |
| 4030 | Expr *E = Attr.getArg(0); |
| 4031 | llvm::APSInt ArgNum(32); |
| 4032 | if (E->isTypeDependent() || E->isValueDependent() || |
| 4033 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 4034 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 4035 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 4036 | << E->getSourceRange(); |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4037 | return; |
| 4038 | } |
| 4039 | |
| 4040 | D->addAttr(::new (S.Context) OpenCLImageAccessAttr( |
| 4041 | Attr.getRange(), S.Context, ArgNum.getZExtValue())); |
| 4042 | } |
| 4043 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4044 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 4045 | const FunctionDecl *FD) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4046 | if (attr.isInvalid()) |
| 4047 | return true; |
| 4048 | |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4049 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
| 4050 | if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 4051 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 4052 | << attr.getName() << ReqArgs; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4053 | attr.setInvalid(); |
| 4054 | return true; |
| 4055 | } |
| 4056 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4057 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 4058 | // move to TargetAttributesSema one day. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4059 | switch (attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4060 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 4061 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 4062 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 4063 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 4064 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
| 4065 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4066 | Expr *Arg = attr.getArg(0); |
| 4067 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4068 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4069 | Diag(attr.getLoc(), diag::err_attribute_argument_type) << attr.getName() |
| 4070 | << AANT_ArgumentString; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4071 | attr.setInvalid(); |
| 4072 | return true; |
| 4073 | } |
| 4074 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4075 | StringRef StrRef = Str->getString(); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4076 | if (StrRef == "aapcs") { |
| 4077 | CC = CC_AAPCS; |
| 4078 | break; |
| 4079 | } else if (StrRef == "aapcs-vfp") { |
| 4080 | CC = CC_AAPCS_VFP; |
| 4081 | break; |
| 4082 | } |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4083 | |
| 4084 | attr.setInvalid(); |
| 4085 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 4086 | return true; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4087 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4088 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4089 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4090 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4091 | } |
| 4092 | |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4093 | const TargetInfo &TI = Context.getTargetInfo(); |
| 4094 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 4095 | if (A == TargetInfo::CCCR_Warning) { |
| 4096 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4097 | |
| 4098 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 4099 | if (FD) |
| 4100 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 4101 | TargetInfo::CCMT_NonMember; |
| 4102 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4103 | } |
| 4104 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4105 | return false; |
| 4106 | } |
| 4107 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4108 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4109 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4110 | |
| 4111 | unsigned numParams; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4112 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4113 | return; |
| 4114 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4115 | if (!isa<ObjCMethodDecl>(D)) { |
| 4116 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4117 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4118 | return; |
| 4119 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4120 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4121 | D->addAttr(::new (S.Context) |
| 4122 | RegparmAttr(Attr.getRange(), S.Context, numParams, |
| 4123 | Attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
| 4126 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 4127 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4128 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 4129 | if (Attr.isInvalid()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4130 | return true; |
| 4131 | |
Aaron Ballman | ffa9d57 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 4132 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4133 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4134 | return true; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4135 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4136 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4137 | Expr *NumParamsExpr = Attr.getArg(0); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4138 | llvm::APSInt NumParams(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 4139 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4140 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 4141 | Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 4142 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 4143 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4144 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4145 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4148 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4149 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4150 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4151 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4152 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4153 | } |
| 4154 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4155 | numParams = NumParams.getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4156 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4157 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4158 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4159 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4160 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4163 | return false; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4164 | } |
| 4165 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4166 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4167 | if (S.LangOpts.CUDA) { |
| 4168 | // check the attribute arguments. |
| 4169 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 4170 | // FIXME: 0 is not okay. |
| 4171 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4172 | return; |
| 4173 | } |
| 4174 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4175 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4176 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4177 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4178 | return; |
| 4179 | } |
| 4180 | |
| 4181 | Expr *MaxThreadsExpr = Attr.getArg(0); |
| 4182 | llvm::APSInt MaxThreads(32); |
| 4183 | if (MaxThreadsExpr->isTypeDependent() || |
| 4184 | MaxThreadsExpr->isValueDependent() || |
| 4185 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4186 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4187 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4188 | << MaxThreadsExpr->getSourceRange(); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4189 | return; |
| 4190 | } |
| 4191 | |
| 4192 | llvm::APSInt MinBlocks(32); |
| 4193 | if (Attr.getNumArgs() > 1) { |
| 4194 | Expr *MinBlocksExpr = Attr.getArg(1); |
| 4195 | if (MinBlocksExpr->isTypeDependent() || |
| 4196 | MinBlocksExpr->isValueDependent() || |
| 4197 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4198 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4199 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4200 | << MinBlocksExpr->getSourceRange(); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4201 | return; |
| 4202 | } |
| 4203 | } |
| 4204 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4205 | D->addAttr(::new (S.Context) |
| 4206 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 4207 | MaxThreads.getZExtValue(), |
| 4208 | MinBlocks.getZExtValue(), |
| 4209 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4210 | } else { |
| 4211 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 4212 | } |
| 4213 | } |
| 4214 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4215 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 4216 | const AttributeList &Attr) { |
| 4217 | StringRef AttrName = Attr.getName()->getName(); |
| 4218 | if (!Attr.getParameterName()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4219 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4220 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4221 | return; |
| 4222 | } |
| 4223 | |
| 4224 | if (Attr.getNumArgs() != 2) { |
| 4225 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 4226 | << Attr.getName() << /* required args = */ 3; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4227 | return; |
| 4228 | } |
| 4229 | |
| 4230 | IdentifierInfo *ArgumentKind = Attr.getParameterName(); |
| 4231 | |
| 4232 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 4233 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4234 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 4235 | return; |
| 4236 | } |
| 4237 | |
| 4238 | uint64_t ArgumentIdx; |
| 4239 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4240 | Attr.getLoc(), 2, |
| 4241 | Attr.getArg(0), ArgumentIdx)) |
| 4242 | return; |
| 4243 | |
| 4244 | uint64_t TypeTagIdx; |
| 4245 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4246 | Attr.getLoc(), 3, |
| 4247 | Attr.getArg(1), TypeTagIdx)) |
| 4248 | return; |
| 4249 | |
| 4250 | bool IsPointer = (AttrName == "pointer_with_type_tag"); |
| 4251 | if (IsPointer) { |
| 4252 | // Ensure that buffer has a pointer type. |
| 4253 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 4254 | if (!BufferTy->isPointerType()) { |
| 4255 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4256 | << Attr.getName(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4257 | } |
| 4258 | } |
| 4259 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4260 | D->addAttr(::new (S.Context) |
| 4261 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 4262 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 4263 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4264 | } |
| 4265 | |
| 4266 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 4267 | const AttributeList &Attr) { |
| 4268 | IdentifierInfo *PointerKind = Attr.getParameterName(); |
| 4269 | if (!PointerKind) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4270 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4271 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4272 | return; |
| 4273 | } |
| 4274 | |
| 4275 | QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL); |
| 4276 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4277 | D->addAttr(::new (S.Context) |
| 4278 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
| 4279 | MatchingCType, |
| 4280 | Attr.getLayoutCompatible(), |
| 4281 | Attr.getMustBeNull(), |
| 4282 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4283 | } |
| 4284 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4285 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4286 | // Checker-specific attribute handlers. |
| 4287 | //===----------------------------------------------------------------------===// |
| 4288 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4289 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4290 | return type->isDependentType() || |
| 4291 | type->isObjCObjectPointerType() || |
| 4292 | S.Context.isObjCNSObjectType(type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4293 | } |
| 4294 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4295 | return type->isDependentType() || |
| 4296 | type->isPointerType() || |
| 4297 | isValidSubjectOfNSAttribute(S, type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4298 | } |
| 4299 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4300 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4301 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4302 | if (!param) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4303 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4304 | << Attr.getRange() << Attr.getName() << ExpectedParameter; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4305 | return; |
| 4306 | } |
| 4307 | |
| 4308 | bool typeOK, cf; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4309 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4310 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 4311 | cf = false; |
| 4312 | } else { |
| 4313 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 4314 | cf = true; |
| 4315 | } |
| 4316 | |
| 4317 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4318 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4319 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4320 | return; |
| 4321 | } |
| 4322 | |
| 4323 | if (cf) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4324 | param->addAttr(::new (S.Context) |
| 4325 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 4326 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4327 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4328 | param->addAttr(::new (S.Context) |
| 4329 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 4330 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4333 | static void handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 4334 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4335 | if (!isa<ObjCMethodDecl>(D)) { |
| 4336 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4337 | << Attr.getRange() << Attr.getName() << ExpectedMethod; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4338 | return; |
| 4339 | } |
| 4340 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4341 | D->addAttr(::new (S.Context) |
| 4342 | NSConsumesSelfAttr(Attr.getRange(), S.Context, |
| 4343 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4344 | } |
| 4345 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4346 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 4347 | const AttributeList &Attr) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4348 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4349 | QualType returnType; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4350 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4351 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4352 | returnType = MD->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4353 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4354 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4355 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | a23bd4c | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 4356 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4357 | returnType = PD->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4358 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4359 | returnType = FD->getResultType(); |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4360 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4361 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4362 | << Attr.getRange() << Attr.getName() |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4363 | << ExpectedFunctionOrMethod; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4364 | return; |
| 4365 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4366 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4367 | bool typeOK; |
| 4368 | bool cf; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4369 | switch (Attr.getKind()) { |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4370 | default: llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4371 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4372 | case AttributeList::AT_NSReturnsRetained: |
| 4373 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4374 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 4375 | cf = false; |
| 4376 | break; |
| 4377 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4378 | case AttributeList::AT_CFReturnsRetained: |
| 4379 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4380 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 4381 | cf = true; |
| 4382 | break; |
| 4383 | } |
| 4384 | |
| 4385 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4386 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4387 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4388 | return; |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4389 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4390 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4391 | switch (Attr.getKind()) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4392 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4393 | llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4394 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4395 | D->addAttr(::new (S.Context) |
| 4396 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 4397 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4398 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4399 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4400 | D->addAttr(::new (S.Context) |
| 4401 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4402 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4403 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4404 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4405 | D->addAttr(::new (S.Context) |
| 4406 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4407 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4408 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4409 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4410 | D->addAttr(::new (S.Context) |
| 4411 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4412 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4413 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4414 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4415 | D->addAttr(::new (S.Context) |
| 4416 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4417 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4418 | return; |
| 4419 | }; |
| 4420 | } |
| 4421 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4422 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4423 | const AttributeList &attr) { |
| 4424 | SourceLocation loc = attr.getLoc(); |
| 4425 | |
| 4426 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4427 | |
Fariborz Jahanian | 94d55d7 | 2012-04-21 17:51:44 +0000 | [diff] [blame] | 4428 | if (!method) { |
Fariborz Jahanian | 0e78afb | 2012-04-20 22:00:46 +0000 | [diff] [blame] | 4429 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4430 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4431 | return; |
| 4432 | } |
| 4433 | |
| 4434 | // Check that the method returns a normal pointer. |
| 4435 | QualType resultType = method->getResultType(); |
Fariborz Jahanian | f2e5945 | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4436 | |
| 4437 | if (!resultType->isReferenceType() && |
| 4438 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4439 | S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 4440 | << SourceRange(loc) |
| 4441 | << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2; |
| 4442 | |
| 4443 | // Drop the attribute. |
| 4444 | return; |
| 4445 | } |
| 4446 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4447 | method->addAttr(::new (S.Context) |
| 4448 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 4449 | attr.getAttributeSpellingListIndex())); |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4450 | } |
| 4451 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4452 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4453 | const AttributeList &attr) { |
| 4454 | SourceLocation loc = attr.getLoc(); |
| 4455 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4456 | |
| 4457 | if (!method) { |
| 4458 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 4459 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
| 4460 | return; |
| 4461 | } |
| 4462 | DeclContext *DC = method->getDeclContext(); |
| 4463 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4464 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4465 | << attr.getName() << 0; |
| 4466 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4467 | return; |
| 4468 | } |
| 4469 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4470 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4471 | << attr.getName() << 1; |
| 4472 | return; |
| 4473 | } |
| 4474 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4475 | method->addAttr(::new (S.Context) |
| 4476 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 4477 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4478 | } |
| 4479 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4480 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 4481 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
| 4482 | if (!isa<FunctionDecl>(D)) { |
| 4483 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4484 | << A.getRange() << A.getName() << ExpectedFunction; |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4485 | return; |
| 4486 | } |
| 4487 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4488 | bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4489 | |
| 4490 | // Check whether there's a conflicting attribute already present. |
| 4491 | Attr *Existing; |
| 4492 | if (IsAudited) { |
| 4493 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 4494 | } else { |
| 4495 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 4496 | } |
| 4497 | if (Existing) { |
| 4498 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 4499 | << A.getName() |
| 4500 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 4501 | << A.getRange() << Existing->getRange(); |
| 4502 | return; |
| 4503 | } |
| 4504 | |
| 4505 | // All clear; add the attribute. |
| 4506 | if (IsAudited) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4507 | D->addAttr(::new (S.Context) |
| 4508 | CFAuditedTransferAttr(A.getRange(), S.Context, |
| 4509 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4510 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4511 | D->addAttr(::new (S.Context) |
| 4512 | CFUnknownTransferAttr(A.getRange(), S.Context, |
| 4513 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4514 | } |
| 4515 | } |
| 4516 | |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4517 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4518 | const AttributeList &Attr) { |
| 4519 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4520 | if (!RD || RD->isUnion()) { |
| 4521 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4522 | << Attr.getRange() << Attr.getName() << ExpectedStruct; |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4523 | } |
| 4524 | |
| 4525 | IdentifierInfo *ParmName = Attr.getParameterName(); |
| 4526 | |
| 4527 | // In Objective-C, verify that the type names an Objective-C type. |
| 4528 | // We don't want to check this outside of ObjC because people sometimes |
| 4529 | // do crazy C declarations of Objective-C types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4530 | if (ParmName && S.getLangOpts().ObjC1) { |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4531 | // Check for an existing type with this name. |
| 4532 | LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(), |
| 4533 | Sema::LookupOrdinaryName); |
| 4534 | if (S.LookupName(R, Sc)) { |
| 4535 | NamedDecl *Target = R.getFoundDecl(); |
| 4536 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 4537 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 4538 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 4539 | } |
| 4540 | } |
| 4541 | } |
| 4542 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4543 | D->addAttr(::new (S.Context) |
| 4544 | NSBridgedAttr(Attr.getRange(), S.Context, ParmName, |
| 4545 | Attr.getAttributeSpellingListIndex())); |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4546 | } |
| 4547 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4548 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4549 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4550 | if (hasDeclarator(D)) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4551 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4552 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4553 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4554 | } |
| 4555 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4556 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4557 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4558 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4559 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4560 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4561 | return; |
| 4562 | } |
| 4563 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4564 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4565 | QualType type = vd->getType(); |
| 4566 | |
| 4567 | if (!type->isDependentType() && |
| 4568 | !type->isObjCLifetimeType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4569 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4570 | << type; |
| 4571 | return; |
| 4572 | } |
| 4573 | |
| 4574 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4575 | |
| 4576 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4577 | // going to infer. |
| 4578 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4579 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4580 | |
| 4581 | switch (lifetime) { |
| 4582 | case Qualifiers::OCL_None: |
| 4583 | assert(type->isDependentType() && |
| 4584 | "didn't infer lifetime for non-dependent type?"); |
| 4585 | break; |
| 4586 | |
| 4587 | case Qualifiers::OCL_Weak: // meaningful |
| 4588 | case Qualifiers::OCL_Strong: // meaningful |
| 4589 | break; |
| 4590 | |
| 4591 | case Qualifiers::OCL_ExplicitNone: |
| 4592 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4593 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4594 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4595 | break; |
| 4596 | } |
| 4597 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4598 | D->addAttr(::new (S.Context) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4599 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 4600 | Attr.getAttributeSpellingListIndex())); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4603 | //===----------------------------------------------------------------------===// |
| 4604 | // Microsoft specific attribute handlers. |
| 4605 | //===----------------------------------------------------------------------===// |
| 4606 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4607 | // Check if MS extensions or some other language extensions are enabled. If |
| 4608 | // not, issue a diagnostic that the given attribute is unused. |
| 4609 | static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr, |
| 4610 | bool OtherExtension = false) { |
| 4611 | if (S.LangOpts.MicrosoftExt || OtherExtension) |
| 4612 | return true; |
| 4613 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4614 | return false; |
| 4615 | } |
| 4616 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4617 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4618 | if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland)) |
| 4619 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4620 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4621 | // check the attribute arguments. |
| 4622 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4623 | return; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4624 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4625 | Expr *Arg = Attr.getArg(0); |
| 4626 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 4627 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4628 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 4629 | << Attr.getName() << AANT_ArgumentString; |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4630 | return; |
| 4631 | } |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4632 | |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4633 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 4634 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4635 | StringRef StrRef = Str->getString(); |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4636 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 4637 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4638 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4639 | // Validate GUID length. |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4640 | if (StrRef.size() != 36) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4641 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4642 | return; |
| 4643 | } |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4644 | |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4645 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4646 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4647 | if (StrRef[i] != '-') { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4648 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4649 | return; |
| 4650 | } |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4651 | } else if (!isHexDigit(StrRef[i])) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4652 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4653 | return; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4654 | } |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4655 | } |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4656 | |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4657 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef, |
| 4658 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4659 | } |
| 4660 | |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4661 | static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4662 | if (!checkMicrosoftExt(S, Attr)) |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4663 | return; |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4664 | |
| 4665 | AttributeList::Kind Kind = Attr.getKind(); |
| 4666 | if (Kind == AttributeList::AT_SingleInheritance) |
| 4667 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4668 | ::new (S.Context) |
| 4669 | SingleInheritanceAttr(Attr.getRange(), S.Context, |
| 4670 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4671 | else if (Kind == AttributeList::AT_MultipleInheritance) |
| 4672 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4673 | ::new (S.Context) |
| 4674 | MultipleInheritanceAttr(Attr.getRange(), S.Context, |
| 4675 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4676 | else if (Kind == AttributeList::AT_VirtualInheritance) |
| 4677 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4678 | ::new (S.Context) |
| 4679 | VirtualInheritanceAttr(Attr.getRange(), S.Context, |
| 4680 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
| 4683 | static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4684 | if (!checkMicrosoftExt(S, Attr)) |
| 4685 | return; |
| 4686 | |
| 4687 | AttributeList::Kind Kind = Attr.getKind(); |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4688 | if (Kind == AttributeList::AT_Win64) |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4689 | D->addAttr( |
| 4690 | ::new (S.Context) Win64Attr(Attr.getRange(), S.Context, |
| 4691 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4692 | } |
| 4693 | |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4694 | static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4695 | if (!checkMicrosoftExt(S, Attr)) |
| 4696 | return; |
| 4697 | D->addAttr(::new (S.Context) |
| 4698 | ForceInlineAttr(Attr.getRange(), S.Context, |
| 4699 | Attr.getAttributeSpellingListIndex())); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4700 | } |
| 4701 | |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4702 | static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4703 | if (!checkMicrosoftExt(S, Attr)) |
| 4704 | return; |
| 4705 | // Check linkage after possibly merging declaratinos. See |
| 4706 | // checkAttributesAfterMerging(). |
| 4707 | D->addAttr(::new (S.Context) |
| 4708 | SelectAnyAttr(Attr.getRange(), S.Context, |
| 4709 | Attr.getAttributeSpellingListIndex())); |
| 4710 | } |
| 4711 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4712 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4713 | // Top Level Sema Entry Points |
| 4714 | //===----------------------------------------------------------------------===// |
| 4715 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4716 | static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4717 | const AttributeList &Attr) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4718 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4719 | case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break; |
| 4720 | case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break; |
| 4721 | case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break; |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4722 | default: |
| 4723 | break; |
| 4724 | } |
| 4725 | } |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4726 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4727 | static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4728 | const AttributeList &Attr) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4729 | switch (Attr.getKind()) { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4730 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 4731 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 4732 | case AttributeList::AT_IBOutletCollection: |
| 4733 | handleIBOutletCollection(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4734 | case AttributeList::AT_AddressSpace: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4735 | case AttributeList::AT_ObjCGC: |
| 4736 | case AttributeList::AT_VectorSize: |
| 4737 | case AttributeList::AT_NeonVectorType: |
| 4738 | case AttributeList::AT_NeonPolyVectorType: |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4739 | case AttributeList::AT_Ptr32: |
| 4740 | case AttributeList::AT_Ptr64: |
| 4741 | case AttributeList::AT_SPtr: |
| 4742 | case AttributeList::AT_UPtr: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4743 | // Ignore these, these are type attributes, handled by |
| 4744 | // ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4745 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4746 | case AttributeList::AT_CUDADevice: |
| 4747 | case AttributeList::AT_CUDAHost: |
| 4748 | case AttributeList::AT_Overloadable: |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4749 | // Ignore, this is a non-inheritable attribute, handled |
| 4750 | // by ProcessNonInheritableDeclAttr. |
| 4751 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4752 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 4753 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 4754 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 4755 | case AttributeList::AT_AlwaysInline: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4756 | handleAlwaysInlineAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4757 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4758 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 4759 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4760 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 4761 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 4762 | case AttributeList::AT_CarriesDependency: |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4763 | handleDependencyAttr(S, scope, D, Attr); |
| 4764 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4765 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
| 4766 | case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break; |
| 4767 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4768 | case AttributeList::AT_CXX11NoReturn: |
| 4769 | handleCXX11NoReturnAttr(S, D, Attr); |
| 4770 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4771 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4772 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4773 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4774 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 4775 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4776 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4777 | break; |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4778 | case AttributeList::AT_MinSize: |
| 4779 | handleMinSizeAttr(S, D, Attr); |
| 4780 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4781 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4782 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4783 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
| 4784 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4785 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4786 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4787 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4788 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
| 4789 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
| 4790 | case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break; |
| 4791 | case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break; |
| 4792 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4793 | case AttributeList::AT_ownership_returns: |
| 4794 | case AttributeList::AT_ownership_takes: |
| 4795 | case AttributeList::AT_ownership_holds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4796 | handleOwnershipAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4797 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4798 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
| 4799 | case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break; |
| 4800 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
| 4801 | case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break; |
| 4802 | case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break; |
| 4803 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4804 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4805 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4806 | handleObjCOwnershipAttr(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4807 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4808 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4809 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4810 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4811 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4812 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4813 | case AttributeList::AT_ObjCRequiresSuper: |
| 4814 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4815 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4816 | case AttributeList::AT_NSBridged: |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4817 | handleNSBridgedAttr(S, scope, D, Attr); break; |
| 4818 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4819 | case AttributeList::AT_CFAuditedTransfer: |
| 4820 | case AttributeList::AT_CFUnknownTransfer: |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4821 | handleCFTransferAttr(S, D, Attr); break; |
| 4822 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4823 | // Checker-specific. |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4824 | case AttributeList::AT_CFConsumed: |
| 4825 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4826 | case AttributeList::AT_NSConsumesSelf: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4827 | handleNSConsumesSelfAttr(S, D, Attr); break; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4828 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4829 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4830 | case AttributeList::AT_NSReturnsNotRetained: |
| 4831 | case AttributeList::AT_CFReturnsNotRetained: |
| 4832 | case AttributeList::AT_NSReturnsRetained: |
| 4833 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4834 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4835 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4836 | case AttributeList::AT_WorkGroupSizeHint: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4837 | case AttributeList::AT_ReqdWorkGroupSize: |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4838 | handleWorkGroupSize(S, D, Attr); break; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 4839 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4840 | case AttributeList::AT_VecTypeHint: |
| 4841 | handleVecTypeHint(S, D, Attr); break; |
| 4842 | |
Joey Gouly | 96cead5 | 2013-03-14 09:54:43 +0000 | [diff] [blame] | 4843 | case AttributeList::AT_Endian: |
| 4844 | handleEndianAttr(S, D, Attr); |
| 4845 | break; |
| 4846 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4847 | case AttributeList::AT_InitPriority: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4848 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4849 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4850 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4851 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4852 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4853 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4854 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4855 | case AttributeList::AT_ArcWeakrefUnavailable: |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 4856 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 4857 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4858 | case AttributeList::AT_ObjCRootClass: |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4859 | handleObjCRootClassAttr(S, D, Attr); |
| 4860 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4861 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 4862 | handleObjCRequiresPropertyDefsAttr (S, D, Attr); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 4863 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4864 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4865 | case AttributeList::AT_ReturnsTwice: |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 4866 | handleReturnsTwiceAttr(S, D, Attr); |
| 4867 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4868 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4869 | case AttributeList::AT_Visibility: |
| 4870 | handleVisibilityAttr(S, D, Attr, false); |
| 4871 | break; |
| 4872 | case AttributeList::AT_TypeVisibility: |
| 4873 | handleVisibilityAttr(S, D, Attr, true); |
| 4874 | break; |
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 4875 | case AttributeList::AT_WarnUnused: |
| 4876 | handleWarnUnusedAttr(S, D, Attr); |
| 4877 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4878 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4879 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4880 | case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; |
| 4881 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4882 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4883 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4884 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4885 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4886 | case AttributeList::AT_ObjCException: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4887 | handleObjCExceptionAttr(S, D, Attr); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 4888 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4889 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4890 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4891 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4892 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4893 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4894 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
| 4895 | case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break; |
| 4896 | case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break; |
| 4897 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4898 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
| 4899 | case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break; |
| 4900 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4901 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4902 | // Just ignore |
| 4903 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4904 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4905 | handleNoInstrumentFunctionAttr(S, D, Attr); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 4906 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4907 | case AttributeList::AT_StdCall: |
| 4908 | case AttributeList::AT_CDecl: |
| 4909 | case AttributeList::AT_FastCall: |
| 4910 | case AttributeList::AT_ThisCall: |
| 4911 | case AttributeList::AT_Pascal: |
| 4912 | case AttributeList::AT_Pcs: |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4913 | case AttributeList::AT_PnaclCall: |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4914 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4915 | handleCallConvAttr(S, D, Attr); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4916 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4917 | case AttributeList::AT_OpenCLKernel: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4918 | handleOpenCLKernelAttr(S, D, Attr); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4919 | break; |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4920 | case AttributeList::AT_OpenCLImageAccess: |
| 4921 | handleOpenCLImageAccessAttr(S, D, Attr); |
| 4922 | break; |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4923 | |
| 4924 | // Microsoft attributes: |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4925 | case AttributeList::AT_MsProperty: break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4926 | case AttributeList::AT_MsStruct: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4927 | handleMsStructAttr(S, D, Attr); |
| 4928 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4929 | case AttributeList::AT_Uuid: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4930 | handleUuidAttr(S, D, Attr); |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4931 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4932 | case AttributeList::AT_SingleInheritance: |
| 4933 | case AttributeList::AT_MultipleInheritance: |
| 4934 | case AttributeList::AT_VirtualInheritance: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4935 | handleInheritanceAttr(S, D, Attr); |
| 4936 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4937 | case AttributeList::AT_Win64: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4938 | handlePortabilityAttr(S, D, Attr); |
| 4939 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4940 | case AttributeList::AT_ForceInline: |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4941 | handleForceInlineAttr(S, D, Attr); |
| 4942 | break; |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4943 | case AttributeList::AT_SelectAny: |
| 4944 | handleSelectAnyAttr(S, D, Attr); |
| 4945 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4946 | |
| 4947 | // Thread safety attributes: |
DeLesley Hutchins | 5c6134f | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 4948 | case AttributeList::AT_AssertExclusiveLock: |
| 4949 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 4950 | break; |
| 4951 | case AttributeList::AT_AssertSharedLock: |
| 4952 | handleAssertSharedLockAttr(S, D, Attr); |
| 4953 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4954 | case AttributeList::AT_GuardedVar: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4955 | handleGuardedVarAttr(S, D, Attr); |
| 4956 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4957 | case AttributeList::AT_PtGuardedVar: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4958 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4959 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4960 | case AttributeList::AT_ScopedLockable: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4961 | handleScopedLockableAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4962 | break; |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4963 | case AttributeList::AT_NoSanitizeAddress: |
| 4964 | handleNoSanitizeAddressAttr(S, D, Attr); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4965 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4966 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4967 | handleNoThreadSafetyAnalysis(S, D, Attr); |
| 4968 | break; |
| 4969 | case AttributeList::AT_NoSanitizeThread: |
| 4970 | handleNoSanitizeThread(S, D, Attr); |
| 4971 | break; |
| 4972 | case AttributeList::AT_NoSanitizeMemory: |
| 4973 | handleNoSanitizeMemory(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4974 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4975 | case AttributeList::AT_Lockable: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4976 | handleLockableAttr(S, D, Attr); |
| 4977 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4978 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4979 | handleGuardedByAttr(S, D, Attr); |
| 4980 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4981 | case AttributeList::AT_PtGuardedBy: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4982 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4983 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4984 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4985 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4986 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4987 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4988 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4989 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4990 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4991 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4992 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4993 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4994 | handleLockReturnedAttr(S, D, Attr); |
| 4995 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4996 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4997 | handleLocksExcludedAttr(S, D, Attr); |
| 4998 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4999 | case AttributeList::AT_SharedLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5000 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5001 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5002 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5003 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5004 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5005 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5006 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5007 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5008 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5009 | handleUnlockFunAttr(S, D, Attr); |
| 5010 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5011 | case AttributeList::AT_AcquiredBefore: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5012 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5013 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5014 | case AttributeList::AT_AcquiredAfter: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5015 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5016 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5017 | |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 5018 | // Uniqueness analysis attributes. |
| 5019 | case AttributeList::AT_Consumes: |
| 5020 | handleConsumesAttr(S, D, Attr); |
| 5021 | break; |
| 5022 | case AttributeList::AT_CallableWhenUnconsumed: |
| 5023 | handleCallableWhenUnconsumedAttr(S, D, Attr); |
| 5024 | break; |
| 5025 | case AttributeList::AT_TestsConsumed: |
| 5026 | handleTestsConsumedAttr(S, D, Attr); |
| 5027 | break; |
| 5028 | case AttributeList::AT_TestsUnconsumed: |
| 5029 | handleTestsUnconsumedAttr(S, D, Attr); |
| 5030 | break; |
| 5031 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 5032 | // Type safety attributes. |
| 5033 | case AttributeList::AT_ArgumentWithTypeTag: |
| 5034 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 5035 | break; |
| 5036 | case AttributeList::AT_TypeTagForDatatype: |
| 5037 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 5038 | break; |
| 5039 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5040 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5041 | // Ask target about the attribute. |
| 5042 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 5043 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 5044 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 5045 | diag::warn_unhandled_ms_attribute_ignored : |
| 5046 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5047 | break; |
| 5048 | } |
| 5049 | } |
| 5050 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5051 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 5052 | /// 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] | 5053 | /// silently ignore it if a GNU attribute. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5054 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 5055 | const AttributeList &Attr, |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5056 | bool NonInheritable, bool Inheritable, |
| 5057 | bool IncludeCXX11Attributes) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5058 | if (Attr.isInvalid()) |
| 5059 | return; |
| 5060 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5061 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 5062 | // instead. |
| 5063 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 5064 | return; |
| 5065 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5066 | if (NonInheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5067 | ProcessNonInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5068 | |
| 5069 | if (Inheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5070 | ProcessInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5071 | } |
| 5072 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5073 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 5074 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 5075 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5076 | const AttributeList *AttrList, |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5077 | bool NonInheritable, bool Inheritable, |
| 5078 | bool IncludeCXX11Attributes) { |
| 5079 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
| 5080 | ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable, |
| 5081 | IncludeCXX11Attributes); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5082 | |
| 5083 | // GCC accepts |
| 5084 | // static int a9 __attribute__((weakref)); |
| 5085 | // but that looks really pointless. We reject it. |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5086 | if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5087 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Rafael Espindola | 4d8a33b | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 5088 | cast<NamedDecl>(D)->getNameAsString(); |
| 5089 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5090 | return; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5091 | } |
| 5092 | } |
| 5093 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 5094 | // Annotation attributes are the only attributes allowed after an access |
| 5095 | // specifier. |
| 5096 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 5097 | const AttributeList *AttrList) { |
| 5098 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5099 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 5100 | handleAnnotateAttr(*this, ASDecl, *l); |
| 5101 | } else { |
| 5102 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 5103 | return true; |
| 5104 | } |
| 5105 | } |
| 5106 | |
| 5107 | return false; |
| 5108 | } |
| 5109 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5110 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 5111 | /// contains any decl attributes that we should warn about. |
| 5112 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 5113 | for ( ; A; A = A->getNext()) { |
| 5114 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 5115 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5116 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 5117 | |
| 5118 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 5119 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 5120 | << A->getName() << A->getRange(); |
| 5121 | } else { |
| 5122 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 5123 | << A->getName() << A->getRange(); |
| 5124 | } |
| 5125 | } |
| 5126 | } |
| 5127 | |
| 5128 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 5129 | /// used to build a declaration, complain about any decl attributes |
| 5130 | /// which might be lying around on it. |
| 5131 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 5132 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 5133 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 5134 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 5135 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 5136 | } |
| 5137 | |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5138 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5139 | /// \#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] | 5140 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 5141 | SourceLocation Loc) { |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5142 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5143 | NamedDecl *NewD = 0; |
| 5144 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5145 | FunctionDecl *NewFD; |
| 5146 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 5147 | // FIXME: Mangling? |
| 5148 | // FIXME: Is the qualifier info correct? |
| 5149 | // FIXME: Is the DeclContext correct? |
| 5150 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 5151 | Loc, Loc, DeclarationName(II), |
| 5152 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5153 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5154 | FD->hasPrototype(), |
| 5155 | false/*isConstexprSpecified*/); |
| 5156 | NewD = NewFD; |
| 5157 | |
| 5158 | if (FD->getQualifier()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5159 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5160 | |
| 5161 | // Fake up parameter variables; they are declared as if this were |
| 5162 | // a typedef. |
| 5163 | QualType FDTy = FD->getType(); |
| 5164 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 5165 | SmallVector<ParmVarDecl*, 16> Params; |
| 5166 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 5167 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 5168 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 5169 | Param->setScopeInfo(0, Params.size()); |
| 5170 | Params.push_back(Param); |
| 5171 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 5172 | NewFD->setParams(Params); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5173 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5174 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 5175 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5176 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5177 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5178 | VD->getStorageClass()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5179 | if (VD->getQualifier()) { |
| 5180 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5181 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5182 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5183 | } |
| 5184 | return NewD; |
| 5185 | } |
| 5186 | |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5187 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5188 | /// applied to it, possibly with an alias. |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5189 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5190 | if (W.getUsed()) return; // only do this once |
| 5191 | W.setUsed(true); |
| 5192 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 5193 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5194 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5195 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 5196 | NDId->getName())); |
| 5197 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5198 | WeakTopLevelDecl.push_back(NewD); |
| 5199 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 5200 | // to insert Decl at TU scope, sorry. |
| 5201 | DeclContext *SavedContext = CurContext; |
| 5202 | CurContext = Context.getTranslationUnitDecl(); |
| 5203 | PushOnScopeChains(NewD, S); |
| 5204 | CurContext = SavedContext; |
| 5205 | } else { // just add weak to existing |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5206 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5207 | } |
| 5208 | } |
| 5209 | |
Rafael Espindola | 65611bf | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5210 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 5211 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 5212 | // have to do this. |
| 5213 | LoadExternalWeakUndeclaredIdentifiers(); |
| 5214 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 5215 | NamedDecl *ND = NULL; |
| 5216 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 5217 | if (VD->isExternC()) |
| 5218 | ND = VD; |
| 5219 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5220 | if (FD->isExternC()) |
| 5221 | ND = FD; |
| 5222 | if (ND) { |
| 5223 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 5224 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 5225 | = WeakUndeclaredIdentifiers.find(Id); |
| 5226 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 5227 | WeakInfo W = I->second; |
| 5228 | DeclApplyPragmaWeak(S, ND, W); |
| 5229 | WeakUndeclaredIdentifiers[Id] = W; |
| 5230 | } |
| 5231 | } |
| 5232 | } |
| 5233 | } |
| 5234 | } |
| 5235 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5236 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 5237 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 5238 | /// 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] | 5239 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD, |
| 5240 | bool NonInheritable, bool Inheritable) { |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5241 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5242 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5243 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5244 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5245 | // Walk the declarator structure, applying decl attributes that were in a type |
| 5246 | // position to the decl itself. This handles cases like: |
| 5247 | // int *__attr__(x)** D; |
| 5248 | // when X is a decl attribute. |
| 5249 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 5250 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5251 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable, |
| 5252 | /*IncludeCXX11Attributes=*/false); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5253 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5254 | // Finally, apply any attributes on the decl itself. |
| 5255 | if (const AttributeList *Attrs = PD.getAttributes()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5256 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5257 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5258 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5259 | /// Is the given declaration allowed to use a forbidden type? |
| 5260 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 5261 | // Private ivars are always okay. Unfortunately, people don't |
| 5262 | // always properly make their ivars private, even in system headers. |
| 5263 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | a6b3380 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 5264 | // Function declarations in sys headers will be marked unavailable. |
| 5265 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 5266 | !isa<FunctionDecl>(decl)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5267 | return false; |
| 5268 | |
| 5269 | // Require it to be declared in a system header. |
| 5270 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 5271 | } |
| 5272 | |
| 5273 | /// Handle a delayed forbidden-type diagnostic. |
| 5274 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 5275 | Decl *decl) { |
| 5276 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 5277 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 5278 | "this system declaration uses an unsupported type")); |
| 5279 | return; |
| 5280 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5281 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5282 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 5283 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5284 | // kind of forbidden type messages on unavailable functions. |
| 5285 | if (FD->hasAttr<UnavailableAttr>() && |
| 5286 | diag.getForbiddenTypeDiagnostic() == |
| 5287 | diag::err_arc_array_param_no_ownership) { |
| 5288 | diag.Triggered = true; |
| 5289 | return; |
| 5290 | } |
| 5291 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5292 | |
| 5293 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 5294 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 5295 | diag.Triggered = true; |
| 5296 | } |
| 5297 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5298 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 5299 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5300 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5301 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5302 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5303 | // When delaying diagnostics to run in the context of a parsed |
| 5304 | // declaration, we only want to actually emit anything if parsing |
| 5305 | // succeeds. |
| 5306 | if (!decl) return; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5307 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5308 | // We emit all the active diagnostics in this pool or any of its |
| 5309 | // parents. In general, we'll get one pool for the decl spec |
| 5310 | // and a child pool for each declarator; in a decl group like: |
| 5311 | // deprecated_typedef foo, *bar, baz(); |
| 5312 | // only the declarator pops will be passed decls. This is correct; |
| 5313 | // we really do need to consider delayed diagnostics from the decl spec |
| 5314 | // for each of the different declarations. |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5315 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5316 | do { |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5317 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5318 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 5319 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 5320 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5321 | if (diag.Triggered) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5322 | continue; |
| 5323 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5324 | switch (diag.Kind) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5325 | case DelayedDiagnostic::Deprecation: |
John McCall | e8c904f | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 5326 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 5327 | if (!decl->isInvalidDecl()) |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5328 | HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5329 | break; |
| 5330 | |
| 5331 | case DelayedDiagnostic::Access: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5332 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5333 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5334 | |
| 5335 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5336 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5337 | break; |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5338 | } |
| 5339 | } |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5340 | } while ((pool = pool->getParent())); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5341 | } |
| 5342 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5343 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 5344 | /// been delayed in the current context instead of in the given pool. |
| 5345 | /// Essentially, this just moves them to the current pool. |
| 5346 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 5347 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 5348 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 5349 | curPool->steal(pool); |
| 5350 | } |
| 5351 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5352 | static bool isDeclDeprecated(Decl *D) { |
| 5353 | do { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5354 | if (D->isDeprecated()) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5355 | return true; |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 5356 | // A category implicitly has the availability of the interface. |
| 5357 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 5358 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5359 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 5360 | return false; |
| 5361 | } |
| 5362 | |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5363 | static void |
| 5364 | DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message, |
| 5365 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5366 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5367 | const ObjCPropertyDecl *ObjCPropery) { |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5368 | DeclarationName Name = D->getDeclName(); |
| 5369 | if (!Message.empty()) { |
| 5370 | S.Diag(Loc, diag::warn_deprecated_message) << Name << Message; |
| 5371 | S.Diag(D->getLocation(), |
| 5372 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5373 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5374 | if (ObjCPropery) |
| 5375 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5376 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5377 | } else if (!UnknownObjCClass) { |
| 5378 | S.Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 5379 | S.Diag(D->getLocation(), |
| 5380 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5381 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5382 | if (ObjCPropery) |
| 5383 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5384 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5385 | } else { |
| 5386 | S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name; |
| 5387 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 5388 | } |
| 5389 | } |
| 5390 | |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 5391 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5392 | Decl *Ctx) { |
| 5393 | if (isDeclDeprecated(Ctx)) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5394 | return; |
| 5395 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5396 | DD.Triggered = true; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5397 | DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(), |
| 5398 | DD.getDeprecationMessage(), DD.Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5399 | DD.getUnknownObjCClass(), |
| 5400 | DD.getObjCProperty()); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5401 | } |
| 5402 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5403 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 5404 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5405 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5406 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5407 | // Delay if we're currently parsing a declaration. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5408 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5409 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 5410 | UnknownObjCClass, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5411 | ObjCProperty, |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5412 | Message)); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5413 | return; |
| 5414 | } |
| 5415 | |
| 5416 | // Otherwise, don't warn if our current context is deprecated. |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 5417 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5418 | return; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5419 | DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5420 | } |