Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1 | //===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements decl-related attribute processing. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 15 | #include "TargetAttributesSema.h" |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | acc5f3e | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | acc5f3e | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Rafael Espindola | d7a60ad | 2013-02-26 19:13:56 +0000 | [diff] [blame] | 22 | #include "clang/AST/Mangle.h" |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 23 | #include "clang/Basic/CharInfo.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 26 | #include "clang/Sema/DeclSpec.h" |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 27 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 28 | #include "clang/Sema/Lookup.h" |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 29 | #include "clang/Sema/Scope.h" |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 31 | using namespace clang; |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 32 | using namespace sema; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 33 | |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 34 | /// These constants match the enumerated choices of |
| 35 | /// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 36 | enum AttributeDeclKind { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 37 | ExpectedFunction, |
| 38 | ExpectedUnion, |
| 39 | ExpectedVariableOrFunction, |
| 40 | ExpectedFunctionOrMethod, |
| 41 | ExpectedParameter, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 42 | ExpectedFunctionMethodOrBlock, |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 43 | ExpectedFunctionMethodOrClass, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 44 | ExpectedFunctionMethodOrParameter, |
| 45 | ExpectedClass, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 46 | ExpectedVariable, |
| 47 | ExpectedMethod, |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 48 | ExpectedVariableFunctionOrLabel, |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 49 | ExpectedFieldOrGlobalVar, |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 50 | ExpectedStruct, |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 51 | ExpectedVariableFunctionOrTag, |
Richard Smith | 5f838aa | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 52 | ExpectedTLSVar, |
| 53 | ExpectedVariableOrField, |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 54 | ExpectedVariableFieldOrTag, |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 55 | ExpectedTypeOrNamespace, |
| 56 | ExpectedObjectiveCInterface |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 59 | //===----------------------------------------------------------------------===// |
| 60 | // Helper functions |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 63 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 64 | bool blocksToo = true) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 65 | QualType Ty; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 66 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 67 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 68 | else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 69 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 70 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 71 | Ty = decl->getUnderlyingType(); |
| 72 | else |
| 73 | return 0; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 75 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 76 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 755f9d2 | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 77 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 78 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 79 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 80 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 83 | // FIXME: We should provide an abstraction around a method or function |
| 84 | // to provide the following bits of information. |
| 85 | |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 86 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 87 | /// type (function or function-typed variable). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 88 | static bool isFunction(const Decl *D) { |
| 89 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 93 | /// type (function or function-typed variable) or an Objective-C |
| 94 | /// method. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 95 | static bool isFunctionOrMethod(const Decl *D) { |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 96 | return isFunction(D) || isa<ObjCMethodDecl>(D); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 99 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 100 | /// type (function or function-typed variable) or an Objective-C |
| 101 | /// method or a block. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 102 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 103 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 104 | return true; |
| 105 | // check for block is more involved. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 106 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 107 | QualType Ty = V->getType(); |
| 108 | return Ty->isBlockPointerType(); |
| 109 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 110 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 111 | } |
| 112 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 113 | /// Return true if the given decl has a declarator that should have |
| 114 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 115 | static bool hasDeclarator(const Decl *D) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 116 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 117 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 118 | isa<ObjCPropertyDecl>(D); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 121 | /// hasFunctionProto - Return true if the given decl has a argument |
| 122 | /// information. This decl should have already passed |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 123 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 124 | static bool hasFunctionProto(const Decl *D) { |
| 125 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 126 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 127 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 128 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 129 | return true; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 134 | /// arguments. It is an error to call this on a K&R function (use |
| 135 | /// hasFunctionProto first). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 136 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 137 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 138 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 139 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 140 | return BD->getNumParams(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 141 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 144 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 145 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 146 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 147 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 148 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 149 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 150 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 153 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 154 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 155 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 156 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 159 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 160 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 161 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 162 | return proto->isVariadic(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 163 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | db9a0ae | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 164 | return BD->isVariadic(); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 165 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 166 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 170 | static bool isInstanceMethod(const Decl *D) { |
| 171 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 172 | return MethodDecl->isInstance(); |
| 173 | return false; |
| 174 | } |
| 175 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 176 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 177 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 178 | if (!PT) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 179 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 180 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 181 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 182 | if (!Cls) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 183 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 184 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 185 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 187 | // FIXME: Should we walk the chain of classes? |
| 188 | return ClsName == &Ctx.Idents.get("NSString") || |
| 189 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 190 | } |
| 191 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 192 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 193 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 194 | if (!PT) |
| 195 | return false; |
| 196 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 197 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 198 | if (!RT) |
| 199 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 200 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 201 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 202 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 203 | return false; |
| 204 | |
| 205 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 206 | } |
| 207 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 208 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 209 | /// output an error. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 210 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 211 | unsigned int Num) { |
| 212 | if (Attr.getNumArgs() != Num) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 213 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 214 | << Attr.getName() << Num; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 215 | return false; |
| 216 | } |
| 217 | |
| 218 | return true; |
| 219 | } |
| 220 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 221 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 222 | /// \brief Check if the attribute has at least as many args as Num. May |
| 223 | /// output an error. |
| 224 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
| 225 | unsigned int Num) { |
| 226 | if (Attr.getNumArgs() < Num) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 227 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| 228 | return false; |
| 229 | } |
| 230 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 231 | return true; |
| 232 | } |
| 233 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 234 | /// \brief Check if IdxExpr is a valid argument index for a function or |
| 235 | /// instance method D. May output an error. |
| 236 | /// |
| 237 | /// \returns true if IdxExpr is a valid index. |
| 238 | static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D, |
| 239 | StringRef AttrName, |
| 240 | SourceLocation AttrLoc, |
| 241 | unsigned AttrArgNum, |
| 242 | const Expr *IdxExpr, |
| 243 | uint64_t &Idx) |
| 244 | { |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 245 | assert(isFunctionOrMethod(D)); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 246 | |
| 247 | // In C++ the implicit 'this' function parameter also counts. |
| 248 | // Parameters are counted from one. |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 249 | bool HP = hasFunctionProto(D); |
| 250 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 251 | bool IV = HP && isFunctionOrMethodVariadic(D); |
| 252 | unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) + |
| 253 | HasImplicitThisParam; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 254 | |
| 255 | llvm::APSInt IdxInt; |
| 256 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 257 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 258 | std::string Name = std::string("'") + AttrName.str() + std::string("'"); |
| 259 | S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str() |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 260 | << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | |
| 264 | Idx = IdxInt.getLimitedValue(); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 265 | if (Idx < 1 || (!IV && Idx > NumArgs)) { |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 266 | S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds) |
| 267 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 268 | return false; |
| 269 | } |
| 270 | Idx--; // Convert to zero-based. |
| 271 | if (HasImplicitThisParam) { |
| 272 | if (Idx == 0) { |
| 273 | S.Diag(AttrLoc, |
| 274 | diag::err_attribute_invalid_implicit_this_argument) |
| 275 | << AttrName << IdxExpr->getSourceRange(); |
| 276 | return false; |
| 277 | } |
| 278 | --Idx; |
| 279 | } |
| 280 | |
| 281 | return true; |
| 282 | } |
| 283 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 284 | /// |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 285 | /// \brief Check if passed in Decl is a field or potentially shared global var |
| 286 | /// \return true if the Decl is a field or potentially shared global variable |
| 287 | /// |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 288 | static bool mayBeSharedVariable(const Decl *D) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 289 | if (isa<FieldDecl>(D)) |
| 290 | return true; |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 291 | if (const VarDecl *vd = dyn_cast<VarDecl>(D)) |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 292 | return vd->hasGlobalStorage() && !vd->getTLSKind(); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 293 | |
| 294 | return false; |
| 295 | } |
| 296 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 297 | /// \brief Check if the passed-in expression is of type int or bool. |
| 298 | static bool isIntOrBool(Expr *Exp) { |
| 299 | QualType QT = Exp->getType(); |
| 300 | return QT->isBooleanType() || QT->isIntegerType(); |
| 301 | } |
| 302 | |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 303 | |
| 304 | // Check to see if the type is a smart pointer of some kind. We assume |
| 305 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 306 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 307 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 308 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 309 | if (Res1.empty()) |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 310 | return false; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 311 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 312 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 313 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 314 | if (Res2.empty()) |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 315 | return false; |
| 316 | |
| 317 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 320 | /// \brief Check if passed in Decl is a pointer type. |
| 321 | /// Note that this function may produce an error message. |
| 322 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 323 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 324 | const AttributeList &Attr) { |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 325 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 326 | QualType QT = vd->getType(); |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 327 | if (QT->isAnyPointerType()) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 328 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 329 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 330 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 331 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 332 | // (We don't want to force template instantiation if we can avoid it, |
| 333 | // since that would alter the order in which templates are instantiated.) |
| 334 | if (RT->isIncompleteType()) |
| 335 | return true; |
| 336 | |
| 337 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 338 | return true; |
| 339 | } |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 340 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 341 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 342 | << Attr.getName()->getName() << QT; |
| 343 | } else { |
| 344 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 345 | << Attr.getName(); |
| 346 | } |
| 347 | return false; |
| 348 | } |
| 349 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 350 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 351 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 352 | static const RecordType *getRecordType(QualType QT) { |
| 353 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 354 | return RT; |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 355 | |
| 356 | // Now check if we point to record type. |
| 357 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 358 | return PT->getPointeeType()->getAs<RecordType>(); |
| 359 | |
| 360 | return 0; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 361 | } |
| 362 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 363 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 364 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 365 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 366 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
| 367 | if (RT->getDecl()->getAttr<LockableAttr>()) |
| 368 | return true; |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 373 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 374 | /// resolves to a lockable object. |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 375 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 376 | QualType Ty) { |
| 377 | const RecordType *RT = getRecordType(Ty); |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 378 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 379 | // Warn if could not get record type for this argument. |
Benjamin Kramer | d77ba89 | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 380 | if (!RT) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 381 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 382 | << Attr.getName() << Ty.getAsString(); |
| 383 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 384 | } |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 385 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 386 | // Don't check for lockable if the class hasn't been defined yet. |
DeLesley Hutchins | 634b293 | 2012-02-16 17:15:51 +0000 | [diff] [blame] | 387 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 388 | return; |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 389 | |
| 390 | // Allow smart pointers to be used as lockable objects. |
| 391 | // FIXME -- Check the type that the smart pointer points to. |
| 392 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 393 | return; |
| 394 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 395 | // Check if the type is lockable. |
| 396 | RecordDecl *RD = RT->getDecl(); |
| 397 | if (RD->getAttr<LockableAttr>()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 398 | return; |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 399 | |
| 400 | // Else check if any base classes are lockable. |
| 401 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 402 | CXXBasePaths BPaths(false, false); |
| 403 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 404 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 405 | } |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 406 | |
| 407 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 408 | << Attr.getName() << Ty.getAsString(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 411 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 412 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 413 | /// \param Sidx The attribute argument index to start checking with. |
| 414 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 415 | /// parameter list. |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 416 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 417 | const AttributeList &Attr, |
| 418 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 419 | int Sidx = 0, |
| 420 | bool ParamIdxOk = false) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 421 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 422 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 423 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 424 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 425 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 426 | Args.push_back(ArgExp); |
| 427 | continue; |
| 428 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 429 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 430 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 431 | if (StrLit->getLength() == 0 || |
| 432 | StrLit->getString() == StringRef("*")) { |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 433 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 434 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 435 | Args.push_back(ArgExp); |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 436 | continue; |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 437 | } |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 438 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 439 | // We allow constant strings to be used as a placeholder for expressions |
| 440 | // that are not valid C++ syntax, but warn that they are ignored. |
| 441 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 442 | Attr.getName(); |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 443 | Args.push_back(ArgExp); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 444 | continue; |
| 445 | } |
| 446 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 447 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 448 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 449 | // A pointer to member expression of the form &MyClass::mu is treated |
| 450 | // specially -- we need to look at the type of the member. |
| 451 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 452 | if (UOp->getOpcode() == UO_AddrOf) |
| 453 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 454 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 455 | ArgTy = DRE->getDecl()->getType(); |
| 456 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 457 | // First see if we can just cast to record type, or point to record type. |
| 458 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 459 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 460 | // Now check if we index into a record type function param. |
| 461 | if(!RT && ParamIdxOk) { |
| 462 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 463 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 464 | if(FD && IL) { |
| 465 | unsigned int NumParams = FD->getNumParams(); |
| 466 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 467 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 468 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 469 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 470 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 471 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 472 | continue; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 473 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 474 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 478 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 479 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 480 | Args.push_back(ArgExp); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 481 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 484 | //===----------------------------------------------------------------------===// |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 485 | // Attribute Implementations |
| 486 | //===----------------------------------------------------------------------===// |
| 487 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 488 | // FIXME: All this manual attribute parsing code is gross. At the |
| 489 | // least add some helper functions to check most argument patterns (# |
| 490 | // and types of args). |
| 491 | |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 492 | enum ThreadAttributeDeclKind { |
| 493 | ThreadExpectedFieldOrGlobalVar, |
| 494 | ThreadExpectedFunctionOrMethod, |
| 495 | ThreadExpectedClassOrStruct |
| 496 | }; |
| 497 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 498 | static bool checkGuardedVarAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 499 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 500 | // D must be either a member field or global (potentially shared) variable. |
| 501 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 502 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 503 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 504 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 507 | return true; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 510 | static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 511 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 512 | return; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 513 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 514 | D->addAttr(::new (S.Context) |
| 515 | GuardedVarAttr(Attr.getRange(), S.Context, |
| 516 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 519 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 520 | const AttributeList &Attr) { |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 521 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 522 | return; |
| 523 | |
| 524 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 525 | return; |
| 526 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 527 | D->addAttr(::new (S.Context) |
| 528 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 529 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 532 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 533 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 534 | Expr* &Arg) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 535 | // D must be either a member field or global (potentially shared) variable. |
| 536 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 537 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 538 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 539 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 540 | } |
| 541 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 542 | SmallVector<Expr*, 1> Args; |
| 543 | // check that all arguments are lockable objects |
| 544 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 545 | unsigned Size = Args.size(); |
| 546 | if (Size != 1) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 547 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 548 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 549 | Arg = Args[0]; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 550 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 551 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 554 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 555 | Expr *Arg = 0; |
| 556 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 557 | return; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 558 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 559 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| 560 | } |
| 561 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 562 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 563 | const AttributeList &Attr) { |
| 564 | Expr *Arg = 0; |
| 565 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 566 | return; |
| 567 | |
| 568 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 569 | return; |
| 570 | |
| 571 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| 572 | S.Context, Arg)); |
| 573 | } |
| 574 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 575 | static bool checkLockableAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 576 | const AttributeList &Attr) { |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 577 | // FIXME: Lockable structs for C code. |
David Blaikie | 88c4b5e | 2013-07-29 18:24:03 +0000 | [diff] [blame] | 578 | if (!isa<RecordDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 579 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 580 | << Attr.getName() << ThreadExpectedClassOrStruct; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 581 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 584 | return true; |
| 585 | } |
| 586 | |
| 587 | static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 588 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 589 | return; |
| 590 | |
| 591 | D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); |
| 592 | } |
| 593 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 594 | static void handleScopedLockableAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 595 | const AttributeList &Attr) { |
| 596 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 597 | return; |
| 598 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 599 | D->addAttr(::new (S.Context) |
| 600 | ScopedLockableAttr(Attr.getRange(), S.Context, |
| 601 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 604 | static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D, |
| 605 | const AttributeList &Attr) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 606 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 607 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 608 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 609 | return; |
| 610 | } |
| 611 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 612 | D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(), |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 613 | S.Context)); |
| 614 | } |
| 615 | |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 616 | static void handleNoSanitizeAddressAttr(Sema &S, Decl *D, |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 617 | const AttributeList &Attr) { |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 618 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 619 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 620 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 621 | return; |
| 622 | } |
| 623 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 624 | D->addAttr(::new (S.Context) |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 625 | NoSanitizeAddressAttr(Attr.getRange(), S.Context, |
| 626 | Attr.getAttributeSpellingListIndex())); |
| 627 | } |
| 628 | |
| 629 | static void handleNoSanitizeMemory(Sema &S, Decl *D, |
| 630 | const AttributeList &Attr) { |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 631 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 632 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 633 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 634 | return; |
| 635 | } |
| 636 | |
| 637 | D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(), |
| 638 | S.Context)); |
| 639 | } |
| 640 | |
| 641 | static void handleNoSanitizeThread(Sema &S, Decl *D, |
| 642 | const AttributeList &Attr) { |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 643 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 644 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 645 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 646 | return; |
| 647 | } |
| 648 | |
| 649 | D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(), |
| 650 | S.Context)); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 653 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 654 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 655 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 656 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 657 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 658 | |
| 659 | // D must be either a member field or global (potentially shared) variable. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 660 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 661 | if (!VD || !mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 662 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 663 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 664 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 665 | } |
| 666 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 667 | // Check that this attribute only applies to lockable types. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 668 | QualType QT = VD->getType(); |
| 669 | if (!QT->isDependentType()) { |
| 670 | const RecordType *RT = getRecordType(QT); |
| 671 | if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 672 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 673 | << Attr.getName(); |
| 674 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 678 | // Check that all arguments are lockable objects. |
| 679 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 680 | if (Args.empty()) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 681 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 682 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 683 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 686 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 687 | const AttributeList &Attr) { |
| 688 | SmallVector<Expr*, 1> Args; |
| 689 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 690 | return; |
| 691 | |
| 692 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 693 | D->addAttr(::new (S.Context) |
| 694 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 695 | StartArg, Args.size(), |
| 696 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 699 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 700 | const AttributeList &Attr) { |
| 701 | SmallVector<Expr*, 1> Args; |
| 702 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 703 | return; |
| 704 | |
| 705 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 706 | D->addAttr(::new (S.Context) |
| 707 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 708 | StartArg, Args.size(), |
| 709 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 712 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 713 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 714 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 715 | // zero or more arguments ok |
| 716 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 717 | // check that the attribute is applied to a function |
| 718 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 719 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 720 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 721 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 724 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 725 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 726 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 727 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 730 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 731 | const AttributeList &Attr) { |
| 732 | SmallVector<Expr*, 1> Args; |
| 733 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 734 | return; |
| 735 | |
| 736 | unsigned Size = Args.size(); |
| 737 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 738 | D->addAttr(::new (S.Context) |
| 739 | SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 740 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 743 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 744 | const AttributeList &Attr) { |
| 745 | SmallVector<Expr*, 1> Args; |
| 746 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 747 | return; |
| 748 | |
| 749 | unsigned Size = Args.size(); |
| 750 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 751 | D->addAttr(::new (S.Context) |
| 752 | ExclusiveLockFunctionAttr(Attr.getRange(), S.Context, |
| 753 | StartArg, Size, |
| 754 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 755 | } |
| 756 | |
DeLesley Hutchins | 5c6134f | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 757 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 758 | const AttributeList &Attr) { |
| 759 | SmallVector<Expr*, 1> Args; |
| 760 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 761 | return; |
| 762 | |
| 763 | unsigned Size = Args.size(); |
| 764 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 765 | D->addAttr(::new (S.Context) |
| 766 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 767 | Attr.getAttributeSpellingListIndex())); |
| 768 | } |
| 769 | |
| 770 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 771 | const AttributeList &Attr) { |
| 772 | SmallVector<Expr*, 1> Args; |
| 773 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 774 | return; |
| 775 | |
| 776 | unsigned Size = Args.size(); |
| 777 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 778 | D->addAttr(::new (S.Context) |
| 779 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 780 | StartArg, Size, |
| 781 | Attr.getAttributeSpellingListIndex())); |
| 782 | } |
| 783 | |
| 784 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 785 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 786 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 787 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 788 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 789 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 790 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 791 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 792 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 793 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 794 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 797 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 798 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 799 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 800 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 804 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 805 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 806 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 809 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 810 | const AttributeList &Attr) { |
| 811 | SmallVector<Expr*, 2> Args; |
| 812 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 813 | return; |
| 814 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 815 | D->addAttr(::new (S.Context) |
| 816 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 817 | Attr.getArgAsExpr(0), |
| 818 | Args.data(), Args.size(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 819 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 822 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 823 | const AttributeList &Attr) { |
| 824 | SmallVector<Expr*, 2> Args; |
| 825 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 826 | return; |
| 827 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 828 | D->addAttr(::new (S.Context) |
| 829 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 830 | Attr.getArgAsExpr(0), |
| 831 | Args.data(), Args.size(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 832 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 835 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 836 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 837 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 838 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 839 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 840 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 841 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 842 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 843 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 844 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 847 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 848 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 849 | if (Args.empty()) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 850 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 851 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 852 | return true; |
| 853 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 854 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 855 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 856 | const AttributeList &Attr) { |
| 857 | SmallVector<Expr*, 1> Args; |
| 858 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 859 | return; |
| 860 | |
| 861 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 862 | D->addAttr(::new (S.Context) |
| 863 | ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context, |
| 864 | StartArg, Args.size(), |
| 865 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 868 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 869 | const AttributeList &Attr) { |
| 870 | SmallVector<Expr*, 1> Args; |
| 871 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 872 | return; |
| 873 | |
| 874 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 875 | D->addAttr(::new (S.Context) |
| 876 | SharedLocksRequiredAttr(Attr.getRange(), S.Context, |
| 877 | StartArg, Args.size(), |
| 878 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 881 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 882 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 883 | // zero or more arguments ok |
| 884 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 885 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 886 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 887 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 888 | return; |
| 889 | } |
| 890 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 891 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 892 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 893 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 894 | unsigned Size = Args.size(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 895 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 896 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 897 | D->addAttr(::new (S.Context) |
| 898 | UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 899 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 903 | const AttributeList &Attr) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 904 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 905 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 906 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 907 | return; |
| 908 | } |
| 909 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 910 | // check that the argument is lockable object |
DeLesley Hutchins | f26efd7 | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 911 | SmallVector<Expr*, 1> Args; |
| 912 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 913 | unsigned Size = Args.size(); |
| 914 | if (Size == 0) |
| 915 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 916 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 917 | D->addAttr(::new (S.Context) |
| 918 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 919 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 923 | const AttributeList &Attr) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 924 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 925 | return; |
| 926 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 927 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 928 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 929 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 930 | return; |
| 931 | } |
| 932 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 933 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 934 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 935 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 936 | unsigned Size = Args.size(); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 937 | if (Size == 0) |
| 938 | return; |
| 939 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 940 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 941 | D->addAttr(::new (S.Context) |
| 942 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 943 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 944 | } |
| 945 | |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 946 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | a33ab60 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 947 | ConsumableAttr::ConsumedState DefaultState; |
| 948 | |
| 949 | if (Attr.isArgIdent(0)) { |
| 950 | StringRef Param = Attr.getArgAsIdent(0)->Ident->getName(); |
| 951 | |
| 952 | if (Param == "unknown") |
| 953 | DefaultState = ConsumableAttr::Unknown; |
| 954 | else if (Param == "consumed") |
| 955 | DefaultState = ConsumableAttr::Consumed; |
| 956 | else if (Param == "unconsumed") |
| 957 | DefaultState = ConsumableAttr::Unconsumed; |
| 958 | else { |
| 959 | S.Diag(Attr.getLoc(), diag::warn_unknown_consumed_state) << Param; |
| 960 | return; |
| 961 | } |
| 962 | |
| 963 | } else { |
| 964 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 965 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 966 | return; |
| 967 | } |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 968 | |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 969 | if (!isa<CXXRecordDecl>(D)) { |
| 970 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 971 | Attr.getName() << ExpectedClass; |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 972 | return; |
| 973 | } |
| 974 | |
| 975 | D->addAttr(::new (S.Context) |
David Blaikie | a33ab60 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 976 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 977 | Attr.getAttributeSpellingListIndex())); |
| 978 | } |
| 979 | |
| 980 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 981 | const AttributeList &Attr) { |
| 982 | ASTContext &CurrContext = S.getASTContext(); |
| 983 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 984 | |
| 985 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 986 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 987 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 988 | RD->getNameAsString(); |
| 989 | |
| 990 | return false; |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | return true; |
| 995 | } |
| 996 | |
| 997 | static void handleConsumesAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 998 | if (!isa<CXXMethodDecl>(D)) { |
| 999 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1000 | Attr.getName() << ExpectedMethod; |
| 1001 | return; |
| 1002 | } |
| 1003 | |
| 1004 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1005 | return; |
| 1006 | |
| 1007 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1008 | ConsumesAttr(Attr.getRange(), S.Context, |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1009 | Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | static void handleCallableWhenUnconsumedAttr(Sema &S, Decl *D, |
| 1013 | const AttributeList &Attr) { |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1014 | if (!isa<CXXMethodDecl>(D)) { |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1015 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1016 | Attr.getName() << ExpectedMethod; |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1017 | return; |
| 1018 | } |
| 1019 | |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1020 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1021 | return; |
| 1022 | |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1023 | D->addAttr(::new (S.Context) |
| 1024 | CallableWhenUnconsumedAttr(Attr.getRange(), S.Context, |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1025 | Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | static void handleTestsConsumedAttr(Sema &S, Decl *D, |
| 1029 | const AttributeList &Attr) { |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1030 | if (!isa<CXXMethodDecl>(D)) { |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1031 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1032 | Attr.getName() << ExpectedMethod; |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1033 | return; |
| 1034 | } |
| 1035 | |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1036 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1037 | return; |
| 1038 | |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1039 | D->addAttr(::new (S.Context) |
| 1040 | TestsConsumedAttr(Attr.getRange(), S.Context, |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1041 | Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | static void handleTestsUnconsumedAttr(Sema &S, Decl *D, |
| 1045 | const AttributeList &Attr) { |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1046 | if (!isa<CXXMethodDecl>(D)) { |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1047 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1048 | Attr.getName() << ExpectedMethod; |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1049 | return; |
| 1050 | } |
| 1051 | |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1052 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1053 | return; |
| 1054 | |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1055 | D->addAttr(::new (S.Context) |
| 1056 | TestsUnconsumedAttr(Attr.getRange(), S.Context, |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1057 | Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1058 | } |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 1059 | |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1060 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 1061 | const AttributeList &Attr) { |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1062 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 1063 | |
| 1064 | if (Attr.isArgIdent(0)) { |
| 1065 | StringRef Param = Attr.getArgAsIdent(0)->Ident->getName(); |
| 1066 | |
| 1067 | if (Param == "unknown") { |
| 1068 | ReturnState = ReturnTypestateAttr::Unknown; |
| 1069 | } else if (Param == "consumed") { |
| 1070 | ReturnState = ReturnTypestateAttr::Consumed; |
| 1071 | } else if (Param == "unconsumed") { |
| 1072 | ReturnState = ReturnTypestateAttr::Unconsumed; |
| 1073 | } else { |
| 1074 | S.Diag(Attr.getLoc(), diag::warn_unknown_consumed_state) << Param; |
| 1075 | return; |
| 1076 | } |
| 1077 | |
| 1078 | } else { |
| 1079 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1080 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1081 | return; |
| 1082 | } |
| 1083 | |
| 1084 | if (!isa<FunctionDecl>(D)) { |
| 1085 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1086 | Attr.getName() << ExpectedFunction; |
| 1087 | return; |
| 1088 | } |
| 1089 | |
| 1090 | // FIXME: This check is currently being done in the analysis. It can be |
| 1091 | // enabled here only after the parser propagates attributes at |
| 1092 | // template specialization definition, not declaration. |
| 1093 | //QualType ReturnType; |
| 1094 | // |
| 1095 | //if (const CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
| 1096 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 1097 | // |
| 1098 | //} else { |
| 1099 | // |
| 1100 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 1101 | //} |
| 1102 | // |
| 1103 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1104 | // |
| 1105 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1106 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1107 | // ReturnType.getAsString(); |
| 1108 | // return; |
| 1109 | //} |
| 1110 | |
| 1111 | D->addAttr(::new (S.Context) |
| 1112 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 1113 | Attr.getAttributeSpellingListIndex())); |
| 1114 | } |
| 1115 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1116 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1117 | const AttributeList &Attr) { |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1118 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 1119 | if (TD == 0) { |
| 1120 | // __attribute__((ext_vector_type(N))) can only be applied to typedefs |
| 1121 | // and type-ids. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1122 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1123 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1124 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1125 | |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1126 | // Remember this typedef decl, we will need it later for diagnostics. |
| 1127 | S.ExtVectorDecls.push_back(TD); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1130 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1131 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1132 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1133 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1134 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1135 | // has no effect. |
Eli Friedman | b68ec6b | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1136 | if (!FD->getType()->isDependentType() && |
| 1137 | !FD->getType()->isIncompleteType() && |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1138 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1139 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1140 | << Attr.getName() << FD->getType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1141 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1142 | FD->addAttr(::new (S.Context) |
| 1143 | PackedAttr(Attr.getRange(), S.Context, |
| 1144 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1145 | } else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1146 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1149 | static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 5f608ae | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 1150 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1151 | RD->addAttr(::new (S.Context) |
| 1152 | MsStructAttr(Attr.getRange(), S.Context, |
| 1153 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | c1a0a73 | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 1154 | else |
| 1155 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1156 | } |
| 1157 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1158 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1159 | // The IBAction attributes only apply to instance methods. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1160 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1161 | if (MD->isInstanceMethod()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1162 | D->addAttr(::new (S.Context) |
| 1163 | IBActionAttr(Attr.getRange(), S.Context, |
| 1164 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1165 | return; |
| 1166 | } |
| 1167 | |
Ted Kremenek | 4ee2bb1 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 1168 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1171 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1172 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1173 | // variables or properties of Objective-C classes. The outlet must also |
| 1174 | // have an object reference type. |
| 1175 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1176 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 0bfaf06 | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1177 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1178 | << Attr.getName() << VD->getType() << 0; |
| 1179 | return false; |
| 1180 | } |
| 1181 | } |
| 1182 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1183 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1184 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1185 | << Attr.getName() << PD->getType() << 1; |
| 1186 | return false; |
| 1187 | } |
| 1188 | } |
| 1189 | else { |
| 1190 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1191 | return false; |
| 1192 | } |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1193 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1194 | return true; |
| 1195 | } |
| 1196 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1197 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1198 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1199 | return; |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1200 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1201 | D->addAttr(::new (S.Context) |
| 1202 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1203 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1206 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1207 | const AttributeList &Attr) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1208 | |
| 1209 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1210 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1211 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1212 | << Attr.getName() << 1; |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1213 | return; |
| 1214 | } |
| 1215 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1216 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1217 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1218 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1219 | IdentifierLoc *IL = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
| 1220 | IdentifierInfo *II; |
| 1221 | SourceLocation ILS; |
| 1222 | if (IL) { |
| 1223 | II = IL->Ident; |
| 1224 | ILS = IL->Loc; |
| 1225 | } else { |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1226 | II = &S.Context.Idents.get("NSObject"); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1227 | } |
Fariborz Jahanian | 3a3400b | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 1228 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1229 | ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1230 | S.getScopeForContext(D->getDeclContext()->getParent())); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1231 | if (!TypeRep) { |
| 1232 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1233 | return; |
| 1234 | } |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1235 | QualType QT = TypeRep.get(); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1236 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1237 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1238 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1239 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1240 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1241 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1242 | return; |
| 1243 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1244 | D->addAttr(::new (S.Context) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1245 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QT, ILS, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1246 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1249 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1250 | if (const RecordType *UT = T->getAsUnionType()) |
| 1251 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1252 | RecordDecl *UD = UT->getDecl(); |
| 1253 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1254 | itend = UD->field_end(); it != itend; ++it) { |
| 1255 | QualType QT = it->getType(); |
| 1256 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1257 | T = QT; |
| 1258 | return; |
| 1259 | } |
| 1260 | } |
| 1261 | } |
| 1262 | } |
| 1263 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1264 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nuno Lopes | 174930d | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1265 | if (!isFunctionOrMethod(D)) { |
| 1266 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1267 | << Attr.getName() << ExpectedFunctionOrMethod; |
Nuno Lopes | 174930d | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1268 | return; |
| 1269 | } |
| 1270 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1271 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1272 | return; |
| 1273 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1274 | SmallVector<unsigned, 8> SizeArgs; |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1275 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1276 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1277 | uint64_t Idx; |
| 1278 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1279 | Attr.getLoc(), i + 1, Ex, Idx)) |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1280 | return; |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1281 | |
| 1282 | // check if the function argument is of an integer type |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1283 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1284 | if (!T->isIntegerType()) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 1285 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1286 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 1287 | << Ex->getSourceRange(); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1288 | return; |
| 1289 | } |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1290 | SizeArgs.push_back(Idx); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | // check if the function returns a pointer |
| 1294 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 1295 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1296 | << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1299 | D->addAttr(::new (S.Context) |
| 1300 | AllocSizeAttr(Attr.getRange(), S.Context, |
| 1301 | SizeArgs.data(), SizeArgs.size(), |
| 1302 | Attr.getAttributeSpellingListIndex())); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1305 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1306 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1307 | // ignore it as well |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1308 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1309 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1310 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1311 | return; |
| 1312 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1313 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1314 | SmallVector<unsigned, 8> NonNullArgs; |
| 1315 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1316 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1317 | uint64_t Idx; |
| 1318 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1319 | Attr.getLoc(), i + 1, Ex, Idx)) |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1320 | return; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1321 | |
| 1322 | // Is the function argument a pointer type? |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1323 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1324 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1325 | |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1326 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1327 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | c9ef405 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1328 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1329 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1330 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1331 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1332 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1333 | NonNullArgs.push_back(Idx); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1334 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1335 | |
| 1336 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1337 | // arguments have a nonnull attribute. |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1338 | if (NonNullArgs.empty()) { |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1339 | for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) { |
| 1340 | QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1341 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1342 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1343 | NonNullArgs.push_back(i); |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1344 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1345 | |
Ted Kremenek | ee1c08c | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1346 | // No pointer arguments? |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1347 | if (NonNullArgs.empty()) { |
| 1348 | // Warn the trivial case only if attribute is not coming from a |
| 1349 | // macro instantiation. |
| 1350 | if (Attr.getLoc().isFileID()) |
| 1351 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1352 | return; |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1353 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1354 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1355 | |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1356 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1357 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1358 | llvm::array_pod_sort(start, start + size); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1359 | D->addAttr(::new (S.Context) |
| 1360 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1361 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1364 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1365 | // This attribute must be applied to a function declaration. |
| 1366 | // The first argument to the attribute must be a string, |
| 1367 | // the name of the resource, for example "malloc". |
| 1368 | // The following arguments must be argument indexes, the arguments must be |
| 1369 | // of integer type for Returns, otherwise of pointer type. |
| 1370 | // The difference between Holds and Takes is that a pointer may still be used |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1371 | // after being held. free() should be __attribute((ownership_takes)), whereas |
| 1372 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1373 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1374 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1375 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1376 | << AL.getName()->getName() << 1 << AANT_ArgumentString; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1377 | return; |
| 1378 | } |
| 1379 | // Figure out our Kind, and check arguments while we're at it. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1380 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1381 | switch (AL.getKind()) { |
| 1382 | case AttributeList::AT_ownership_takes: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1383 | K = OwnershipAttr::Takes; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1384 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1385 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1386 | << AL.getName() << 2; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1387 | return; |
| 1388 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1389 | break; |
| 1390 | case AttributeList::AT_ownership_holds: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1391 | K = OwnershipAttr::Holds; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1392 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1393 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1394 | << AL.getName() << 2; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1395 | return; |
| 1396 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1397 | break; |
| 1398 | case AttributeList::AT_ownership_returns: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1399 | K = OwnershipAttr::Returns; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1400 | if (AL.getNumArgs() > 2) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1401 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1402 | << AL.getName() << AL.getNumArgs() + 2; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1403 | return; |
| 1404 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1405 | break; |
| 1406 | default: |
| 1407 | // This should never happen given how we are called. |
| 1408 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1411 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1412 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1413 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1414 | return; |
| 1415 | } |
| 1416 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1417 | StringRef Module = AL.getArgAsIdent(0)->Ident->getName(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1418 | |
| 1419 | // Normalize the argument, __foo__ becomes foo. |
| 1420 | if (Module.startswith("__") && Module.endswith("__")) |
| 1421 | Module = Module.substr(2, Module.size() - 4); |
| 1422 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1423 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1424 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1425 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1426 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1427 | uint64_t Idx; |
| 1428 | if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(), |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1429 | AL.getLoc(), i, Ex, Idx)) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1430 | return; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1431 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1432 | switch (K) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1433 | case OwnershipAttr::Takes: |
| 1434 | case OwnershipAttr::Holds: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1435 | // Is the function argument a pointer type? |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1436 | QualType T = getFunctionOrMethodArgType(D, Idx); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1437 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| 1438 | // FIXME: Should also highlight argument in decl. |
| 1439 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1440 | << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1441 | << "pointer" |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1442 | << Ex->getSourceRange(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1443 | continue; |
| 1444 | } |
| 1445 | break; |
| 1446 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1447 | case OwnershipAttr::Returns: { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1448 | if (AL.getNumArgs() > 2) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1449 | // Is the function argument an integer type? |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1450 | Expr *IdxExpr = AL.getArgAsExpr(1); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1451 | llvm::APSInt ArgNum(32); |
| 1452 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1453 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1454 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
| 1455 | << "ownership_returns" << "integer" |
| 1456 | << IdxExpr->getSourceRange(); |
| 1457 | return; |
| 1458 | } |
| 1459 | } |
| 1460 | break; |
| 1461 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1462 | } // switch |
| 1463 | |
| 1464 | // Check we don't have a conflict with another ownership attribute. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1465 | for (specific_attr_iterator<OwnershipAttr> |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1466 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1467 | e = D->specific_attr_end<OwnershipAttr>(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1468 | i != e; ++i) { |
| 1469 | if ((*i)->getOwnKind() != K) { |
| 1470 | for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); |
| 1471 | I!=E; ++I) { |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1472 | if (Idx == *I) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1473 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1474 | << AL.getName()->getName() << "ownership_*"; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1475 | } |
| 1476 | } |
| 1477 | } |
| 1478 | } |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1479 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | unsigned* start = OwnershipArgs.data(); |
| 1483 | unsigned size = OwnershipArgs.size(); |
| 1484 | llvm::array_pod_sort(start, start + size); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1485 | |
| 1486 | if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1487 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1488 | << AL.getName() << 2; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1489 | return; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1490 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1491 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1492 | D->addAttr(::new (S.Context) |
| 1493 | OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size, |
| 1494 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1497 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1498 | // Check the attribute arguments. |
| 1499 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1500 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1501 | << Attr.getName() << 1; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1502 | return; |
| 1503 | } |
| 1504 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1505 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1506 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1507 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1508 | return; |
| 1509 | } |
| 1510 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1511 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1512 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1513 | // gcc rejects |
| 1514 | // class c { |
| 1515 | // static int a __attribute__((weakref ("v2"))); |
| 1516 | // static int b() __attribute__((weakref ("f3"))); |
| 1517 | // }; |
| 1518 | // and ignores the attributes of |
| 1519 | // void f(void) { |
| 1520 | // static int a __attribute__((weakref ("v2"))); |
| 1521 | // } |
| 1522 | // we reject them |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1523 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1524 | if (!Ctx->isFileContext()) { |
| 1525 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1526 | nd->getNameAsString(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1527 | return; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | // The GCC manual says |
| 1531 | // |
| 1532 | // At present, a declaration to which `weakref' is attached can only |
| 1533 | // be `static'. |
| 1534 | // |
| 1535 | // It also says |
| 1536 | // |
| 1537 | // Without a TARGET, |
| 1538 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1539 | // equivalent to `weak'. |
| 1540 | // |
| 1541 | // gcc 4.4.1 will accept |
| 1542 | // int a7 __attribute__((weakref)); |
| 1543 | // as |
| 1544 | // int a7 __attribute__((weak)); |
| 1545 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1546 | // it if this behaviour is actually used. |
| 1547 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1548 | // GCC rejects |
| 1549 | // static ((alias ("y"), weakref)). |
| 1550 | // Should we? How to check that weakref is before or after alias? |
| 1551 | |
Aaron Ballman | be116e2 | 2013-09-09 23:40:31 +0000 | [diff] [blame^] | 1552 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1553 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1554 | // StringRef parameter it was given anyway. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1555 | if (Attr.isArgExpr(0)) { |
| 1556 | Expr *Arg = Attr.getArgAsExpr(0); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1557 | Arg = Arg->IgnoreParenCasts(); |
| 1558 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1559 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1560 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1561 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1562 | << Attr.getName() << 1 << AANT_ArgumentString; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1563 | return; |
| 1564 | } |
| 1565 | // GCC will accept anything as the argument of weakref. Should we |
| 1566 | // check for an existing decl? |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1567 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1568 | Str->getString())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1571 | D->addAttr(::new (S.Context) |
| 1572 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1573 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1576 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1577 | StringLiteral *Str = 0; |
| 1578 | if (Attr.isArgExpr(0)) |
| 1579 | Str = dyn_cast<StringLiteral>(Attr.getArgAsExpr(0)->IgnoreParenCasts()); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1580 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1581 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1582 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1583 | << Attr.getName() << AANT_ArgumentString; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1584 | return; |
| 1585 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1586 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1587 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | f5fe292 | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1588 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1589 | return; |
| 1590 | } |
| 1591 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1592 | // FIXME: check if target symbol exists in current file |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1593 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1594 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1595 | Str->getString(), |
| 1596 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1599 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1600 | if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) { |
| 1601 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1602 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1603 | return; |
| 1604 | } |
| 1605 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1606 | D->addAttr(::new (S.Context) |
| 1607 | MinSizeAttr(Attr.getRange(), S.Context, |
| 1608 | Attr.getAttributeSpellingListIndex())); |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1611 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1612 | if (!isa<FunctionDecl>(D)) { |
| 1613 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1614 | << Attr.getName() << ExpectedFunction; |
| 1615 | return; |
| 1616 | } |
| 1617 | |
| 1618 | if (D->hasAttr<HotAttr>()) { |
| 1619 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1620 | << Attr.getName() << "hot"; |
| 1621 | return; |
| 1622 | } |
| 1623 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1624 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1625 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1629 | if (!isa<FunctionDecl>(D)) { |
| 1630 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1631 | << Attr.getName() << ExpectedFunction; |
| 1632 | return; |
| 1633 | } |
| 1634 | |
| 1635 | if (D->hasAttr<ColdAttr>()) { |
| 1636 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1637 | << Attr.getName() << "cold"; |
| 1638 | return; |
| 1639 | } |
| 1640 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1641 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1642 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1645 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1646 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1647 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1648 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1649 | return; |
| 1650 | } |
| 1651 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1652 | D->addAttr(::new (S.Context) |
| 1653 | NakedAttr(Attr.getRange(), S.Context, |
| 1654 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1657 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 1658 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1659 | if (!isa<FunctionDecl>(D)) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1660 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1661 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1662 | return; |
| 1663 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1664 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1665 | D->addAttr(::new (S.Context) |
| 1666 | AlwaysInlineAttr(Attr.getRange(), S.Context, |
| 1667 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1670 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1671 | const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1672 | Expr *Arg = Attr.getArgAsExpr(0); |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1673 | Arg = Arg->IgnoreParenCasts(); |
| 1674 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1675 | |
| 1676 | // Check that it is a string. |
| 1677 | if (!Str) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1678 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1679 | << Attr.getName() << AANT_ArgumentString; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1680 | return; |
| 1681 | } |
| 1682 | |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 1683 | if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) { |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1684 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1685 | << Attr.getName() << ExpectedTLSVar; |
| 1686 | return; |
| 1687 | } |
| 1688 | |
| 1689 | // Check that the value. |
| 1690 | StringRef Model = Str->getString(); |
| 1691 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1692 | && Model != "initial-exec" && Model != "local-exec") { |
| 1693 | S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg); |
| 1694 | return; |
| 1695 | } |
| 1696 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1697 | D->addAttr(::new (S.Context) |
| 1698 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1699 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1702 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1703 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1704 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1705 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1706 | D->addAttr(::new (S.Context) |
| 1707 | MallocAttr(Attr.getRange(), S.Context, |
| 1708 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1709 | return; |
| 1710 | } |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1713 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1716 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1717 | D->addAttr(::new (S.Context) |
| 1718 | MayAliasAttr(Attr.getRange(), S.Context, |
| 1719 | Attr.getAttributeSpellingListIndex())); |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1722 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1723 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1724 | D->addAttr(::new (S.Context) |
| 1725 | NoCommonAttr(Attr.getRange(), S.Context, |
| 1726 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1727 | else |
| 1728 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1729 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1732 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 3e1aca2 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1733 | if (S.LangOpts.CPlusPlus) { |
| 1734 | S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus); |
| 1735 | return; |
| 1736 | } |
| 1737 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1738 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1739 | D->addAttr(::new (S.Context) |
| 1740 | CommonAttr(Attr.getRange(), S.Context, |
| 1741 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1742 | else |
| 1743 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1744 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1747 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1748 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1749 | |
| 1750 | if (S.CheckNoReturnAttr(attr)) return; |
| 1751 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1752 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1753 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1754 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1755 | return; |
| 1756 | } |
| 1757 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1758 | D->addAttr(::new (S.Context) |
| 1759 | NoReturnAttr(attr.getRange(), S.Context, |
| 1760 | attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1764 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1765 | attr.setInvalid(); |
| 1766 | return true; |
| 1767 | } |
| 1768 | |
| 1769 | return false; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1772 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1773 | const AttributeList &Attr) { |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1774 | |
| 1775 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1776 | // because 'analyzer_noreturn' does not impact the type. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1777 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1778 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1779 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1780 | && !VD->getType()->isFunctionPointerType())) { |
| 1781 | S.Diag(Attr.getLoc(), |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1782 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1783 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1784 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1785 | return; |
| 1786 | } |
| 1787 | } |
| 1788 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1789 | D->addAttr(::new (S.Context) |
| 1790 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1791 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1794 | static void handleCXX11NoReturnAttr(Sema &S, Decl *D, |
| 1795 | const AttributeList &Attr) { |
| 1796 | // C++11 [dcl.attr.noreturn]p1: |
| 1797 | // The attribute may be applied to the declarator-id in a function |
| 1798 | // declaration. |
| 1799 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 1800 | if (!FD) { |
| 1801 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1802 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1803 | return; |
| 1804 | } |
| 1805 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1806 | D->addAttr(::new (S.Context) |
| 1807 | CXX11NoReturnAttr(Attr.getRange(), S.Context, |
| 1808 | Attr.getAttributeSpellingListIndex())); |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1811 | // PS3 PPU-specific. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1812 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1813 | /* |
| 1814 | Returning a Vector Class in Registers |
| 1815 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1816 | According to the PPU ABI specifications, a class with a single member of |
| 1817 | vector type is returned in memory when used as the return value of a function. |
| 1818 | This results in inefficient code when implementing vector classes. To return |
| 1819 | the value in a single vector register, add the vecreturn attribute to the |
| 1820 | class definition. This attribute is also applicable to struct types. |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1821 | |
| 1822 | Example: |
| 1823 | |
| 1824 | struct Vector |
| 1825 | { |
| 1826 | __vector float xyzw; |
| 1827 | } __attribute__((vecreturn)); |
| 1828 | |
| 1829 | Vector Add(Vector lhs, Vector rhs) |
| 1830 | { |
| 1831 | Vector result; |
| 1832 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1833 | return result; // This will be returned in a register |
| 1834 | } |
| 1835 | */ |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1836 | if (!isa<RecordDecl>(D)) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1837 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1838 | << Attr.getName() << ExpectedClass; |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1839 | return; |
| 1840 | } |
| 1841 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1842 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1843 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1844 | return; |
| 1845 | } |
| 1846 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1847 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1848 | int count = 0; |
| 1849 | |
| 1850 | if (!isa<CXXRecordDecl>(record)) { |
| 1851 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1852 | return; |
| 1853 | } |
| 1854 | |
| 1855 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1856 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1857 | return; |
| 1858 | } |
| 1859 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1860 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1861 | iter != record->field_end(); iter++) { |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1862 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1863 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1864 | return; |
| 1865 | } |
| 1866 | count++; |
| 1867 | } |
| 1868 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1869 | D->addAttr(::new (S.Context) |
| 1870 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1871 | Attr.getAttributeSpellingListIndex())); |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1874 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1875 | const AttributeList &Attr) { |
| 1876 | if (isa<ParmVarDecl>(D)) { |
| 1877 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1878 | // parameter of a function declaration or lambda. |
| 1879 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1880 | S.Diag(Attr.getLoc(), |
| 1881 | diag::err_carries_dependency_param_not_function_decl); |
| 1882 | return; |
| 1883 | } |
| 1884 | } else if (!isa<FunctionDecl>(D)) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1885 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1886 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1887 | return; |
| 1888 | } |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1889 | |
| 1890 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1891 | Attr.getRange(), S.Context, |
| 1892 | Attr.getAttributeSpellingListIndex())); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1895 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1896 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 568eae4 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1897 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1898 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1899 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1900 | return; |
| 1901 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1902 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1903 | D->addAttr(::new (S.Context) |
| 1904 | UnusedAttr(Attr.getRange(), S.Context, |
| 1905 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1908 | static void handleReturnsTwiceAttr(Sema &S, Decl *D, |
| 1909 | const AttributeList &Attr) { |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1910 | if (!isa<FunctionDecl>(D)) { |
| 1911 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1912 | << Attr.getName() << ExpectedFunction; |
| 1913 | return; |
| 1914 | } |
| 1915 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1916 | D->addAttr(::new (S.Context) |
| 1917 | ReturnsTwiceAttr(Attr.getRange(), S.Context, |
| 1918 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1921 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1922 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 29535ba | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 1923 | if (VD->hasLocalStorage()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1924 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 1925 | return; |
| 1926 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1927 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1928 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1929 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1930 | return; |
| 1931 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1932 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1933 | D->addAttr(::new (S.Context) |
| 1934 | UsedAttr(Attr.getRange(), S.Context, |
| 1935 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1938 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1939 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1940 | if (Attr.getNumArgs() > 1) { |
| 1941 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1942 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1943 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1944 | |
| 1945 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1946 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1947 | Expr *E = Attr.getArgAsExpr(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1948 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1949 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1950 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1951 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1952 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1953 | << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1954 | return; |
| 1955 | } |
| 1956 | priority = Idx.getZExtValue(); |
| 1957 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1958 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1959 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1960 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1961 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1962 | return; |
| 1963 | } |
| 1964 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1965 | D->addAttr(::new (S.Context) |
| 1966 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1967 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1968 | } |
| 1969 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1970 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1971 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1972 | if (Attr.getNumArgs() > 1) { |
| 1973 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1974 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1975 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1976 | |
| 1977 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1978 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1979 | Expr *E = Attr.getArgAsExpr(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1980 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1981 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1982 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1983 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1984 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1985 | << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1986 | return; |
| 1987 | } |
| 1988 | priority = Idx.getZExtValue(); |
| 1989 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1990 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1991 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1992 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1993 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1994 | return; |
| 1995 | } |
| 1996 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1997 | D->addAttr(::new (S.Context) |
| 1998 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 1999 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2002 | template <typename AttrTy> |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 2003 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 2004 | const AttributeList &Attr) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2005 | unsigned NumArgs = Attr.getNumArgs(); |
| 2006 | if (NumArgs > 1) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2007 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2008 | return; |
| 2009 | } |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2010 | |
| 2011 | // Handle the case where the attribute has a text message. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2012 | StringRef Str; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2013 | if (Attr.isArgExpr(0)) { |
| 2014 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArgAsExpr(0)); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 2015 | if (!SE) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2016 | S.Diag(Attr.getArgAsExpr(0)->getLocStart(), |
| 2017 | diag::err_attribute_argument_type) << Attr.getName() |
| 2018 | << AANT_ArgumentString; |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 2019 | return; |
| 2020 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2021 | Str = SE->getString(); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 2022 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2023 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2024 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 2025 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2028 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 2029 | const AttributeList &Attr) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2030 | D->addAttr(::new (S.Context) |
| 2031 | ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context, |
| 2032 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2035 | static void handleObjCRootClassAttr(Sema &S, Decl *D, |
| 2036 | const AttributeList &Attr) { |
| 2037 | if (!isa<ObjCInterfaceDecl>(D)) { |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 2038 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2039 | << Attr.getName() << ExpectedObjectiveCInterface; |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2040 | return; |
| 2041 | } |
| 2042 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2043 | D->addAttr(::new (S.Context) |
| 2044 | ObjCRootClassAttr(Attr.getRange(), S.Context, |
| 2045 | Attr.getAttributeSpellingListIndex())); |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2048 | static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, |
| 2049 | const AttributeList &Attr) { |
Fariborz Jahanian | 341b8be | 2012-01-03 22:52:32 +0000 | [diff] [blame] | 2050 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 2051 | S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis); |
| 2052 | return; |
| 2053 | } |
| 2054 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2055 | D->addAttr(::new (S.Context) |
| 2056 | ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context, |
| 2057 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2060 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2061 | IdentifierInfo *Platform, |
| 2062 | VersionTuple Introduced, |
| 2063 | VersionTuple Deprecated, |
| 2064 | VersionTuple Obsoleted) { |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2065 | StringRef PlatformName |
| 2066 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2067 | if (PlatformName.empty()) |
| 2068 | PlatformName = Platform->getName(); |
| 2069 | |
| 2070 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2071 | // of these steps are needed). |
| 2072 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2073 | !(Introduced <= Deprecated)) { |
| 2074 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2075 | << 1 << PlatformName << Deprecated.getAsString() |
| 2076 | << 0 << Introduced.getAsString(); |
| 2077 | return true; |
| 2078 | } |
| 2079 | |
| 2080 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2081 | !(Introduced <= Obsoleted)) { |
| 2082 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2083 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2084 | << 0 << Introduced.getAsString(); |
| 2085 | return true; |
| 2086 | } |
| 2087 | |
| 2088 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2089 | !(Deprecated <= Obsoleted)) { |
| 2090 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2091 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2092 | << 1 << Deprecated.getAsString(); |
| 2093 | return true; |
| 2094 | } |
| 2095 | |
| 2096 | return false; |
| 2097 | } |
| 2098 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2099 | /// \brief Check whether the two versions match. |
| 2100 | /// |
| 2101 | /// If either version tuple is empty, then they are assumed to match. If |
| 2102 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2103 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2104 | bool BeforeIsOkay) { |
| 2105 | if (X.empty() || Y.empty()) |
| 2106 | return true; |
| 2107 | |
| 2108 | if (X == Y) |
| 2109 | return true; |
| 2110 | |
| 2111 | if (BeforeIsOkay && X < Y) |
| 2112 | return true; |
| 2113 | |
| 2114 | return false; |
| 2115 | } |
| 2116 | |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2117 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2118 | IdentifierInfo *Platform, |
| 2119 | VersionTuple Introduced, |
| 2120 | VersionTuple Deprecated, |
| 2121 | VersionTuple Obsoleted, |
| 2122 | bool IsUnavailable, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2123 | StringRef Message, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2124 | bool Override, |
| 2125 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2126 | VersionTuple MergedIntroduced = Introduced; |
| 2127 | VersionTuple MergedDeprecated = Deprecated; |
| 2128 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2129 | bool FoundAny = false; |
| 2130 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2131 | if (D->hasAttrs()) { |
| 2132 | AttrVec &Attrs = D->getAttrs(); |
| 2133 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 2134 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 2135 | if (!OldAA) { |
| 2136 | ++i; |
| 2137 | continue; |
| 2138 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2139 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2140 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2141 | if (OldPlatform != Platform) { |
| 2142 | ++i; |
| 2143 | continue; |
| 2144 | } |
| 2145 | |
| 2146 | FoundAny = true; |
| 2147 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2148 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2149 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2150 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2151 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2152 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 2153 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 2154 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 2155 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 72daa3f | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 2156 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2157 | if (Override) { |
| 2158 | int Which = -1; |
| 2159 | VersionTuple FirstVersion; |
| 2160 | VersionTuple SecondVersion; |
| 2161 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 2162 | Which = 0; |
| 2163 | FirstVersion = OldIntroduced; |
| 2164 | SecondVersion = Introduced; |
| 2165 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 2166 | Which = 1; |
| 2167 | FirstVersion = Deprecated; |
| 2168 | SecondVersion = OldDeprecated; |
| 2169 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 2170 | Which = 2; |
| 2171 | FirstVersion = Obsoleted; |
| 2172 | SecondVersion = OldObsoleted; |
| 2173 | } |
| 2174 | |
| 2175 | if (Which == -1) { |
| 2176 | Diag(OldAA->getLocation(), |
| 2177 | diag::warn_mismatched_availability_override_unavail) |
| 2178 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2179 | } else { |
| 2180 | Diag(OldAA->getLocation(), |
| 2181 | diag::warn_mismatched_availability_override) |
| 2182 | << Which |
| 2183 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2184 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 2185 | } |
| 2186 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2187 | } else { |
| 2188 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2189 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2190 | } |
| 2191 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2192 | Attrs.erase(Attrs.begin() + i); |
| 2193 | --e; |
| 2194 | continue; |
| 2195 | } |
| 2196 | |
| 2197 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2198 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2199 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2200 | |
| 2201 | if (MergedIntroduced2.empty()) |
| 2202 | MergedIntroduced2 = OldIntroduced; |
| 2203 | if (MergedDeprecated2.empty()) |
| 2204 | MergedDeprecated2 = OldDeprecated; |
| 2205 | if (MergedObsoleted2.empty()) |
| 2206 | MergedObsoleted2 = OldObsoleted; |
| 2207 | |
| 2208 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2209 | MergedIntroduced2, MergedDeprecated2, |
| 2210 | MergedObsoleted2)) { |
| 2211 | Attrs.erase(Attrs.begin() + i); |
| 2212 | --e; |
| 2213 | continue; |
| 2214 | } |
| 2215 | |
| 2216 | MergedIntroduced = MergedIntroduced2; |
| 2217 | MergedDeprecated = MergedDeprecated2; |
| 2218 | MergedObsoleted = MergedObsoleted2; |
| 2219 | ++i; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2220 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | if (FoundAny && |
| 2224 | MergedIntroduced == Introduced && |
| 2225 | MergedDeprecated == Deprecated && |
| 2226 | MergedObsoleted == Obsoleted) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2227 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2228 | |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2229 | // Only create a new attribute if !Override, but we want to do |
| 2230 | // the checking. |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2231 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2232 | MergedDeprecated, MergedObsoleted) && |
| 2233 | !Override) { |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2234 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 2235 | Introduced, Deprecated, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2236 | Obsoleted, IsUnavailable, Message, |
| 2237 | AttrSpellingListIndex); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2238 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2239 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2240 | } |
| 2241 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2242 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2243 | const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2244 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 2245 | return; |
| 2246 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2247 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2248 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2249 | IdentifierInfo *II = Platform->Ident; |
| 2250 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 2251 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 2252 | << Platform->Ident; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2253 | |
Rafael Espindola | 8c4222a | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2254 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 2255 | if (!ND) { |
| 2256 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2257 | return; |
| 2258 | } |
| 2259 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2260 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2261 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2262 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2263 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2264 | StringRef Str; |
| 2265 | const StringLiteral *SE = |
| 2266 | dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr()); |
| 2267 | if (SE) |
| 2268 | Str = SE->getString(); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2269 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2270 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2271 | Introduced.Version, |
| 2272 | Deprecated.Version, |
| 2273 | Obsoleted.Version, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2274 | IsUnavailable, Str, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2275 | /*Override=*/false, |
| 2276 | Index); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2277 | if (NewAttr) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2278 | D->addAttr(NewAttr); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2281 | template <class T> |
| 2282 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2283 | typename T::VisibilityType value, |
| 2284 | unsigned attrSpellingListIndex) { |
| 2285 | T *existingAttr = D->getAttr<T>(); |
| 2286 | if (existingAttr) { |
| 2287 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2288 | if (existingValue == value) |
| 2289 | return NULL; |
| 2290 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2291 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2292 | D->dropAttr<T>(); |
| 2293 | } |
| 2294 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2295 | } |
| 2296 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2297 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2298 | VisibilityAttr::VisibilityType Vis, |
| 2299 | unsigned AttrSpellingListIndex) { |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2300 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2301 | AttrSpellingListIndex); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2304 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2305 | TypeVisibilityAttr::VisibilityType Vis, |
| 2306 | unsigned AttrSpellingListIndex) { |
| 2307 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2308 | AttrSpellingListIndex); |
| 2309 | } |
| 2310 | |
| 2311 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2312 | bool isTypeVisibility) { |
| 2313 | // Visibility attributes don't mean anything on a typedef. |
| 2314 | if (isa<TypedefNameDecl>(D)) { |
| 2315 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2316 | << Attr.getName(); |
| 2317 | return; |
| 2318 | } |
| 2319 | |
| 2320 | // 'type_visibility' can only go on a type or namespace. |
| 2321 | if (isTypeVisibility && |
| 2322 | !(isa<TagDecl>(D) || |
| 2323 | isa<ObjCInterfaceDecl>(D) || |
| 2324 | isa<NamespaceDecl>(D))) { |
| 2325 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2326 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2327 | return; |
| 2328 | } |
| 2329 | |
Benjamin Kramer | ae3a83f | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2330 | // Check that the argument is a string literal. |
| 2331 | StringLiteral *Str = 0; |
| 2332 | if (Attr.isArgExpr(0)) |
| 2333 | Str = dyn_cast<StringLiteral>(Attr.getArgAsExpr(0)->IgnoreParenCasts()); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2334 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2335 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2336 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 2337 | << Attr.getName() << AANT_ArgumentString; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2338 | return; |
| 2339 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2340 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2341 | StringRef TypeStr = Str->getString(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2342 | VisibilityAttr::VisibilityType type; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2343 | |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2344 | if (TypeStr == "default") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2345 | type = VisibilityAttr::Default; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2346 | else if (TypeStr == "hidden") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2347 | type = VisibilityAttr::Hidden; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2348 | else if (TypeStr == "internal") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2349 | type = VisibilityAttr::Hidden; // FIXME |
John McCall | 4188760 | 2012-01-29 01:20:30 +0000 | [diff] [blame] | 2350 | else if (TypeStr == "protected") { |
| 2351 | // Complain about attempts to use protected visibility on targets |
| 2352 | // (like Darwin) that don't support it. |
| 2353 | if (!S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2354 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2355 | type = VisibilityAttr::Default; |
| 2356 | } else { |
| 2357 | type = VisibilityAttr::Protected; |
| 2358 | } |
| 2359 | } else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2360 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2361 | return; |
| 2362 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2363 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2364 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2365 | clang::Attr *newAttr; |
| 2366 | if (isTypeVisibility) { |
| 2367 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2368 | (TypeVisibilityAttr::VisibilityType) type, |
| 2369 | Index); |
| 2370 | } else { |
| 2371 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2372 | } |
| 2373 | if (newAttr) |
| 2374 | D->addAttr(newAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2377 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2378 | const AttributeList &Attr) { |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2379 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 2380 | if (!method) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2381 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2382 | << ExpectedMethod; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2383 | return; |
| 2384 | } |
| 2385 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2386 | if (!Attr.isArgIdent(0)) { |
| 2387 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2388 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2389 | return; |
| 2390 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2391 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2392 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2393 | |
| 2394 | StringRef param = IL->Ident->getName(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2395 | ObjCMethodFamilyAttr::FamilyKind family; |
| 2396 | if (param == "none") |
| 2397 | family = ObjCMethodFamilyAttr::OMF_None; |
| 2398 | else if (param == "alloc") |
| 2399 | family = ObjCMethodFamilyAttr::OMF_alloc; |
| 2400 | else if (param == "copy") |
| 2401 | family = ObjCMethodFamilyAttr::OMF_copy; |
| 2402 | else if (param == "init") |
| 2403 | family = ObjCMethodFamilyAttr::OMF_init; |
| 2404 | else if (param == "mutableCopy") |
| 2405 | family = ObjCMethodFamilyAttr::OMF_mutableCopy; |
| 2406 | else if (param == "new") |
| 2407 | family = ObjCMethodFamilyAttr::OMF_new; |
| 2408 | else { |
| 2409 | // Just warn and ignore it. This is future-proof against new |
| 2410 | // families being used in system headers. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2411 | S.Diag(IL->Loc, diag::warn_unknown_method_family); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2412 | return; |
| 2413 | } |
| 2414 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2415 | if (family == ObjCMethodFamilyAttr::OMF_init && |
| 2416 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2417 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2418 | << method->getResultType(); |
| 2419 | // Ignore the attribute. |
| 2420 | return; |
| 2421 | } |
| 2422 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2423 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2424 | S.Context, family)); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2425 | } |
| 2426 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2427 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 2428 | const AttributeList &Attr) { |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2429 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 2430 | if (OCI == 0) { |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 2431 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2432 | << Attr.getName() << ExpectedObjectiveCInterface; |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2433 | return; |
| 2434 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2435 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2436 | D->addAttr(::new (S.Context) |
| 2437 | ObjCExceptionAttr(Attr.getRange(), S.Context, |
| 2438 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2441 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2442 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2443 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2444 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2445 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2446 | return; |
| 2447 | } |
| 2448 | } |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2449 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2450 | QualType T = PD->getType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2451 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2452 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2453 | return; |
| 2454 | } |
| 2455 | } |
| 2456 | else { |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2457 | // It is okay to include this attribute on properties, e.g.: |
| 2458 | // |
| 2459 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2460 | // |
| 2461 | // In this case it follows tradition and suppresses an error in the above |
| 2462 | // case. |
Fariborz Jahanian | 9b2eb7b | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2463 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2464 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2465 | D->addAttr(::new (S.Context) |
| 2466 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2467 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2470 | static void |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2471 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2472 | if (!isa<FunctionDecl>(D)) { |
| 2473 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 2474 | return; |
| 2475 | } |
| 2476 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2477 | D->addAttr(::new (S.Context) |
| 2478 | OverloadableAttr(Attr.getRange(), S.Context, |
| 2479 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2482 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2483 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2484 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2485 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2486 | return; |
| 2487 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2488 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2489 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2490 | BlocksAttr::BlockType type; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2491 | if (II->isStr("byref")) |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2492 | type = BlocksAttr::ByRef; |
| 2493 | else { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2494 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) << "blocks" |
| 2495 | << 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. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2775 | Expr *ArgExpr = Attr.getArgAsExpr(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2776 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2777 | if (!SE) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2778 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
| 2779 | << Attr.getName() << AANT_ArgumentString; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2780 | return; |
| 2781 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2782 | |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2783 | // If the target wants to validate the section specifier, make it happen. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2784 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString()); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2785 | if (!Error.empty()) { |
| 2786 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 2787 | << Error; |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2788 | return; |
| 2789 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2790 | |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2791 | // This attribute cannot be applied to local variables. |
| 2792 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 2793 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 2794 | return; |
| 2795 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2796 | |
| 2797 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2798 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2799 | SE->getString(), Index); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2800 | if (NewAttr) |
| 2801 | D->addAttr(NewAttr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2804 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2805 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2806 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2807 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2808 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2809 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2810 | D->addAttr(::new (S.Context) |
| 2811 | NoThrowAttr(Attr.getRange(), S.Context, |
| 2812 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2813 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2814 | } |
| 2815 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2816 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2817 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2818 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2819 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2820 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2821 | D->addAttr(::new (S.Context) |
| 2822 | ConstAttr(Attr.getRange(), S.Context, |
| 2823 | Attr.getAttributeSpellingListIndex() )); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2824 | } |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2827 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2828 | D->addAttr(::new (S.Context) |
| 2829 | PureAttr(Attr.getRange(), S.Context, |
| 2830 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2833 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2834 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2835 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2836 | << Attr.getName() << 1; |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2837 | return; |
| 2838 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2839 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2840 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2841 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2842 | if (!VD || !VD->hasLocalStorage()) { |
| 2843 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 2844 | return; |
| 2845 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2846 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2847 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2848 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2849 | // Look up the function |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2850 | // FIXME: Lookup probably isn't looking in the right place |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2851 | NamedDecl *CleanupDecl |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2852 | = S.LookupSingleName(S.TUScope, IL->Ident, IL->Loc, |
| 2853 | Sema::LookupOrdinaryName); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2854 | if (!CleanupDecl) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2855 | S.Diag(IL->Loc, diag::err_attribute_cleanup_arg_not_found) << IL->Ident; |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2856 | return; |
| 2857 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2858 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2859 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 2860 | if (!FD) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2861 | S.Diag(IL->Loc, diag::err_attribute_cleanup_arg_not_function) << IL->Ident; |
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 | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2866 | S.Diag(IL->Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2867 | << IL->Ident; |
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 | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2877 | S.Diag(IL->Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 2878 | IL->Ident << 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())); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2885 | S.MarkFunctionReferenced(IL->Loc, FD); |
| 2886 | S.DiagnoseUseOfDecl(FD, IL->Loc); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2889 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2890 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2891 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2892 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2893 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2894 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2895 | return; |
| 2896 | } |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2897 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2898 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2899 | uint64_t ArgIdx; |
| 2900 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 2901 | Attr.getLoc(), 1, IdxExpr, ArgIdx)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2902 | return; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2903 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2904 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2905 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2906 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2907 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2908 | if (not_nsstring_type && |
| 2909 | !isCFStringType(Ty, S.Context) && |
| 2910 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2911 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2912 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2913 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2914 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2915 | << IdxExpr->getSourceRange(); |
| 2916 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2917 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2918 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2919 | if (!isNSStringType(Ty, S.Context) && |
| 2920 | !isCFStringType(Ty, S.Context) && |
| 2921 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2922 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2923 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2924 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2925 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2926 | << IdxExpr->getSourceRange(); |
| 2927 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2930 | // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex |
| 2931 | // because that has corrected for the implicit this parameter, and is zero- |
| 2932 | // based. The attribute expects what the user wrote explicitly. |
| 2933 | llvm::APSInt Val; |
| 2934 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2935 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2936 | D->addAttr(::new (S.Context) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2937 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2938 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2941 | enum FormatAttrKind { |
| 2942 | CFStringFormat, |
| 2943 | NSStringFormat, |
| 2944 | StrftimeFormat, |
| 2945 | SupportedFormat, |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2946 | IgnoredFormat, |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2947 | InvalidFormat |
| 2948 | }; |
| 2949 | |
| 2950 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2951 | /// types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2952 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2953 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2954 | // Check for formats that get handled specially. |
| 2955 | .Case("NSString", NSStringFormat) |
| 2956 | .Case("CFString", CFStringFormat) |
| 2957 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2958 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2959 | // Otherwise, check for supported formats. |
| 2960 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2961 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2962 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2963 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2964 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2965 | .Default(InvalidFormat); |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2968 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2969 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2970 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2971 | const AttributeList &Attr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2972 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2973 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2974 | return; |
| 2975 | } |
| 2976 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2977 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2978 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2979 | Attr.setInvalid(); |
| 2980 | return; |
| 2981 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2982 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2983 | if (S.Context.getAsArrayType(T)) |
| 2984 | T = S.Context.getBaseElementType(T); |
| 2985 | if (!T->getAs<RecordType>()) { |
| 2986 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2987 | Attr.setInvalid(); |
| 2988 | return; |
| 2989 | } |
| 2990 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2991 | Expr *priorityExpr = Attr.getArgAsExpr(0); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2992 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2993 | llvm::APSInt priority(32); |
| 2994 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 2995 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 2996 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 2997 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 2998 | << priorityExpr->getSourceRange(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2999 | Attr.setInvalid(); |
| 3000 | return; |
| 3001 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 3002 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3003 | if (prioritynum < 101 || prioritynum > 65535) { |
| 3004 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 3005 | << priorityExpr->getSourceRange(); |
| 3006 | Attr.setInvalid(); |
| 3007 | return; |
| 3008 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3009 | D->addAttr(::new (S.Context) |
| 3010 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 3011 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3014 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 3015 | IdentifierInfo *Format, int FormatIdx, |
| 3016 | int FirstArg, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3017 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3018 | // Check whether we already have an equivalent format attribute. |
| 3019 | for (specific_attr_iterator<FormatAttr> |
| 3020 | i = D->specific_attr_begin<FormatAttr>(), |
| 3021 | e = D->specific_attr_end<FormatAttr>(); |
| 3022 | i != e ; ++i) { |
| 3023 | FormatAttr *f = *i; |
| 3024 | if (f->getType() == Format && |
| 3025 | f->getFormatIdx() == FormatIdx && |
| 3026 | f->getFirstArg() == FirstArg) { |
| 3027 | // If we don't have a valid location for this attribute, adopt the |
| 3028 | // location. |
| 3029 | if (f->getLocation().isInvalid()) |
| 3030 | f->setRange(Range); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3031 | return NULL; |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3032 | } |
| 3033 | } |
| 3034 | |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3035 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 3036 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3037 | } |
| 3038 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3039 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3040 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3041 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3042 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3043 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3044 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3045 | return; |
| 3046 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3047 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3048 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3049 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3050 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3051 | return; |
| 3052 | } |
| 3053 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3054 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3055 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3056 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 3057 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3058 | unsigned FirstIdx = 1; |
| 3059 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3060 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 3061 | StringRef Format = II->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3062 | |
| 3063 | // Normalize the argument, __foo__ becomes foo. |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3064 | if (Format.startswith("__") && Format.endswith("__")) { |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3065 | Format = Format.substr(2, Format.size() - 4); |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3066 | // If we've modified the string name, we need a new identifier for it. |
| 3067 | II = &S.Context.Idents.get(Format); |
| 3068 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3069 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3070 | // Check for supported formats. |
| 3071 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3072 | |
| 3073 | if (Kind == IgnoredFormat) |
| 3074 | return; |
| 3075 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3076 | if (Kind == InvalidFormat) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3077 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3078 | << "format" << II->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3079 | return; |
| 3080 | } |
| 3081 | |
| 3082 | // checks for the 2nd argument |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3083 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3084 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3085 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 3086 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3087 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3088 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3089 | << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3090 | return; |
| 3091 | } |
| 3092 | |
| 3093 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3094 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3095 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3096 | return; |
| 3097 | } |
| 3098 | |
| 3099 | // FIXME: Do we need to bounds check? |
| 3100 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3101 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3102 | if (HasImplicitThisParam) { |
| 3103 | if (ArgIdx == 0) { |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3104 | S.Diag(Attr.getLoc(), |
| 3105 | diag::err_format_attribute_implicit_this_format_string) |
| 3106 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3107 | return; |
| 3108 | } |
| 3109 | ArgIdx--; |
| 3110 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3111 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3112 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3113 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3114 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3115 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3116 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3117 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3118 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3119 | return; |
| 3120 | } |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3121 | } else if (Kind == NSStringFormat) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3122 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3123 | // semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3124 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3125 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3126 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3127 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3128 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3129 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3130 | } else if (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3131 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3132 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3133 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3134 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3135 | return; |
| 3136 | } |
| 3137 | |
| 3138 | // check the 3rd argument |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3139 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3140 | llvm::APSInt FirstArg(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3141 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 3142 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3143 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3144 | << Attr.getName() << 3 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3145 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3146 | return; |
| 3147 | } |
| 3148 | |
| 3149 | // check if the function is variadic if the 3rd argument non-zero |
| 3150 | if (FirstArg != 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3151 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3152 | ++NumArgs; // +1 for ... |
| 3153 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3154 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3155 | return; |
| 3156 | } |
| 3157 | } |
| 3158 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3159 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3160 | // variable the input is just the current time + the format string. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3161 | if (Kind == StrftimeFormat) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3162 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3163 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 3164 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3165 | return; |
| 3166 | } |
| 3167 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3168 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3169 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3170 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3171 | return; |
| 3172 | } |
| 3173 | |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3174 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3175 | Idx.getZExtValue(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3176 | FirstArg.getZExtValue(), |
| 3177 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3178 | if (NewAttr) |
| 3179 | D->addAttr(NewAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3182 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 3183 | const AttributeList &Attr) { |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3184 | // Try to find the underlying union declaration. |
| 3185 | RecordDecl *RD = 0; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3186 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3187 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3188 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3189 | else |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3190 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3191 | |
| 3192 | if (!RD || !RD->isUnion()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3193 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3194 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3195 | return; |
| 3196 | } |
| 3197 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3198 | if (!RD->isCompleteDefinition()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3199 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3200 | diag::warn_transparent_union_attribute_not_definition); |
| 3201 | return; |
| 3202 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3203 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3204 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3205 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3206 | if (Field == FieldEnd) { |
| 3207 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3208 | return; |
| 3209 | } |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3210 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3211 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3212 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3213 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3214 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3215 | diag::warn_transparent_union_attribute_floating) |
| 3216 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3217 | return; |
| 3218 | } |
| 3219 | |
| 3220 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3221 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3222 | for (; Field != FieldEnd; ++Field) { |
| 3223 | QualType FieldType = Field->getType(); |
| 3224 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 3225 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 3226 | // Warn if we drop the attribute. |
| 3227 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3228 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3229 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3230 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3231 | diag::warn_transparent_union_attribute_field_size_align) |
| 3232 | << isSize << Field->getDeclName() << FieldBits; |
| 3233 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3234 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3235 | diag::note_transparent_union_first_field_size_align) |
| 3236 | << isSize << FirstBits; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3237 | return; |
| 3238 | } |
| 3239 | } |
| 3240 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3241 | RD->addAttr(::new (S.Context) |
| 3242 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 3243 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3246 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3247 | Expr *ArgExpr = Attr.getArgAsExpr(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3248 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3249 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3250 | // Make sure that there is a string literal as the annotation's single |
| 3251 | // argument. |
| 3252 | if (!SE) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3253 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
| 3254 | << Attr.getName() << AANT_ArgumentString; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3255 | return; |
| 3256 | } |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3257 | |
| 3258 | // Don't duplicate annotations that are already set. |
| 3259 | for (specific_attr_iterator<AnnotateAttr> |
| 3260 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 3261 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
| 3262 | if ((*i)->getAnnotation() == SE->getString()) |
| 3263 | return; |
| 3264 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3265 | |
| 3266 | D->addAttr(::new (S.Context) |
| 3267 | AnnotateAttr(Attr.getRange(), S.Context, SE->getString(), |
| 3268 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3271 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3272 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3273 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3274 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3275 | << Attr.getName() << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3276 | return; |
| 3277 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3278 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3279 | if (Attr.getNumArgs() == 0) { |
| 3280 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 3281 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 3282 | return; |
| 3283 | } |
| 3284 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3285 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3286 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3287 | S.Diag(Attr.getEllipsisLoc(), |
| 3288 | diag::err_pack_expansion_without_parameter_packs); |
| 3289 | return; |
| 3290 | } |
| 3291 | |
| 3292 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 3293 | return; |
| 3294 | |
| 3295 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 3296 | Attr.isPackExpansion()); |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
| 3299 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3300 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3301 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3302 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3303 | |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3304 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3305 | if (TmpAttr.isAlignas()) { |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3306 | // C++11 [dcl.align]p1: |
| 3307 | // An alignment-specifier may be applied to a variable or to a class |
| 3308 | // data member, but it shall not be applied to a bit-field, a function |
| 3309 | // parameter, the formal parameter of a catch clause, or a variable |
| 3310 | // declared with the register storage class specifier. An |
| 3311 | // alignment-specifier may also be applied to the declaration of a class |
| 3312 | // or enumeration type. |
| 3313 | // C11 6.7.5/2: |
| 3314 | // An alignment attribute shall not be specified in a declaration of |
| 3315 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3316 | // object declared with the register storage-class specifier. |
| 3317 | int DiagKind = -1; |
| 3318 | if (isa<ParmVarDecl>(D)) { |
| 3319 | DiagKind = 0; |
| 3320 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3321 | if (VD->getStorageClass() == SC_Register) |
| 3322 | DiagKind = 1; |
| 3323 | if (VD->isExceptionVariable()) |
| 3324 | DiagKind = 2; |
| 3325 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 3326 | if (FD->isBitField()) |
| 3327 | DiagKind = 3; |
| 3328 | } else if (!isa<TagDecl>(D)) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3329 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) |
| 3330 | << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'") |
Richard Smith | 5f838aa | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3331 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3332 | : ExpectedVariableFieldOrTag); |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3333 | return; |
| 3334 | } |
| 3335 | if (DiagKind != -1) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3336 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 3337 | << TmpAttr.isC11() << DiagKind; |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3338 | return; |
| 3339 | } |
| 3340 | } |
| 3341 | |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3342 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3343 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3344 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3345 | AA->setPackExpansion(IsPackExpansion); |
| 3346 | D->addAttr(AA); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3347 | return; |
| 3348 | } |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3349 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3350 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3351 | llvm::APSInt Alignment(32); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3352 | ExprResult ICE |
| 3353 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3354 | diag::err_aligned_attribute_argument_not_int, |
| 3355 | /*AllowFold*/ false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3356 | if (ICE.isInvalid()) |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3357 | return; |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3358 | |
| 3359 | // C++11 [dcl.align]p2: |
| 3360 | // -- if the constant expression evaluates to zero, the alignment |
| 3361 | // specifier shall have no effect |
| 3362 | // C11 6.7.5p6: |
| 3363 | // An alignment specification of zero has no effect. |
| 3364 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 3365 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3366 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 3367 | << E->getSourceRange(); |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3368 | return; |
| 3369 | } |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3370 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3371 | if (TmpAttr.isDeclspec()) { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3372 | // We've already verified it's a power of 2, now let's make sure it's |
| 3373 | // 8192 or less. |
| 3374 | if (Alignment.getZExtValue() > 8192) { |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3375 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3376 | << E->getSourceRange(); |
| 3377 | return; |
| 3378 | } |
| 3379 | } |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3380 | |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3381 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 3382 | ICE.take(), SpellingListIndex); |
| 3383 | AA->setPackExpansion(IsPackExpansion); |
| 3384 | D->addAttr(AA); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3385 | } |
| 3386 | |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3387 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3388 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3389 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3390 | // FIXME: Perform checking of type validity |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3391 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3392 | SpellingListIndex); |
| 3393 | AA->setPackExpansion(IsPackExpansion); |
| 3394 | D->addAttr(AA); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3395 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3396 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3397 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3398 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3399 | |
| 3400 | QualType Ty; |
| 3401 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3402 | Ty = VD->getType(); |
| 3403 | else |
| 3404 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
Richard Smith | 4da0903 | 2013-02-22 09:21:42 +0000 | [diff] [blame] | 3405 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3406 | return; |
| 3407 | |
| 3408 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3409 | // The combined effect of all alignment attributes in a declaration shall |
| 3410 | // not specify an alignment that is less strict than the alignment that |
| 3411 | // would otherwise be required for the entity being declared. |
| 3412 | AlignedAttr *AlignasAttr = 0; |
| 3413 | unsigned Align = 0; |
| 3414 | for (specific_attr_iterator<AlignedAttr> |
| 3415 | I = D->specific_attr_begin<AlignedAttr>(), |
| 3416 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 3417 | if (I->isAlignmentDependent()) |
| 3418 | return; |
| 3419 | if (I->isAlignas()) |
| 3420 | AlignasAttr = *I; |
| 3421 | Align = std::max(Align, I->getAlignment(Context)); |
| 3422 | } |
| 3423 | |
| 3424 | if (AlignasAttr && Align) { |
| 3425 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 3426 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 3427 | if (NaturalAlign > RequestedAlign) |
| 3428 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 3429 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 3430 | } |
| 3431 | } |
| 3432 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3433 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3434 | /// type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3435 | /// |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3436 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3437 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3438 | /// HImode, not an intermediate pointer. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3439 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3440 | // This attribute isn't documented, but glibc uses it. It changes |
| 3441 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3442 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 750f73a | 2013-07-30 14:29:12 +0000 | [diff] [blame] | 3443 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3444 | << AANT_ArgumentIdentifier; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3445 | return; |
| 3446 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3447 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3448 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
| 3449 | StringRef Str = Name->getName(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3450 | |
| 3451 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3452 | if (Str.startswith("__") && Str.endswith("__")) |
| 3453 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3454 | |
| 3455 | unsigned DestWidth = 0; |
| 3456 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3457 | bool ComplexMode = false; |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3458 | switch (Str.size()) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3459 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3460 | switch (Str[0]) { |
| 3461 | case 'Q': DestWidth = 8; break; |
| 3462 | case 'H': DestWidth = 16; break; |
| 3463 | case 'S': DestWidth = 32; break; |
| 3464 | case 'D': DestWidth = 64; break; |
| 3465 | case 'X': DestWidth = 96; break; |
| 3466 | case 'T': DestWidth = 128; break; |
| 3467 | } |
| 3468 | if (Str[1] == 'F') { |
| 3469 | IntegerMode = false; |
| 3470 | } else if (Str[1] == 'C') { |
| 3471 | IntegerMode = false; |
| 3472 | ComplexMode = true; |
| 3473 | } else if (Str[1] != 'I') { |
| 3474 | DestWidth = 0; |
| 3475 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3476 | break; |
| 3477 | case 4: |
| 3478 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3479 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3480 | if (Str == "word") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3481 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3482 | else if (Str == "byte") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3483 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3484 | break; |
| 3485 | case 7: |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3486 | if (Str == "pointer") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3487 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3488 | break; |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3489 | case 11: |
| 3490 | if (Str == "unwind_word") |
Rafael Espindola | 0b1de54 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3491 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3492 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3493 | } |
| 3494 | |
| 3495 | QualType OldTy; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3496 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3497 | OldTy = TD->getUnderlyingType(); |
| 3498 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3499 | OldTy = VD->getType(); |
| 3500 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3501 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3502 | << "mode" << Attr.getRange(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3503 | return; |
| 3504 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3505 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3506 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3507 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3508 | else if (IntegerMode) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3509 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3510 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3511 | } else if (ComplexMode) { |
| 3512 | if (!OldTy->isComplexType()) |
| 3513 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3514 | } else { |
| 3515 | if (!OldTy->isFloatingType()) |
| 3516 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3517 | } |
| 3518 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3519 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3520 | // and friends, at least with glibc. |
| 3521 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 3522 | // width on unusual platforms. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3523 | // FIXME: Make sure floating-point mappings are accurate |
| 3524 | // FIXME: Support XF and TF types |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3525 | QualType NewTy; |
| 3526 | switch (DestWidth) { |
| 3527 | case 0: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3528 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3529 | return; |
| 3530 | default: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3531 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3532 | return; |
| 3533 | case 8: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3534 | if (!IntegerMode) { |
| 3535 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3536 | return; |
| 3537 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3538 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3539 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3540 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3541 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3542 | break; |
| 3543 | case 16: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3544 | if (!IntegerMode) { |
| 3545 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3546 | return; |
| 3547 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3548 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3549 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3550 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3551 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3552 | break; |
| 3553 | case 32: |
| 3554 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3555 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3556 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3557 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3558 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3559 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3560 | break; |
| 3561 | case 64: |
| 3562 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3563 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3564 | else if (OldTy->isSignedIntegerType()) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3565 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3566 | NewTy = S.Context.LongTy; |
| 3567 | else |
| 3568 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3569 | else |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3570 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3571 | NewTy = S.Context.UnsignedLongTy; |
| 3572 | else |
| 3573 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3574 | break; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3575 | case 96: |
| 3576 | NewTy = S.Context.LongDoubleTy; |
| 3577 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3578 | case 128: |
Roman Divacky | 1964554 | 2013-07-03 21:08:41 +0000 | [diff] [blame] | 3579 | if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() != |
| 3580 | &llvm::APFloat::PPCDoubleDouble) { |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3581 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3582 | return; |
| 3583 | } |
Roman Divacky | f0d14cb | 2013-07-03 20:48:06 +0000 | [diff] [blame] | 3584 | if (IntegerMode) { |
| 3585 | if (OldTy->isSignedIntegerType()) |
| 3586 | NewTy = S.Context.Int128Ty; |
| 3587 | else |
| 3588 | NewTy = S.Context.UnsignedInt128Ty; |
| 3589 | } else |
| 3590 | NewTy = S.Context.LongDoubleTy; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3591 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3594 | if (ComplexMode) { |
| 3595 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3596 | } |
| 3597 | |
| 3598 | // Install the new type. |
Enea Zaffanella | c2fa6b6 | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3599 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3600 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 3601 | else |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3602 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | c2fa6b6 | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3603 | |
| 3604 | D->addAttr(::new (S.Context) |
| 3605 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 3606 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3607 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3608 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3609 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3610 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3611 | if (!VD->hasGlobalStorage()) |
| 3612 | S.Diag(Attr.getLoc(), |
| 3613 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3614 | << Attr.getName(); |
| 3615 | } else if (!isFunctionOrMethod(D)) { |
| 3616 | S.Diag(Attr.getLoc(), |
| 3617 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3618 | << Attr.getName(); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3619 | return; |
| 3620 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3621 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3622 | D->addAttr(::new (S.Context) |
| 3623 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3624 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3625 | } |
| 3626 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3627 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3628 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3629 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3630 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3631 | return; |
| 3632 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3633 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3634 | D->addAttr(::new (S.Context) |
| 3635 | NoInlineAttr(Attr.getRange(), S.Context, |
| 3636 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3637 | } |
| 3638 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3639 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 3640 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3641 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3642 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3643 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3644 | return; |
| 3645 | } |
| 3646 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3647 | D->addAttr(::new (S.Context) |
| 3648 | NoInstrumentFunctionAttr(Attr.getRange(), S.Context, |
| 3649 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3650 | } |
| 3651 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3652 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3653 | if (S.LangOpts.CUDA) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3654 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3655 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3656 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3657 | return; |
| 3658 | } |
| 3659 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3660 | D->addAttr(::new (S.Context) |
| 3661 | CUDAConstantAttr(Attr.getRange(), S.Context, |
| 3662 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3663 | } else { |
| 3664 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 3665 | } |
| 3666 | } |
| 3667 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3668 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3669 | if (S.LangOpts.CUDA) { |
| 3670 | // check the attribute arguments. |
| 3671 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3672 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3673 | << Attr.getName() << 0; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3674 | return; |
| 3675 | } |
| 3676 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3677 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3678 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3679 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3680 | return; |
| 3681 | } |
| 3682 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3683 | D->addAttr(::new (S.Context) |
| 3684 | CUDADeviceAttr(Attr.getRange(), S.Context, |
| 3685 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3686 | } else { |
| 3687 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 3688 | } |
| 3689 | } |
| 3690 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3691 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3692 | if (S.LangOpts.CUDA) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3693 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3694 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3695 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3696 | return; |
| 3697 | } |
| 3698 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3699 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3700 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 3701 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3702 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3703 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3704 | << FD->getType() |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3705 | << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(), |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3706 | "void"); |
| 3707 | } else { |
| 3708 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3709 | << FD->getType(); |
| 3710 | } |
| 3711 | return; |
| 3712 | } |
| 3713 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3714 | D->addAttr(::new (S.Context) |
| 3715 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
| 3716 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3717 | } else { |
| 3718 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 3719 | } |
| 3720 | } |
| 3721 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3722 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3723 | if (S.LangOpts.CUDA) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3724 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3725 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3726 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3727 | return; |
| 3728 | } |
| 3729 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3730 | D->addAttr(::new (S.Context) |
| 3731 | CUDAHostAttr(Attr.getRange(), S.Context, |
| 3732 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3733 | } else { |
| 3734 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 3735 | } |
| 3736 | } |
| 3737 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3738 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3739 | if (S.LangOpts.CUDA) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3740 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3741 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3742 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3743 | return; |
| 3744 | } |
| 3745 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3746 | D->addAttr(::new (S.Context) |
| 3747 | CUDASharedAttr(Attr.getRange(), S.Context, |
| 3748 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3749 | } else { |
| 3750 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 3751 | } |
| 3752 | } |
| 3753 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3754 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3755 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3756 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3757 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3758 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3759 | return; |
| 3760 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3761 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3762 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3763 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3764 | return; |
| 3765 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3766 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3767 | D->addAttr(::new (S.Context) |
| 3768 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3769 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3770 | } |
| 3771 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3772 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3773 | if (hasDeclarator(D)) return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3774 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3775 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3776 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3777 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3778 | CallingConv CC; |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3779 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3780 | return; |
| 3781 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3782 | if (!isa<ObjCMethodDecl>(D)) { |
| 3783 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3784 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3785 | return; |
| 3786 | } |
| 3787 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3788 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3789 | case AttributeList::AT_FastCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3790 | D->addAttr(::new (S.Context) |
| 3791 | FastCallAttr(Attr.getRange(), S.Context, |
| 3792 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3793 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3794 | case AttributeList::AT_StdCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3795 | D->addAttr(::new (S.Context) |
| 3796 | StdCallAttr(Attr.getRange(), S.Context, |
| 3797 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3798 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3799 | case AttributeList::AT_ThisCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3800 | D->addAttr(::new (S.Context) |
| 3801 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3802 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 04633eb | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3803 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3804 | case AttributeList::AT_CDecl: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3805 | D->addAttr(::new (S.Context) |
| 3806 | CDeclAttr(Attr.getRange(), S.Context, |
| 3807 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3808 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3809 | case AttributeList::AT_Pascal: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3810 | D->addAttr(::new (S.Context) |
| 3811 | PascalAttr(Attr.getRange(), S.Context, |
| 3812 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3813 | return; |
Charles Davis | e8519c3 | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3814 | case AttributeList::AT_MSABI: |
| 3815 | D->addAttr(::new (S.Context) |
| 3816 | MSABIAttr(Attr.getRange(), S.Context, |
| 3817 | Attr.getAttributeSpellingListIndex())); |
| 3818 | return; |
| 3819 | case AttributeList::AT_SysVABI: |
| 3820 | D->addAttr(::new (S.Context) |
| 3821 | SysVABIAttr(Attr.getRange(), S.Context, |
| 3822 | Attr.getAttributeSpellingListIndex())); |
| 3823 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3824 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3825 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3826 | switch (CC) { |
| 3827 | case CC_AAPCS: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3828 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3829 | break; |
| 3830 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3831 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3832 | break; |
| 3833 | default: |
| 3834 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3835 | } |
| 3836 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3837 | D->addAttr(::new (S.Context) |
| 3838 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3839 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3840 | return; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3841 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3842 | case AttributeList::AT_PnaclCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3843 | D->addAttr(::new (S.Context) |
| 3844 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 3845 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3846 | return; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3847 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3848 | D->addAttr(::new (S.Context) |
| 3849 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3850 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3851 | return; |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3852 | |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3853 | default: |
| 3854 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3855 | } |
| 3856 | } |
| 3857 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3858 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3859 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3862 | static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3863 | Expr *E = Attr.getArgAsExpr(0); |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3864 | llvm::APSInt ArgNum(32); |
| 3865 | if (E->isTypeDependent() || E->isValueDependent() || |
| 3866 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 3867 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3868 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3869 | << E->getSourceRange(); |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3870 | return; |
| 3871 | } |
| 3872 | |
| 3873 | D->addAttr(::new (S.Context) OpenCLImageAccessAttr( |
| 3874 | Attr.getRange(), S.Context, ArgNum.getZExtValue())); |
| 3875 | } |
| 3876 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3877 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3878 | const FunctionDecl *FD) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3879 | if (attr.isInvalid()) |
| 3880 | return true; |
| 3881 | |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3882 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3883 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3884 | attr.setInvalid(); |
| 3885 | return true; |
| 3886 | } |
| 3887 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3888 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 3889 | // move to TargetAttributesSema one day. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3890 | switch (attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3891 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3892 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3893 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3894 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3895 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
Charles Davis | e8519c3 | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3896 | case AttributeList::AT_MSABI: |
| 3897 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 3898 | CC_X86_64Win64; |
| 3899 | break; |
| 3900 | case AttributeList::AT_SysVABI: |
| 3901 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 3902 | CC_C; |
| 3903 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3904 | case AttributeList::AT_Pcs: { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3905 | StringLiteral *Str = 0; |
| 3906 | if (attr.isArgExpr(0)) |
| 3907 | Str = dyn_cast<StringLiteral>(attr.getArgAsExpr(0)); |
| 3908 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3909 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3910 | Diag(attr.getLoc(), diag::err_attribute_argument_type) << attr.getName() |
| 3911 | << AANT_ArgumentString; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3912 | attr.setInvalid(); |
| 3913 | return true; |
| 3914 | } |
| 3915 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3916 | StringRef StrRef = Str->getString(); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3917 | if (StrRef == "aapcs") { |
| 3918 | CC = CC_AAPCS; |
| 3919 | break; |
| 3920 | } else if (StrRef == "aapcs-vfp") { |
| 3921 | CC = CC_AAPCS_VFP; |
| 3922 | break; |
| 3923 | } |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3924 | |
| 3925 | attr.setInvalid(); |
| 3926 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3927 | return true; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3928 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3929 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3930 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3931 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3934 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3935 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 3936 | if (A == TargetInfo::CCCR_Warning) { |
| 3937 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3938 | |
| 3939 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3940 | if (FD) |
| 3941 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3942 | TargetInfo::CCMT_NonMember; |
| 3943 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3946 | return false; |
| 3947 | } |
| 3948 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3949 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3950 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3951 | |
| 3952 | unsigned numParams; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3953 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3954 | return; |
| 3955 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3956 | if (!isa<ObjCMethodDecl>(D)) { |
| 3957 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3958 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3959 | return; |
| 3960 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3961 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3962 | D->addAttr(::new (S.Context) |
| 3963 | RegparmAttr(Attr.getRange(), S.Context, numParams, |
| 3964 | Attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3965 | } |
| 3966 | |
| 3967 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3968 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3969 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3970 | if (Attr.isInvalid()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3971 | return true; |
| 3972 | |
Aaron Ballman | ffa9d57 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3973 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3974 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3975 | return true; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3976 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3977 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3978 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3979 | llvm::APSInt NumParams(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3980 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3981 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 3982 | Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3983 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3984 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3985 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3986 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3987 | } |
| 3988 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3989 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3990 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3991 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3992 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3993 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3994 | } |
| 3995 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3996 | numParams = NumParams.getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3997 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3998 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3999 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4000 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4001 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4004 | return false; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4005 | } |
| 4006 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4007 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4008 | if (S.LangOpts.CUDA) { |
| 4009 | // check the attribute arguments. |
| 4010 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 4011 | // FIXME: 0 is not okay. |
| 4012 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4013 | return; |
| 4014 | } |
| 4015 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4016 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4017 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4018 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4019 | return; |
| 4020 | } |
| 4021 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4022 | Expr *MaxThreadsExpr = Attr.getArgAsExpr(0); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4023 | llvm::APSInt MaxThreads(32); |
| 4024 | if (MaxThreadsExpr->isTypeDependent() || |
| 4025 | MaxThreadsExpr->isValueDependent() || |
| 4026 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4027 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4028 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4029 | << MaxThreadsExpr->getSourceRange(); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4030 | return; |
| 4031 | } |
| 4032 | |
| 4033 | llvm::APSInt MinBlocks(32); |
| 4034 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4035 | Expr *MinBlocksExpr = Attr.getArgAsExpr(1); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4036 | if (MinBlocksExpr->isTypeDependent() || |
| 4037 | MinBlocksExpr->isValueDependent() || |
| 4038 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4039 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4040 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4041 | << MinBlocksExpr->getSourceRange(); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4042 | return; |
| 4043 | } |
| 4044 | } |
| 4045 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4046 | D->addAttr(::new (S.Context) |
| 4047 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 4048 | MaxThreads.getZExtValue(), |
| 4049 | MinBlocks.getZExtValue(), |
| 4050 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4051 | } else { |
| 4052 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 4053 | } |
| 4054 | } |
| 4055 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4056 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 4057 | const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4058 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4059 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4060 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4061 | return; |
| 4062 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4063 | |
| 4064 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4065 | return; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4066 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4067 | StringRef AttrName = Attr.getName()->getName(); |
| 4068 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4069 | |
| 4070 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 4071 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4072 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 4073 | return; |
| 4074 | } |
| 4075 | |
| 4076 | uint64_t ArgumentIdx; |
| 4077 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4078 | Attr.getLoc(), 2, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4079 | Attr.getArgAsExpr(1), ArgumentIdx)) |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4080 | return; |
| 4081 | |
| 4082 | uint64_t TypeTagIdx; |
| 4083 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4084 | Attr.getLoc(), 3, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4085 | Attr.getArgAsExpr(2), TypeTagIdx)) |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4086 | return; |
| 4087 | |
| 4088 | bool IsPointer = (AttrName == "pointer_with_type_tag"); |
| 4089 | if (IsPointer) { |
| 4090 | // Ensure that buffer has a pointer type. |
| 4091 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 4092 | if (!BufferTy->isPointerType()) { |
| 4093 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4094 | << Attr.getName(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4095 | } |
| 4096 | } |
| 4097 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4098 | D->addAttr(::new (S.Context) |
| 4099 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 4100 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 4101 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 4105 | const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4106 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4107 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4108 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4109 | return; |
| 4110 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4111 | |
| 4112 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4113 | return; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4114 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4115 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4116 | QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL); |
| 4117 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4118 | D->addAttr(::new (S.Context) |
| 4119 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
| 4120 | MatchingCType, |
| 4121 | Attr.getLayoutCompatible(), |
| 4122 | Attr.getMustBeNull(), |
| 4123 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4126 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4127 | // Checker-specific attribute handlers. |
| 4128 | //===----------------------------------------------------------------------===// |
| 4129 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4130 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4131 | return type->isDependentType() || |
| 4132 | type->isObjCObjectPointerType() || |
| 4133 | S.Context.isObjCNSObjectType(type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4134 | } |
| 4135 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4136 | return type->isDependentType() || |
| 4137 | type->isPointerType() || |
| 4138 | isValidSubjectOfNSAttribute(S, type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4141 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4142 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4143 | if (!param) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4144 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4145 | << Attr.getRange() << Attr.getName() << ExpectedParameter; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4146 | return; |
| 4147 | } |
| 4148 | |
| 4149 | bool typeOK, cf; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4150 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4151 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 4152 | cf = false; |
| 4153 | } else { |
| 4154 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 4155 | cf = true; |
| 4156 | } |
| 4157 | |
| 4158 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4159 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4160 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4161 | return; |
| 4162 | } |
| 4163 | |
| 4164 | if (cf) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4165 | param->addAttr(::new (S.Context) |
| 4166 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 4167 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4168 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4169 | param->addAttr(::new (S.Context) |
| 4170 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 4171 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4172 | } |
| 4173 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4174 | static void handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 4175 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4176 | if (!isa<ObjCMethodDecl>(D)) { |
| 4177 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4178 | << Attr.getRange() << Attr.getName() << ExpectedMethod; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4179 | return; |
| 4180 | } |
| 4181 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4182 | D->addAttr(::new (S.Context) |
| 4183 | NSConsumesSelfAttr(Attr.getRange(), S.Context, |
| 4184 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4187 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 4188 | const AttributeList &Attr) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4189 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4190 | QualType returnType; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4191 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4192 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4193 | returnType = MD->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4194 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4195 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4196 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | a23bd4c | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 4197 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4198 | returnType = PD->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4199 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4200 | returnType = FD->getResultType(); |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4201 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4202 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4203 | << Attr.getRange() << Attr.getName() |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4204 | << ExpectedFunctionOrMethod; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4205 | return; |
| 4206 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4207 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4208 | bool typeOK; |
| 4209 | bool cf; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4210 | switch (Attr.getKind()) { |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4211 | default: llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4212 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4213 | case AttributeList::AT_NSReturnsRetained: |
| 4214 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4215 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 4216 | cf = false; |
| 4217 | break; |
| 4218 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4219 | case AttributeList::AT_CFReturnsRetained: |
| 4220 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4221 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 4222 | cf = true; |
| 4223 | break; |
| 4224 | } |
| 4225 | |
| 4226 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4227 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4228 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4229 | return; |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4230 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4231 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4232 | switch (Attr.getKind()) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4233 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4234 | llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4235 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4236 | D->addAttr(::new (S.Context) |
| 4237 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 4238 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4239 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4240 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4241 | D->addAttr(::new (S.Context) |
| 4242 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4243 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4244 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4245 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4246 | D->addAttr(::new (S.Context) |
| 4247 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4248 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4249 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4250 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4251 | D->addAttr(::new (S.Context) |
| 4252 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4253 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4254 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4255 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4256 | D->addAttr(::new (S.Context) |
| 4257 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4258 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4259 | return; |
| 4260 | }; |
| 4261 | } |
| 4262 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4263 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4264 | const AttributeList &attr) { |
| 4265 | SourceLocation loc = attr.getLoc(); |
| 4266 | |
| 4267 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4268 | |
Fariborz Jahanian | 94d55d7 | 2012-04-21 17:51:44 +0000 | [diff] [blame] | 4269 | if (!method) { |
Fariborz Jahanian | 0e78afb | 2012-04-20 22:00:46 +0000 | [diff] [blame] | 4270 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4271 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4272 | return; |
| 4273 | } |
| 4274 | |
| 4275 | // Check that the method returns a normal pointer. |
| 4276 | QualType resultType = method->getResultType(); |
Fariborz Jahanian | f2e5945 | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4277 | |
| 4278 | if (!resultType->isReferenceType() && |
| 4279 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4280 | S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 4281 | << SourceRange(loc) |
| 4282 | << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2; |
| 4283 | |
| 4284 | // Drop the attribute. |
| 4285 | return; |
| 4286 | } |
| 4287 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4288 | method->addAttr(::new (S.Context) |
| 4289 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 4290 | attr.getAttributeSpellingListIndex())); |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4291 | } |
| 4292 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4293 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4294 | const AttributeList &attr) { |
| 4295 | SourceLocation loc = attr.getLoc(); |
| 4296 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4297 | |
| 4298 | if (!method) { |
| 4299 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 4300 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
| 4301 | return; |
| 4302 | } |
| 4303 | DeclContext *DC = method->getDeclContext(); |
| 4304 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4305 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4306 | << attr.getName() << 0; |
| 4307 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4308 | return; |
| 4309 | } |
| 4310 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4311 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4312 | << attr.getName() << 1; |
| 4313 | return; |
| 4314 | } |
| 4315 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4316 | method->addAttr(::new (S.Context) |
| 4317 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 4318 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4319 | } |
| 4320 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4321 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 4322 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
| 4323 | if (!isa<FunctionDecl>(D)) { |
| 4324 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4325 | << A.getRange() << A.getName() << ExpectedFunction; |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4326 | return; |
| 4327 | } |
| 4328 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4329 | bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4330 | |
| 4331 | // Check whether there's a conflicting attribute already present. |
| 4332 | Attr *Existing; |
| 4333 | if (IsAudited) { |
| 4334 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 4335 | } else { |
| 4336 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 4337 | } |
| 4338 | if (Existing) { |
| 4339 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 4340 | << A.getName() |
| 4341 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 4342 | << A.getRange() << Existing->getRange(); |
| 4343 | return; |
| 4344 | } |
| 4345 | |
| 4346 | // All clear; add the attribute. |
| 4347 | if (IsAudited) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4348 | D->addAttr(::new (S.Context) |
| 4349 | CFAuditedTransferAttr(A.getRange(), S.Context, |
| 4350 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4351 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4352 | D->addAttr(::new (S.Context) |
| 4353 | CFUnknownTransferAttr(A.getRange(), S.Context, |
| 4354 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4355 | } |
| 4356 | } |
| 4357 | |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4358 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4359 | const AttributeList &Attr) { |
| 4360 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4361 | if (!RD || RD->isUnion()) { |
| 4362 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4363 | << Attr.getRange() << Attr.getName() << ExpectedStruct; |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4364 | } |
| 4365 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4366 | IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4367 | |
| 4368 | // In Objective-C, verify that the type names an Objective-C type. |
| 4369 | // We don't want to check this outside of ObjC because people sometimes |
| 4370 | // do crazy C declarations of Objective-C types. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4371 | if (Parm && S.getLangOpts().ObjC1) { |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4372 | // Check for an existing type with this name. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4373 | LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc, |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4374 | Sema::LookupOrdinaryName); |
| 4375 | if (S.LookupName(R, Sc)) { |
| 4376 | NamedDecl *Target = R.getFoundDecl(); |
| 4377 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 4378 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 4379 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 4380 | } |
| 4381 | } |
| 4382 | } |
| 4383 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4384 | D->addAttr(::new (S.Context) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4385 | NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4386 | Attr.getAttributeSpellingListIndex())); |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4387 | } |
| 4388 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4389 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4390 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4391 | if (hasDeclarator(D)) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4392 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4393 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4394 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4395 | } |
| 4396 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4397 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4398 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4399 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4400 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4401 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4402 | return; |
| 4403 | } |
| 4404 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4405 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4406 | QualType type = vd->getType(); |
| 4407 | |
| 4408 | if (!type->isDependentType() && |
| 4409 | !type->isObjCLifetimeType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4410 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4411 | << type; |
| 4412 | return; |
| 4413 | } |
| 4414 | |
| 4415 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4416 | |
| 4417 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4418 | // going to infer. |
| 4419 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4420 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4421 | |
| 4422 | switch (lifetime) { |
| 4423 | case Qualifiers::OCL_None: |
| 4424 | assert(type->isDependentType() && |
| 4425 | "didn't infer lifetime for non-dependent type?"); |
| 4426 | break; |
| 4427 | |
| 4428 | case Qualifiers::OCL_Weak: // meaningful |
| 4429 | case Qualifiers::OCL_Strong: // meaningful |
| 4430 | break; |
| 4431 | |
| 4432 | case Qualifiers::OCL_ExplicitNone: |
| 4433 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4434 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4435 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4436 | break; |
| 4437 | } |
| 4438 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4439 | D->addAttr(::new (S.Context) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4440 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 4441 | Attr.getAttributeSpellingListIndex())); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4444 | //===----------------------------------------------------------------------===// |
| 4445 | // Microsoft specific attribute handlers. |
| 4446 | //===----------------------------------------------------------------------===// |
| 4447 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4448 | // Check if MS extensions or some other language extensions are enabled. If |
| 4449 | // not, issue a diagnostic that the given attribute is unused. |
| 4450 | static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr, |
| 4451 | bool OtherExtension = false) { |
| 4452 | if (S.LangOpts.MicrosoftExt || OtherExtension) |
| 4453 | return true; |
| 4454 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4455 | return false; |
| 4456 | } |
| 4457 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4458 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4459 | if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland)) |
| 4460 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4461 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4462 | Expr *Arg = Attr.getArgAsExpr(0); |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4463 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 4464 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4465 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 4466 | << Attr.getName() << AANT_ArgumentString; |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4467 | return; |
| 4468 | } |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4469 | |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4470 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 4471 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4472 | StringRef StrRef = Str->getString(); |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4473 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 4474 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4475 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4476 | // Validate GUID length. |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4477 | if (StrRef.size() != 36) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4478 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4479 | return; |
| 4480 | } |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4481 | |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4482 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4483 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4484 | if (StrRef[i] != '-') { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4485 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4486 | return; |
| 4487 | } |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4488 | } else if (!isHexDigit(StrRef[i])) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4489 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4490 | return; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4491 | } |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4492 | } |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4493 | |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4494 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef, |
| 4495 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4496 | } |
| 4497 | |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4498 | static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4499 | if (!checkMicrosoftExt(S, Attr)) |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4500 | return; |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4501 | |
| 4502 | AttributeList::Kind Kind = Attr.getKind(); |
| 4503 | if (Kind == AttributeList::AT_SingleInheritance) |
| 4504 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4505 | ::new (S.Context) |
| 4506 | SingleInheritanceAttr(Attr.getRange(), S.Context, |
| 4507 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4508 | else if (Kind == AttributeList::AT_MultipleInheritance) |
| 4509 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4510 | ::new (S.Context) |
| 4511 | MultipleInheritanceAttr(Attr.getRange(), S.Context, |
| 4512 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4513 | else if (Kind == AttributeList::AT_VirtualInheritance) |
| 4514 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4515 | ::new (S.Context) |
| 4516 | VirtualInheritanceAttr(Attr.getRange(), S.Context, |
| 4517 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4518 | } |
| 4519 | |
| 4520 | static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4521 | if (!checkMicrosoftExt(S, Attr)) |
| 4522 | return; |
| 4523 | |
| 4524 | AttributeList::Kind Kind = Attr.getKind(); |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4525 | if (Kind == AttributeList::AT_Win64) |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4526 | D->addAttr( |
| 4527 | ::new (S.Context) Win64Attr(Attr.getRange(), S.Context, |
| 4528 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4529 | } |
| 4530 | |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4531 | static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4532 | if (!checkMicrosoftExt(S, Attr)) |
| 4533 | return; |
| 4534 | D->addAttr(::new (S.Context) |
| 4535 | ForceInlineAttr(Attr.getRange(), S.Context, |
| 4536 | Attr.getAttributeSpellingListIndex())); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4537 | } |
| 4538 | |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4539 | static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4540 | if (!checkMicrosoftExt(S, Attr)) |
| 4541 | return; |
| 4542 | // Check linkage after possibly merging declaratinos. See |
| 4543 | // checkAttributesAfterMerging(). |
| 4544 | D->addAttr(::new (S.Context) |
| 4545 | SelectAnyAttr(Attr.getRange(), S.Context, |
| 4546 | Attr.getAttributeSpellingListIndex())); |
| 4547 | } |
| 4548 | |
Aaron Ballman | bbb3b32 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4549 | /// Handles semantic checking for features that are common to all attributes, |
| 4550 | /// such as checking whether a parameter was properly specified, or the correct |
| 4551 | /// number of arguments were passed, etc. |
| 4552 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 4553 | const AttributeList &Attr) { |
| 4554 | // Several attributes carry different semantics than the parsing requires, so |
| 4555 | // those are opted out of the common handling. |
| 4556 | // |
| 4557 | // We also bail on unknown and ignored attributes because those are handled |
| 4558 | // as part of the target-specific handling logic. |
| 4559 | if (Attr.hasCustomParsing() || |
| 4560 | Attr.getKind() == AttributeList::UnknownAttribute || |
| 4561 | Attr.getKind() == AttributeList::IgnoredAttribute) |
| 4562 | return false; |
| 4563 | |
| 4564 | // If there are no optional arguments, then checking for the argument count |
| 4565 | // is trivial. |
| 4566 | if (Attr.getMinArgs() == Attr.getMaxArgs() && |
| 4567 | !checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 4568 | return true; |
| 4569 | return false; |
| 4570 | } |
| 4571 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4572 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4573 | // Top Level Sema Entry Points |
| 4574 | //===----------------------------------------------------------------------===// |
| 4575 | |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4576 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4577 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 4578 | /// silently ignore it if a GNU attribute. |
| 4579 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4580 | const AttributeList &Attr, |
| 4581 | bool IncludeCXX11Attributes) { |
| 4582 | if (Attr.isInvalid()) |
| 4583 | return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4584 | |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4585 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 4586 | // instead. |
| 4587 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 4588 | return; |
| 4589 | |
Aaron Ballman | bbb3b32 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4590 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 4591 | return; |
| 4592 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4593 | switch (Attr.getKind()) { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4594 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 4595 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 4596 | case AttributeList::AT_IBOutletCollection: |
| 4597 | handleIBOutletCollection(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4598 | case AttributeList::AT_AddressSpace: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4599 | case AttributeList::AT_ObjCGC: |
| 4600 | case AttributeList::AT_VectorSize: |
| 4601 | case AttributeList::AT_NeonVectorType: |
| 4602 | case AttributeList::AT_NeonPolyVectorType: |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4603 | case AttributeList::AT_Ptr32: |
| 4604 | case AttributeList::AT_Ptr64: |
| 4605 | case AttributeList::AT_SPtr: |
| 4606 | case AttributeList::AT_UPtr: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4607 | // Ignore these, these are type attributes, handled by |
| 4608 | // ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4609 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4610 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 4611 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 4612 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 4613 | case AttributeList::AT_AlwaysInline: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4614 | handleAlwaysInlineAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4615 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4616 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 4617 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4618 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 4619 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 4620 | case AttributeList::AT_CarriesDependency: |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4621 | handleDependencyAttr(S, scope, D, Attr); |
| 4622 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4623 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
| 4624 | case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break; |
| 4625 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4626 | case AttributeList::AT_CXX11NoReturn: |
| 4627 | handleCXX11NoReturnAttr(S, D, Attr); |
| 4628 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4629 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4630 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4631 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4632 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 4633 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4634 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4635 | break; |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4636 | case AttributeList::AT_MinSize: |
| 4637 | handleMinSizeAttr(S, D, Attr); |
| 4638 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4639 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4640 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4641 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
Aaron Ballman | 1951323 | 2013-08-28 23:13:26 +0000 | [diff] [blame] | 4642 | case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break; |
| 4643 | case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4644 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4645 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4646 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4647 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4648 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
| 4649 | case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break; |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4650 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4651 | case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break; |
| 4652 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4653 | case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4654 | case AttributeList::AT_ownership_returns: |
| 4655 | case AttributeList::AT_ownership_takes: |
| 4656 | case AttributeList::AT_ownership_holds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4657 | handleOwnershipAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4658 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4659 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
| 4660 | case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break; |
| 4661 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
| 4662 | case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break; |
| 4663 | case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break; |
| 4664 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4665 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4666 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4667 | handleObjCOwnershipAttr(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4668 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4669 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4670 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4671 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4672 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4673 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4674 | case AttributeList::AT_ObjCRequiresSuper: |
| 4675 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4676 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4677 | case AttributeList::AT_NSBridged: |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4678 | handleNSBridgedAttr(S, scope, D, Attr); break; |
| 4679 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4680 | case AttributeList::AT_CFAuditedTransfer: |
| 4681 | case AttributeList::AT_CFUnknownTransfer: |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4682 | handleCFTransferAttr(S, D, Attr); break; |
| 4683 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4684 | // Checker-specific. |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4685 | case AttributeList::AT_CFConsumed: |
| 4686 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4687 | case AttributeList::AT_NSConsumesSelf: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4688 | handleNSConsumesSelfAttr(S, D, Attr); break; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4689 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4690 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4691 | case AttributeList::AT_NSReturnsNotRetained: |
| 4692 | case AttributeList::AT_CFReturnsNotRetained: |
| 4693 | case AttributeList::AT_NSReturnsRetained: |
| 4694 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4695 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4696 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4697 | case AttributeList::AT_WorkGroupSizeHint: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4698 | case AttributeList::AT_ReqdWorkGroupSize: |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4699 | handleWorkGroupSize(S, D, Attr); break; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 4700 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4701 | case AttributeList::AT_VecTypeHint: |
| 4702 | handleVecTypeHint(S, D, Attr); break; |
| 4703 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4704 | case AttributeList::AT_InitPriority: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4705 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4706 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4707 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4708 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4709 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4710 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4711 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4712 | case AttributeList::AT_ArcWeakrefUnavailable: |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 4713 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 4714 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4715 | case AttributeList::AT_ObjCRootClass: |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4716 | handleObjCRootClassAttr(S, D, Attr); |
| 4717 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4718 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 4719 | handleObjCRequiresPropertyDefsAttr (S, D, Attr); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 4720 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4721 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4722 | case AttributeList::AT_ReturnsTwice: |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 4723 | handleReturnsTwiceAttr(S, D, Attr); |
| 4724 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4725 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4726 | case AttributeList::AT_Visibility: |
| 4727 | handleVisibilityAttr(S, D, Attr, false); |
| 4728 | break; |
| 4729 | case AttributeList::AT_TypeVisibility: |
| 4730 | handleVisibilityAttr(S, D, Attr, true); |
| 4731 | break; |
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 4732 | case AttributeList::AT_WarnUnused: |
| 4733 | handleWarnUnusedAttr(S, D, Attr); |
| 4734 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4735 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4736 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4737 | case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; |
| 4738 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4739 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4740 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4741 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4742 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4743 | case AttributeList::AT_ObjCException: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4744 | handleObjCExceptionAttr(S, D, Attr); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 4745 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4746 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4747 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4748 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4749 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4750 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4751 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
| 4752 | case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break; |
| 4753 | case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break; |
| 4754 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4755 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
| 4756 | case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break; |
| 4757 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4758 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4759 | // Just ignore |
| 4760 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4761 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4762 | handleNoInstrumentFunctionAttr(S, D, Attr); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 4763 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4764 | case AttributeList::AT_StdCall: |
| 4765 | case AttributeList::AT_CDecl: |
| 4766 | case AttributeList::AT_FastCall: |
| 4767 | case AttributeList::AT_ThisCall: |
| 4768 | case AttributeList::AT_Pascal: |
Charles Davis | e8519c3 | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4769 | case AttributeList::AT_MSABI: |
| 4770 | case AttributeList::AT_SysVABI: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4771 | case AttributeList::AT_Pcs: |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4772 | case AttributeList::AT_PnaclCall: |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4773 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4774 | handleCallConvAttr(S, D, Attr); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4775 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4776 | case AttributeList::AT_OpenCLKernel: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4777 | handleOpenCLKernelAttr(S, D, Attr); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4778 | break; |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4779 | case AttributeList::AT_OpenCLImageAccess: |
| 4780 | handleOpenCLImageAccessAttr(S, D, Attr); |
| 4781 | break; |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4782 | |
| 4783 | // Microsoft attributes: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4784 | case AttributeList::AT_MsStruct: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4785 | handleMsStructAttr(S, D, Attr); |
| 4786 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4787 | case AttributeList::AT_Uuid: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4788 | handleUuidAttr(S, D, Attr); |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4789 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4790 | case AttributeList::AT_SingleInheritance: |
| 4791 | case AttributeList::AT_MultipleInheritance: |
| 4792 | case AttributeList::AT_VirtualInheritance: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4793 | handleInheritanceAttr(S, D, Attr); |
| 4794 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4795 | case AttributeList::AT_Win64: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4796 | handlePortabilityAttr(S, D, Attr); |
| 4797 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4798 | case AttributeList::AT_ForceInline: |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4799 | handleForceInlineAttr(S, D, Attr); |
| 4800 | break; |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4801 | case AttributeList::AT_SelectAny: |
| 4802 | handleSelectAnyAttr(S, D, Attr); |
| 4803 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4804 | |
| 4805 | // Thread safety attributes: |
DeLesley Hutchins | 5c6134f | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 4806 | case AttributeList::AT_AssertExclusiveLock: |
| 4807 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 4808 | break; |
| 4809 | case AttributeList::AT_AssertSharedLock: |
| 4810 | handleAssertSharedLockAttr(S, D, Attr); |
| 4811 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4812 | case AttributeList::AT_GuardedVar: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4813 | handleGuardedVarAttr(S, D, Attr); |
| 4814 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4815 | case AttributeList::AT_PtGuardedVar: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4816 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4817 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4818 | case AttributeList::AT_ScopedLockable: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4819 | handleScopedLockableAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4820 | break; |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4821 | case AttributeList::AT_NoSanitizeAddress: |
| 4822 | handleNoSanitizeAddressAttr(S, D, Attr); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4823 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4824 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4825 | handleNoThreadSafetyAnalysis(S, D, Attr); |
| 4826 | break; |
| 4827 | case AttributeList::AT_NoSanitizeThread: |
| 4828 | handleNoSanitizeThread(S, D, Attr); |
| 4829 | break; |
| 4830 | case AttributeList::AT_NoSanitizeMemory: |
| 4831 | handleNoSanitizeMemory(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4832 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4833 | case AttributeList::AT_Lockable: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4834 | handleLockableAttr(S, D, Attr); |
| 4835 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4836 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4837 | handleGuardedByAttr(S, D, Attr); |
| 4838 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4839 | case AttributeList::AT_PtGuardedBy: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4840 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4841 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4842 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4843 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4844 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4845 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4846 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4847 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4848 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4849 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4850 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4851 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4852 | handleLockReturnedAttr(S, D, Attr); |
| 4853 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4854 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4855 | handleLocksExcludedAttr(S, D, Attr); |
| 4856 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4857 | case AttributeList::AT_SharedLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4858 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4859 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4860 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4861 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4862 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4863 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4864 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4865 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4866 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4867 | handleUnlockFunAttr(S, D, Attr); |
| 4868 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4869 | case AttributeList::AT_AcquiredBefore: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4870 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4871 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4872 | case AttributeList::AT_AcquiredAfter: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4873 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4874 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4875 | |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4876 | // Uniqueness analysis attributes. |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 4877 | case AttributeList::AT_Consumable: |
| 4878 | handleConsumableAttr(S, D, Attr); |
| 4879 | break; |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4880 | case AttributeList::AT_Consumes: |
| 4881 | handleConsumesAttr(S, D, Attr); |
| 4882 | break; |
| 4883 | case AttributeList::AT_CallableWhenUnconsumed: |
| 4884 | handleCallableWhenUnconsumedAttr(S, D, Attr); |
| 4885 | break; |
| 4886 | case AttributeList::AT_TestsConsumed: |
| 4887 | handleTestsConsumedAttr(S, D, Attr); |
| 4888 | break; |
| 4889 | case AttributeList::AT_TestsUnconsumed: |
| 4890 | handleTestsUnconsumedAttr(S, D, Attr); |
| 4891 | break; |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 4892 | case AttributeList::AT_ReturnTypestate: |
| 4893 | handleReturnTypestateAttr(S, D, Attr); |
| 4894 | break; |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4895 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4896 | // Type safety attributes. |
| 4897 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4898 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4899 | break; |
| 4900 | case AttributeList::AT_TypeTagForDatatype: |
| 4901 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4902 | break; |
| 4903 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4904 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4905 | // Ask target about the attribute. |
| 4906 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 4907 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4908 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 4909 | diag::warn_unhandled_ms_attribute_ignored : |
| 4910 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4911 | break; |
| 4912 | } |
| 4913 | } |
| 4914 | |
| 4915 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4916 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4917 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4918 | const AttributeList *AttrList, |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4919 | bool IncludeCXX11Attributes) { |
| 4920 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4921 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4922 | |
| 4923 | // GCC accepts |
| 4924 | // static int a9 __attribute__((weakref)); |
| 4925 | // but that looks really pointless. We reject it. |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4926 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4927 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Rafael Espindola | 4d8a33b | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4928 | cast<NamedDecl>(D)->getNameAsString(); |
| 4929 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4930 | return; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4931 | } |
| 4932 | } |
| 4933 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4934 | // Annotation attributes are the only attributes allowed after an access |
| 4935 | // specifier. |
| 4936 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4937 | const AttributeList *AttrList) { |
| 4938 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4939 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4940 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4941 | } else { |
| 4942 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4943 | return true; |
| 4944 | } |
| 4945 | } |
| 4946 | |
| 4947 | return false; |
| 4948 | } |
| 4949 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4950 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 4951 | /// contains any decl attributes that we should warn about. |
| 4952 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 4953 | for ( ; A; A = A->getNext()) { |
| 4954 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 4955 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4956 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 4957 | |
| 4958 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 4959 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 4960 | << A->getName() << A->getRange(); |
| 4961 | } else { |
| 4962 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 4963 | << A->getName() << A->getRange(); |
| 4964 | } |
| 4965 | } |
| 4966 | } |
| 4967 | |
| 4968 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 4969 | /// used to build a declaration, complain about any decl attributes |
| 4970 | /// which might be lying around on it. |
| 4971 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 4972 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 4973 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 4974 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 4975 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 4976 | } |
| 4977 | |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4978 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4979 | /// \#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] | 4980 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 4981 | SourceLocation Loc) { |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4982 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4983 | NamedDecl *NewD = 0; |
| 4984 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4985 | FunctionDecl *NewFD; |
| 4986 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 4987 | // FIXME: Mangling? |
| 4988 | // FIXME: Is the qualifier info correct? |
| 4989 | // FIXME: Is the DeclContext correct? |
| 4990 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 4991 | Loc, Loc, DeclarationName(II), |
| 4992 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4993 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4994 | FD->hasPrototype(), |
| 4995 | false/*isConstexprSpecified*/); |
| 4996 | NewD = NewFD; |
| 4997 | |
| 4998 | if (FD->getQualifier()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4999 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5000 | |
| 5001 | // Fake up parameter variables; they are declared as if this were |
| 5002 | // a typedef. |
| 5003 | QualType FDTy = FD->getType(); |
| 5004 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 5005 | SmallVector<ParmVarDecl*, 16> Params; |
| 5006 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 5007 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 5008 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 5009 | Param->setScopeInfo(0, Params.size()); |
| 5010 | Params.push_back(Param); |
| 5011 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 5012 | NewFD->setParams(Params); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5013 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5014 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 5015 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5016 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5017 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5018 | VD->getStorageClass()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5019 | if (VD->getQualifier()) { |
| 5020 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5021 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5022 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5023 | } |
| 5024 | return NewD; |
| 5025 | } |
| 5026 | |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5027 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5028 | /// applied to it, possibly with an alias. |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5029 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5030 | if (W.getUsed()) return; // only do this once |
| 5031 | W.setUsed(true); |
| 5032 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 5033 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5034 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5035 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 5036 | NDId->getName())); |
| 5037 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5038 | WeakTopLevelDecl.push_back(NewD); |
| 5039 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 5040 | // to insert Decl at TU scope, sorry. |
| 5041 | DeclContext *SavedContext = CurContext; |
| 5042 | CurContext = Context.getTranslationUnitDecl(); |
| 5043 | PushOnScopeChains(NewD, S); |
| 5044 | CurContext = SavedContext; |
| 5045 | } else { // just add weak to existing |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5046 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5047 | } |
| 5048 | } |
| 5049 | |
Rafael Espindola | 65611bf | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5050 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 5051 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 5052 | // have to do this. |
| 5053 | LoadExternalWeakUndeclaredIdentifiers(); |
| 5054 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 5055 | NamedDecl *ND = NULL; |
| 5056 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 5057 | if (VD->isExternC()) |
| 5058 | ND = VD; |
| 5059 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5060 | if (FD->isExternC()) |
| 5061 | ND = FD; |
| 5062 | if (ND) { |
| 5063 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 5064 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 5065 | = WeakUndeclaredIdentifiers.find(Id); |
| 5066 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 5067 | WeakInfo W = I->second; |
| 5068 | DeclApplyPragmaWeak(S, ND, W); |
| 5069 | WeakUndeclaredIdentifiers[Id] = W; |
| 5070 | } |
| 5071 | } |
| 5072 | } |
| 5073 | } |
| 5074 | } |
| 5075 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5076 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 5077 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 5078 | /// 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] | 5079 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5080 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5081 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5082 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5083 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5084 | // Walk the declarator structure, applying decl attributes that were in a type |
| 5085 | // position to the decl itself. This handles cases like: |
| 5086 | // int *__attr__(x)** D; |
| 5087 | // when X is a decl attribute. |
| 5088 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 5089 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5090 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5091 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5092 | // Finally, apply any attributes on the decl itself. |
| 5093 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5094 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5095 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5096 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5097 | /// Is the given declaration allowed to use a forbidden type? |
| 5098 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 5099 | // Private ivars are always okay. Unfortunately, people don't |
| 5100 | // always properly make their ivars private, even in system headers. |
| 5101 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | a6b3380 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 5102 | // Function declarations in sys headers will be marked unavailable. |
| 5103 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 5104 | !isa<FunctionDecl>(decl)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5105 | return false; |
| 5106 | |
| 5107 | // Require it to be declared in a system header. |
| 5108 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 5109 | } |
| 5110 | |
| 5111 | /// Handle a delayed forbidden-type diagnostic. |
| 5112 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 5113 | Decl *decl) { |
| 5114 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 5115 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 5116 | "this system declaration uses an unsupported type")); |
| 5117 | return; |
| 5118 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5119 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5120 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 5121 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5122 | // kind of forbidden type messages on unavailable functions. |
| 5123 | if (FD->hasAttr<UnavailableAttr>() && |
| 5124 | diag.getForbiddenTypeDiagnostic() == |
| 5125 | diag::err_arc_array_param_no_ownership) { |
| 5126 | diag.Triggered = true; |
| 5127 | return; |
| 5128 | } |
| 5129 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5130 | |
| 5131 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 5132 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 5133 | diag.Triggered = true; |
| 5134 | } |
| 5135 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5136 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 5137 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5138 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5139 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5140 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5141 | // When delaying diagnostics to run in the context of a parsed |
| 5142 | // declaration, we only want to actually emit anything if parsing |
| 5143 | // succeeds. |
| 5144 | if (!decl) return; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5145 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5146 | // We emit all the active diagnostics in this pool or any of its |
| 5147 | // parents. In general, we'll get one pool for the decl spec |
| 5148 | // and a child pool for each declarator; in a decl group like: |
| 5149 | // deprecated_typedef foo, *bar, baz(); |
| 5150 | // only the declarator pops will be passed decls. This is correct; |
| 5151 | // we really do need to consider delayed diagnostics from the decl spec |
| 5152 | // for each of the different declarations. |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5153 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5154 | do { |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5155 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5156 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 5157 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 5158 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5159 | if (diag.Triggered) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5160 | continue; |
| 5161 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5162 | switch (diag.Kind) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5163 | case DelayedDiagnostic::Deprecation: |
John McCall | e8c904f | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 5164 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 5165 | if (!decl->isInvalidDecl()) |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5166 | HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5167 | break; |
| 5168 | |
| 5169 | case DelayedDiagnostic::Access: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5170 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5171 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5172 | |
| 5173 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5174 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5175 | break; |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5176 | } |
| 5177 | } |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5178 | } while ((pool = pool->getParent())); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5179 | } |
| 5180 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5181 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 5182 | /// been delayed in the current context instead of in the given pool. |
| 5183 | /// Essentially, this just moves them to the current pool. |
| 5184 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 5185 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 5186 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 5187 | curPool->steal(pool); |
| 5188 | } |
| 5189 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5190 | static bool isDeclDeprecated(Decl *D) { |
| 5191 | do { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5192 | if (D->isDeprecated()) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5193 | return true; |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 5194 | // A category implicitly has the availability of the interface. |
| 5195 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 5196 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5197 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 5198 | return false; |
| 5199 | } |
| 5200 | |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5201 | static void |
| 5202 | DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message, |
| 5203 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5204 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5205 | const ObjCPropertyDecl *ObjCPropery) { |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5206 | DeclarationName Name = D->getDeclName(); |
| 5207 | if (!Message.empty()) { |
| 5208 | S.Diag(Loc, diag::warn_deprecated_message) << Name << Message; |
| 5209 | S.Diag(D->getLocation(), |
| 5210 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5211 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5212 | if (ObjCPropery) |
| 5213 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5214 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5215 | } else if (!UnknownObjCClass) { |
| 5216 | S.Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 5217 | S.Diag(D->getLocation(), |
| 5218 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5219 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5220 | if (ObjCPropery) |
| 5221 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5222 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5223 | } else { |
| 5224 | S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name; |
| 5225 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 5226 | } |
| 5227 | } |
| 5228 | |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 5229 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5230 | Decl *Ctx) { |
| 5231 | if (isDeclDeprecated(Ctx)) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5232 | return; |
| 5233 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5234 | DD.Triggered = true; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5235 | DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(), |
| 5236 | DD.getDeprecationMessage(), DD.Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5237 | DD.getUnknownObjCClass(), |
| 5238 | DD.getObjCProperty()); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5239 | } |
| 5240 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5241 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 5242 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5243 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5244 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5245 | // Delay if we're currently parsing a declaration. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5246 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5247 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 5248 | UnknownObjCClass, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5249 | ObjCProperty, |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5250 | Message)); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5251 | return; |
| 5252 | } |
| 5253 | |
| 5254 | // Otherwise, don't warn if our current context is deprecated. |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 5255 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5256 | return; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5257 | DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5258 | } |