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" |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 26 | #include "clang/Lex/Preprocessor.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 27 | #include "clang/Sema/DeclSpec.h" |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 28 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 29 | #include "clang/Sema/Lookup.h" |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 30 | #include "clang/Sema/Scope.h" |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 32 | using namespace clang; |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 33 | using namespace sema; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 34 | |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 35 | /// These constants match the enumerated choices of |
| 36 | /// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 37 | enum AttributeDeclKind { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 38 | ExpectedFunction, |
| 39 | ExpectedUnion, |
| 40 | ExpectedVariableOrFunction, |
| 41 | ExpectedFunctionOrMethod, |
| 42 | ExpectedParameter, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 43 | ExpectedFunctionMethodOrBlock, |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 44 | ExpectedFunctionMethodOrClass, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 45 | ExpectedFunctionMethodOrParameter, |
| 46 | ExpectedClass, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 47 | ExpectedVariable, |
| 48 | ExpectedMethod, |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 49 | ExpectedVariableFunctionOrLabel, |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 50 | ExpectedFieldOrGlobalVar, |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 51 | ExpectedStruct, |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 52 | ExpectedVariableFunctionOrTag, |
Richard Smith | 5f838aa | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 53 | ExpectedTLSVar, |
| 54 | ExpectedVariableOrField, |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 55 | ExpectedVariableFieldOrTag, |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 56 | ExpectedTypeOrNamespace, |
Fariborz Jahanian | 937ec1d | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 57 | ExpectedObjectiveCInterface, |
| 58 | ExpectedMethodOrProperty |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 61 | //===----------------------------------------------------------------------===// |
| 62 | // Helper functions |
| 63 | //===----------------------------------------------------------------------===// |
| 64 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 65 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 66 | bool blocksToo = true) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 67 | QualType Ty; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 68 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(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 FieldDecl *decl = dyn_cast<FieldDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 71 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 72 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 73 | Ty = decl->getUnderlyingType(); |
| 74 | else |
| 75 | return 0; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 77 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 78 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 755f9d2 | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 79 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 80 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 81 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 82 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 85 | // FIXME: We should provide an abstraction around a method or function |
| 86 | // to provide the following bits of information. |
| 87 | |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 88 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 89 | /// type (function or function-typed variable). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 90 | static bool isFunction(const Decl *D) { |
| 91 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 95 | /// type (function or function-typed variable) or an Objective-C |
| 96 | /// method. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 97 | static bool isFunctionOrMethod(const Decl *D) { |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 98 | return isFunction(D) || isa<ObjCMethodDecl>(D); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 101 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 102 | /// type (function or function-typed variable) or an Objective-C |
| 103 | /// method or a block. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 104 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 105 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 106 | return true; |
| 107 | // check for block is more involved. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 108 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 109 | QualType Ty = V->getType(); |
| 110 | return Ty->isBlockPointerType(); |
| 111 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 112 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 113 | } |
| 114 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 115 | /// Return true if the given decl has a declarator that should have |
| 116 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 117 | static bool hasDeclarator(const Decl *D) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 118 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 119 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 120 | isa<ObjCPropertyDecl>(D); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 123 | /// hasFunctionProto - Return true if the given decl has a argument |
| 124 | /// information. This decl should have already passed |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 125 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 126 | static bool hasFunctionProto(const Decl *D) { |
| 127 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 128 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 129 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 130 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 131 | return true; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 136 | /// arguments. It is an error to call this on a K&R function (use |
| 137 | /// hasFunctionProto first). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 138 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 139 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 140 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 141 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 142 | return BD->getNumParams(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 143 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 146 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 147 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 148 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 149 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 150 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 151 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 152 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 155 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 156 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 157 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 158 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 161 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 162 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 163 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 164 | return proto->isVariadic(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 165 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | db9a0ae | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 166 | return BD->isVariadic(); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 167 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 168 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 172 | static bool isInstanceMethod(const Decl *D) { |
| 173 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 174 | return MethodDecl->isInstance(); |
| 175 | return false; |
| 176 | } |
| 177 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 178 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 179 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 180 | if (!PT) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 181 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 182 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 183 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 184 | if (!Cls) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 185 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 186 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 187 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 188 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 189 | // FIXME: Should we walk the chain of classes? |
| 190 | return ClsName == &Ctx.Idents.get("NSString") || |
| 191 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 192 | } |
| 193 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 194 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 195 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 196 | if (!PT) |
| 197 | return false; |
| 198 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 199 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 200 | if (!RT) |
| 201 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 202 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 203 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 204 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 205 | return false; |
| 206 | |
| 207 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 208 | } |
| 209 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 210 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 211 | /// output an error. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 212 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 213 | unsigned int Num) { |
| 214 | if (Attr.getNumArgs() != Num) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 215 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 216 | << Attr.getName() << Num; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 217 | return false; |
| 218 | } |
| 219 | |
| 220 | return true; |
| 221 | } |
| 222 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 223 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 224 | /// \brief Check if the attribute has at least as many args as Num. May |
| 225 | /// output an error. |
| 226 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
| 227 | unsigned int Num) { |
| 228 | if (Attr.getNumArgs() < Num) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 229 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| 230 | return false; |
| 231 | } |
| 232 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 233 | return true; |
| 234 | } |
| 235 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 236 | /// \brief Check if IdxExpr is a valid argument index for a function or |
| 237 | /// instance method D. May output an error. |
| 238 | /// |
| 239 | /// \returns true if IdxExpr is a valid index. |
| 240 | static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D, |
| 241 | StringRef AttrName, |
| 242 | SourceLocation AttrLoc, |
| 243 | unsigned AttrArgNum, |
| 244 | const Expr *IdxExpr, |
| 245 | uint64_t &Idx) |
| 246 | { |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 247 | assert(isFunctionOrMethod(D)); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 248 | |
| 249 | // In C++ the implicit 'this' function parameter also counts. |
| 250 | // Parameters are counted from one. |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 251 | bool HP = hasFunctionProto(D); |
| 252 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 253 | bool IV = HP && isFunctionOrMethodVariadic(D); |
| 254 | unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) + |
| 255 | HasImplicitThisParam; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 256 | |
| 257 | llvm::APSInt IdxInt; |
| 258 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 259 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 260 | std::string Name = std::string("'") + AttrName.str() + std::string("'"); |
| 261 | S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str() |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 262 | << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 263 | return false; |
| 264 | } |
| 265 | |
| 266 | Idx = IdxInt.getLimitedValue(); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 267 | if (Idx < 1 || (!IV && Idx > NumArgs)) { |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 268 | S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds) |
| 269 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 270 | return false; |
| 271 | } |
| 272 | Idx--; // Convert to zero-based. |
| 273 | if (HasImplicitThisParam) { |
| 274 | if (Idx == 0) { |
| 275 | S.Diag(AttrLoc, |
| 276 | diag::err_attribute_invalid_implicit_this_argument) |
| 277 | << AttrName << IdxExpr->getSourceRange(); |
| 278 | return false; |
| 279 | } |
| 280 | --Idx; |
| 281 | } |
| 282 | |
| 283 | return true; |
| 284 | } |
| 285 | |
Aaron Ballman | 422ac9e | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 286 | /// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal. |
| 287 | /// If not emit an error and return false. If the argument is an identifier it |
| 288 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 289 | /// literal. |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 290 | bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr, |
| 291 | unsigned ArgNum, StringRef &Str, |
Tim Northover | 64eac85 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 292 | SourceLocation *ArgLocation) { |
Aaron Ballman | 422ac9e | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 293 | // Look for identifiers. If we have one emit a hint to fix it to a literal. |
| 294 | if (Attr.isArgIdent(ArgNum)) { |
| 295 | IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum); |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 296 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Aaron Ballman | 422ac9e | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 297 | << Attr.getName() << AANT_ArgumentString |
| 298 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 299 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 422ac9e | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 300 | Str = Loc->Ident->getName(); |
| 301 | if (ArgLocation) |
| 302 | *ArgLocation = Loc->Loc; |
| 303 | return true; |
| 304 | } |
| 305 | |
| 306 | // Now check for an actual string literal. |
| 307 | Expr *ArgExpr = Attr.getArgAsExpr(ArgNum); |
| 308 | StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
| 309 | if (ArgLocation) |
| 310 | *ArgLocation = ArgExpr->getLocStart(); |
| 311 | |
| 312 | if (!Literal || !Literal->isAscii()) { |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 313 | Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
Aaron Ballman | 422ac9e | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 314 | << Attr.getName() << AANT_ArgumentString; |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | Str = Literal->getString(); |
| 319 | return true; |
| 320 | } |
| 321 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 322 | /// |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 323 | /// \brief Check if passed in Decl is a field or potentially shared global var |
| 324 | /// \return true if the Decl is a field or potentially shared global variable |
| 325 | /// |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 326 | static bool mayBeSharedVariable(const Decl *D) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 327 | if (isa<FieldDecl>(D)) |
| 328 | return true; |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 329 | if (const VarDecl *vd = dyn_cast<VarDecl>(D)) |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 330 | return vd->hasGlobalStorage() && !vd->getTLSKind(); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 331 | |
| 332 | return false; |
| 333 | } |
| 334 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 335 | /// \brief Check if the passed-in expression is of type int or bool. |
| 336 | static bool isIntOrBool(Expr *Exp) { |
| 337 | QualType QT = Exp->getType(); |
| 338 | return QT->isBooleanType() || QT->isIntegerType(); |
| 339 | } |
| 340 | |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 341 | |
| 342 | // Check to see if the type is a smart pointer of some kind. We assume |
| 343 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 344 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 345 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 346 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 347 | if (Res1.empty()) |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 348 | return false; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 349 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 350 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 351 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 352 | if (Res2.empty()) |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 353 | return false; |
| 354 | |
| 355 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 358 | /// \brief Check if passed in Decl is a pointer type. |
| 359 | /// Note that this function may produce an error message. |
| 360 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 361 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 362 | const AttributeList &Attr) { |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 363 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 364 | QualType QT = vd->getType(); |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 365 | if (QT->isAnyPointerType()) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 366 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 367 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 368 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 369 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 370 | // (We don't want to force template instantiation if we can avoid it, |
| 371 | // since that would alter the order in which templates are instantiated.) |
| 372 | if (RT->isIncompleteType()) |
| 373 | return true; |
| 374 | |
| 375 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 376 | return true; |
| 377 | } |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 378 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 379 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 380 | << Attr.getName()->getName() << QT; |
| 381 | } else { |
| 382 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 383 | << Attr.getName(); |
| 384 | } |
| 385 | return false; |
| 386 | } |
| 387 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 388 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 389 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 390 | static const RecordType *getRecordType(QualType QT) { |
| 391 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 392 | return RT; |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 393 | |
| 394 | // Now check if we point to record type. |
| 395 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 396 | return PT->getPointeeType()->getAs<RecordType>(); |
| 397 | |
| 398 | return 0; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 399 | } |
| 400 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 401 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 402 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 403 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 404 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
| 405 | if (RT->getDecl()->getAttr<LockableAttr>()) |
| 406 | return true; |
| 407 | return false; |
| 408 | } |
| 409 | |
| 410 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 411 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 412 | /// resolves to a lockable object. |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 413 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 414 | QualType Ty) { |
| 415 | const RecordType *RT = getRecordType(Ty); |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 416 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 417 | // Warn if could not get record type for this argument. |
Benjamin Kramer | d77ba89 | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 418 | if (!RT) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 419 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 420 | << Attr.getName() << Ty.getAsString(); |
| 421 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 422 | } |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 423 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 424 | // 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] | 425 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 426 | return; |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 427 | |
| 428 | // Allow smart pointers to be used as lockable objects. |
| 429 | // FIXME -- Check the type that the smart pointer points to. |
| 430 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 431 | return; |
| 432 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 433 | // Check if the type is lockable. |
| 434 | RecordDecl *RD = RT->getDecl(); |
| 435 | if (RD->getAttr<LockableAttr>()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 436 | return; |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 437 | |
| 438 | // Else check if any base classes are lockable. |
| 439 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 440 | CXXBasePaths BPaths(false, false); |
| 441 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 442 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 443 | } |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 444 | |
| 445 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 446 | << Attr.getName() << Ty.getAsString(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 449 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 450 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 451 | /// \param Sidx The attribute argument index to start checking with. |
| 452 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 453 | /// parameter list. |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 454 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 455 | const AttributeList &Attr, |
| 456 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 457 | int Sidx = 0, |
| 458 | bool ParamIdxOk = false) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 459 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 460 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 461 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 462 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 463 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 464 | Args.push_back(ArgExp); |
| 465 | continue; |
| 466 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 467 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 468 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 469 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | f37e4f2 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 470 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 471 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 472 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 473 | Args.push_back(ArgExp); |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 474 | continue; |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 475 | } |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 476 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 477 | // We allow constant strings to be used as a placeholder for expressions |
| 478 | // that are not valid C++ syntax, but warn that they are ignored. |
| 479 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 480 | Attr.getName(); |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 481 | Args.push_back(ArgExp); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 482 | continue; |
| 483 | } |
| 484 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 485 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 486 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 487 | // A pointer to member expression of the form &MyClass::mu is treated |
| 488 | // specially -- we need to look at the type of the member. |
| 489 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 490 | if (UOp->getOpcode() == UO_AddrOf) |
| 491 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 492 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 493 | ArgTy = DRE->getDecl()->getType(); |
| 494 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 495 | // First see if we can just cast to record type, or point to record type. |
| 496 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 497 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 498 | // Now check if we index into a record type function param. |
| 499 | if(!RT && ParamIdxOk) { |
| 500 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 501 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 502 | if(FD && IL) { |
| 503 | unsigned int NumParams = FD->getNumParams(); |
| 504 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 505 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 506 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 507 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 508 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 509 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 510 | continue; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 511 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 512 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 516 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 517 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 518 | Args.push_back(ArgExp); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 519 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 522 | //===----------------------------------------------------------------------===// |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 523 | // Attribute Implementations |
| 524 | //===----------------------------------------------------------------------===// |
| 525 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 526 | // FIXME: All this manual attribute parsing code is gross. At the |
| 527 | // least add some helper functions to check most argument patterns (# |
| 528 | // and types of args). |
| 529 | |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 530 | enum ThreadAttributeDeclKind { |
| 531 | ThreadExpectedFieldOrGlobalVar, |
| 532 | ThreadExpectedFunctionOrMethod, |
| 533 | ThreadExpectedClassOrStruct |
| 534 | }; |
| 535 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 536 | static bool checkGuardedVarAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 537 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 538 | // D must be either a member field or global (potentially shared) variable. |
| 539 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 540 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 541 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 542 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 545 | return true; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 548 | static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 549 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 550 | return; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 551 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 552 | D->addAttr(::new (S.Context) |
| 553 | GuardedVarAttr(Attr.getRange(), S.Context, |
| 554 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 557 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 558 | const AttributeList &Attr) { |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 559 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 560 | return; |
| 561 | |
| 562 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 563 | return; |
| 564 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 565 | D->addAttr(::new (S.Context) |
| 566 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 567 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 570 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 571 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 572 | Expr* &Arg) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 573 | // D must be either a member field or global (potentially shared) variable. |
| 574 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 575 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 576 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 577 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 578 | } |
| 579 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 580 | SmallVector<Expr*, 1> Args; |
| 581 | // check that all arguments are lockable objects |
| 582 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 583 | unsigned Size = Args.size(); |
| 584 | if (Size != 1) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 585 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 586 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 587 | Arg = Args[0]; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 588 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 589 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 592 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 593 | Expr *Arg = 0; |
| 594 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 595 | return; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 596 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 597 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| 598 | } |
| 599 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 600 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 601 | const AttributeList &Attr) { |
| 602 | Expr *Arg = 0; |
| 603 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 604 | return; |
| 605 | |
| 606 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 607 | return; |
| 608 | |
| 609 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| 610 | S.Context, Arg)); |
| 611 | } |
| 612 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 613 | static bool checkLockableAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 614 | const AttributeList &Attr) { |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 615 | // FIXME: Lockable structs for C code. |
David Blaikie | 88c4b5e | 2013-07-29 18:24:03 +0000 | [diff] [blame] | 616 | if (!isa<RecordDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 617 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 618 | << Attr.getName() << ThreadExpectedClassOrStruct; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 619 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 622 | return true; |
| 623 | } |
| 624 | |
| 625 | static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 626 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 627 | return; |
| 628 | |
| 629 | D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); |
| 630 | } |
| 631 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 632 | static void handleScopedLockableAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 633 | const AttributeList &Attr) { |
| 634 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 635 | return; |
| 636 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 637 | D->addAttr(::new (S.Context) |
| 638 | ScopedLockableAttr(Attr.getRange(), S.Context, |
| 639 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 642 | static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D, |
| 643 | const AttributeList &Attr) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 644 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 645 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 646 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 647 | return; |
| 648 | } |
| 649 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 650 | D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(), |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 651 | S.Context)); |
| 652 | } |
| 653 | |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 654 | static void handleNoSanitizeAddressAttr(Sema &S, Decl *D, |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 655 | const AttributeList &Attr) { |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 656 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 657 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 658 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 659 | return; |
| 660 | } |
| 661 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 662 | D->addAttr(::new (S.Context) |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 663 | NoSanitizeAddressAttr(Attr.getRange(), S.Context, |
| 664 | Attr.getAttributeSpellingListIndex())); |
| 665 | } |
| 666 | |
| 667 | static void handleNoSanitizeMemory(Sema &S, Decl *D, |
| 668 | const AttributeList &Attr) { |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 669 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 670 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 671 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(), |
| 676 | S.Context)); |
| 677 | } |
| 678 | |
| 679 | static void handleNoSanitizeThread(Sema &S, Decl *D, |
| 680 | const AttributeList &Attr) { |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 681 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 682 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 683 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 684 | return; |
| 685 | } |
| 686 | |
| 687 | D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(), |
| 688 | S.Context)); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 691 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 692 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 693 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 694 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 695 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 696 | |
| 697 | // D must be either a member field or global (potentially shared) variable. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 698 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 699 | if (!VD || !mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 700 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 701 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 702 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 703 | } |
| 704 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 705 | // Check that this attribute only applies to lockable types. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 706 | QualType QT = VD->getType(); |
| 707 | if (!QT->isDependentType()) { |
| 708 | const RecordType *RT = getRecordType(QT); |
| 709 | if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 710 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 711 | << Attr.getName(); |
| 712 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 716 | // Check that all arguments are lockable objects. |
| 717 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 718 | if (Args.empty()) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 719 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 720 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 721 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 724 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 725 | const AttributeList &Attr) { |
| 726 | SmallVector<Expr*, 1> Args; |
| 727 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 728 | return; |
| 729 | |
| 730 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 731 | D->addAttr(::new (S.Context) |
| 732 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 733 | StartArg, Args.size(), |
| 734 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 737 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 738 | const AttributeList &Attr) { |
| 739 | SmallVector<Expr*, 1> Args; |
| 740 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 741 | return; |
| 742 | |
| 743 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 744 | D->addAttr(::new (S.Context) |
| 745 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 746 | StartArg, Args.size(), |
| 747 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 750 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 751 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 752 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 753 | // zero or more arguments ok |
| 754 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 755 | // check that the attribute is applied to a function |
| 756 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 757 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 758 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 759 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 762 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 763 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 764 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 765 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 768 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 769 | const AttributeList &Attr) { |
| 770 | SmallVector<Expr*, 1> Args; |
| 771 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 772 | return; |
| 773 | |
| 774 | unsigned Size = Args.size(); |
| 775 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 776 | D->addAttr(::new (S.Context) |
| 777 | SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 778 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 781 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 782 | const AttributeList &Attr) { |
| 783 | SmallVector<Expr*, 1> Args; |
| 784 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 785 | return; |
| 786 | |
| 787 | unsigned Size = Args.size(); |
| 788 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 789 | D->addAttr(::new (S.Context) |
| 790 | ExclusiveLockFunctionAttr(Attr.getRange(), S.Context, |
| 791 | StartArg, Size, |
| 792 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 793 | } |
| 794 | |
DeLesley Hutchins | 5c6134f | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 795 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 796 | const AttributeList &Attr) { |
| 797 | SmallVector<Expr*, 1> Args; |
| 798 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 799 | return; |
| 800 | |
| 801 | unsigned Size = Args.size(); |
| 802 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 803 | D->addAttr(::new (S.Context) |
| 804 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 805 | Attr.getAttributeSpellingListIndex())); |
| 806 | } |
| 807 | |
| 808 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 809 | const AttributeList &Attr) { |
| 810 | SmallVector<Expr*, 1> Args; |
| 811 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 812 | return; |
| 813 | |
| 814 | unsigned Size = Args.size(); |
| 815 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 816 | D->addAttr(::new (S.Context) |
| 817 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 818 | StartArg, Size, |
| 819 | Attr.getAttributeSpellingListIndex())); |
| 820 | } |
| 821 | |
| 822 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 823 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 824 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 825 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 826 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 827 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 828 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 829 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 830 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 831 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 832 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 835 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 836 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 837 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 838 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 842 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 843 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 844 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 847 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 848 | const AttributeList &Attr) { |
| 849 | SmallVector<Expr*, 2> Args; |
| 850 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 851 | return; |
| 852 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 853 | D->addAttr(::new (S.Context) |
| 854 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 855 | Attr.getArgAsExpr(0), |
| 856 | Args.data(), Args.size(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 857 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 860 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 861 | const AttributeList &Attr) { |
| 862 | SmallVector<Expr*, 2> Args; |
| 863 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 864 | return; |
| 865 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 866 | D->addAttr(::new (S.Context) |
| 867 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 868 | Attr.getArgAsExpr(0), |
| 869 | Args.data(), Args.size(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 870 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 873 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 874 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 875 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 876 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 877 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 878 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 879 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 880 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 881 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 882 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 885 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 886 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 887 | if (Args.empty()) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 888 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 889 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 890 | return true; |
| 891 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 892 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 893 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 894 | const AttributeList &Attr) { |
| 895 | SmallVector<Expr*, 1> Args; |
| 896 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 897 | return; |
| 898 | |
| 899 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 900 | D->addAttr(::new (S.Context) |
| 901 | ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context, |
| 902 | StartArg, Args.size(), |
| 903 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 906 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 907 | const AttributeList &Attr) { |
| 908 | SmallVector<Expr*, 1> Args; |
| 909 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 910 | return; |
| 911 | |
| 912 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 913 | D->addAttr(::new (S.Context) |
| 914 | SharedLocksRequiredAttr(Attr.getRange(), S.Context, |
| 915 | StartArg, Args.size(), |
| 916 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 919 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 920 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 921 | // zero or more arguments ok |
| 922 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 923 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 924 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 925 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 926 | return; |
| 927 | } |
| 928 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 929 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 930 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 931 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 932 | unsigned Size = Args.size(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 933 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 934 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 935 | D->addAttr(::new (S.Context) |
| 936 | UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 937 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 941 | const AttributeList &Attr) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 942 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 943 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 944 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 945 | return; |
| 946 | } |
| 947 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 948 | // check that the argument is lockable object |
DeLesley Hutchins | f26efd7 | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 949 | SmallVector<Expr*, 1> Args; |
| 950 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 951 | unsigned Size = Args.size(); |
| 952 | if (Size == 0) |
| 953 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 954 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 955 | D->addAttr(::new (S.Context) |
| 956 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 957 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 961 | const AttributeList &Attr) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 962 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 963 | return; |
| 964 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 965 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 966 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 967 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 968 | return; |
| 969 | } |
| 970 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 971 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 972 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 973 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 974 | unsigned Size = Args.size(); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 975 | if (Size == 0) |
| 976 | return; |
| 977 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 978 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 979 | D->addAttr(::new (S.Context) |
| 980 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 981 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 982 | } |
| 983 | |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 984 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | a33ab60 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 985 | ConsumableAttr::ConsumedState DefaultState; |
| 986 | |
| 987 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 988 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 989 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 990 | DefaultState)) { |
| 991 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 992 | << Attr.getName() << IL->Ident; |
David Blaikie | a33ab60 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 993 | return; |
| 994 | } |
David Blaikie | a33ab60 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 995 | } else { |
| 996 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 997 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 998 | return; |
| 999 | } |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1000 | |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1001 | if (!isa<CXXRecordDecl>(D)) { |
| 1002 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1003 | Attr.getName() << ExpectedClass; |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1004 | return; |
| 1005 | } |
| 1006 | |
| 1007 | D->addAttr(::new (S.Context) |
David Blaikie | a33ab60 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1008 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1009 | Attr.getAttributeSpellingListIndex())); |
| 1010 | } |
| 1011 | |
| 1012 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 1013 | const AttributeList &Attr) { |
| 1014 | ASTContext &CurrContext = S.getASTContext(); |
| 1015 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 1016 | |
| 1017 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 1018 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 1019 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 1020 | RD->getNameAsString(); |
| 1021 | |
| 1022 | return false; |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | return true; |
| 1027 | } |
| 1028 | |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1029 | |
DeLesley Hutchins | 6654085 | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1030 | static void handleCallableWhenAttr(Sema &S, Decl *D, |
| 1031 | const AttributeList &Attr) { |
Aaron Ballman | 9aa9261 | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1032 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1033 | return; |
DeLesley Hutchins | 6654085 | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1034 | |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1035 | if (!isa<CXXMethodDecl>(D)) { |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1036 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1037 | Attr.getName() << ExpectedMethod; |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1038 | return; |
| 1039 | } |
| 1040 | |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1041 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1042 | return; |
| 1043 | |
DeLesley Hutchins | 6654085 | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1044 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 1045 | for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) { |
| 1046 | CallableWhenAttr::ConsumedState CallableState; |
| 1047 | |
Aaron Ballman | 9025a18 | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1048 | StringRef StateString; |
| 1049 | SourceLocation Loc; |
| 1050 | if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc)) |
| 1051 | return; |
| 1052 | |
| 1053 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
| 1054 | CallableState)) { |
| 1055 | S.Diag(Loc, diag::warn_attribute_type_not_supported) |
| 1056 | << Attr.getName() << StateString; |
DeLesley Hutchins | 6654085 | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1057 | return; |
| 1058 | } |
Aaron Ballman | 9025a18 | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1059 | |
| 1060 | States.push_back(CallableState); |
DeLesley Hutchins | 6654085 | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1063 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | 6654085 | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1064 | CallableWhenAttr(Attr.getRange(), S.Context, States.data(), |
| 1065 | States.size(), Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1068 | |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1069 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 1070 | const AttributeList &Attr) { |
DeLesley Hutchins | cd0f6d7 | 2013-10-17 22:53:04 +0000 | [diff] [blame^] | 1071 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
| 1072 | |
| 1073 | if (!(isa<FunctionDecl>(D) || isa<ParmVarDecl>(D))) { |
| 1074 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1075 | Attr.getName() << ExpectedFunctionMethodOrParameter; |
| 1076 | return; |
| 1077 | } |
| 1078 | |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1079 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 1080 | |
| 1081 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1082 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 1083 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1084 | ReturnState)) { |
| 1085 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 1086 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1087 | return; |
| 1088 | } |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1089 | } else { |
| 1090 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1091 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1092 | return; |
| 1093 | } |
| 1094 | |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1095 | // FIXME: This check is currently being done in the analysis. It can be |
| 1096 | // enabled here only after the parser propagates attributes at |
| 1097 | // template specialization definition, not declaration. |
| 1098 | //QualType ReturnType; |
| 1099 | // |
DeLesley Hutchins | cd0f6d7 | 2013-10-17 22:53:04 +0000 | [diff] [blame^] | 1100 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 1101 | // ReturnType = Param->getType(); |
| 1102 | // |
| 1103 | //} else if (const CXXConstructorDecl *Constructor = |
| 1104 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1105 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 1106 | // |
| 1107 | //} else { |
| 1108 | // |
| 1109 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 1110 | //} |
| 1111 | // |
| 1112 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1113 | // |
| 1114 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1115 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1116 | // ReturnType.getAsString(); |
| 1117 | // return; |
| 1118 | //} |
| 1119 | |
| 1120 | D->addAttr(::new (S.Context) |
| 1121 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 1122 | Attr.getAttributeSpellingListIndex())); |
| 1123 | } |
| 1124 | |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1125 | |
| 1126 | static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 9aa9261 | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1127 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1128 | return; |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1129 | |
| 1130 | if (!isa<CXXMethodDecl>(D)) { |
| 1131 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1132 | Attr.getName() << ExpectedMethod; |
| 1133 | return; |
| 1134 | } |
| 1135 | |
| 1136 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1137 | return; |
| 1138 | |
| 1139 | SetTypestateAttr::ConsumedState NewState; |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1140 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 2b1599c | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1141 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1142 | StringRef Param = Ident->Ident->getName(); |
| 1143 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 1144 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1145 | << Attr.getName() << Param; |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1146 | return; |
| 1147 | } |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1148 | } else { |
| 1149 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1150 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1151 | return; |
| 1152 | } |
| 1153 | |
| 1154 | D->addAttr(::new (S.Context) |
| 1155 | SetTypestateAttr(Attr.getRange(), S.Context, NewState, |
| 1156 | Attr.getAttributeSpellingListIndex())); |
| 1157 | } |
| 1158 | |
| 1159 | static void handleTestsTypestateAttr(Sema &S, Decl *D, |
| 1160 | const AttributeList &Attr) { |
Aaron Ballman | 9aa9261 | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1161 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1162 | return; |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1163 | |
| 1164 | if (!isa<CXXMethodDecl>(D)) { |
| 1165 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1166 | Attr.getName() << ExpectedMethod; |
| 1167 | return; |
| 1168 | } |
| 1169 | |
| 1170 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1171 | return; |
| 1172 | |
Aaron Ballman | 2b1599c | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1173 | TestsTypestateAttr::ConsumedState TestState; |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1174 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 2b1599c | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1175 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1176 | StringRef Param = Ident->Ident->getName(); |
| 1177 | if (!TestsTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
| 1178 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1179 | << Attr.getName() << Param; |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1180 | return; |
| 1181 | } |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1182 | } else { |
| 1183 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1184 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1185 | return; |
| 1186 | } |
| 1187 | |
| 1188 | D->addAttr(::new (S.Context) |
| 1189 | TestsTypestateAttr(Attr.getRange(), S.Context, TestState, |
| 1190 | Attr.getAttributeSpellingListIndex())); |
| 1191 | } |
| 1192 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1193 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1194 | const AttributeList &Attr) { |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1195 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 1196 | if (TD == 0) { |
| 1197 | // __attribute__((ext_vector_type(N))) can only be applied to typedefs |
| 1198 | // and type-ids. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1199 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1200 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1201 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1202 | |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1203 | // Remember this typedef decl, we will need it later for diagnostics. |
| 1204 | S.ExtVectorDecls.push_back(TD); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1207 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1208 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1209 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1210 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1211 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1212 | // has no effect. |
Eli Friedman | b68ec6b | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1213 | if (!FD->getType()->isDependentType() && |
| 1214 | !FD->getType()->isIncompleteType() && |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1215 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1216 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1217 | << Attr.getName() << FD->getType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1218 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1219 | FD->addAttr(::new (S.Context) |
| 1220 | PackedAttr(Attr.getRange(), S.Context, |
| 1221 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1222 | } else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1223 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1226 | static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 5f608ae | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 1227 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1228 | RD->addAttr(::new (S.Context) |
| 1229 | MsStructAttr(Attr.getRange(), S.Context, |
| 1230 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | c1a0a73 | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 1231 | else |
| 1232 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1233 | } |
| 1234 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1235 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1236 | // The IBAction attributes only apply to instance methods. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1237 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1238 | if (MD->isInstanceMethod()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1239 | D->addAttr(::new (S.Context) |
| 1240 | IBActionAttr(Attr.getRange(), S.Context, |
| 1241 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1242 | return; |
| 1243 | } |
| 1244 | |
Ted Kremenek | 4ee2bb1 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 1245 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1248 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1249 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1250 | // variables or properties of Objective-C classes. The outlet must also |
| 1251 | // have an object reference type. |
| 1252 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1253 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 0bfaf06 | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1254 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1255 | << Attr.getName() << VD->getType() << 0; |
| 1256 | return false; |
| 1257 | } |
| 1258 | } |
| 1259 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1260 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1261 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1262 | << Attr.getName() << PD->getType() << 1; |
| 1263 | return false; |
| 1264 | } |
| 1265 | } |
| 1266 | else { |
| 1267 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1268 | return false; |
| 1269 | } |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1270 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1271 | return true; |
| 1272 | } |
| 1273 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1274 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1275 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1276 | return; |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1277 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1278 | D->addAttr(::new (S.Context) |
| 1279 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1280 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1283 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1284 | const AttributeList &Attr) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1285 | |
| 1286 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1287 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1288 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1289 | << Attr.getName() << 1; |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1290 | return; |
| 1291 | } |
| 1292 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1293 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1294 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1295 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1296 | IdentifierLoc *IL = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
| 1297 | IdentifierInfo *II; |
| 1298 | SourceLocation ILS; |
| 1299 | if (IL) { |
| 1300 | II = IL->Ident; |
| 1301 | ILS = IL->Loc; |
| 1302 | } else { |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1303 | II = &S.Context.Idents.get("NSObject"); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1304 | } |
Fariborz Jahanian | 3a3400b | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 1305 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1306 | ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1307 | S.getScopeForContext(D->getDeclContext()->getParent())); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1308 | if (!TypeRep) { |
| 1309 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1310 | return; |
| 1311 | } |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1312 | QualType QT = TypeRep.get(); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1313 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1314 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1315 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1316 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1317 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1318 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1319 | return; |
| 1320 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1321 | D->addAttr(::new (S.Context) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1322 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QT, ILS, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1323 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1326 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1327 | if (const RecordType *UT = T->getAsUnionType()) |
| 1328 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1329 | RecordDecl *UD = UT->getDecl(); |
| 1330 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1331 | itend = UD->field_end(); it != itend; ++it) { |
| 1332 | QualType QT = it->getType(); |
| 1333 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1334 | T = QT; |
| 1335 | return; |
| 1336 | } |
| 1337 | } |
| 1338 | } |
| 1339 | } |
| 1340 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1341 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nuno Lopes | 174930d | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1342 | if (!isFunctionOrMethod(D)) { |
| 1343 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1344 | << Attr.getName() << ExpectedFunctionOrMethod; |
Nuno Lopes | 174930d | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1345 | return; |
| 1346 | } |
| 1347 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1348 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1349 | return; |
| 1350 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1351 | SmallVector<unsigned, 8> SizeArgs; |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1352 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1353 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1354 | uint64_t Idx; |
| 1355 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1356 | Attr.getLoc(), i + 1, Ex, Idx)) |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1357 | return; |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1358 | |
| 1359 | // check if the function argument is of an integer type |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1360 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1361 | if (!T->isIntegerType()) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 1362 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1363 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 1364 | << Ex->getSourceRange(); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1365 | return; |
| 1366 | } |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1367 | SizeArgs.push_back(Idx); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | // check if the function returns a pointer |
| 1371 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 1372 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1373 | << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1376 | D->addAttr(::new (S.Context) |
| 1377 | AllocSizeAttr(Attr.getRange(), S.Context, |
| 1378 | SizeArgs.data(), SizeArgs.size(), |
| 1379 | Attr.getAttributeSpellingListIndex())); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1382 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1383 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1384 | // ignore it as well |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1385 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1386 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1387 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1388 | return; |
| 1389 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1390 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1391 | SmallVector<unsigned, 8> NonNullArgs; |
| 1392 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1393 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1394 | uint64_t Idx; |
| 1395 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1396 | Attr.getLoc(), i + 1, Ex, Idx)) |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1397 | return; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1398 | |
| 1399 | // Is the function argument a pointer type? |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1400 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1401 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1402 | |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1403 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1404 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | c9ef405 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1405 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1406 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1407 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1408 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1409 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1410 | NonNullArgs.push_back(Idx); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1411 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1412 | |
| 1413 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1414 | // arguments have a nonnull attribute. |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1415 | if (NonNullArgs.empty()) { |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1416 | for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) { |
| 1417 | QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1418 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1419 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1420 | NonNullArgs.push_back(i); |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1421 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1422 | |
Ted Kremenek | ee1c08c | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1423 | // No pointer arguments? |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1424 | if (NonNullArgs.empty()) { |
| 1425 | // Warn the trivial case only if attribute is not coming from a |
| 1426 | // macro instantiation. |
| 1427 | if (Attr.getLoc().isFileID()) |
| 1428 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1429 | return; |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1430 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1431 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1432 | |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1433 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1434 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1435 | llvm::array_pod_sort(start, start + size); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1436 | D->addAttr(::new (S.Context) |
| 1437 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1438 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1441 | static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) { |
| 1442 | switch (K) { |
| 1443 | case OwnershipAttr::Holds: return "'ownership_holds'"; |
| 1444 | case OwnershipAttr::Takes: return "'ownership_takes'"; |
| 1445 | case OwnershipAttr::Returns: return "'ownership_returns'"; |
| 1446 | } |
| 1447 | llvm_unreachable("unknown ownership"); |
| 1448 | } |
| 1449 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1450 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1451 | // This attribute must be applied to a function declaration. The first |
| 1452 | // argument to the attribute must be an identifier, the name of the resource, |
| 1453 | // for example: malloc. The following arguments must be argument indexes, the |
| 1454 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1455 | // The difference between Holds and Takes is that a pointer may still be used |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1456 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1457 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1458 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1459 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1460 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1461 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1462 | return; |
| 1463 | } |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1464 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1465 | // Figure out our Kind, and check arguments while we're at it. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1466 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1467 | switch (AL.getKind()) { |
| 1468 | case AttributeList::AT_ownership_takes: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1469 | K = OwnershipAttr::Takes; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1470 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1471 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1472 | return; |
| 1473 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1474 | break; |
| 1475 | case AttributeList::AT_ownership_holds: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1476 | K = OwnershipAttr::Holds; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1477 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1478 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1479 | return; |
| 1480 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1481 | break; |
| 1482 | case AttributeList::AT_ownership_returns: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1483 | K = OwnershipAttr::Returns; |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1484 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1485 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1486 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1487 | return; |
| 1488 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1489 | break; |
| 1490 | default: |
| 1491 | // This should never happen given how we are called. |
| 1492 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1495 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1496 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1497 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1498 | return; |
| 1499 | } |
| 1500 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1501 | StringRef Module = AL.getArgAsIdent(0)->Ident->getName(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1502 | |
| 1503 | // Normalize the argument, __foo__ becomes foo. |
| 1504 | if (Module.startswith("__") && Module.endswith("__")) |
| 1505 | Module = Module.substr(2, Module.size() - 4); |
| 1506 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1507 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1508 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1509 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1510 | uint64_t Idx; |
| 1511 | if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(), |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1512 | AL.getLoc(), i, Ex, Idx)) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1513 | return; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1514 | |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1515 | // Is the function argument a pointer type? |
| 1516 | QualType T = getFunctionOrMethodArgType(D, Idx); |
| 1517 | int Err = -1; // No error |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1518 | switch (K) { |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1519 | case OwnershipAttr::Takes: |
| 1520 | case OwnershipAttr::Holds: |
| 1521 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1522 | Err = 0; |
| 1523 | break; |
| 1524 | case OwnershipAttr::Returns: |
| 1525 | if (!T->isIntegerType()) |
| 1526 | Err = 1; |
| 1527 | break; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1528 | } |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1529 | if (-1 != Err) { |
Fariborz Jahanian | 937ec1d | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1530 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1531 | << Ex->getSourceRange(); |
| 1532 | return; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1533 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1534 | |
| 1535 | // Check we don't have a conflict with another ownership attribute. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1536 | for (specific_attr_iterator<OwnershipAttr> |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1537 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1538 | e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) { |
| 1539 | if ((*i)->getOwnKind() != K && (*i)->args_end() != |
| 1540 | std::find((*i)->args_begin(), (*i)->args_end(), Idx)) { |
| 1541 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1542 | << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind()); |
| 1543 | return; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1544 | } |
| 1545 | } |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1546 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | unsigned* start = OwnershipArgs.data(); |
| 1550 | unsigned size = OwnershipArgs.size(); |
| 1551 | llvm::array_pod_sort(start, start + size); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1552 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1553 | D->addAttr(::new (S.Context) |
| 1554 | OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size, |
| 1555 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1558 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1559 | // Check the attribute arguments. |
| 1560 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1561 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1562 | << Attr.getName() << 1; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1563 | return; |
| 1564 | } |
| 1565 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1566 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1567 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1568 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1569 | return; |
| 1570 | } |
| 1571 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1572 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1573 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1574 | // gcc rejects |
| 1575 | // class c { |
| 1576 | // static int a __attribute__((weakref ("v2"))); |
| 1577 | // static int b() __attribute__((weakref ("f3"))); |
| 1578 | // }; |
| 1579 | // and ignores the attributes of |
| 1580 | // void f(void) { |
| 1581 | // static int a __attribute__((weakref ("v2"))); |
| 1582 | // } |
| 1583 | // we reject them |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1584 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1585 | if (!Ctx->isFileContext()) { |
| 1586 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1587 | nd->getNameAsString(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1588 | return; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | // The GCC manual says |
| 1592 | // |
| 1593 | // At present, a declaration to which `weakref' is attached can only |
| 1594 | // be `static'. |
| 1595 | // |
| 1596 | // It also says |
| 1597 | // |
| 1598 | // Without a TARGET, |
| 1599 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1600 | // equivalent to `weak'. |
| 1601 | // |
| 1602 | // gcc 4.4.1 will accept |
| 1603 | // int a7 __attribute__((weakref)); |
| 1604 | // as |
| 1605 | // int a7 __attribute__((weak)); |
| 1606 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1607 | // it if this behaviour is actually used. |
| 1608 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1609 | // GCC rejects |
| 1610 | // static ((alias ("y"), weakref)). |
| 1611 | // Should we? How to check that weakref is before or after alias? |
| 1612 | |
Aaron Ballman | be116e2 | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1613 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1614 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1615 | // StringRef parameter it was given anyway. |
Aaron Ballman | 422ac9e | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1616 | StringRef Str; |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1617 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1618 | // GCC will accept anything as the argument of weakref. Should we |
| 1619 | // check for an existing decl? |
Aaron Ballman | 422ac9e | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1620 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1621 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1622 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1623 | D->addAttr(::new (S.Context) |
| 1624 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1625 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1628 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1629 | StringRef Str; |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1630 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1631 | return; |
| 1632 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1633 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | f5fe292 | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1634 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1635 | return; |
| 1636 | } |
| 1637 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1638 | // FIXME: check if target symbol exists in current file |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1639 | |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1640 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1641 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1644 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1645 | if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) { |
| 1646 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1647 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1648 | return; |
| 1649 | } |
| 1650 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1651 | D->addAttr(::new (S.Context) |
| 1652 | MinSizeAttr(Attr.getRange(), S.Context, |
| 1653 | Attr.getAttributeSpellingListIndex())); |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1656 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1657 | if (!isa<FunctionDecl>(D)) { |
| 1658 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1659 | << Attr.getName() << ExpectedFunction; |
| 1660 | return; |
| 1661 | } |
| 1662 | |
| 1663 | if (D->hasAttr<HotAttr>()) { |
| 1664 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1665 | << Attr.getName() << "hot"; |
| 1666 | return; |
| 1667 | } |
| 1668 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1669 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1670 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1674 | if (!isa<FunctionDecl>(D)) { |
| 1675 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1676 | << Attr.getName() << ExpectedFunction; |
| 1677 | return; |
| 1678 | } |
| 1679 | |
| 1680 | if (D->hasAttr<ColdAttr>()) { |
| 1681 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1682 | << Attr.getName() << "cold"; |
| 1683 | return; |
| 1684 | } |
| 1685 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1686 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1687 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1690 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1691 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1692 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1693 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1694 | return; |
| 1695 | } |
| 1696 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1697 | D->addAttr(::new (S.Context) |
| 1698 | NakedAttr(Attr.getRange(), S.Context, |
| 1699 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1702 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 1703 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1704 | if (!isa<FunctionDecl>(D)) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1705 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1706 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1707 | return; |
| 1708 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1709 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1710 | D->addAttr(::new (S.Context) |
| 1711 | AlwaysInlineAttr(Attr.getRange(), S.Context, |
| 1712 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1715 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1716 | const AttributeList &Attr) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1717 | StringRef Model; |
| 1718 | SourceLocation LiteralLoc; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1719 | // Check that it is a string. |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1720 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1721 | return; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1722 | |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 1723 | if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) { |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1724 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1725 | << Attr.getName() << ExpectedTLSVar; |
| 1726 | return; |
| 1727 | } |
| 1728 | |
| 1729 | // Check that the value. |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1730 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1731 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1732 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1733 | return; |
| 1734 | } |
| 1735 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1736 | D->addAttr(::new (S.Context) |
| 1737 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1738 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1741 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1742 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1743 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1744 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1745 | D->addAttr(::new (S.Context) |
| 1746 | MallocAttr(Attr.getRange(), S.Context, |
| 1747 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1748 | return; |
| 1749 | } |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1752 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1755 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1756 | D->addAttr(::new (S.Context) |
| 1757 | MayAliasAttr(Attr.getRange(), S.Context, |
| 1758 | Attr.getAttributeSpellingListIndex())); |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1761 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1762 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1763 | D->addAttr(::new (S.Context) |
| 1764 | NoCommonAttr(Attr.getRange(), S.Context, |
| 1765 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1766 | else |
| 1767 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1768 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1771 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 3e1aca2 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1772 | if (S.LangOpts.CPlusPlus) { |
| 1773 | S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus); |
| 1774 | return; |
| 1775 | } |
| 1776 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1777 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1778 | D->addAttr(::new (S.Context) |
| 1779 | CommonAttr(Attr.getRange(), S.Context, |
| 1780 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1781 | else |
| 1782 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1783 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1786 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1787 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1788 | |
| 1789 | if (S.CheckNoReturnAttr(attr)) return; |
| 1790 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1791 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1792 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1793 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1794 | return; |
| 1795 | } |
| 1796 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1797 | D->addAttr(::new (S.Context) |
| 1798 | NoReturnAttr(attr.getRange(), S.Context, |
| 1799 | attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
| 1802 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1803 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1804 | attr.setInvalid(); |
| 1805 | return true; |
| 1806 | } |
| 1807 | |
| 1808 | return false; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1811 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1812 | const AttributeList &Attr) { |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1813 | |
| 1814 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1815 | // because 'analyzer_noreturn' does not impact the type. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1816 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1817 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1818 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1819 | && !VD->getType()->isFunctionPointerType())) { |
| 1820 | S.Diag(Attr.getLoc(), |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1821 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1822 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1823 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1824 | return; |
| 1825 | } |
| 1826 | } |
| 1827 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1828 | D->addAttr(::new (S.Context) |
| 1829 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1830 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1833 | static void handleCXX11NoReturnAttr(Sema &S, Decl *D, |
| 1834 | const AttributeList &Attr) { |
| 1835 | // C++11 [dcl.attr.noreturn]p1: |
| 1836 | // The attribute may be applied to the declarator-id in a function |
| 1837 | // declaration. |
| 1838 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 1839 | if (!FD) { |
| 1840 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1841 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1842 | return; |
| 1843 | } |
| 1844 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1845 | D->addAttr(::new (S.Context) |
| 1846 | CXX11NoReturnAttr(Attr.getRange(), S.Context, |
| 1847 | Attr.getAttributeSpellingListIndex())); |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1850 | // PS3 PPU-specific. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1851 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1852 | /* |
| 1853 | Returning a Vector Class in Registers |
| 1854 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1855 | According to the PPU ABI specifications, a class with a single member of |
| 1856 | vector type is returned in memory when used as the return value of a function. |
| 1857 | This results in inefficient code when implementing vector classes. To return |
| 1858 | the value in a single vector register, add the vecreturn attribute to the |
| 1859 | class definition. This attribute is also applicable to struct types. |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1860 | |
| 1861 | Example: |
| 1862 | |
| 1863 | struct Vector |
| 1864 | { |
| 1865 | __vector float xyzw; |
| 1866 | } __attribute__((vecreturn)); |
| 1867 | |
| 1868 | Vector Add(Vector lhs, Vector rhs) |
| 1869 | { |
| 1870 | Vector result; |
| 1871 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1872 | return result; // This will be returned in a register |
| 1873 | } |
| 1874 | */ |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1875 | if (!isa<RecordDecl>(D)) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1876 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1877 | << Attr.getName() << ExpectedClass; |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1878 | return; |
| 1879 | } |
| 1880 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1881 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1882 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1883 | return; |
| 1884 | } |
| 1885 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1886 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1887 | int count = 0; |
| 1888 | |
| 1889 | if (!isa<CXXRecordDecl>(record)) { |
| 1890 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1891 | return; |
| 1892 | } |
| 1893 | |
| 1894 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1895 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1896 | return; |
| 1897 | } |
| 1898 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1899 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1900 | iter != record->field_end(); iter++) { |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1901 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1902 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1903 | return; |
| 1904 | } |
| 1905 | count++; |
| 1906 | } |
| 1907 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1908 | D->addAttr(::new (S.Context) |
| 1909 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1910 | Attr.getAttributeSpellingListIndex())); |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1913 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1914 | const AttributeList &Attr) { |
| 1915 | if (isa<ParmVarDecl>(D)) { |
| 1916 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1917 | // parameter of a function declaration or lambda. |
| 1918 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1919 | S.Diag(Attr.getLoc(), |
| 1920 | diag::err_carries_dependency_param_not_function_decl); |
| 1921 | return; |
| 1922 | } |
| 1923 | } else if (!isa<FunctionDecl>(D)) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1924 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1925 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1926 | return; |
| 1927 | } |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1928 | |
| 1929 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1930 | Attr.getRange(), S.Context, |
| 1931 | Attr.getAttributeSpellingListIndex())); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1934 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1935 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 568eae4 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1936 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1937 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1938 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1939 | return; |
| 1940 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1941 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1942 | D->addAttr(::new (S.Context) |
| 1943 | UnusedAttr(Attr.getRange(), S.Context, |
| 1944 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1947 | static void handleReturnsTwiceAttr(Sema &S, Decl *D, |
| 1948 | const AttributeList &Attr) { |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1949 | if (!isa<FunctionDecl>(D)) { |
| 1950 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1951 | << Attr.getName() << ExpectedFunction; |
| 1952 | return; |
| 1953 | } |
| 1954 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1955 | D->addAttr(::new (S.Context) |
| 1956 | ReturnsTwiceAttr(Attr.getRange(), S.Context, |
| 1957 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1960 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1961 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 29535ba | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 1962 | if (VD->hasLocalStorage()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1963 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 1964 | return; |
| 1965 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1966 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1967 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1968 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1969 | return; |
| 1970 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1971 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1972 | D->addAttr(::new (S.Context) |
| 1973 | UsedAttr(Attr.getRange(), S.Context, |
| 1974 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1977 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1978 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1979 | if (Attr.getNumArgs() > 1) { |
| 1980 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1981 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1982 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1983 | |
| 1984 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1985 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1986 | Expr *E = Attr.getArgAsExpr(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1987 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1988 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1989 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1990 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1991 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1992 | << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1993 | return; |
| 1994 | } |
| 1995 | priority = Idx.getZExtValue(); |
| 1996 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1997 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1998 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1999 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2000 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2001 | return; |
| 2002 | } |
| 2003 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2004 | D->addAttr(::new (S.Context) |
| 2005 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 2006 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2009 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2010 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2011 | if (Attr.getNumArgs() > 1) { |
| 2012 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2013 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2014 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2015 | |
| 2016 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 2017 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2018 | Expr *E = Attr.getArgAsExpr(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2019 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2020 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2021 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2022 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2023 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2024 | << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2025 | return; |
| 2026 | } |
| 2027 | priority = Idx.getZExtValue(); |
| 2028 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2029 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2030 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2031 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2032 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2033 | return; |
| 2034 | } |
| 2035 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2036 | D->addAttr(::new (S.Context) |
| 2037 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 2038 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2041 | template <typename AttrTy> |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 2042 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 2043 | const AttributeList &Attr) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2044 | unsigned NumArgs = Attr.getNumArgs(); |
| 2045 | if (NumArgs > 1) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2046 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2047 | return; |
| 2048 | } |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2049 | |
| 2050 | // Handle the case where the attribute has a text message. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2051 | StringRef Str; |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2052 | if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2053 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2054 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2055 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 2056 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2059 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 2060 | const AttributeList &Attr) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2061 | D->addAttr(::new (S.Context) |
| 2062 | ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context, |
| 2063 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2066 | static void handleObjCRootClassAttr(Sema &S, Decl *D, |
| 2067 | const AttributeList &Attr) { |
| 2068 | if (!isa<ObjCInterfaceDecl>(D)) { |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 2069 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2070 | << Attr.getName() << ExpectedObjectiveCInterface; |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2071 | return; |
| 2072 | } |
| 2073 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2074 | D->addAttr(::new (S.Context) |
| 2075 | ObjCRootClassAttr(Attr.getRange(), S.Context, |
| 2076 | Attr.getAttributeSpellingListIndex())); |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2079 | static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, |
| 2080 | const AttributeList &Attr) { |
Fariborz Jahanian | 341b8be | 2012-01-03 22:52:32 +0000 | [diff] [blame] | 2081 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 2082 | S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis); |
| 2083 | return; |
| 2084 | } |
| 2085 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2086 | D->addAttr(::new (S.Context) |
| 2087 | ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context, |
| 2088 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2091 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2092 | IdentifierInfo *Platform, |
| 2093 | VersionTuple Introduced, |
| 2094 | VersionTuple Deprecated, |
| 2095 | VersionTuple Obsoleted) { |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2096 | StringRef PlatformName |
| 2097 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2098 | if (PlatformName.empty()) |
| 2099 | PlatformName = Platform->getName(); |
| 2100 | |
| 2101 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2102 | // of these steps are needed). |
| 2103 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2104 | !(Introduced <= Deprecated)) { |
| 2105 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2106 | << 1 << PlatformName << Deprecated.getAsString() |
| 2107 | << 0 << Introduced.getAsString(); |
| 2108 | return true; |
| 2109 | } |
| 2110 | |
| 2111 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2112 | !(Introduced <= Obsoleted)) { |
| 2113 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2114 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2115 | << 0 << Introduced.getAsString(); |
| 2116 | return true; |
| 2117 | } |
| 2118 | |
| 2119 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2120 | !(Deprecated <= Obsoleted)) { |
| 2121 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2122 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2123 | << 1 << Deprecated.getAsString(); |
| 2124 | return true; |
| 2125 | } |
| 2126 | |
| 2127 | return false; |
| 2128 | } |
| 2129 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2130 | /// \brief Check whether the two versions match. |
| 2131 | /// |
| 2132 | /// If either version tuple is empty, then they are assumed to match. If |
| 2133 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2134 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2135 | bool BeforeIsOkay) { |
| 2136 | if (X.empty() || Y.empty()) |
| 2137 | return true; |
| 2138 | |
| 2139 | if (X == Y) |
| 2140 | return true; |
| 2141 | |
| 2142 | if (BeforeIsOkay && X < Y) |
| 2143 | return true; |
| 2144 | |
| 2145 | return false; |
| 2146 | } |
| 2147 | |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2148 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2149 | IdentifierInfo *Platform, |
| 2150 | VersionTuple Introduced, |
| 2151 | VersionTuple Deprecated, |
| 2152 | VersionTuple Obsoleted, |
| 2153 | bool IsUnavailable, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2154 | StringRef Message, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2155 | bool Override, |
| 2156 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2157 | VersionTuple MergedIntroduced = Introduced; |
| 2158 | VersionTuple MergedDeprecated = Deprecated; |
| 2159 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2160 | bool FoundAny = false; |
| 2161 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2162 | if (D->hasAttrs()) { |
| 2163 | AttrVec &Attrs = D->getAttrs(); |
| 2164 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 2165 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 2166 | if (!OldAA) { |
| 2167 | ++i; |
| 2168 | continue; |
| 2169 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2170 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2171 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2172 | if (OldPlatform != Platform) { |
| 2173 | ++i; |
| 2174 | continue; |
| 2175 | } |
| 2176 | |
| 2177 | FoundAny = true; |
| 2178 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2179 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2180 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2181 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2182 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2183 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 2184 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 2185 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 2186 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 72daa3f | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 2187 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2188 | if (Override) { |
| 2189 | int Which = -1; |
| 2190 | VersionTuple FirstVersion; |
| 2191 | VersionTuple SecondVersion; |
| 2192 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 2193 | Which = 0; |
| 2194 | FirstVersion = OldIntroduced; |
| 2195 | SecondVersion = Introduced; |
| 2196 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 2197 | Which = 1; |
| 2198 | FirstVersion = Deprecated; |
| 2199 | SecondVersion = OldDeprecated; |
| 2200 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 2201 | Which = 2; |
| 2202 | FirstVersion = Obsoleted; |
| 2203 | SecondVersion = OldObsoleted; |
| 2204 | } |
| 2205 | |
| 2206 | if (Which == -1) { |
| 2207 | Diag(OldAA->getLocation(), |
| 2208 | diag::warn_mismatched_availability_override_unavail) |
| 2209 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2210 | } else { |
| 2211 | Diag(OldAA->getLocation(), |
| 2212 | diag::warn_mismatched_availability_override) |
| 2213 | << Which |
| 2214 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2215 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 2216 | } |
| 2217 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2218 | } else { |
| 2219 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2220 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2221 | } |
| 2222 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2223 | Attrs.erase(Attrs.begin() + i); |
| 2224 | --e; |
| 2225 | continue; |
| 2226 | } |
| 2227 | |
| 2228 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2229 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2230 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2231 | |
| 2232 | if (MergedIntroduced2.empty()) |
| 2233 | MergedIntroduced2 = OldIntroduced; |
| 2234 | if (MergedDeprecated2.empty()) |
| 2235 | MergedDeprecated2 = OldDeprecated; |
| 2236 | if (MergedObsoleted2.empty()) |
| 2237 | MergedObsoleted2 = OldObsoleted; |
| 2238 | |
| 2239 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2240 | MergedIntroduced2, MergedDeprecated2, |
| 2241 | MergedObsoleted2)) { |
| 2242 | Attrs.erase(Attrs.begin() + i); |
| 2243 | --e; |
| 2244 | continue; |
| 2245 | } |
| 2246 | |
| 2247 | MergedIntroduced = MergedIntroduced2; |
| 2248 | MergedDeprecated = MergedDeprecated2; |
| 2249 | MergedObsoleted = MergedObsoleted2; |
| 2250 | ++i; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2251 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2252 | } |
| 2253 | |
| 2254 | if (FoundAny && |
| 2255 | MergedIntroduced == Introduced && |
| 2256 | MergedDeprecated == Deprecated && |
| 2257 | MergedObsoleted == Obsoleted) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2258 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2259 | |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2260 | // Only create a new attribute if !Override, but we want to do |
| 2261 | // the checking. |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2262 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2263 | MergedDeprecated, MergedObsoleted) && |
| 2264 | !Override) { |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2265 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 2266 | Introduced, Deprecated, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2267 | Obsoleted, IsUnavailable, Message, |
| 2268 | AttrSpellingListIndex); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2269 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2270 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2273 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2274 | const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2275 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 2276 | return; |
| 2277 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2278 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2279 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2280 | IdentifierInfo *II = Platform->Ident; |
| 2281 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 2282 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 2283 | << Platform->Ident; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2284 | |
Rafael Espindola | 8c4222a | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2285 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 2286 | if (!ND) { |
| 2287 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2288 | return; |
| 2289 | } |
| 2290 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2291 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2292 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2293 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2294 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2295 | StringRef Str; |
Benjamin Kramer | c561714 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 2296 | if (const StringLiteral *SE = |
| 2297 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2298 | Str = SE->getString(); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2299 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2300 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2301 | Introduced.Version, |
| 2302 | Deprecated.Version, |
| 2303 | Obsoleted.Version, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2304 | IsUnavailable, Str, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2305 | /*Override=*/false, |
| 2306 | Index); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2307 | if (NewAttr) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2308 | D->addAttr(NewAttr); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2309 | } |
| 2310 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2311 | template <class T> |
| 2312 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2313 | typename T::VisibilityType value, |
| 2314 | unsigned attrSpellingListIndex) { |
| 2315 | T *existingAttr = D->getAttr<T>(); |
| 2316 | if (existingAttr) { |
| 2317 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2318 | if (existingValue == value) |
| 2319 | return NULL; |
| 2320 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2321 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2322 | D->dropAttr<T>(); |
| 2323 | } |
| 2324 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2325 | } |
| 2326 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2327 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2328 | VisibilityAttr::VisibilityType Vis, |
| 2329 | unsigned AttrSpellingListIndex) { |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2330 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2331 | AttrSpellingListIndex); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2334 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2335 | TypeVisibilityAttr::VisibilityType Vis, |
| 2336 | unsigned AttrSpellingListIndex) { |
| 2337 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2338 | AttrSpellingListIndex); |
| 2339 | } |
| 2340 | |
| 2341 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2342 | bool isTypeVisibility) { |
| 2343 | // Visibility attributes don't mean anything on a typedef. |
| 2344 | if (isa<TypedefNameDecl>(D)) { |
| 2345 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2346 | << Attr.getName(); |
| 2347 | return; |
| 2348 | } |
| 2349 | |
| 2350 | // 'type_visibility' can only go on a type or namespace. |
| 2351 | if (isTypeVisibility && |
| 2352 | !(isa<TagDecl>(D) || |
| 2353 | isa<ObjCInterfaceDecl>(D) || |
| 2354 | isa<NamespaceDecl>(D))) { |
| 2355 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2356 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2357 | return; |
| 2358 | } |
| 2359 | |
Benjamin Kramer | ae3a83f | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2360 | // Check that the argument is a string literal. |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2361 | StringRef TypeStr; |
| 2362 | SourceLocation LiteralLoc; |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2363 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2364 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2365 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2366 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2367 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2368 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2369 | << Attr.getName() << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2370 | return; |
| 2371 | } |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2372 | |
| 2373 | // Complain about attempts to use protected visibility on targets |
| 2374 | // (like Darwin) that don't support it. |
| 2375 | if (type == VisibilityAttr::Protected && |
| 2376 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2377 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2378 | type = VisibilityAttr::Default; |
| 2379 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2380 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2381 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2382 | clang::Attr *newAttr; |
| 2383 | if (isTypeVisibility) { |
| 2384 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2385 | (TypeVisibilityAttr::VisibilityType) type, |
| 2386 | Index); |
| 2387 | } else { |
| 2388 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2389 | } |
| 2390 | if (newAttr) |
| 2391 | D->addAttr(newAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2394 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2395 | const AttributeList &Attr) { |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2396 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 2397 | if (!method) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2398 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2399 | << ExpectedMethod; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2400 | return; |
| 2401 | } |
| 2402 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2403 | if (!Attr.isArgIdent(0)) { |
| 2404 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2405 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2406 | return; |
| 2407 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2408 | |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2409 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2410 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2411 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 2412 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 2413 | << IL->Ident; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2414 | return; |
| 2415 | } |
| 2416 | |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2417 | if (F == ObjCMethodFamilyAttr::OMF_init && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2418 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2419 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2420 | << method->getResultType(); |
| 2421 | // Ignore the attribute. |
| 2422 | return; |
| 2423 | } |
| 2424 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2425 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2426 | S.Context, F)); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2427 | } |
| 2428 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2429 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 2430 | const AttributeList &Attr) { |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2431 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 2432 | if (OCI == 0) { |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 2433 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2434 | << Attr.getName() << ExpectedObjectiveCInterface; |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2435 | return; |
| 2436 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2437 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2438 | D->addAttr(::new (S.Context) |
| 2439 | ObjCExceptionAttr(Attr.getRange(), S.Context, |
| 2440 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2443 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2444 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2445 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2446 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2447 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2448 | return; |
| 2449 | } |
| 2450 | } |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2451 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2452 | QualType T = PD->getType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2453 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2454 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2455 | return; |
| 2456 | } |
| 2457 | } |
| 2458 | else { |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2459 | // It is okay to include this attribute on properties, e.g.: |
| 2460 | // |
| 2461 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2462 | // |
| 2463 | // In this case it follows tradition and suppresses an error in the above |
| 2464 | // case. |
Fariborz Jahanian | 9b2eb7b | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2465 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2466 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2467 | D->addAttr(::new (S.Context) |
| 2468 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2469 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2470 | } |
| 2471 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2472 | static void |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2473 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2474 | if (!isa<FunctionDecl>(D)) { |
| 2475 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 2476 | return; |
| 2477 | } |
| 2478 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2479 | D->addAttr(::new (S.Context) |
| 2480 | OverloadableAttr(Attr.getRange(), S.Context, |
| 2481 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2484 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2485 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2486 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2487 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2488 | return; |
| 2489 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2490 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2491 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2492 | BlocksAttr::BlockType type; |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2493 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2494 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2495 | << Attr.getName() << II; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2496 | return; |
| 2497 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2498 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2499 | D->addAttr(::new (S.Context) |
| 2500 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2501 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2504 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2505 | // check the attribute arguments. |
| 2506 | if (Attr.getNumArgs() > 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2507 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2508 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2509 | } |
| 2510 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2511 | unsigned sentinel = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2512 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2513 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2514 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2515 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2516 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2517 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2518 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2519 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2520 | return; |
| 2521 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2522 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2523 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2524 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2525 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2526 | return; |
| 2527 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2528 | |
| 2529 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2532 | unsigned nullPos = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2533 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2534 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2535 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2536 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2537 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2538 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2539 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2540 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2541 | return; |
| 2542 | } |
| 2543 | nullPos = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2544 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2545 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2546 | // FIXME: This error message could be improved, it would be nice |
| 2547 | // to say what the bounds actually are. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2548 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2549 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2550 | return; |
| 2551 | } |
| 2552 | } |
| 2553 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2554 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2555 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2556 | if (isa<FunctionNoProtoType>(FT)) { |
| 2557 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2558 | return; |
| 2559 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2560 | |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2561 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2562 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2563 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2564 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2565 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2566 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2567 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2568 | return; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2569 | } |
Eli Friedman | a0b2ba1 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2570 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2571 | if (!BD->isVariadic()) { |
| 2572 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2573 | return; |
| 2574 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2575 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2576 | QualType Ty = V->getType(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2577 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2578 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2579 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2580 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2581 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2582 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2583 | return; |
| 2584 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2585 | } else { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2586 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2587 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2588 | return; |
| 2589 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2590 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2591 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2592 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2593 | return; |
| 2594 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2595 | D->addAttr(::new (S.Context) |
| 2596 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2597 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 2600 | static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 2601 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
| 2602 | RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context)); |
| 2603 | else |
| 2604 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2605 | } |
| 2606 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2607 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 2608 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2609 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Kaelyn Uhrain | d449c79 | 2012-11-13 00:18:47 +0000 | [diff] [blame] | 2610 | << Attr.getName() << ExpectedFunctionMethodOrClass; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2611 | return; |
| 2612 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2613 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2614 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2615 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2616 | << Attr.getName() << 0; |
Nuno Lopes | f857798 | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2617 | return; |
| 2618 | } |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2619 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2620 | if (MD->getResultType()->isVoidType()) { |
| 2621 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2622 | << Attr.getName() << 1; |
| 2623 | return; |
| 2624 | } |
| 2625 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2626 | D->addAttr(::new (S.Context) |
| 2627 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2628 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2629 | } |
| 2630 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2631 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2632 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
Fariborz Jahanian | 13c7fcc | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 2633 | if (isa<CXXRecordDecl>(D)) { |
| 2634 | D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| 2635 | return; |
| 2636 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2637 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2638 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | f23ecd9 | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 2639 | return; |
| 2640 | } |
| 2641 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2642 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 2643 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2644 | nd->addAttr(::new (S.Context) |
| 2645 | WeakAttr(Attr.getRange(), S.Context, |
| 2646 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2647 | } |
| 2648 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2649 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2650 | // weak_import only applies to variable & function declarations. |
| 2651 | bool isDef = false; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2652 | if (!D->canBeWeakImported(isDef)) { |
| 2653 | if (isDef) |
Reid Kleckner | beba3e8 | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2654 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2655 | << "weak_import"; |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2656 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2657 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 90eed21 | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2658 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2659 | // Nothing to warn about here. |
| 2660 | } else |
Fariborz Jahanian | c034974 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2661 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2662 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2663 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2664 | return; |
| 2665 | } |
| 2666 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2667 | D->addAttr(::new (S.Context) |
| 2668 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2669 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2672 | // Handles reqd_work_group_size and work_group_size_hint. |
| 2673 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2674 | const AttributeList &Attr) { |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2675 | unsigned WGSize[3]; |
| 2676 | for (unsigned i = 0; i < 3; ++i) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2677 | Expr *E = Attr.getArgAsExpr(i); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2678 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2679 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2680 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 2681 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 2682 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 2683 | << E->getSourceRange(); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2684 | return; |
| 2685 | } |
| 2686 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 2687 | } |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2688 | |
| 2689 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2690 | && D->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 2691 | ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>(); |
| 2692 | if (!(A->getXDim() == WGSize[0] && |
| 2693 | A->getYDim() == WGSize[1] && |
| 2694 | A->getZDim() == WGSize[2])) { |
| 2695 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2696 | Attr.getName(); |
| 2697 | } |
| 2698 | } |
| 2699 | |
| 2700 | if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint |
| 2701 | && D->hasAttr<WorkGroupSizeHintAttr>()) { |
| 2702 | WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>(); |
| 2703 | if (!(A->getXDim() == WGSize[0] && |
| 2704 | A->getYDim() == WGSize[1] && |
| 2705 | A->getZDim() == WGSize[2])) { |
| 2706 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2707 | Attr.getName(); |
| 2708 | } |
| 2709 | } |
| 2710 | |
| 2711 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize) |
| 2712 | D->addAttr(::new (S.Context) |
| 2713 | ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2714 | WGSize[0], WGSize[1], WGSize[2], |
| 2715 | Attr.getAttributeSpellingListIndex())); |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2716 | else |
| 2717 | D->addAttr(::new (S.Context) |
| 2718 | WorkGroupSizeHintAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2719 | WGSize[0], WGSize[1], WGSize[2], |
| 2720 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2723 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2724 | assert(Attr.getKind() == AttributeList::AT_VecTypeHint); |
| 2725 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2726 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2727 | return; |
| 2728 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2729 | if (!Attr.hasParsedType()) { |
| 2730 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2731 | << Attr.getName() << 1; |
| 2732 | return; |
| 2733 | } |
| 2734 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2735 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg()); |
| 2736 | |
| 2737 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2738 | (ParmType->isBooleanType() || |
| 2739 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2740 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2741 | << ParmType; |
| 2742 | return; |
| 2743 | } |
| 2744 | |
| 2745 | if (Attr.getKind() == AttributeList::AT_VecTypeHint && |
| 2746 | D->hasAttr<VecTypeHintAttr>()) { |
| 2747 | VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>(); |
| 2748 | if (A->getTypeHint() != ParmType) { |
| 2749 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2750 | return; |
| 2751 | } |
| 2752 | } |
| 2753 | |
| 2754 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
| 2755 | ParmType, Attr.getLoc())); |
| 2756 | } |
| 2757 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2758 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2759 | StringRef Name, |
| 2760 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2761 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2762 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2763 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2764 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2765 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2766 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2767 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2768 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2769 | AttrSpellingListIndex); |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2772 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2773 | // Make sure that there is a string literal as the sections's single |
| 2774 | // argument. |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2775 | StringRef Str; |
| 2776 | SourceLocation LiteralLoc; |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2777 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2778 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2779 | |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2780 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2781 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2782 | if (!Error.empty()) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2783 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2784 | << Error; |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2785 | return; |
| 2786 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2787 | |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2788 | // This attribute cannot be applied to local variables. |
| 2789 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2790 | S.Diag(LiteralLoc, diag::err_attribute_section_local_variable); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2791 | return; |
| 2792 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2793 | |
| 2794 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2795 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2796 | if (NewAttr) |
| 2797 | D->addAttr(NewAttr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2800 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2801 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2802 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2803 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2804 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2805 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2806 | D->addAttr(::new (S.Context) |
| 2807 | NoThrowAttr(Attr.getRange(), S.Context, |
| 2808 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2809 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2812 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2813 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2814 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2815 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2816 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2817 | D->addAttr(::new (S.Context) |
| 2818 | ConstAttr(Attr.getRange(), S.Context, |
| 2819 | Attr.getAttributeSpellingListIndex() )); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2820 | } |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2823 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2824 | D->addAttr(::new (S.Context) |
| 2825 | PureAttr(Attr.getRange(), S.Context, |
| 2826 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2829 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2830 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2831 | if (!VD || !VD->hasLocalStorage()) { |
Aaron Ballman | ebaee6b | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2832 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2833 | return; |
| 2834 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2835 | |
Aaron Ballman | ebaee6b | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2836 | Expr *E = Attr.getArgAsExpr(0); |
| 2837 | SourceLocation Loc = E->getExprLoc(); |
| 2838 | FunctionDecl *FD = 0; |
| 2839 | DeclarationNameInfo NI; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2840 | |
Aaron Ballman | ebaee6b | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2841 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 2842 | // will warn the user. |
| 2843 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2844 | if (DRE->hasQualifier()) |
| 2845 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 2846 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 2847 | NI = DRE->getNameInfo(); |
| 2848 | if (!FD) { |
| 2849 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 2850 | << NI.getName(); |
| 2851 | return; |
| 2852 | } |
| 2853 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2854 | if (ULE->hasExplicitTemplateArgs()) |
| 2855 | S.Diag(Loc, diag::warn_cleanup_ext); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2856 | |
Aaron Ballman | ebaee6b | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2857 | // This will diagnose the case where the function cannot be found. |
| 2858 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 2859 | NI = ULE->getNameInfo(); |
| 2860 | } else { |
| 2861 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2862 | return; |
| 2863 | } |
| 2864 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2865 | if (FD->getNumParams() != 1) { |
Aaron Ballman | ebaee6b | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2866 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2867 | << NI.getName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2868 | return; |
| 2869 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2870 | |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2871 | // We're currently more strict than GCC about what function types we accept. |
| 2872 | // If this ever proves to be a problem it should be easy to fix. |
| 2873 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2874 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2875 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2876 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | ebaee6b | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2877 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 2878 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2879 | return; |
| 2880 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2881 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2882 | D->addAttr(::new (S.Context) |
| 2883 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2884 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2887 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2888 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2889 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2890 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2891 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2892 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2893 | return; |
| 2894 | } |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2895 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2896 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2897 | uint64_t ArgIdx; |
| 2898 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 2899 | Attr.getLoc(), 1, IdxExpr, ArgIdx)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2900 | return; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2901 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2902 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2903 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2904 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2905 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2906 | if (not_nsstring_type && |
| 2907 | !isCFStringType(Ty, S.Context) && |
| 2908 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2909 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2910 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2911 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2912 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2913 | << IdxExpr->getSourceRange(); |
| 2914 | return; |
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 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2917 | if (!isNSStringType(Ty, S.Context) && |
| 2918 | !isCFStringType(Ty, S.Context) && |
| 2919 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2920 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2921 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2922 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2923 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2924 | << IdxExpr->getSourceRange(); |
| 2925 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2928 | // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex |
| 2929 | // because that has corrected for the implicit this parameter, and is zero- |
| 2930 | // based. The attribute expects what the user wrote explicitly. |
| 2931 | llvm::APSInt Val; |
| 2932 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2933 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2934 | D->addAttr(::new (S.Context) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2935 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2936 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2939 | enum FormatAttrKind { |
| 2940 | CFStringFormat, |
| 2941 | NSStringFormat, |
| 2942 | StrftimeFormat, |
| 2943 | SupportedFormat, |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2944 | IgnoredFormat, |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2945 | InvalidFormat |
| 2946 | }; |
| 2947 | |
| 2948 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2949 | /// types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2950 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2951 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2952 | // Check for formats that get handled specially. |
| 2953 | .Case("NSString", NSStringFormat) |
| 2954 | .Case("CFString", CFStringFormat) |
| 2955 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2956 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2957 | // Otherwise, check for supported formats. |
| 2958 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2959 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2960 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2961 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2962 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2963 | .Default(InvalidFormat); |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2966 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2967 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2968 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2969 | const AttributeList &Attr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2970 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2971 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2972 | return; |
| 2973 | } |
| 2974 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2975 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2976 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2977 | Attr.setInvalid(); |
| 2978 | return; |
| 2979 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2980 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2981 | if (S.Context.getAsArrayType(T)) |
| 2982 | T = S.Context.getBaseElementType(T); |
| 2983 | if (!T->getAs<RecordType>()) { |
| 2984 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2985 | Attr.setInvalid(); |
| 2986 | return; |
| 2987 | } |
| 2988 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2989 | Expr *priorityExpr = Attr.getArgAsExpr(0); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2990 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2991 | llvm::APSInt priority(32); |
| 2992 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 2993 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 2994 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 2995 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 2996 | << priorityExpr->getSourceRange(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2997 | Attr.setInvalid(); |
| 2998 | return; |
| 2999 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 3000 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3001 | if (prioritynum < 101 || prioritynum > 65535) { |
| 3002 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 3003 | << priorityExpr->getSourceRange(); |
| 3004 | Attr.setInvalid(); |
| 3005 | return; |
| 3006 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3007 | D->addAttr(::new (S.Context) |
| 3008 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 3009 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3010 | } |
| 3011 | |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3012 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 3013 | IdentifierInfo *Format, int FormatIdx, |
| 3014 | int FirstArg, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3015 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3016 | // Check whether we already have an equivalent format attribute. |
| 3017 | for (specific_attr_iterator<FormatAttr> |
| 3018 | i = D->specific_attr_begin<FormatAttr>(), |
| 3019 | e = D->specific_attr_end<FormatAttr>(); |
| 3020 | i != e ; ++i) { |
| 3021 | FormatAttr *f = *i; |
| 3022 | if (f->getType() == Format && |
| 3023 | f->getFormatIdx() == FormatIdx && |
| 3024 | f->getFirstArg() == FirstArg) { |
| 3025 | // If we don't have a valid location for this attribute, adopt the |
| 3026 | // location. |
| 3027 | if (f->getLocation().isInvalid()) |
| 3028 | f->setRange(Range); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3029 | return NULL; |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3030 | } |
| 3031 | } |
| 3032 | |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3033 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 3034 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3035 | } |
| 3036 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3037 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3038 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3039 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3040 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3041 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3042 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3043 | return; |
| 3044 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3045 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3046 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3047 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3048 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3049 | return; |
| 3050 | } |
| 3051 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3052 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3053 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3054 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 3055 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3056 | unsigned FirstIdx = 1; |
| 3057 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3058 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 3059 | StringRef Format = II->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3060 | |
| 3061 | // Normalize the argument, __foo__ becomes foo. |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3062 | if (Format.startswith("__") && Format.endswith("__")) { |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3063 | Format = Format.substr(2, Format.size() - 4); |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3064 | // If we've modified the string name, we need a new identifier for it. |
| 3065 | II = &S.Context.Idents.get(Format); |
| 3066 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3067 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3068 | // Check for supported formats. |
| 3069 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3070 | |
| 3071 | if (Kind == IgnoredFormat) |
| 3072 | return; |
| 3073 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3074 | if (Kind == InvalidFormat) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3075 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3076 | << "format" << II->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3077 | return; |
| 3078 | } |
| 3079 | |
| 3080 | // checks for the 2nd argument |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3081 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3082 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3083 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 3084 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3085 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3086 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3087 | << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3088 | return; |
| 3089 | } |
| 3090 | |
| 3091 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3092 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3093 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3094 | return; |
| 3095 | } |
| 3096 | |
| 3097 | // FIXME: Do we need to bounds check? |
| 3098 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3099 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3100 | if (HasImplicitThisParam) { |
| 3101 | if (ArgIdx == 0) { |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3102 | S.Diag(Attr.getLoc(), |
| 3103 | diag::err_format_attribute_implicit_this_format_string) |
| 3104 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3105 | return; |
| 3106 | } |
| 3107 | ArgIdx--; |
| 3108 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3109 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3110 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3111 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3112 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3113 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3114 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3115 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3116 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3117 | return; |
| 3118 | } |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3119 | } else if (Kind == NSStringFormat) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3120 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3121 | // semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3122 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3123 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3124 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3125 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3126 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3127 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3128 | } else if (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3129 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3130 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3131 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3132 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3133 | return; |
| 3134 | } |
| 3135 | |
| 3136 | // check the 3rd argument |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3137 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3138 | llvm::APSInt FirstArg(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3139 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 3140 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3141 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3142 | << Attr.getName() << 3 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3143 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3144 | return; |
| 3145 | } |
| 3146 | |
| 3147 | // check if the function is variadic if the 3rd argument non-zero |
| 3148 | if (FirstArg != 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3149 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3150 | ++NumArgs; // +1 for ... |
| 3151 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3152 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3153 | return; |
| 3154 | } |
| 3155 | } |
| 3156 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3157 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3158 | // variable the input is just the current time + the format string. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3159 | if (Kind == StrftimeFormat) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3160 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3161 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 3162 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3163 | return; |
| 3164 | } |
| 3165 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3166 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3167 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3168 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3169 | return; |
| 3170 | } |
| 3171 | |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3172 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3173 | Idx.getZExtValue(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3174 | FirstArg.getZExtValue(), |
| 3175 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3176 | if (NewAttr) |
| 3177 | D->addAttr(NewAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3178 | } |
| 3179 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3180 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 3181 | const AttributeList &Attr) { |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3182 | // Try to find the underlying union declaration. |
| 3183 | RecordDecl *RD = 0; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3184 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3185 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3186 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3187 | else |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3188 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3189 | |
| 3190 | if (!RD || !RD->isUnion()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3191 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3192 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3193 | return; |
| 3194 | } |
| 3195 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3196 | if (!RD->isCompleteDefinition()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3197 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3198 | diag::warn_transparent_union_attribute_not_definition); |
| 3199 | return; |
| 3200 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3201 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3202 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3203 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3204 | if (Field == FieldEnd) { |
| 3205 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3206 | return; |
| 3207 | } |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3208 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3209 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3210 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3211 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3212 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3213 | diag::warn_transparent_union_attribute_floating) |
| 3214 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3215 | return; |
| 3216 | } |
| 3217 | |
| 3218 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3219 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3220 | for (; Field != FieldEnd; ++Field) { |
| 3221 | QualType FieldType = Field->getType(); |
| 3222 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 3223 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 3224 | // Warn if we drop the attribute. |
| 3225 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3226 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3227 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3228 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3229 | diag::warn_transparent_union_attribute_field_size_align) |
| 3230 | << isSize << Field->getDeclName() << FieldBits; |
| 3231 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3232 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3233 | diag::note_transparent_union_first_field_size_align) |
| 3234 | << isSize << FirstBits; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3235 | return; |
| 3236 | } |
| 3237 | } |
| 3238 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3239 | RD->addAttr(::new (S.Context) |
| 3240 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 3241 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3242 | } |
| 3243 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3244 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3245 | // Make sure that there is a string literal as the annotation's single |
| 3246 | // argument. |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3247 | StringRef Str; |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3248 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3249 | return; |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3250 | |
| 3251 | // Don't duplicate annotations that are already set. |
| 3252 | for (specific_attr_iterator<AnnotateAttr> |
| 3253 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 3254 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3255 | if ((*i)->getAnnotation() == Str) |
| 3256 | return; |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3257 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3258 | |
| 3259 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3260 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3261 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3262 | } |
| 3263 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3264 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3265 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3266 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3267 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3268 | << Attr.getName() << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3269 | return; |
| 3270 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3271 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3272 | if (Attr.getNumArgs() == 0) { |
| 3273 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 3274 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 3275 | return; |
| 3276 | } |
| 3277 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3278 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3279 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3280 | S.Diag(Attr.getEllipsisLoc(), |
| 3281 | diag::err_pack_expansion_without_parameter_packs); |
| 3282 | return; |
| 3283 | } |
| 3284 | |
| 3285 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 3286 | return; |
| 3287 | |
| 3288 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 3289 | Attr.isPackExpansion()); |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3293 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3294 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3295 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3296 | |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3297 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3298 | if (TmpAttr.isAlignas()) { |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3299 | // C++11 [dcl.align]p1: |
| 3300 | // An alignment-specifier may be applied to a variable or to a class |
| 3301 | // data member, but it shall not be applied to a bit-field, a function |
| 3302 | // parameter, the formal parameter of a catch clause, or a variable |
| 3303 | // declared with the register storage class specifier. An |
| 3304 | // alignment-specifier may also be applied to the declaration of a class |
| 3305 | // or enumeration type. |
| 3306 | // C11 6.7.5/2: |
| 3307 | // An alignment attribute shall not be specified in a declaration of |
| 3308 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3309 | // object declared with the register storage-class specifier. |
| 3310 | int DiagKind = -1; |
| 3311 | if (isa<ParmVarDecl>(D)) { |
| 3312 | DiagKind = 0; |
| 3313 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3314 | if (VD->getStorageClass() == SC_Register) |
| 3315 | DiagKind = 1; |
| 3316 | if (VD->isExceptionVariable()) |
| 3317 | DiagKind = 2; |
| 3318 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 3319 | if (FD->isBitField()) |
| 3320 | DiagKind = 3; |
| 3321 | } else if (!isa<TagDecl>(D)) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3322 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) |
| 3323 | << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'") |
Richard Smith | 5f838aa | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3324 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3325 | : ExpectedVariableFieldOrTag); |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3326 | return; |
| 3327 | } |
| 3328 | if (DiagKind != -1) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3329 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 3330 | << TmpAttr.isC11() << DiagKind; |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3331 | return; |
| 3332 | } |
| 3333 | } |
| 3334 | |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3335 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3336 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3337 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3338 | AA->setPackExpansion(IsPackExpansion); |
| 3339 | D->addAttr(AA); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3340 | return; |
| 3341 | } |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3342 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3343 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3344 | llvm::APSInt Alignment(32); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3345 | ExprResult ICE |
| 3346 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3347 | diag::err_aligned_attribute_argument_not_int, |
| 3348 | /*AllowFold*/ false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3349 | if (ICE.isInvalid()) |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3350 | return; |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3351 | |
| 3352 | // C++11 [dcl.align]p2: |
| 3353 | // -- if the constant expression evaluates to zero, the alignment |
| 3354 | // specifier shall have no effect |
| 3355 | // C11 6.7.5p6: |
| 3356 | // An alignment specification of zero has no effect. |
| 3357 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 3358 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3359 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 3360 | << E->getSourceRange(); |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3361 | return; |
| 3362 | } |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3363 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3364 | if (TmpAttr.isDeclspec()) { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3365 | // We've already verified it's a power of 2, now let's make sure it's |
| 3366 | // 8192 or less. |
| 3367 | if (Alignment.getZExtValue() > 8192) { |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3368 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3369 | << E->getSourceRange(); |
| 3370 | return; |
| 3371 | } |
| 3372 | } |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3373 | |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3374 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 3375 | ICE.take(), SpellingListIndex); |
| 3376 | AA->setPackExpansion(IsPackExpansion); |
| 3377 | D->addAttr(AA); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3378 | } |
| 3379 | |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3380 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3381 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3382 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3383 | // FIXME: Perform checking of type validity |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3384 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3385 | SpellingListIndex); |
| 3386 | AA->setPackExpansion(IsPackExpansion); |
| 3387 | D->addAttr(AA); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3388 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3389 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3390 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3391 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3392 | |
| 3393 | QualType Ty; |
| 3394 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3395 | Ty = VD->getType(); |
| 3396 | else |
| 3397 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
Richard Smith | 4da0903 | 2013-02-22 09:21:42 +0000 | [diff] [blame] | 3398 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3399 | return; |
| 3400 | |
| 3401 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3402 | // The combined effect of all alignment attributes in a declaration shall |
| 3403 | // not specify an alignment that is less strict than the alignment that |
| 3404 | // would otherwise be required for the entity being declared. |
| 3405 | AlignedAttr *AlignasAttr = 0; |
| 3406 | unsigned Align = 0; |
| 3407 | for (specific_attr_iterator<AlignedAttr> |
| 3408 | I = D->specific_attr_begin<AlignedAttr>(), |
| 3409 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 3410 | if (I->isAlignmentDependent()) |
| 3411 | return; |
| 3412 | if (I->isAlignas()) |
| 3413 | AlignasAttr = *I; |
| 3414 | Align = std::max(Align, I->getAlignment(Context)); |
| 3415 | } |
| 3416 | |
| 3417 | if (AlignasAttr && Align) { |
| 3418 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 3419 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 3420 | if (NaturalAlign > RequestedAlign) |
| 3421 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 3422 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 3423 | } |
| 3424 | } |
| 3425 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3426 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3427 | /// type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3428 | /// |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3429 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3430 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3431 | /// HImode, not an intermediate pointer. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3432 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3433 | // This attribute isn't documented, but glibc uses it. It changes |
| 3434 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3435 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 750f73a | 2013-07-30 14:29:12 +0000 | [diff] [blame] | 3436 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3437 | << AANT_ArgumentIdentifier; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3438 | return; |
| 3439 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3440 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3441 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
| 3442 | StringRef Str = Name->getName(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3443 | |
| 3444 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3445 | if (Str.startswith("__") && Str.endswith("__")) |
| 3446 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3447 | |
| 3448 | unsigned DestWidth = 0; |
| 3449 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3450 | bool ComplexMode = false; |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3451 | switch (Str.size()) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3452 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3453 | switch (Str[0]) { |
| 3454 | case 'Q': DestWidth = 8; break; |
| 3455 | case 'H': DestWidth = 16; break; |
| 3456 | case 'S': DestWidth = 32; break; |
| 3457 | case 'D': DestWidth = 64; break; |
| 3458 | case 'X': DestWidth = 96; break; |
| 3459 | case 'T': DestWidth = 128; break; |
| 3460 | } |
| 3461 | if (Str[1] == 'F') { |
| 3462 | IntegerMode = false; |
| 3463 | } else if (Str[1] == 'C') { |
| 3464 | IntegerMode = false; |
| 3465 | ComplexMode = true; |
| 3466 | } else if (Str[1] != 'I') { |
| 3467 | DestWidth = 0; |
| 3468 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3469 | break; |
| 3470 | case 4: |
| 3471 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3472 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3473 | if (Str == "word") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3474 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3475 | else if (Str == "byte") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3476 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3477 | break; |
| 3478 | case 7: |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3479 | if (Str == "pointer") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3480 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3481 | break; |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3482 | case 11: |
| 3483 | if (Str == "unwind_word") |
Rafael Espindola | 0b1de54 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3484 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3485 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | QualType OldTy; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3489 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3490 | OldTy = TD->getUnderlyingType(); |
| 3491 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3492 | OldTy = VD->getType(); |
| 3493 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3494 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3495 | << "mode" << Attr.getRange(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3496 | return; |
| 3497 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3498 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3499 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3500 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3501 | else if (IntegerMode) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3502 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3503 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3504 | } else if (ComplexMode) { |
| 3505 | if (!OldTy->isComplexType()) |
| 3506 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3507 | } else { |
| 3508 | if (!OldTy->isFloatingType()) |
| 3509 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3510 | } |
| 3511 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3512 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3513 | // and friends, at least with glibc. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3514 | // FIXME: Make sure floating-point mappings are accurate |
| 3515 | // FIXME: Support XF and TF types |
Stepan Dyatkovskiy | d569295 | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3516 | if (!DestWidth) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3517 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3518 | return; |
Stepan Dyatkovskiy | d569295 | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3519 | } |
| 3520 | |
| 3521 | QualType NewTy; |
| 3522 | |
| 3523 | if (IntegerMode) |
| 3524 | NewTy = S.Context.getIntTypeForBitwidth(DestWidth, |
| 3525 | OldTy->isSignedIntegerType()); |
| 3526 | else |
| 3527 | NewTy = S.Context.getRealTypeForBitwidth(DestWidth); |
| 3528 | |
| 3529 | if (NewTy.isNull()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3530 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3531 | return; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3532 | } |
| 3533 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3534 | if (ComplexMode) { |
| 3535 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
| 3538 | // Install the new type. |
Enea Zaffanella | c2fa6b6 | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3539 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3540 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 3541 | else |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3542 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | c2fa6b6 | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3543 | |
| 3544 | D->addAttr(::new (S.Context) |
| 3545 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 3546 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3547 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3548 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3549 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3550 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3551 | if (!VD->hasGlobalStorage()) |
| 3552 | S.Diag(Attr.getLoc(), |
| 3553 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3554 | << Attr.getName(); |
| 3555 | } else if (!isFunctionOrMethod(D)) { |
| 3556 | S.Diag(Attr.getLoc(), |
| 3557 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3558 | << Attr.getName(); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3559 | return; |
| 3560 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3561 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3562 | D->addAttr(::new (S.Context) |
| 3563 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3564 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3565 | } |
| 3566 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3567 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3568 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3569 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3570 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3571 | return; |
| 3572 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3573 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3574 | D->addAttr(::new (S.Context) |
| 3575 | NoInlineAttr(Attr.getRange(), S.Context, |
| 3576 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3577 | } |
| 3578 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3579 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 3580 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3581 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3582 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3583 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3584 | return; |
| 3585 | } |
| 3586 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3587 | D->addAttr(::new (S.Context) |
| 3588 | NoInstrumentFunctionAttr(Attr.getRange(), S.Context, |
| 3589 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3590 | } |
| 3591 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3592 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3593 | if (S.LangOpts.CUDA) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3594 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3595 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3596 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3597 | return; |
| 3598 | } |
| 3599 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3600 | D->addAttr(::new (S.Context) |
| 3601 | CUDAConstantAttr(Attr.getRange(), S.Context, |
| 3602 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3603 | } else { |
| 3604 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 3605 | } |
| 3606 | } |
| 3607 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3608 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3609 | if (S.LangOpts.CUDA) { |
| 3610 | // check the attribute arguments. |
| 3611 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3612 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3613 | << Attr.getName() << 0; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3614 | return; |
| 3615 | } |
| 3616 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3617 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3618 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3619 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3620 | return; |
| 3621 | } |
| 3622 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3623 | D->addAttr(::new (S.Context) |
| 3624 | CUDADeviceAttr(Attr.getRange(), S.Context, |
| 3625 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3626 | } else { |
| 3627 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 3628 | } |
| 3629 | } |
| 3630 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3631 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3632 | if (S.LangOpts.CUDA) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3633 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3634 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3635 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3636 | return; |
| 3637 | } |
| 3638 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3639 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3640 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 3641 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3642 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3643 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3644 | << FD->getType() |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3645 | << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(), |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3646 | "void"); |
| 3647 | } else { |
| 3648 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3649 | << FD->getType(); |
| 3650 | } |
| 3651 | return; |
| 3652 | } |
| 3653 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3654 | D->addAttr(::new (S.Context) |
| 3655 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
| 3656 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3657 | } else { |
| 3658 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 3659 | } |
| 3660 | } |
| 3661 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3662 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3663 | if (S.LangOpts.CUDA) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3664 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3665 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3666 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3667 | return; |
| 3668 | } |
| 3669 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3670 | D->addAttr(::new (S.Context) |
| 3671 | CUDAHostAttr(Attr.getRange(), S.Context, |
| 3672 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3673 | } else { |
| 3674 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 3675 | } |
| 3676 | } |
| 3677 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3678 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3679 | if (S.LangOpts.CUDA) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3680 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3681 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3682 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3683 | return; |
| 3684 | } |
| 3685 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3686 | D->addAttr(::new (S.Context) |
| 3687 | CUDASharedAttr(Attr.getRange(), S.Context, |
| 3688 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3689 | } else { |
| 3690 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 3691 | } |
| 3692 | } |
| 3693 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3694 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3695 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3696 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3697 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3698 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3699 | return; |
| 3700 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3701 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3702 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3703 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3704 | return; |
| 3705 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3706 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3707 | D->addAttr(::new (S.Context) |
| 3708 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3709 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3712 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3713 | if (hasDeclarator(D)) return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3714 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3715 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3716 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3717 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3718 | CallingConv CC; |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3719 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3720 | return; |
| 3721 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3722 | if (!isa<ObjCMethodDecl>(D)) { |
| 3723 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3724 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3725 | return; |
| 3726 | } |
| 3727 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3728 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3729 | case AttributeList::AT_FastCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3730 | D->addAttr(::new (S.Context) |
| 3731 | FastCallAttr(Attr.getRange(), S.Context, |
| 3732 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3733 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3734 | case AttributeList::AT_StdCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3735 | D->addAttr(::new (S.Context) |
| 3736 | StdCallAttr(Attr.getRange(), S.Context, |
| 3737 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3738 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3739 | case AttributeList::AT_ThisCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3740 | D->addAttr(::new (S.Context) |
| 3741 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3742 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 04633eb | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3743 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3744 | case AttributeList::AT_CDecl: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3745 | D->addAttr(::new (S.Context) |
| 3746 | CDeclAttr(Attr.getRange(), S.Context, |
| 3747 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3748 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3749 | case AttributeList::AT_Pascal: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3750 | D->addAttr(::new (S.Context) |
| 3751 | PascalAttr(Attr.getRange(), S.Context, |
| 3752 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3753 | return; |
Charles Davis | e8519c3 | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3754 | case AttributeList::AT_MSABI: |
| 3755 | D->addAttr(::new (S.Context) |
| 3756 | MSABIAttr(Attr.getRange(), S.Context, |
| 3757 | Attr.getAttributeSpellingListIndex())); |
| 3758 | return; |
| 3759 | case AttributeList::AT_SysVABI: |
| 3760 | D->addAttr(::new (S.Context) |
| 3761 | SysVABIAttr(Attr.getRange(), S.Context, |
| 3762 | Attr.getAttributeSpellingListIndex())); |
| 3763 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3764 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3765 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3766 | switch (CC) { |
| 3767 | case CC_AAPCS: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3768 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3769 | break; |
| 3770 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3771 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3772 | break; |
| 3773 | default: |
| 3774 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3777 | D->addAttr(::new (S.Context) |
| 3778 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3779 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3780 | return; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3781 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3782 | case AttributeList::AT_PnaclCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3783 | D->addAttr(::new (S.Context) |
| 3784 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 3785 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3786 | return; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3787 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3788 | D->addAttr(::new (S.Context) |
| 3789 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3790 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3791 | return; |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3792 | |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3793 | default: |
| 3794 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3795 | } |
| 3796 | } |
| 3797 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3798 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3799 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3800 | } |
| 3801 | |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3802 | static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3803 | Expr *E = Attr.getArgAsExpr(0); |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3804 | llvm::APSInt ArgNum(32); |
| 3805 | if (E->isTypeDependent() || E->isValueDependent() || |
| 3806 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 3807 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3808 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3809 | << E->getSourceRange(); |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3810 | return; |
| 3811 | } |
| 3812 | |
| 3813 | D->addAttr(::new (S.Context) OpenCLImageAccessAttr( |
| 3814 | Attr.getRange(), S.Context, ArgNum.getZExtValue())); |
| 3815 | } |
| 3816 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3817 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3818 | const FunctionDecl *FD) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3819 | if (attr.isInvalid()) |
| 3820 | return true; |
| 3821 | |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3822 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3823 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3824 | attr.setInvalid(); |
| 3825 | return true; |
| 3826 | } |
| 3827 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3828 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 3829 | // move to TargetAttributesSema one day. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3830 | switch (attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3831 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3832 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3833 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3834 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3835 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
Charles Davis | e8519c3 | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3836 | case AttributeList::AT_MSABI: |
| 3837 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 3838 | CC_X86_64Win64; |
| 3839 | break; |
| 3840 | case AttributeList::AT_SysVABI: |
| 3841 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 3842 | CC_C; |
| 3843 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3844 | case AttributeList::AT_Pcs: { |
Aaron Ballman | fbf6f5c | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 3845 | StringRef StrRef; |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3846 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3847 | attr.setInvalid(); |
| 3848 | return true; |
| 3849 | } |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3850 | if (StrRef == "aapcs") { |
| 3851 | CC = CC_AAPCS; |
| 3852 | break; |
| 3853 | } else if (StrRef == "aapcs-vfp") { |
| 3854 | CC = CC_AAPCS_VFP; |
| 3855 | break; |
| 3856 | } |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3857 | |
| 3858 | attr.setInvalid(); |
| 3859 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3860 | return true; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3861 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3862 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3863 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3864 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3867 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3868 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 3869 | if (A == TargetInfo::CCCR_Warning) { |
| 3870 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3871 | |
| 3872 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3873 | if (FD) |
| 3874 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3875 | TargetInfo::CCMT_NonMember; |
| 3876 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3877 | } |
| 3878 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3879 | return false; |
| 3880 | } |
| 3881 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3882 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3883 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3884 | |
| 3885 | unsigned numParams; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3886 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3887 | return; |
| 3888 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3889 | if (!isa<ObjCMethodDecl>(D)) { |
| 3890 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3891 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3892 | return; |
| 3893 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3894 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3895 | D->addAttr(::new (S.Context) |
| 3896 | RegparmAttr(Attr.getRange(), S.Context, numParams, |
| 3897 | Attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
| 3900 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3901 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3902 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3903 | if (Attr.isInvalid()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3904 | return true; |
| 3905 | |
Aaron Ballman | ffa9d57 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3906 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3907 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3908 | return true; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3909 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3910 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3911 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3912 | llvm::APSInt NumParams(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3913 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3914 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 3915 | Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3916 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3917 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3918 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3919 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3920 | } |
| 3921 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3922 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3923 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3924 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3925 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3926 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3927 | } |
| 3928 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3929 | numParams = NumParams.getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3930 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3931 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3932 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3933 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3934 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3937 | return false; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3938 | } |
| 3939 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3940 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3941 | if (S.LangOpts.CUDA) { |
| 3942 | // check the attribute arguments. |
| 3943 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 3944 | // FIXME: 0 is not okay. |
| 3945 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3946 | return; |
| 3947 | } |
| 3948 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3949 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3950 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3951 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3952 | return; |
| 3953 | } |
| 3954 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3955 | Expr *MaxThreadsExpr = Attr.getArgAsExpr(0); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3956 | llvm::APSInt MaxThreads(32); |
| 3957 | if (MaxThreadsExpr->isTypeDependent() || |
| 3958 | MaxThreadsExpr->isValueDependent() || |
| 3959 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3960 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3961 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3962 | << MaxThreadsExpr->getSourceRange(); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3963 | return; |
| 3964 | } |
| 3965 | |
| 3966 | llvm::APSInt MinBlocks(32); |
| 3967 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3968 | Expr *MinBlocksExpr = Attr.getArgAsExpr(1); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3969 | if (MinBlocksExpr->isTypeDependent() || |
| 3970 | MinBlocksExpr->isValueDependent() || |
| 3971 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3972 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3973 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3974 | << MinBlocksExpr->getSourceRange(); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3975 | return; |
| 3976 | } |
| 3977 | } |
| 3978 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3979 | D->addAttr(::new (S.Context) |
| 3980 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 3981 | MaxThreads.getZExtValue(), |
| 3982 | MinBlocks.getZExtValue(), |
| 3983 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3984 | } else { |
| 3985 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 3986 | } |
| 3987 | } |
| 3988 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3989 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 3990 | const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3991 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3992 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3993 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3994 | return; |
| 3995 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3996 | |
| 3997 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3998 | return; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3999 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4000 | StringRef AttrName = Attr.getName()->getName(); |
| 4001 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4002 | |
| 4003 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 4004 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4005 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 4006 | return; |
| 4007 | } |
| 4008 | |
| 4009 | uint64_t ArgumentIdx; |
| 4010 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4011 | Attr.getLoc(), 2, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4012 | Attr.getArgAsExpr(1), ArgumentIdx)) |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4013 | return; |
| 4014 | |
| 4015 | uint64_t TypeTagIdx; |
| 4016 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4017 | Attr.getLoc(), 3, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4018 | Attr.getArgAsExpr(2), TypeTagIdx)) |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4019 | return; |
| 4020 | |
| 4021 | bool IsPointer = (AttrName == "pointer_with_type_tag"); |
| 4022 | if (IsPointer) { |
| 4023 | // Ensure that buffer has a pointer type. |
| 4024 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 4025 | if (!BufferTy->isPointerType()) { |
| 4026 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4027 | << Attr.getName(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4028 | } |
| 4029 | } |
| 4030 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4031 | D->addAttr(::new (S.Context) |
| 4032 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 4033 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 4034 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4035 | } |
| 4036 | |
| 4037 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 4038 | const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4039 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4040 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4041 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4042 | return; |
| 4043 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4044 | |
| 4045 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4046 | return; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4047 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4048 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4049 | QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL); |
| 4050 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4051 | D->addAttr(::new (S.Context) |
| 4052 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
| 4053 | MatchingCType, |
| 4054 | Attr.getLayoutCompatible(), |
| 4055 | Attr.getMustBeNull(), |
| 4056 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4057 | } |
| 4058 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4059 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4060 | // Checker-specific attribute handlers. |
| 4061 | //===----------------------------------------------------------------------===// |
| 4062 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4063 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4064 | return type->isDependentType() || |
| 4065 | type->isObjCObjectPointerType() || |
| 4066 | S.Context.isObjCNSObjectType(type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4067 | } |
| 4068 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4069 | return type->isDependentType() || |
| 4070 | type->isPointerType() || |
| 4071 | isValidSubjectOfNSAttribute(S, type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4072 | } |
| 4073 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4074 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4075 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4076 | if (!param) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4077 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4078 | << Attr.getRange() << Attr.getName() << ExpectedParameter; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4079 | return; |
| 4080 | } |
| 4081 | |
| 4082 | bool typeOK, cf; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4083 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4084 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 4085 | cf = false; |
| 4086 | } else { |
| 4087 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 4088 | cf = true; |
| 4089 | } |
| 4090 | |
| 4091 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4092 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4093 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4094 | return; |
| 4095 | } |
| 4096 | |
| 4097 | if (cf) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4098 | param->addAttr(::new (S.Context) |
| 4099 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 4100 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4101 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4102 | param->addAttr(::new (S.Context) |
| 4103 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 4104 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4105 | } |
| 4106 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4107 | static void handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 4108 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4109 | if (!isa<ObjCMethodDecl>(D)) { |
| 4110 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4111 | << Attr.getRange() << Attr.getName() << ExpectedMethod; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4112 | return; |
| 4113 | } |
| 4114 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4115 | D->addAttr(::new (S.Context) |
| 4116 | NSConsumesSelfAttr(Attr.getRange(), S.Context, |
| 4117 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4118 | } |
| 4119 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4120 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 4121 | const AttributeList &Attr) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4122 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4123 | QualType returnType; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4124 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4125 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4126 | returnType = MD->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4127 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4128 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4129 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | a23bd4c | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 4130 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4131 | returnType = PD->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4132 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4133 | returnType = FD->getResultType(); |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4134 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4135 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4136 | << Attr.getRange() << Attr.getName() |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4137 | << ExpectedFunctionOrMethod; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4138 | return; |
| 4139 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4140 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4141 | bool typeOK; |
| 4142 | bool cf; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4143 | switch (Attr.getKind()) { |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4144 | default: llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4145 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4146 | case AttributeList::AT_NSReturnsRetained: |
| 4147 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4148 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 4149 | cf = false; |
| 4150 | break; |
| 4151 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4152 | case AttributeList::AT_CFReturnsRetained: |
| 4153 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4154 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 4155 | cf = true; |
| 4156 | break; |
| 4157 | } |
| 4158 | |
| 4159 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4160 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4161 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4162 | return; |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4163 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4164 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4165 | switch (Attr.getKind()) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4166 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4167 | llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4168 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4169 | D->addAttr(::new (S.Context) |
| 4170 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 4171 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4172 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4173 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4174 | D->addAttr(::new (S.Context) |
| 4175 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4176 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4177 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4178 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4179 | D->addAttr(::new (S.Context) |
| 4180 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4181 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4182 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4183 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4184 | D->addAttr(::new (S.Context) |
| 4185 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4186 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4187 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4188 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4189 | D->addAttr(::new (S.Context) |
| 4190 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4191 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4192 | return; |
| 4193 | }; |
| 4194 | } |
| 4195 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4196 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4197 | const AttributeList &attr) { |
Fariborz Jahanian | af1d28f | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 4198 | const int EP_ObjCMethod = 1; |
| 4199 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | f35a356 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 4200 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4201 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 937ec1d | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4202 | QualType resultType; |
| 4203 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4204 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4205 | |
Fariborz Jahanian | 94d55d7 | 2012-04-21 17:51:44 +0000 | [diff] [blame] | 4206 | if (!method) { |
Fariborz Jahanian | 937ec1d | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4207 | ObjCPropertyDecl *property = dyn_cast<ObjCPropertyDecl>(D); |
| 4208 | if (!property) { |
| 4209 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 4210 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethodOrProperty; |
| 4211 | return; |
| 4212 | } |
| 4213 | resultType = property->getType(); |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4214 | } |
Fariborz Jahanian | 937ec1d | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4215 | else |
| 4216 | // Check that the method returns a normal pointer. |
| 4217 | resultType = method->getResultType(); |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4218 | |
Fariborz Jahanian | f2e5945 | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4219 | if (!resultType->isReferenceType() && |
| 4220 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 937ec1d | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4221 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4222 | << SourceRange(loc) |
Fariborz Jahanian | f35a356 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 4223 | << attr.getName() << (method ? EP_ObjCMethod : EP_ObjCProperty) |
| 4224 | << /*non-retainable pointer*/ 2; |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4225 | |
| 4226 | // Drop the attribute. |
| 4227 | return; |
| 4228 | } |
| 4229 | |
Fariborz Jahanian | 937ec1d | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4230 | D->addAttr(::new (S.Context) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4231 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 4232 | attr.getAttributeSpellingListIndex())); |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4235 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4236 | const AttributeList &attr) { |
| 4237 | SourceLocation loc = attr.getLoc(); |
| 4238 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4239 | |
| 4240 | if (!method) { |
| 4241 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 4242 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
| 4243 | return; |
| 4244 | } |
| 4245 | DeclContext *DC = method->getDeclContext(); |
| 4246 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4247 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4248 | << attr.getName() << 0; |
| 4249 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4250 | return; |
| 4251 | } |
| 4252 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4253 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4254 | << attr.getName() << 1; |
| 4255 | return; |
| 4256 | } |
| 4257 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4258 | method->addAttr(::new (S.Context) |
| 4259 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 4260 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4261 | } |
| 4262 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4263 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 4264 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
| 4265 | if (!isa<FunctionDecl>(D)) { |
| 4266 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4267 | << A.getRange() << A.getName() << ExpectedFunction; |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4268 | return; |
| 4269 | } |
| 4270 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4271 | bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4272 | |
| 4273 | // Check whether there's a conflicting attribute already present. |
| 4274 | Attr *Existing; |
| 4275 | if (IsAudited) { |
| 4276 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 4277 | } else { |
| 4278 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 4279 | } |
| 4280 | if (Existing) { |
| 4281 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 4282 | << A.getName() |
| 4283 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 4284 | << A.getRange() << Existing->getRange(); |
| 4285 | return; |
| 4286 | } |
| 4287 | |
| 4288 | // All clear; add the attribute. |
| 4289 | if (IsAudited) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4290 | D->addAttr(::new (S.Context) |
| 4291 | CFAuditedTransferAttr(A.getRange(), S.Context, |
| 4292 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4293 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4294 | D->addAttr(::new (S.Context) |
| 4295 | CFUnknownTransferAttr(A.getRange(), S.Context, |
| 4296 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4297 | } |
| 4298 | } |
| 4299 | |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4300 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4301 | const AttributeList &Attr) { |
| 4302 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4303 | if (!RD || RD->isUnion()) { |
| 4304 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4305 | << Attr.getRange() << Attr.getName() << ExpectedStruct; |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4308 | IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4309 | |
| 4310 | // In Objective-C, verify that the type names an Objective-C type. |
| 4311 | // We don't want to check this outside of ObjC because people sometimes |
| 4312 | // do crazy C declarations of Objective-C types. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4313 | if (Parm && S.getLangOpts().ObjC1) { |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4314 | // Check for an existing type with this name. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4315 | LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc, |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4316 | Sema::LookupOrdinaryName); |
| 4317 | if (S.LookupName(R, Sc)) { |
| 4318 | NamedDecl *Target = R.getFoundDecl(); |
| 4319 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 4320 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 4321 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 4322 | } |
| 4323 | } |
| 4324 | } |
| 4325 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4326 | D->addAttr(::new (S.Context) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4327 | NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4328 | Attr.getAttributeSpellingListIndex())); |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4329 | } |
| 4330 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4331 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4332 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4333 | if (hasDeclarator(D)) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4334 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4335 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4336 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4337 | } |
| 4338 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4339 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4340 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4341 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4342 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4343 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4344 | return; |
| 4345 | } |
| 4346 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4347 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4348 | QualType type = vd->getType(); |
| 4349 | |
| 4350 | if (!type->isDependentType() && |
| 4351 | !type->isObjCLifetimeType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4352 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4353 | << type; |
| 4354 | return; |
| 4355 | } |
| 4356 | |
| 4357 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4358 | |
| 4359 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4360 | // going to infer. |
| 4361 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4362 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4363 | |
| 4364 | switch (lifetime) { |
| 4365 | case Qualifiers::OCL_None: |
| 4366 | assert(type->isDependentType() && |
| 4367 | "didn't infer lifetime for non-dependent type?"); |
| 4368 | break; |
| 4369 | |
| 4370 | case Qualifiers::OCL_Weak: // meaningful |
| 4371 | case Qualifiers::OCL_Strong: // meaningful |
| 4372 | break; |
| 4373 | |
| 4374 | case Qualifiers::OCL_ExplicitNone: |
| 4375 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4376 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4377 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4378 | break; |
| 4379 | } |
| 4380 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4381 | D->addAttr(::new (S.Context) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4382 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 4383 | Attr.getAttributeSpellingListIndex())); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4384 | } |
| 4385 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4386 | //===----------------------------------------------------------------------===// |
| 4387 | // Microsoft specific attribute handlers. |
| 4388 | //===----------------------------------------------------------------------===// |
| 4389 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4390 | // Check if MS extensions or some other language extensions are enabled. If |
| 4391 | // not, issue a diagnostic that the given attribute is unused. |
| 4392 | static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr, |
| 4393 | bool OtherExtension = false) { |
| 4394 | if (S.LangOpts.MicrosoftExt || OtherExtension) |
| 4395 | return true; |
| 4396 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4397 | return false; |
| 4398 | } |
| 4399 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4400 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4401 | if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland)) |
| 4402 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4403 | |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4404 | StringRef StrRef; |
| 4405 | SourceLocation LiteralLoc; |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 4406 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4407 | return; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4408 | |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4409 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 4410 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4411 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 4412 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4413 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4414 | // Validate GUID length. |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4415 | if (StrRef.size() != 36) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4416 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4417 | return; |
| 4418 | } |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4419 | |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4420 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4421 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4422 | if (StrRef[i] != '-') { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4423 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4424 | return; |
| 4425 | } |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4426 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4427 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4428 | return; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4429 | } |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4430 | } |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4431 | |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4432 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef, |
| 4433 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4436 | static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4437 | if (!checkMicrosoftExt(S, Attr)) |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4438 | return; |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4439 | |
| 4440 | AttributeList::Kind Kind = Attr.getKind(); |
| 4441 | if (Kind == AttributeList::AT_SingleInheritance) |
| 4442 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4443 | ::new (S.Context) |
| 4444 | SingleInheritanceAttr(Attr.getRange(), S.Context, |
| 4445 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4446 | else if (Kind == AttributeList::AT_MultipleInheritance) |
| 4447 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4448 | ::new (S.Context) |
| 4449 | MultipleInheritanceAttr(Attr.getRange(), S.Context, |
| 4450 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4451 | else if (Kind == AttributeList::AT_VirtualInheritance) |
| 4452 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4453 | ::new (S.Context) |
| 4454 | VirtualInheritanceAttr(Attr.getRange(), S.Context, |
| 4455 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4456 | } |
| 4457 | |
| 4458 | static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4459 | if (!checkMicrosoftExt(S, Attr)) |
| 4460 | return; |
| 4461 | |
| 4462 | AttributeList::Kind Kind = Attr.getKind(); |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4463 | if (Kind == AttributeList::AT_Win64) |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4464 | D->addAttr( |
| 4465 | ::new (S.Context) Win64Attr(Attr.getRange(), S.Context, |
| 4466 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4467 | } |
| 4468 | |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4469 | static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4470 | if (!checkMicrosoftExt(S, Attr)) |
| 4471 | return; |
| 4472 | D->addAttr(::new (S.Context) |
| 4473 | ForceInlineAttr(Attr.getRange(), S.Context, |
| 4474 | Attr.getAttributeSpellingListIndex())); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4475 | } |
| 4476 | |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4477 | static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4478 | if (!checkMicrosoftExt(S, Attr)) |
| 4479 | return; |
| 4480 | // Check linkage after possibly merging declaratinos. See |
| 4481 | // checkAttributesAfterMerging(). |
| 4482 | D->addAttr(::new (S.Context) |
| 4483 | SelectAnyAttr(Attr.getRange(), S.Context, |
| 4484 | Attr.getAttributeSpellingListIndex())); |
| 4485 | } |
| 4486 | |
Aaron Ballman | bbb3b32 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4487 | /// Handles semantic checking for features that are common to all attributes, |
| 4488 | /// such as checking whether a parameter was properly specified, or the correct |
| 4489 | /// number of arguments were passed, etc. |
| 4490 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 4491 | const AttributeList &Attr) { |
| 4492 | // Several attributes carry different semantics than the parsing requires, so |
| 4493 | // those are opted out of the common handling. |
| 4494 | // |
| 4495 | // We also bail on unknown and ignored attributes because those are handled |
| 4496 | // as part of the target-specific handling logic. |
| 4497 | if (Attr.hasCustomParsing() || |
| 4498 | Attr.getKind() == AttributeList::UnknownAttribute || |
| 4499 | Attr.getKind() == AttributeList::IgnoredAttribute) |
| 4500 | return false; |
| 4501 | |
| 4502 | // If there are no optional arguments, then checking for the argument count |
| 4503 | // is trivial. |
| 4504 | if (Attr.getMinArgs() == Attr.getMaxArgs() && |
| 4505 | !checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 4506 | return true; |
| 4507 | return false; |
| 4508 | } |
| 4509 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4510 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4511 | // Top Level Sema Entry Points |
| 4512 | //===----------------------------------------------------------------------===// |
| 4513 | |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4514 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4515 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 4516 | /// silently ignore it if a GNU attribute. |
| 4517 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4518 | const AttributeList &Attr, |
| 4519 | bool IncludeCXX11Attributes) { |
| 4520 | if (Attr.isInvalid()) |
| 4521 | return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4522 | |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4523 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 4524 | // instead. |
| 4525 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 4526 | return; |
| 4527 | |
Aaron Ballman | bbb3b32 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4528 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 4529 | return; |
| 4530 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4531 | switch (Attr.getKind()) { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4532 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 4533 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 4534 | case AttributeList::AT_IBOutletCollection: |
| 4535 | handleIBOutletCollection(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4536 | case AttributeList::AT_AddressSpace: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4537 | case AttributeList::AT_ObjCGC: |
| 4538 | case AttributeList::AT_VectorSize: |
| 4539 | case AttributeList::AT_NeonVectorType: |
| 4540 | case AttributeList::AT_NeonPolyVectorType: |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4541 | case AttributeList::AT_Ptr32: |
| 4542 | case AttributeList::AT_Ptr64: |
| 4543 | case AttributeList::AT_SPtr: |
| 4544 | case AttributeList::AT_UPtr: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4545 | // Ignore these, these are type attributes, handled by |
| 4546 | // ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4547 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4548 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 4549 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 4550 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 4551 | case AttributeList::AT_AlwaysInline: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4552 | handleAlwaysInlineAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4553 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4554 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 4555 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4556 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 4557 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 4558 | case AttributeList::AT_CarriesDependency: |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4559 | handleDependencyAttr(S, scope, D, Attr); |
| 4560 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4561 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
| 4562 | case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break; |
| 4563 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4564 | case AttributeList::AT_CXX11NoReturn: |
| 4565 | handleCXX11NoReturnAttr(S, D, Attr); |
| 4566 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4567 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4568 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4569 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4570 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 4571 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4572 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4573 | break; |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4574 | case AttributeList::AT_MinSize: |
| 4575 | handleMinSizeAttr(S, D, Attr); |
| 4576 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4577 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4578 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4579 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
Aaron Ballman | 1951323 | 2013-08-28 23:13:26 +0000 | [diff] [blame] | 4580 | case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break; |
| 4581 | case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4582 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4583 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4584 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4585 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4586 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
| 4587 | case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break; |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4588 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4589 | case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break; |
| 4590 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4591 | case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4592 | case AttributeList::AT_ownership_returns: |
| 4593 | case AttributeList::AT_ownership_takes: |
| 4594 | case AttributeList::AT_ownership_holds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4595 | handleOwnershipAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4596 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4597 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
| 4598 | case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break; |
| 4599 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
| 4600 | case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break; |
| 4601 | case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break; |
| 4602 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4603 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4604 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4605 | handleObjCOwnershipAttr(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4606 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4607 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4608 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4609 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4610 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4611 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4612 | case AttributeList::AT_ObjCRequiresSuper: |
| 4613 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4614 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4615 | case AttributeList::AT_NSBridged: |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4616 | handleNSBridgedAttr(S, scope, D, Attr); break; |
| 4617 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4618 | case AttributeList::AT_CFAuditedTransfer: |
| 4619 | case AttributeList::AT_CFUnknownTransfer: |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4620 | handleCFTransferAttr(S, D, Attr); break; |
| 4621 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4622 | // Checker-specific. |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4623 | case AttributeList::AT_CFConsumed: |
| 4624 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4625 | case AttributeList::AT_NSConsumesSelf: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4626 | handleNSConsumesSelfAttr(S, D, Attr); break; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4627 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4628 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4629 | case AttributeList::AT_NSReturnsNotRetained: |
| 4630 | case AttributeList::AT_CFReturnsNotRetained: |
| 4631 | case AttributeList::AT_NSReturnsRetained: |
| 4632 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4633 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4634 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4635 | case AttributeList::AT_WorkGroupSizeHint: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4636 | case AttributeList::AT_ReqdWorkGroupSize: |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4637 | handleWorkGroupSize(S, D, Attr); break; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 4638 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4639 | case AttributeList::AT_VecTypeHint: |
| 4640 | handleVecTypeHint(S, D, Attr); break; |
| 4641 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4642 | case AttributeList::AT_InitPriority: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4643 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4644 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4645 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4646 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4647 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4648 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4649 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4650 | case AttributeList::AT_ArcWeakrefUnavailable: |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 4651 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 4652 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4653 | case AttributeList::AT_ObjCRootClass: |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4654 | handleObjCRootClassAttr(S, D, Attr); |
| 4655 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4656 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 4657 | handleObjCRequiresPropertyDefsAttr (S, D, Attr); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 4658 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4659 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4660 | case AttributeList::AT_ReturnsTwice: |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 4661 | handleReturnsTwiceAttr(S, D, Attr); |
| 4662 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4663 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4664 | case AttributeList::AT_Visibility: |
| 4665 | handleVisibilityAttr(S, D, Attr, false); |
| 4666 | break; |
| 4667 | case AttributeList::AT_TypeVisibility: |
| 4668 | handleVisibilityAttr(S, D, Attr, true); |
| 4669 | break; |
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 4670 | case AttributeList::AT_WarnUnused: |
| 4671 | handleWarnUnusedAttr(S, D, Attr); |
| 4672 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4673 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4674 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4675 | case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; |
| 4676 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4677 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4678 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4679 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4680 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4681 | case AttributeList::AT_ObjCException: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4682 | handleObjCExceptionAttr(S, D, Attr); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 4683 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4684 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4685 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4686 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4687 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4688 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4689 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
| 4690 | case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break; |
| 4691 | case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break; |
| 4692 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4693 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
| 4694 | case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break; |
| 4695 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4696 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4697 | // Just ignore |
| 4698 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4699 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4700 | handleNoInstrumentFunctionAttr(S, D, Attr); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 4701 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4702 | case AttributeList::AT_StdCall: |
| 4703 | case AttributeList::AT_CDecl: |
| 4704 | case AttributeList::AT_FastCall: |
| 4705 | case AttributeList::AT_ThisCall: |
| 4706 | case AttributeList::AT_Pascal: |
Charles Davis | e8519c3 | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4707 | case AttributeList::AT_MSABI: |
| 4708 | case AttributeList::AT_SysVABI: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4709 | case AttributeList::AT_Pcs: |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4710 | case AttributeList::AT_PnaclCall: |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4711 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4712 | handleCallConvAttr(S, D, Attr); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4713 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4714 | case AttributeList::AT_OpenCLKernel: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4715 | handleOpenCLKernelAttr(S, D, Attr); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4716 | break; |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4717 | case AttributeList::AT_OpenCLImageAccess: |
| 4718 | handleOpenCLImageAccessAttr(S, D, Attr); |
| 4719 | break; |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4720 | |
| 4721 | // Microsoft attributes: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4722 | case AttributeList::AT_MsStruct: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4723 | handleMsStructAttr(S, D, Attr); |
| 4724 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4725 | case AttributeList::AT_Uuid: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4726 | handleUuidAttr(S, D, Attr); |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4727 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4728 | case AttributeList::AT_SingleInheritance: |
| 4729 | case AttributeList::AT_MultipleInheritance: |
| 4730 | case AttributeList::AT_VirtualInheritance: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4731 | handleInheritanceAttr(S, D, Attr); |
| 4732 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4733 | case AttributeList::AT_Win64: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4734 | handlePortabilityAttr(S, D, Attr); |
| 4735 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4736 | case AttributeList::AT_ForceInline: |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4737 | handleForceInlineAttr(S, D, Attr); |
| 4738 | break; |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4739 | case AttributeList::AT_SelectAny: |
| 4740 | handleSelectAnyAttr(S, D, Attr); |
| 4741 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4742 | |
| 4743 | // Thread safety attributes: |
DeLesley Hutchins | 5c6134f | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 4744 | case AttributeList::AT_AssertExclusiveLock: |
| 4745 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 4746 | break; |
| 4747 | case AttributeList::AT_AssertSharedLock: |
| 4748 | handleAssertSharedLockAttr(S, D, Attr); |
| 4749 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4750 | case AttributeList::AT_GuardedVar: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4751 | handleGuardedVarAttr(S, D, Attr); |
| 4752 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4753 | case AttributeList::AT_PtGuardedVar: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4754 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4755 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4756 | case AttributeList::AT_ScopedLockable: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4757 | handleScopedLockableAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4758 | break; |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4759 | case AttributeList::AT_NoSanitizeAddress: |
| 4760 | handleNoSanitizeAddressAttr(S, D, Attr); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4761 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4762 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4763 | handleNoThreadSafetyAnalysis(S, D, Attr); |
| 4764 | break; |
| 4765 | case AttributeList::AT_NoSanitizeThread: |
| 4766 | handleNoSanitizeThread(S, D, Attr); |
| 4767 | break; |
| 4768 | case AttributeList::AT_NoSanitizeMemory: |
| 4769 | handleNoSanitizeMemory(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4770 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4771 | case AttributeList::AT_Lockable: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4772 | handleLockableAttr(S, D, Attr); |
| 4773 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4774 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4775 | handleGuardedByAttr(S, D, Attr); |
| 4776 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4777 | case AttributeList::AT_PtGuardedBy: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4778 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4779 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4780 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4781 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4782 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4783 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4784 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4785 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4786 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4787 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4788 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4789 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4790 | handleLockReturnedAttr(S, D, Attr); |
| 4791 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4792 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4793 | handleLocksExcludedAttr(S, D, Attr); |
| 4794 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4795 | case AttributeList::AT_SharedLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4796 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4797 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4798 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4799 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4800 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4801 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4802 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4803 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4804 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4805 | handleUnlockFunAttr(S, D, Attr); |
| 4806 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4807 | case AttributeList::AT_AcquiredBefore: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4808 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4809 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4810 | case AttributeList::AT_AcquiredAfter: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4811 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4812 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4813 | |
DeLesley Hutchins | 1bf6343 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 4814 | // Consumed analysis attributes. |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 4815 | case AttributeList::AT_Consumable: |
| 4816 | handleConsumableAttr(S, D, Attr); |
| 4817 | break; |
DeLesley Hutchins | 6654085 | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 4818 | case AttributeList::AT_CallableWhen: |
| 4819 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4820 | break; |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 4821 | case AttributeList::AT_ReturnTypestate: |
| 4822 | handleReturnTypestateAttr(S, D, Attr); |
| 4823 | break; |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4824 | case AttributeList::AT_SetTypestate: |
| 4825 | handleSetTypestateAttr(S, D, Attr); |
| 4826 | break; |
| 4827 | case AttributeList::AT_TestsTypestate: |
| 4828 | handleTestsTypestateAttr(S, D, Attr); |
| 4829 | break; |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4830 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4831 | // Type safety attributes. |
| 4832 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4833 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4834 | break; |
| 4835 | case AttributeList::AT_TypeTagForDatatype: |
| 4836 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4837 | break; |
| 4838 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4839 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4840 | // Ask target about the attribute. |
| 4841 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 4842 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4843 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 4844 | diag::warn_unhandled_ms_attribute_ignored : |
| 4845 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4846 | break; |
| 4847 | } |
| 4848 | } |
| 4849 | |
| 4850 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4851 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4852 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4853 | const AttributeList *AttrList, |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4854 | bool IncludeCXX11Attributes) { |
| 4855 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4856 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4857 | |
| 4858 | // GCC accepts |
| 4859 | // static int a9 __attribute__((weakref)); |
| 4860 | // but that looks really pointless. We reject it. |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4861 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4862 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Rafael Espindola | 4d8a33b | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4863 | cast<NamedDecl>(D)->getNameAsString(); |
| 4864 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4865 | return; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4866 | } |
| 4867 | } |
| 4868 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4869 | // Annotation attributes are the only attributes allowed after an access |
| 4870 | // specifier. |
| 4871 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4872 | const AttributeList *AttrList) { |
| 4873 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4874 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4875 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4876 | } else { |
| 4877 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4878 | return true; |
| 4879 | } |
| 4880 | } |
| 4881 | |
| 4882 | return false; |
| 4883 | } |
| 4884 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4885 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 4886 | /// contains any decl attributes that we should warn about. |
| 4887 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 4888 | for ( ; A; A = A->getNext()) { |
| 4889 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 4890 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4891 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 4892 | |
| 4893 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 4894 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 4895 | << A->getName() << A->getRange(); |
| 4896 | } else { |
| 4897 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 4898 | << A->getName() << A->getRange(); |
| 4899 | } |
| 4900 | } |
| 4901 | } |
| 4902 | |
| 4903 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 4904 | /// used to build a declaration, complain about any decl attributes |
| 4905 | /// which might be lying around on it. |
| 4906 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 4907 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 4908 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 4909 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 4910 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 4911 | } |
| 4912 | |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4913 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4914 | /// \#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] | 4915 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 4916 | SourceLocation Loc) { |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4917 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4918 | NamedDecl *NewD = 0; |
| 4919 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4920 | FunctionDecl *NewFD; |
| 4921 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 4922 | // FIXME: Mangling? |
| 4923 | // FIXME: Is the qualifier info correct? |
| 4924 | // FIXME: Is the DeclContext correct? |
| 4925 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 4926 | Loc, Loc, DeclarationName(II), |
| 4927 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4928 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4929 | FD->hasPrototype(), |
| 4930 | false/*isConstexprSpecified*/); |
| 4931 | NewD = NewFD; |
| 4932 | |
| 4933 | if (FD->getQualifier()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4934 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4935 | |
| 4936 | // Fake up parameter variables; they are declared as if this were |
| 4937 | // a typedef. |
| 4938 | QualType FDTy = FD->getType(); |
| 4939 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 4940 | SmallVector<ParmVarDecl*, 16> Params; |
| 4941 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 4942 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 4943 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 4944 | Param->setScopeInfo(0, Params.size()); |
| 4945 | Params.push_back(Param); |
| 4946 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 4947 | NewFD->setParams(Params); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4948 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4949 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 4950 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4951 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4952 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4953 | VD->getStorageClass()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4954 | if (VD->getQualifier()) { |
| 4955 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4956 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4957 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4958 | } |
| 4959 | return NewD; |
| 4960 | } |
| 4961 | |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4962 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4963 | /// applied to it, possibly with an alias. |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4964 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4965 | if (W.getUsed()) return; // only do this once |
| 4966 | W.setUsed(true); |
| 4967 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 4968 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4969 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4970 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 4971 | NDId->getName())); |
| 4972 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4973 | WeakTopLevelDecl.push_back(NewD); |
| 4974 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 4975 | // to insert Decl at TU scope, sorry. |
| 4976 | DeclContext *SavedContext = CurContext; |
| 4977 | CurContext = Context.getTranslationUnitDecl(); |
| 4978 | PushOnScopeChains(NewD, S); |
| 4979 | CurContext = SavedContext; |
| 4980 | } else { // just add weak to existing |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4981 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4982 | } |
| 4983 | } |
| 4984 | |
Rafael Espindola | 65611bf | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 4985 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 4986 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 4987 | // have to do this. |
| 4988 | LoadExternalWeakUndeclaredIdentifiers(); |
| 4989 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 4990 | NamedDecl *ND = NULL; |
| 4991 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 4992 | if (VD->isExternC()) |
| 4993 | ND = VD; |
| 4994 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 4995 | if (FD->isExternC()) |
| 4996 | ND = FD; |
| 4997 | if (ND) { |
| 4998 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 4999 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 5000 | = WeakUndeclaredIdentifiers.find(Id); |
| 5001 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 5002 | WeakInfo W = I->second; |
| 5003 | DeclApplyPragmaWeak(S, ND, W); |
| 5004 | WeakUndeclaredIdentifiers[Id] = W; |
| 5005 | } |
| 5006 | } |
| 5007 | } |
| 5008 | } |
| 5009 | } |
| 5010 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5011 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 5012 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 5013 | /// specified in many different places, and we need to find and apply them all. |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5014 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5015 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5016 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5017 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5018 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5019 | // Walk the declarator structure, applying decl attributes that were in a type |
| 5020 | // position to the decl itself. This handles cases like: |
| 5021 | // int *__attr__(x)** D; |
| 5022 | // when X is a decl attribute. |
| 5023 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 5024 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5025 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5026 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5027 | // Finally, apply any attributes on the decl itself. |
| 5028 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5029 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5030 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5031 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5032 | /// Is the given declaration allowed to use a forbidden type? |
| 5033 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 5034 | // Private ivars are always okay. Unfortunately, people don't |
| 5035 | // always properly make their ivars private, even in system headers. |
| 5036 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | a6b3380 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 5037 | // Function declarations in sys headers will be marked unavailable. |
| 5038 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 5039 | !isa<FunctionDecl>(decl)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5040 | return false; |
| 5041 | |
| 5042 | // Require it to be declared in a system header. |
| 5043 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 5044 | } |
| 5045 | |
| 5046 | /// Handle a delayed forbidden-type diagnostic. |
| 5047 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 5048 | Decl *decl) { |
| 5049 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 5050 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 5051 | "this system declaration uses an unsupported type")); |
| 5052 | return; |
| 5053 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5054 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5055 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 5056 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5057 | // kind of forbidden type messages on unavailable functions. |
| 5058 | if (FD->hasAttr<UnavailableAttr>() && |
| 5059 | diag.getForbiddenTypeDiagnostic() == |
| 5060 | diag::err_arc_array_param_no_ownership) { |
| 5061 | diag.Triggered = true; |
| 5062 | return; |
| 5063 | } |
| 5064 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5065 | |
| 5066 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 5067 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 5068 | diag.Triggered = true; |
| 5069 | } |
| 5070 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5071 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 5072 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5073 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5074 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5075 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5076 | // When delaying diagnostics to run in the context of a parsed |
| 5077 | // declaration, we only want to actually emit anything if parsing |
| 5078 | // succeeds. |
| 5079 | if (!decl) return; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5080 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5081 | // We emit all the active diagnostics in this pool or any of its |
| 5082 | // parents. In general, we'll get one pool for the decl spec |
| 5083 | // and a child pool for each declarator; in a decl group like: |
| 5084 | // deprecated_typedef foo, *bar, baz(); |
| 5085 | // only the declarator pops will be passed decls. This is correct; |
| 5086 | // we really do need to consider delayed diagnostics from the decl spec |
| 5087 | // for each of the different declarations. |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5088 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5089 | do { |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5090 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5091 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 5092 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 5093 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5094 | if (diag.Triggered) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5095 | continue; |
| 5096 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5097 | switch (diag.Kind) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5098 | case DelayedDiagnostic::Deprecation: |
John McCall | e8c904f | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 5099 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 5100 | if (!decl->isInvalidDecl()) |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5101 | HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5102 | break; |
| 5103 | |
| 5104 | case DelayedDiagnostic::Access: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5105 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5106 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5107 | |
| 5108 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5109 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5110 | break; |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5111 | } |
| 5112 | } |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5113 | } while ((pool = pool->getParent())); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5114 | } |
| 5115 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5116 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 5117 | /// been delayed in the current context instead of in the given pool. |
| 5118 | /// Essentially, this just moves them to the current pool. |
| 5119 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 5120 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 5121 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 5122 | curPool->steal(pool); |
| 5123 | } |
| 5124 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5125 | static bool isDeclDeprecated(Decl *D) { |
| 5126 | do { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5127 | if (D->isDeprecated()) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5128 | return true; |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 5129 | // A category implicitly has the availability of the interface. |
| 5130 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 5131 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5132 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 5133 | return false; |
| 5134 | } |
| 5135 | |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5136 | static void |
| 5137 | DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message, |
| 5138 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5139 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5140 | const ObjCPropertyDecl *ObjCPropery) { |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5141 | DeclarationName Name = D->getDeclName(); |
| 5142 | if (!Message.empty()) { |
| 5143 | S.Diag(Loc, diag::warn_deprecated_message) << Name << Message; |
| 5144 | S.Diag(D->getLocation(), |
| 5145 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5146 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5147 | if (ObjCPropery) |
| 5148 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5149 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5150 | } else if (!UnknownObjCClass) { |
| 5151 | S.Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 5152 | S.Diag(D->getLocation(), |
| 5153 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5154 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5155 | if (ObjCPropery) |
| 5156 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5157 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5158 | } else { |
| 5159 | S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name; |
| 5160 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 5161 | } |
| 5162 | } |
| 5163 | |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 5164 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5165 | Decl *Ctx) { |
| 5166 | if (isDeclDeprecated(Ctx)) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5167 | return; |
| 5168 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5169 | DD.Triggered = true; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5170 | DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(), |
| 5171 | DD.getDeprecationMessage(), DD.Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5172 | DD.getUnknownObjCClass(), |
| 5173 | DD.getObjCProperty()); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5174 | } |
| 5175 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5176 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 5177 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5178 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5179 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5180 | // Delay if we're currently parsing a declaration. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5181 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5182 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 5183 | UnknownObjCClass, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5184 | ObjCProperty, |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5185 | Message)); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5186 | return; |
| 5187 | } |
| 5188 | |
| 5189 | // Otherwise, don't warn if our current context is deprecated. |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 5190 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5191 | return; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5192 | DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5193 | } |