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" |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 22 | #include "clang/Basic/CharInfo.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 25 | #include "clang/Sema/DeclSpec.h" |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 26 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 27 | #include "clang/Sema/Lookup.h" |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 28 | #include "clang/Sema/Scope.h" |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 30 | using namespace clang; |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 31 | using namespace sema; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 32 | |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 33 | /// These constants match the enumerated choices of |
| 34 | /// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 35 | enum AttributeDeclKind { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 36 | ExpectedFunction, |
| 37 | ExpectedUnion, |
| 38 | ExpectedVariableOrFunction, |
| 39 | ExpectedFunctionOrMethod, |
| 40 | ExpectedParameter, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 41 | ExpectedFunctionMethodOrBlock, |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 42 | ExpectedFunctionMethodOrClass, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 43 | ExpectedFunctionMethodOrParameter, |
| 44 | ExpectedClass, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 45 | ExpectedVariable, |
| 46 | ExpectedMethod, |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 47 | ExpectedVariableFunctionOrLabel, |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 48 | ExpectedFieldOrGlobalVar, |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 49 | ExpectedStruct, |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 50 | ExpectedVariableFunctionOrTag, |
Richard Smith | 5f838aa | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 51 | ExpectedTLSVar, |
| 52 | ExpectedVariableOrField, |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 53 | ExpectedVariableFieldOrTag, |
| 54 | ExpectedTypeOrNamespace |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 57 | //===----------------------------------------------------------------------===// |
| 58 | // Helper functions |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 61 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 62 | bool blocksToo = true) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 63 | QualType Ty; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 64 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 65 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 66 | else if (const FieldDecl *decl = dyn_cast<FieldDecl>(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 TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 69 | Ty = decl->getUnderlyingType(); |
| 70 | else |
| 71 | return 0; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 73 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 74 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 755f9d2 | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 75 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 76 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 77 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 78 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 81 | // FIXME: We should provide an abstraction around a method or function |
| 82 | // to provide the following bits of information. |
| 83 | |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 84 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 85 | /// type (function or function-typed variable). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 86 | static bool isFunction(const Decl *D) { |
| 87 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 91 | /// type (function or function-typed variable) or an Objective-C |
| 92 | /// method. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 93 | static bool isFunctionOrMethod(const Decl *D) { |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 94 | return isFunction(D) || isa<ObjCMethodDecl>(D); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 97 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 98 | /// type (function or function-typed variable) or an Objective-C |
| 99 | /// method or a block. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 100 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 101 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 102 | return true; |
| 103 | // check for block is more involved. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 104 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 105 | QualType Ty = V->getType(); |
| 106 | return Ty->isBlockPointerType(); |
| 107 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 108 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 109 | } |
| 110 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 111 | /// Return true if the given decl has a declarator that should have |
| 112 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 113 | static bool hasDeclarator(const Decl *D) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 114 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 115 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 116 | isa<ObjCPropertyDecl>(D); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 119 | /// hasFunctionProto - Return true if the given decl has a argument |
| 120 | /// information. This decl should have already passed |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 121 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 122 | static bool hasFunctionProto(const Decl *D) { |
| 123 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 124 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 125 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 126 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 127 | return true; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 132 | /// arguments. It is an error to call this on a K&R function (use |
| 133 | /// hasFunctionProto first). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 134 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 135 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 136 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 137 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 138 | return BD->getNumParams(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 139 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 142 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 143 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 144 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 145 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 146 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 147 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 148 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 151 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 152 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 153 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 154 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 157 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 158 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 159 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 160 | return proto->isVariadic(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 161 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | db9a0ae | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 162 | return BD->isVariadic(); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 163 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 164 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 168 | static bool isInstanceMethod(const Decl *D) { |
| 169 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 170 | return MethodDecl->isInstance(); |
| 171 | return false; |
| 172 | } |
| 173 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 174 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 175 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 176 | if (!PT) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 177 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 178 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 179 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 180 | if (!Cls) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 181 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 182 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 183 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 184 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 185 | // FIXME: Should we walk the chain of classes? |
| 186 | return ClsName == &Ctx.Idents.get("NSString") || |
| 187 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 188 | } |
| 189 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 190 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 191 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 192 | if (!PT) |
| 193 | return false; |
| 194 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 195 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 196 | if (!RT) |
| 197 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 198 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 199 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 200 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 201 | return false; |
| 202 | |
| 203 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 204 | } |
| 205 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 206 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 207 | /// output an error. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 208 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 209 | unsigned int Num) { |
| 210 | if (Attr.getNumArgs() != Num) { |
| 211 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num; |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | return true; |
| 216 | } |
| 217 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 218 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 219 | /// \brief Check if the attribute has at least as many args as Num. May |
| 220 | /// output an error. |
| 221 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
| 222 | unsigned int Num) { |
| 223 | if (Attr.getNumArgs() < Num) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 224 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| 225 | return false; |
| 226 | } |
| 227 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 228 | return true; |
| 229 | } |
| 230 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 231 | /// \brief Check if IdxExpr is a valid argument index for a function or |
| 232 | /// instance method D. May output an error. |
| 233 | /// |
| 234 | /// \returns true if IdxExpr is a valid index. |
| 235 | static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D, |
| 236 | StringRef AttrName, |
| 237 | SourceLocation AttrLoc, |
| 238 | unsigned AttrArgNum, |
| 239 | const Expr *IdxExpr, |
| 240 | uint64_t &Idx) |
| 241 | { |
| 242 | assert(isFunctionOrMethod(D) && hasFunctionProto(D)); |
| 243 | |
| 244 | // In C++ the implicit 'this' function parameter also counts. |
| 245 | // Parameters are counted from one. |
| 246 | const bool HasImplicitThisParam = isInstanceMethod(D); |
| 247 | const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| 248 | const unsigned FirstIdx = 1; |
| 249 | |
| 250 | llvm::APSInt IdxInt; |
| 251 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 252 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
| 253 | S.Diag(AttrLoc, diag::err_attribute_argument_n_not_int) |
| 254 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | Idx = IdxInt.getLimitedValue(); |
| 259 | if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) { |
| 260 | S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds) |
| 261 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 262 | return false; |
| 263 | } |
| 264 | Idx--; // Convert to zero-based. |
| 265 | if (HasImplicitThisParam) { |
| 266 | if (Idx == 0) { |
| 267 | S.Diag(AttrLoc, |
| 268 | diag::err_attribute_invalid_implicit_this_argument) |
| 269 | << AttrName << IdxExpr->getSourceRange(); |
| 270 | return false; |
| 271 | } |
| 272 | --Idx; |
| 273 | } |
| 274 | |
| 275 | return true; |
| 276 | } |
| 277 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 278 | /// |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 279 | /// \brief Check if passed in Decl is a field or potentially shared global var |
| 280 | /// \return true if the Decl is a field or potentially shared global variable |
| 281 | /// |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 282 | static bool mayBeSharedVariable(const Decl *D) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 283 | if (isa<FieldDecl>(D)) |
| 284 | return true; |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 285 | if (const VarDecl *vd = dyn_cast<VarDecl>(D)) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 286 | return (vd->hasGlobalStorage() && !(vd->isThreadSpecified())); |
| 287 | |
| 288 | return false; |
| 289 | } |
| 290 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 291 | /// \brief Check if the passed-in expression is of type int or bool. |
| 292 | static bool isIntOrBool(Expr *Exp) { |
| 293 | QualType QT = Exp->getType(); |
| 294 | return QT->isBooleanType() || QT->isIntegerType(); |
| 295 | } |
| 296 | |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 297 | |
| 298 | // Check to see if the type is a smart pointer of some kind. We assume |
| 299 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 300 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 301 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 302 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 303 | if (Res1.empty()) |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 304 | return false; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 305 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 306 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 307 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 308 | if (Res2.empty()) |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 309 | return false; |
| 310 | |
| 311 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 314 | /// \brief Check if passed in Decl is a pointer type. |
| 315 | /// Note that this function may produce an error message. |
| 316 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 317 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 318 | const AttributeList &Attr) { |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 319 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 320 | QualType QT = vd->getType(); |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 321 | if (QT->isAnyPointerType()) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 322 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 323 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 324 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 325 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 326 | // (We don't want to force template instantiation if we can avoid it, |
| 327 | // since that would alter the order in which templates are instantiated.) |
| 328 | if (RT->isIncompleteType()) |
| 329 | return true; |
| 330 | |
| 331 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 332 | return true; |
| 333 | } |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 334 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 335 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 336 | << Attr.getName()->getName() << QT; |
| 337 | } else { |
| 338 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 339 | << Attr.getName(); |
| 340 | } |
| 341 | return false; |
| 342 | } |
| 343 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 344 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 345 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 346 | static const RecordType *getRecordType(QualType QT) { |
| 347 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 348 | return RT; |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 349 | |
| 350 | // Now check if we point to record type. |
| 351 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 352 | return PT->getPointeeType()->getAs<RecordType>(); |
| 353 | |
| 354 | return 0; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 355 | } |
| 356 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 357 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 358 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 359 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 360 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
| 361 | if (RT->getDecl()->getAttr<LockableAttr>()) |
| 362 | return true; |
| 363 | return false; |
| 364 | } |
| 365 | |
| 366 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 367 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 368 | /// resolves to a lockable object. |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 369 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 370 | QualType Ty) { |
| 371 | const RecordType *RT = getRecordType(Ty); |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 372 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 373 | // Warn if could not get record type for this argument. |
Benjamin Kramer | d77ba89 | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 374 | if (!RT) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 375 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 376 | << Attr.getName() << Ty.getAsString(); |
| 377 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 378 | } |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 379 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 380 | // 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] | 381 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 382 | return; |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 383 | |
| 384 | // Allow smart pointers to be used as lockable objects. |
| 385 | // FIXME -- Check the type that the smart pointer points to. |
| 386 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 387 | return; |
| 388 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 389 | // Check if the type is lockable. |
| 390 | RecordDecl *RD = RT->getDecl(); |
| 391 | if (RD->getAttr<LockableAttr>()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 392 | return; |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 393 | |
| 394 | // Else check if any base classes are lockable. |
| 395 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 396 | CXXBasePaths BPaths(false, false); |
| 397 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 398 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 399 | } |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 400 | |
| 401 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 402 | << Attr.getName() << Ty.getAsString(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 405 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 406 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 407 | /// \param Sidx The attribute argument index to start checking with. |
| 408 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 409 | /// parameter list. |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 410 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 411 | const AttributeList &Attr, |
| 412 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 413 | int Sidx = 0, |
| 414 | bool ParamIdxOk = false) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 415 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 416 | Expr *ArgExp = Attr.getArg(Idx); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 417 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 418 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 419 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 420 | Args.push_back(ArgExp); |
| 421 | continue; |
| 422 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 423 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 424 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 425 | if (StrLit->getLength() == 0 || |
| 426 | StrLit->getString() == StringRef("*")) { |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 427 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 428 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 429 | Args.push_back(ArgExp); |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 430 | continue; |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 431 | } |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 432 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 433 | // We allow constant strings to be used as a placeholder for expressions |
| 434 | // that are not valid C++ syntax, but warn that they are ignored. |
| 435 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 436 | Attr.getName(); |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 437 | Args.push_back(ArgExp); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 438 | continue; |
| 439 | } |
| 440 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 441 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 442 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 443 | // A pointer to member expression of the form &MyClass::mu is treated |
| 444 | // specially -- we need to look at the type of the member. |
| 445 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 446 | if (UOp->getOpcode() == UO_AddrOf) |
| 447 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 448 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 449 | ArgTy = DRE->getDecl()->getType(); |
| 450 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 451 | // First see if we can just cast to record type, or point to record type. |
| 452 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 453 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 454 | // Now check if we index into a record type function param. |
| 455 | if(!RT && ParamIdxOk) { |
| 456 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 457 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 458 | if(FD && IL) { |
| 459 | unsigned int NumParams = FD->getNumParams(); |
| 460 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 461 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 462 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 463 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 464 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 465 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 466 | continue; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 467 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 468 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 472 | checkForLockableRecord(S, D, Attr, ArgTy); |
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 | Args.push_back(ArgExp); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 475 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 478 | //===----------------------------------------------------------------------===// |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 479 | // Attribute Implementations |
| 480 | //===----------------------------------------------------------------------===// |
| 481 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 482 | // FIXME: All this manual attribute parsing code is gross. At the |
| 483 | // least add some helper functions to check most argument patterns (# |
| 484 | // and types of args). |
| 485 | |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 486 | enum ThreadAttributeDeclKind { |
| 487 | ThreadExpectedFieldOrGlobalVar, |
| 488 | ThreadExpectedFunctionOrMethod, |
| 489 | ThreadExpectedClassOrStruct |
| 490 | }; |
| 491 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 492 | static bool checkGuardedVarAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 493 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 494 | assert(!Attr.isInvalid()); |
| 495 | |
| 496 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 497 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 498 | |
| 499 | // D must be either a member field or global (potentially shared) variable. |
| 500 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 501 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 502 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 503 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 506 | return true; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 509 | static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 510 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 511 | return; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 512 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 513 | D->addAttr(::new (S.Context) |
| 514 | GuardedVarAttr(Attr.getRange(), S.Context, |
| 515 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 518 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 519 | const AttributeList &Attr) { |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 520 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 521 | return; |
| 522 | |
| 523 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 524 | return; |
| 525 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 526 | D->addAttr(::new (S.Context) |
| 527 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 528 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 531 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 532 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 533 | Expr* &Arg) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 534 | assert(!Attr.isInvalid()); |
| 535 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 536 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 537 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 538 | |
| 539 | // D must be either a member field or global (potentially shared) variable. |
| 540 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 541 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 542 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 543 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 544 | } |
| 545 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 546 | SmallVector<Expr*, 1> Args; |
| 547 | // check that all arguments are lockable objects |
| 548 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 549 | unsigned Size = Args.size(); |
| 550 | if (Size != 1) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 551 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 552 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 553 | Arg = Args[0]; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 554 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 555 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 558 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 559 | Expr *Arg = 0; |
| 560 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 561 | return; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 562 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 563 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| 564 | } |
| 565 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 566 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 567 | const AttributeList &Attr) { |
| 568 | Expr *Arg = 0; |
| 569 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 570 | return; |
| 571 | |
| 572 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 573 | return; |
| 574 | |
| 575 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| 576 | S.Context, Arg)); |
| 577 | } |
| 578 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 579 | static bool checkLockableAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 580 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 581 | assert(!Attr.isInvalid()); |
| 582 | |
| 583 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 584 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 585 | |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 586 | // FIXME: Lockable structs for C code. |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 587 | if (!isa<CXXRecordDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 588 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 589 | << Attr.getName() << ThreadExpectedClassOrStruct; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 590 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 593 | return true; |
| 594 | } |
| 595 | |
| 596 | static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 597 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 598 | return; |
| 599 | |
| 600 | D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); |
| 601 | } |
| 602 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 603 | static void handleScopedLockableAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 604 | const AttributeList &Attr) { |
| 605 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 606 | return; |
| 607 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 608 | D->addAttr(::new (S.Context) |
| 609 | ScopedLockableAttr(Attr.getRange(), S.Context, |
| 610 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | static void handleNoThreadSafetyAttr(Sema &S, Decl *D, |
| 614 | const AttributeList &Attr) { |
| 615 | assert(!Attr.isInvalid()); |
| 616 | |
| 617 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 618 | return; |
| 619 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 620 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 621 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 622 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 623 | return; |
| 624 | } |
| 625 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 626 | D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(), |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 627 | S.Context)); |
| 628 | } |
| 629 | |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 630 | static void handleNoAddressSafetyAttr(Sema &S, Decl *D, |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 631 | const AttributeList &Attr) { |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 632 | assert(!Attr.isInvalid()); |
| 633 | |
| 634 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 635 | return; |
| 636 | |
| 637 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 638 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 639 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 640 | return; |
| 641 | } |
| 642 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 643 | D->addAttr(::new (S.Context) |
| 644 | NoAddressSafetyAnalysisAttr(Attr.getRange(), S.Context, |
| 645 | Attr.getAttributeSpellingListIndex())); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 648 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 649 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 650 | SmallVector<Expr*, 1> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 651 | assert(!Attr.isInvalid()); |
| 652 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 653 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 654 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 655 | |
| 656 | // D must be either a member field or global (potentially shared) variable. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 657 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 658 | if (!VD || !mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 659 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 660 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 661 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 662 | } |
| 663 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 664 | // Check that this attribute only applies to lockable types. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 665 | QualType QT = VD->getType(); |
| 666 | if (!QT->isDependentType()) { |
| 667 | const RecordType *RT = getRecordType(QT); |
| 668 | if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 669 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 670 | << Attr.getName(); |
| 671 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 675 | // Check that all arguments are lockable objects. |
| 676 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 677 | if (Args.size() == 0) |
| 678 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 679 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 680 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 683 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 684 | const AttributeList &Attr) { |
| 685 | SmallVector<Expr*, 1> Args; |
| 686 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 687 | return; |
| 688 | |
| 689 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 690 | D->addAttr(::new (S.Context) |
| 691 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 692 | StartArg, Args.size(), |
| 693 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 696 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 697 | const AttributeList &Attr) { |
| 698 | SmallVector<Expr*, 1> Args; |
| 699 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 700 | return; |
| 701 | |
| 702 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 703 | D->addAttr(::new (S.Context) |
| 704 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 705 | StartArg, Args.size(), |
| 706 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 709 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 710 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 711 | SmallVector<Expr*, 1> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 712 | assert(!Attr.isInvalid()); |
| 713 | |
| 714 | // zero or more arguments ok |
| 715 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 716 | // check that the attribute is applied to a function |
| 717 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 718 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 719 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 720 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 723 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 724 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 725 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 726 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 729 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 730 | const AttributeList &Attr) { |
| 731 | SmallVector<Expr*, 1> Args; |
| 732 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 733 | return; |
| 734 | |
| 735 | unsigned Size = Args.size(); |
| 736 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 737 | D->addAttr(::new (S.Context) |
| 738 | SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 739 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 742 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 743 | const AttributeList &Attr) { |
| 744 | SmallVector<Expr*, 1> Args; |
| 745 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 746 | return; |
| 747 | |
| 748 | unsigned Size = Args.size(); |
| 749 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 750 | D->addAttr(::new (S.Context) |
| 751 | ExclusiveLockFunctionAttr(Attr.getRange(), S.Context, |
| 752 | StartArg, Size, |
| 753 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 756 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 757 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 758 | SmallVector<Expr*, 2> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 759 | assert(!Attr.isInvalid()); |
| 760 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 761 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 762 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 763 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 764 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 765 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 766 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 767 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 770 | if (!isIntOrBool(Attr.getArg(0))) { |
| 771 | S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 772 | << Attr.getName(); |
| 773 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 777 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 778 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 779 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 782 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 783 | const AttributeList &Attr) { |
| 784 | SmallVector<Expr*, 2> Args; |
| 785 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 786 | return; |
| 787 | |
| 788 | unsigned Size = Args.size(); |
| 789 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 790 | D->addAttr(::new (S.Context) |
| 791 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
| 792 | Attr.getArg(0), StartArg, Size, |
| 793 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 796 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 797 | const AttributeList &Attr) { |
| 798 | SmallVector<Expr*, 2> Args; |
| 799 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 800 | return; |
| 801 | |
| 802 | unsigned Size = Args.size(); |
| 803 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 804 | D->addAttr(::new (S.Context) |
| 805 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
| 806 | Attr.getArg(0), StartArg, Size, |
| 807 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 810 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 811 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 812 | SmallVector<Expr*, 1> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 813 | assert(!Attr.isInvalid()); |
| 814 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 815 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 816 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 817 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 818 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 819 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 820 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 821 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 824 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 825 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 826 | if (Args.size() == 0) |
| 827 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 828 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 829 | return true; |
| 830 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 831 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 832 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 833 | const AttributeList &Attr) { |
| 834 | SmallVector<Expr*, 1> Args; |
| 835 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 836 | return; |
| 837 | |
| 838 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 839 | D->addAttr(::new (S.Context) |
| 840 | ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context, |
| 841 | StartArg, Args.size(), |
| 842 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 845 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 846 | const AttributeList &Attr) { |
| 847 | SmallVector<Expr*, 1> Args; |
| 848 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 849 | return; |
| 850 | |
| 851 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 852 | D->addAttr(::new (S.Context) |
| 853 | SharedLocksRequiredAttr(Attr.getRange(), S.Context, |
| 854 | StartArg, Args.size(), |
| 855 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 858 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 859 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 860 | assert(!Attr.isInvalid()); |
| 861 | |
| 862 | // zero or more arguments ok |
| 863 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 864 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 865 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 866 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 867 | return; |
| 868 | } |
| 869 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 870 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 871 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 872 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 873 | unsigned Size = Args.size(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 874 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 875 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 876 | D->addAttr(::new (S.Context) |
| 877 | UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 878 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | static void handleLockReturnedAttr(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 | assert(!Attr.isInvalid()); |
| 884 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 885 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 886 | return; |
| 887 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 888 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 889 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 890 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 891 | return; |
| 892 | } |
| 893 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 894 | // check that the argument is lockable object |
DeLesley Hutchins | f26efd7 | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 895 | SmallVector<Expr*, 1> Args; |
| 896 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 897 | unsigned Size = Args.size(); |
| 898 | if (Size == 0) |
| 899 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 900 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 901 | D->addAttr(::new (S.Context) |
| 902 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 903 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 907 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 908 | assert(!Attr.isInvalid()); |
| 909 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 910 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 911 | return; |
| 912 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 913 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 914 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 915 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 916 | return; |
| 917 | } |
| 918 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 919 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 920 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 921 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 922 | unsigned Size = Args.size(); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 923 | if (Size == 0) |
| 924 | return; |
| 925 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 926 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 927 | D->addAttr(::new (S.Context) |
| 928 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 929 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 933 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 934 | const AttributeList &Attr) { |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 935 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 936 | if (TD == 0) { |
| 937 | // __attribute__((ext_vector_type(N))) can only be applied to typedefs |
| 938 | // and type-ids. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 939 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 940 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 941 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 942 | |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 943 | // Remember this typedef decl, we will need it later for diagnostics. |
| 944 | S.ExtVectorDecls.push_back(TD); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 947 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 948 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 949 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 950 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 951 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 952 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 953 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 954 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 955 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 956 | // has no effect. |
Eli Friedman | b68ec6b | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 957 | if (!FD->getType()->isDependentType() && |
| 958 | !FD->getType()->isIncompleteType() && |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 959 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 960 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 961 | << Attr.getName() << FD->getType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 962 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 963 | FD->addAttr(::new (S.Context) |
| 964 | PackedAttr(Attr.getRange(), S.Context, |
| 965 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 966 | } else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 967 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 970 | static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 5f608ae | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 971 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 972 | RD->addAttr(::new (S.Context) |
| 973 | MsStructAttr(Attr.getRange(), S.Context, |
| 974 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | c1a0a73 | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 975 | else |
| 976 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 977 | } |
| 978 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 979 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 980 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 981 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 982 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 983 | |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 984 | // The IBAction attributes only apply to instance methods. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 985 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 986 | if (MD->isInstanceMethod()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 987 | D->addAttr(::new (S.Context) |
| 988 | IBActionAttr(Attr.getRange(), S.Context, |
| 989 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 990 | return; |
| 991 | } |
| 992 | |
Ted Kremenek | 4ee2bb1 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 993 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 996 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 997 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 998 | // variables or properties of Objective-C classes. The outlet must also |
| 999 | // have an object reference type. |
| 1000 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1001 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 0bfaf06 | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1002 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1003 | << Attr.getName() << VD->getType() << 0; |
| 1004 | return false; |
| 1005 | } |
| 1006 | } |
| 1007 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1008 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1009 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1010 | << Attr.getName() << PD->getType() << 1; |
| 1011 | return false; |
| 1012 | } |
| 1013 | } |
| 1014 | else { |
| 1015 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1016 | return false; |
| 1017 | } |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1018 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1019 | return true; |
| 1020 | } |
| 1021 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1022 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1023 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1024 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1025 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1026 | |
| 1027 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1028 | return; |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1029 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1030 | D->addAttr(::new (S.Context) |
| 1031 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1032 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1035 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1036 | const AttributeList &Attr) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1037 | |
| 1038 | // The iboutletcollection attribute can have zero or one arguments. |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1039 | if (Attr.getParameterName() && Attr.getNumArgs() > 0) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1040 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1041 | return; |
| 1042 | } |
| 1043 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1044 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1045 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1046 | |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1047 | IdentifierInfo *II = Attr.getParameterName(); |
| 1048 | if (!II) |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1049 | II = &S.Context.Idents.get("NSObject"); |
Fariborz Jahanian | 3a3400b | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 1050 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1051 | ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1052 | S.getScopeForContext(D->getDeclContext()->getParent())); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1053 | if (!TypeRep) { |
| 1054 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1055 | return; |
| 1056 | } |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1057 | QualType QT = TypeRep.get(); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1058 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1059 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1060 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1061 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1062 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1063 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1064 | return; |
| 1065 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1066 | D->addAttr(::new (S.Context) |
| 1067 | IBOutletCollectionAttr(Attr.getRange(),S.Context, |
| 1068 | QT, Attr.getParameterLoc(), |
| 1069 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1072 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1073 | if (const RecordType *UT = T->getAsUnionType()) |
| 1074 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1075 | RecordDecl *UD = UT->getDecl(); |
| 1076 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1077 | itend = UD->field_end(); it != itend; ++it) { |
| 1078 | QualType QT = it->getType(); |
| 1079 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1080 | T = QT; |
| 1081 | return; |
| 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1087 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nuno Lopes | 174930d | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1088 | if (!isFunctionOrMethod(D)) { |
| 1089 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1090 | << "alloc_size" << ExpectedFunctionOrMethod; |
| 1091 | return; |
| 1092 | } |
| 1093 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1094 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1095 | return; |
| 1096 | |
| 1097 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1098 | // counted from one. |
| 1099 | bool HasImplicitThisParam = isInstanceMethod(D); |
Argyrios Kyrtzidis | 28965bf | 2013-02-22 06:58:28 +0000 | [diff] [blame] | 1100 | unsigned NumArgs; |
| 1101 | if (hasFunctionProto(D)) |
| 1102 | NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| 1103 | else |
| 1104 | NumArgs = 0; |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1105 | |
| 1106 | SmallVector<unsigned, 8> SizeArgs; |
| 1107 | |
| 1108 | for (AttributeList::arg_iterator I = Attr.arg_begin(), |
| 1109 | E = Attr.arg_end(); I!=E; ++I) { |
| 1110 | // The argument must be an integer constant expression. |
| 1111 | Expr *Ex = *I; |
| 1112 | llvm::APSInt ArgNum; |
| 1113 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 1114 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1115 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1116 | << "alloc_size" << Ex->getSourceRange(); |
| 1117 | return; |
| 1118 | } |
| 1119 | |
| 1120 | uint64_t x = ArgNum.getZExtValue(); |
| 1121 | |
| 1122 | if (x < 1 || x > NumArgs) { |
| 1123 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1124 | << "alloc_size" << I.getArgNum() << Ex->getSourceRange(); |
| 1125 | return; |
| 1126 | } |
| 1127 | |
| 1128 | --x; |
| 1129 | if (HasImplicitThisParam) { |
| 1130 | if (x == 0) { |
| 1131 | S.Diag(Attr.getLoc(), |
| 1132 | diag::err_attribute_invalid_implicit_this_argument) |
| 1133 | << "alloc_size" << Ex->getSourceRange(); |
| 1134 | return; |
| 1135 | } |
| 1136 | --x; |
| 1137 | } |
| 1138 | |
| 1139 | // check if the function argument is of an integer type |
| 1140 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
| 1141 | if (!T->isIntegerType()) { |
| 1142 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1143 | << "alloc_size" << Ex->getSourceRange(); |
| 1144 | return; |
| 1145 | } |
| 1146 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1147 | SizeArgs.push_back(x); |
| 1148 | } |
| 1149 | |
| 1150 | // check if the function returns a pointer |
| 1151 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 1152 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
| 1153 | << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
| 1154 | } |
| 1155 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1156 | D->addAttr(::new (S.Context) |
| 1157 | AllocSizeAttr(Attr.getRange(), S.Context, |
| 1158 | SizeArgs.data(), SizeArgs.size(), |
| 1159 | Attr.getAttributeSpellingListIndex())); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1162 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1163 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1164 | // ignore it as well |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1165 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1166 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1167 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1168 | return; |
| 1169 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1170 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1171 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1172 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1173 | bool HasImplicitThisParam = isInstanceMethod(D); |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1174 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1175 | |
| 1176 | // The nonnull attribute only applies to pointers. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1177 | SmallVector<unsigned, 10> NonNullArgs; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1178 | |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1179 | for (AttributeList::arg_iterator I = Attr.arg_begin(), |
| 1180 | E = Attr.arg_end(); I != E; ++I) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1181 | // The argument must be an integer constant expression. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1182 | Expr *Ex = *I; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1183 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1184 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 1185 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1186 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1187 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1188 | return; |
| 1189 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1190 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1191 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1192 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1193 | if (x < 1 || x > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1194 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 1195 | << "nonnull" << I.getArgNum() << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1196 | return; |
| 1197 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1198 | |
Ted Kremenek | 465172f | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 1199 | --x; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1200 | if (HasImplicitThisParam) { |
| 1201 | if (x == 0) { |
| 1202 | S.Diag(Attr.getLoc(), |
| 1203 | diag::err_attribute_invalid_implicit_this_argument) |
| 1204 | << "nonnull" << Ex->getSourceRange(); |
| 1205 | return; |
| 1206 | } |
| 1207 | --x; |
| 1208 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1209 | |
| 1210 | // Is the function argument a pointer type? |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1211 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1212 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1213 | |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1214 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1215 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | c9ef405 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1216 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1217 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1218 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1219 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1220 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1221 | NonNullArgs.push_back(x); |
| 1222 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1223 | |
| 1224 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1225 | // arguments have a nonnull attribute. |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1226 | if (NonNullArgs.empty()) { |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1227 | for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) { |
| 1228 | QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1229 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1230 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1231 | NonNullArgs.push_back(i); |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1232 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1233 | |
Ted Kremenek | ee1c08c | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1234 | // No pointer arguments? |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1235 | if (NonNullArgs.empty()) { |
| 1236 | // Warn the trivial case only if attribute is not coming from a |
| 1237 | // macro instantiation. |
| 1238 | if (Attr.getLoc().isFileID()) |
| 1239 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1240 | return; |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1241 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1242 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1243 | |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1244 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1245 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1246 | llvm::array_pod_sort(start, start + size); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1247 | D->addAttr(::new (S.Context) |
| 1248 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1249 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1252 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1253 | // This attribute must be applied to a function declaration. |
| 1254 | // The first argument to the attribute must be a string, |
| 1255 | // the name of the resource, for example "malloc". |
| 1256 | // The following arguments must be argument indexes, the arguments must be |
| 1257 | // of integer type for Returns, otherwise of pointer type. |
| 1258 | // 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] | 1259 | // after being held. free() should be __attribute((ownership_takes)), whereas |
| 1260 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1261 | |
| 1262 | if (!AL.getParameterName()) { |
| 1263 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1264 | << AL.getName()->getName() << 1; |
| 1265 | return; |
| 1266 | } |
| 1267 | // Figure out our Kind, and check arguments while we're at it. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1268 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1269 | switch (AL.getKind()) { |
| 1270 | case AttributeList::AT_ownership_takes: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1271 | K = OwnershipAttr::Takes; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1272 | if (AL.getNumArgs() < 1) { |
| 1273 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1274 | return; |
| 1275 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1276 | break; |
| 1277 | case AttributeList::AT_ownership_holds: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1278 | K = OwnershipAttr::Holds; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1279 | if (AL.getNumArgs() < 1) { |
| 1280 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1281 | return; |
| 1282 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1283 | break; |
| 1284 | case AttributeList::AT_ownership_returns: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1285 | K = OwnershipAttr::Returns; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1286 | if (AL.getNumArgs() > 1) { |
| 1287 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1288 | << AL.getNumArgs() + 1; |
| 1289 | return; |
| 1290 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1291 | break; |
| 1292 | default: |
| 1293 | // This should never happen given how we are called. |
| 1294 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1297 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1298 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1299 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1300 | return; |
| 1301 | } |
| 1302 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1303 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1304 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1305 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1306 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1307 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1308 | StringRef Module = AL.getParameterName()->getName(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1309 | |
| 1310 | // Normalize the argument, __foo__ becomes foo. |
| 1311 | if (Module.startswith("__") && Module.endswith("__")) |
| 1312 | Module = Module.substr(2, Module.size() - 4); |
| 1313 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1314 | SmallVector<unsigned, 10> OwnershipArgs; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1315 | |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1316 | for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E; |
| 1317 | ++I) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1318 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1319 | Expr *IdxExpr = *I; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1320 | llvm::APSInt ArgNum(32); |
| 1321 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1322 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1323 | S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int) |
| 1324 | << AL.getName()->getName() << IdxExpr->getSourceRange(); |
| 1325 | continue; |
| 1326 | } |
| 1327 | |
| 1328 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
| 1329 | |
| 1330 | if (x > NumArgs || x < 1) { |
| 1331 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1332 | << AL.getName()->getName() << x << IdxExpr->getSourceRange(); |
| 1333 | continue; |
| 1334 | } |
| 1335 | --x; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1336 | if (HasImplicitThisParam) { |
| 1337 | if (x == 0) { |
| 1338 | S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 1339 | << "ownership" << IdxExpr->getSourceRange(); |
| 1340 | return; |
| 1341 | } |
| 1342 | --x; |
| 1343 | } |
| 1344 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1345 | switch (K) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1346 | case OwnershipAttr::Takes: |
| 1347 | case OwnershipAttr::Holds: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1348 | // Is the function argument a pointer type? |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1349 | QualType T = getFunctionOrMethodArgType(D, x); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1350 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| 1351 | // FIXME: Should also highlight argument in decl. |
| 1352 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1353 | << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1354 | << "pointer" |
| 1355 | << IdxExpr->getSourceRange(); |
| 1356 | continue; |
| 1357 | } |
| 1358 | break; |
| 1359 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1360 | case OwnershipAttr::Returns: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1361 | if (AL.getNumArgs() > 1) { |
| 1362 | // Is the function argument an integer type? |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1363 | Expr *IdxExpr = AL.getArg(0); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1364 | llvm::APSInt ArgNum(32); |
| 1365 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1366 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1367 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
| 1368 | << "ownership_returns" << "integer" |
| 1369 | << IdxExpr->getSourceRange(); |
| 1370 | return; |
| 1371 | } |
| 1372 | } |
| 1373 | break; |
| 1374 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1375 | } // switch |
| 1376 | |
| 1377 | // Check we don't have a conflict with another ownership attribute. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1378 | for (specific_attr_iterator<OwnershipAttr> |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1379 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1380 | e = D->specific_attr_end<OwnershipAttr>(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1381 | i != e; ++i) { |
| 1382 | if ((*i)->getOwnKind() != K) { |
| 1383 | for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); |
| 1384 | I!=E; ++I) { |
| 1385 | if (x == *I) { |
| 1386 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1387 | << AL.getName()->getName() << "ownership_*"; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1388 | } |
| 1389 | } |
| 1390 | } |
| 1391 | } |
| 1392 | OwnershipArgs.push_back(x); |
| 1393 | } |
| 1394 | |
| 1395 | unsigned* start = OwnershipArgs.data(); |
| 1396 | unsigned size = OwnershipArgs.size(); |
| 1397 | llvm::array_pod_sort(start, start + size); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1398 | |
| 1399 | if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { |
| 1400 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1401 | return; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1402 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1403 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1404 | D->addAttr(::new (S.Context) |
| 1405 | OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size, |
| 1406 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1409 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1410 | // Check the attribute arguments. |
| 1411 | if (Attr.getNumArgs() > 1) { |
| 1412 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1413 | return; |
| 1414 | } |
| 1415 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1416 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1417 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1418 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1419 | return; |
| 1420 | } |
| 1421 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1422 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1423 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1424 | // gcc rejects |
| 1425 | // class c { |
| 1426 | // static int a __attribute__((weakref ("v2"))); |
| 1427 | // static int b() __attribute__((weakref ("f3"))); |
| 1428 | // }; |
| 1429 | // and ignores the attributes of |
| 1430 | // void f(void) { |
| 1431 | // static int a __attribute__((weakref ("v2"))); |
| 1432 | // } |
| 1433 | // we reject them |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1434 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1435 | if (!Ctx->isFileContext()) { |
| 1436 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1437 | nd->getNameAsString(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1438 | return; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | // The GCC manual says |
| 1442 | // |
| 1443 | // At present, a declaration to which `weakref' is attached can only |
| 1444 | // be `static'. |
| 1445 | // |
| 1446 | // It also says |
| 1447 | // |
| 1448 | // Without a TARGET, |
| 1449 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1450 | // equivalent to `weak'. |
| 1451 | // |
| 1452 | // gcc 4.4.1 will accept |
| 1453 | // int a7 __attribute__((weakref)); |
| 1454 | // as |
| 1455 | // int a7 __attribute__((weak)); |
| 1456 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1457 | // it if this behaviour is actually used. |
| 1458 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1459 | // GCC rejects |
| 1460 | // static ((alias ("y"), weakref)). |
| 1461 | // Should we? How to check that weakref is before or after alias? |
| 1462 | |
| 1463 | if (Attr.getNumArgs() == 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1464 | Expr *Arg = Attr.getArg(0); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1465 | Arg = Arg->IgnoreParenCasts(); |
| 1466 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1467 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1468 | if (!Str || !Str->isAscii()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1469 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1470 | << "weakref" << 1; |
| 1471 | return; |
| 1472 | } |
| 1473 | // GCC will accept anything as the argument of weakref. Should we |
| 1474 | // check for an existing decl? |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1475 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1476 | Str->getString())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1479 | D->addAttr(::new (S.Context) |
| 1480 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1481 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1484 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1485 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1486 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1487 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1488 | return; |
| 1489 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1490 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1491 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1492 | Arg = Arg->IgnoreParenCasts(); |
| 1493 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1494 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1495 | if (!Str || !Str->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1496 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1497 | << "alias" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1498 | return; |
| 1499 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1500 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1501 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | f5fe292 | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1502 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1503 | return; |
| 1504 | } |
| 1505 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1506 | // FIXME: check if target symbol exists in current file |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1507 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1508 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1509 | Str->getString(), |
| 1510 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1513 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1514 | // Check the attribute arguments. |
| 1515 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1516 | return; |
| 1517 | |
| 1518 | if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) { |
| 1519 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1520 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1521 | return; |
| 1522 | } |
| 1523 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1524 | D->addAttr(::new (S.Context) |
| 1525 | MinSizeAttr(Attr.getRange(), S.Context, |
| 1526 | Attr.getAttributeSpellingListIndex())); |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1529 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1530 | // Check the attribute arguments. |
| 1531 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1532 | return; |
| 1533 | |
| 1534 | if (!isa<FunctionDecl>(D)) { |
| 1535 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1536 | << Attr.getName() << ExpectedFunction; |
| 1537 | return; |
| 1538 | } |
| 1539 | |
| 1540 | if (D->hasAttr<HotAttr>()) { |
| 1541 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1542 | << Attr.getName() << "hot"; |
| 1543 | return; |
| 1544 | } |
| 1545 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1546 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1547 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1551 | // Check the attribute arguments. |
| 1552 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1553 | return; |
| 1554 | |
| 1555 | if (!isa<FunctionDecl>(D)) { |
| 1556 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1557 | << Attr.getName() << ExpectedFunction; |
| 1558 | return; |
| 1559 | } |
| 1560 | |
| 1561 | if (D->hasAttr<ColdAttr>()) { |
| 1562 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1563 | << Attr.getName() << "cold"; |
| 1564 | return; |
| 1565 | } |
| 1566 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1567 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1568 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1571 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1572 | // Check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1573 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1574 | return; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1575 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1576 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1577 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1578 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1579 | return; |
| 1580 | } |
| 1581 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1582 | D->addAttr(::new (S.Context) |
| 1583 | NakedAttr(Attr.getRange(), S.Context, |
| 1584 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1587 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 1588 | const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1589 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1590 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1591 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1592 | return; |
| 1593 | } |
| 1594 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1595 | if (!isa<FunctionDecl>(D)) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1596 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1597 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1598 | return; |
| 1599 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1600 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1601 | D->addAttr(::new (S.Context) |
| 1602 | AlwaysInlineAttr(Attr.getRange(), S.Context, |
| 1603 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1606 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1607 | const AttributeList &Attr) { |
| 1608 | // Check the attribute arguments. |
| 1609 | if (Attr.getNumArgs() != 1) { |
| 1610 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1611 | return; |
| 1612 | } |
| 1613 | |
| 1614 | Expr *Arg = Attr.getArg(0); |
| 1615 | Arg = Arg->IgnoreParenCasts(); |
| 1616 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1617 | |
| 1618 | // Check that it is a string. |
| 1619 | if (!Str) { |
| 1620 | S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model"; |
| 1621 | return; |
| 1622 | } |
| 1623 | |
| 1624 | if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) { |
| 1625 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1626 | << Attr.getName() << ExpectedTLSVar; |
| 1627 | return; |
| 1628 | } |
| 1629 | |
| 1630 | // Check that the value. |
| 1631 | StringRef Model = Str->getString(); |
| 1632 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1633 | && Model != "initial-exec" && Model != "local-exec") { |
| 1634 | S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg); |
| 1635 | return; |
| 1636 | } |
| 1637 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1638 | D->addAttr(::new (S.Context) |
| 1639 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1640 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1643 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1644 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1645 | if (Attr.hasParameterOrArguments()) { |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1646 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1647 | return; |
| 1648 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1649 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1650 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1651 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1652 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1653 | D->addAttr(::new (S.Context) |
| 1654 | MallocAttr(Attr.getRange(), S.Context, |
| 1655 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1656 | return; |
| 1657 | } |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1660 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1663 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1664 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1665 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1666 | return; |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1667 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1668 | D->addAttr(::new (S.Context) |
| 1669 | MayAliasAttr(Attr.getRange(), S.Context, |
| 1670 | Attr.getAttributeSpellingListIndex())); |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1673 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1674 | assert(!Attr.isInvalid()); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1675 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1676 | D->addAttr(::new (S.Context) |
| 1677 | NoCommonAttr(Attr.getRange(), S.Context, |
| 1678 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1679 | else |
| 1680 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1681 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1684 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1685 | assert(!Attr.isInvalid()); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1686 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1687 | D->addAttr(::new (S.Context) |
| 1688 | CommonAttr(Attr.getRange(), S.Context, |
| 1689 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1690 | else |
| 1691 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1692 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1695 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1696 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1697 | |
| 1698 | if (S.CheckNoReturnAttr(attr)) return; |
| 1699 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1700 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1701 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1702 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1703 | return; |
| 1704 | } |
| 1705 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1706 | D->addAttr(::new (S.Context) |
| 1707 | NoReturnAttr(attr.getRange(), S.Context, |
| 1708 | attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1712 | if (attr.hasParameterOrArguments()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1713 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1714 | attr.setInvalid(); |
| 1715 | return true; |
| 1716 | } |
| 1717 | |
| 1718 | return false; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1721 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1722 | const AttributeList &Attr) { |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1723 | |
| 1724 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1725 | // because 'analyzer_noreturn' does not impact the type. |
| 1726 | |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1727 | if(!checkAttributeNumArgs(S, Attr, 0)) |
| 1728 | return; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1729 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1730 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1731 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1732 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1733 | && !VD->getType()->isFunctionPointerType())) { |
| 1734 | S.Diag(Attr.getLoc(), |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1735 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1736 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1737 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1738 | return; |
| 1739 | } |
| 1740 | } |
| 1741 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1742 | D->addAttr(::new (S.Context) |
| 1743 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1744 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1747 | static void handleCXX11NoReturnAttr(Sema &S, Decl *D, |
| 1748 | const AttributeList &Attr) { |
| 1749 | // C++11 [dcl.attr.noreturn]p1: |
| 1750 | // The attribute may be applied to the declarator-id in a function |
| 1751 | // declaration. |
| 1752 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 1753 | if (!FD) { |
| 1754 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1755 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1756 | return; |
| 1757 | } |
| 1758 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1759 | D->addAttr(::new (S.Context) |
| 1760 | CXX11NoReturnAttr(Attr.getRange(), S.Context, |
| 1761 | Attr.getAttributeSpellingListIndex())); |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1764 | // PS3 PPU-specific. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1765 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1766 | /* |
| 1767 | Returning a Vector Class in Registers |
| 1768 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1769 | According to the PPU ABI specifications, a class with a single member of |
| 1770 | vector type is returned in memory when used as the return value of a function. |
| 1771 | This results in inefficient code when implementing vector classes. To return |
| 1772 | the value in a single vector register, add the vecreturn attribute to the |
| 1773 | class definition. This attribute is also applicable to struct types. |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1774 | |
| 1775 | Example: |
| 1776 | |
| 1777 | struct Vector |
| 1778 | { |
| 1779 | __vector float xyzw; |
| 1780 | } __attribute__((vecreturn)); |
| 1781 | |
| 1782 | Vector Add(Vector lhs, Vector rhs) |
| 1783 | { |
| 1784 | Vector result; |
| 1785 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1786 | return result; // This will be returned in a register |
| 1787 | } |
| 1788 | */ |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1789 | if (!isa<RecordDecl>(D)) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1790 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1791 | << Attr.getName() << ExpectedClass; |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1792 | return; |
| 1793 | } |
| 1794 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1795 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1796 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1797 | return; |
| 1798 | } |
| 1799 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1800 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1801 | int count = 0; |
| 1802 | |
| 1803 | if (!isa<CXXRecordDecl>(record)) { |
| 1804 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1805 | return; |
| 1806 | } |
| 1807 | |
| 1808 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1809 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1810 | return; |
| 1811 | } |
| 1812 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1813 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1814 | iter != record->field_end(); iter++) { |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1815 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1816 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1817 | return; |
| 1818 | } |
| 1819 | count++; |
| 1820 | } |
| 1821 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1822 | D->addAttr(::new (S.Context) |
| 1823 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1824 | Attr.getAttributeSpellingListIndex())); |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1827 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1828 | const AttributeList &Attr) { |
| 1829 | if (isa<ParmVarDecl>(D)) { |
| 1830 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1831 | // parameter of a function declaration or lambda. |
| 1832 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1833 | S.Diag(Attr.getLoc(), |
| 1834 | diag::err_carries_dependency_param_not_function_decl); |
| 1835 | return; |
| 1836 | } |
| 1837 | } else if (!isa<FunctionDecl>(D)) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1838 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1839 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1840 | return; |
| 1841 | } |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1842 | |
| 1843 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1844 | Attr.getRange(), S.Context, |
| 1845 | Attr.getAttributeSpellingListIndex())); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1848 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1849 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1850 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1851 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1852 | return; |
| 1853 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1854 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1855 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 568eae4 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1856 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1857 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1858 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1859 | return; |
| 1860 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1861 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1862 | D->addAttr(::new (S.Context) |
| 1863 | UnusedAttr(Attr.getRange(), S.Context, |
| 1864 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1867 | static void handleReturnsTwiceAttr(Sema &S, Decl *D, |
| 1868 | const AttributeList &Attr) { |
| 1869 | // check the attribute arguments. |
| 1870 | if (Attr.hasParameterOrArguments()) { |
| 1871 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1872 | return; |
| 1873 | } |
| 1874 | |
| 1875 | if (!isa<FunctionDecl>(D)) { |
| 1876 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1877 | << Attr.getName() << ExpectedFunction; |
| 1878 | return; |
| 1879 | } |
| 1880 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1881 | D->addAttr(::new (S.Context) |
| 1882 | ReturnsTwiceAttr(Attr.getRange(), S.Context, |
| 1883 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1886 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1887 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1888 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1889 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1890 | return; |
| 1891 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1892 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1893 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Daniel Dunbar | 186204b | 2009-02-13 22:48:56 +0000 | [diff] [blame] | 1894 | if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1895 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 1896 | return; |
| 1897 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1898 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1899 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1900 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1901 | return; |
| 1902 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1903 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1904 | D->addAttr(::new (S.Context) |
| 1905 | UsedAttr(Attr.getRange(), S.Context, |
| 1906 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1909 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1910 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1911 | if (Attr.getNumArgs() > 1) { |
| 1912 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1913 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1914 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1915 | |
| 1916 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1917 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1918 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1919 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1920 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1921 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1922 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1923 | << "constructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1924 | return; |
| 1925 | } |
| 1926 | priority = Idx.getZExtValue(); |
| 1927 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1928 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1929 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1930 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1931 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1932 | return; |
| 1933 | } |
| 1934 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1935 | D->addAttr(::new (S.Context) |
| 1936 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1937 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1940 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1941 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1942 | if (Attr.getNumArgs() > 1) { |
| 1943 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1944 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1945 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1946 | |
| 1947 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1948 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1949 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1950 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1951 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1952 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1953 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1954 | << "destructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1955 | return; |
| 1956 | } |
| 1957 | priority = Idx.getZExtValue(); |
| 1958 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1959 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1960 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1961 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1962 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1963 | return; |
| 1964 | } |
| 1965 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1966 | D->addAttr(::new (S.Context) |
| 1967 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 1968 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1971 | template <typename AttrTy> |
| 1972 | static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr, |
| 1973 | const char *Name) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1974 | unsigned NumArgs = Attr.getNumArgs(); |
| 1975 | if (NumArgs > 1) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1976 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1977 | return; |
| 1978 | } |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1979 | |
| 1980 | // Handle the case where the attribute has a text message. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1981 | StringRef Str; |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1982 | if (NumArgs == 1) { |
| 1983 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1984 | if (!SE) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1985 | S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string) |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1986 | << Name; |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1987 | return; |
| 1988 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1989 | Str = SE->getString(); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1990 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1991 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1992 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 1993 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1996 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 1997 | const AttributeList &Attr) { |
| 1998 | unsigned NumArgs = Attr.getNumArgs(); |
| 1999 | if (NumArgs > 0) { |
| 2000 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 2001 | return; |
| 2002 | } |
| 2003 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2004 | D->addAttr(::new (S.Context) |
| 2005 | ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context, |
| 2006 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2009 | static void handleObjCRootClassAttr(Sema &S, Decl *D, |
| 2010 | const AttributeList &Attr) { |
| 2011 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 2012 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 2013 | return; |
| 2014 | } |
| 2015 | |
| 2016 | unsigned NumArgs = Attr.getNumArgs(); |
| 2017 | if (NumArgs > 0) { |
| 2018 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 2019 | return; |
| 2020 | } |
| 2021 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2022 | D->addAttr(::new (S.Context) |
| 2023 | ObjCRootClassAttr(Attr.getRange(), S.Context, |
| 2024 | Attr.getAttributeSpellingListIndex())); |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2027 | static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, |
| 2028 | const AttributeList &Attr) { |
Fariborz Jahanian | 341b8be | 2012-01-03 22:52:32 +0000 | [diff] [blame] | 2029 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 2030 | S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis); |
| 2031 | return; |
| 2032 | } |
| 2033 | |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2034 | unsigned NumArgs = Attr.getNumArgs(); |
| 2035 | if (NumArgs > 0) { |
| 2036 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 2037 | return; |
| 2038 | } |
| 2039 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2040 | D->addAttr(::new (S.Context) |
| 2041 | ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context, |
| 2042 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2045 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2046 | IdentifierInfo *Platform, |
| 2047 | VersionTuple Introduced, |
| 2048 | VersionTuple Deprecated, |
| 2049 | VersionTuple Obsoleted) { |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2050 | StringRef PlatformName |
| 2051 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2052 | if (PlatformName.empty()) |
| 2053 | PlatformName = Platform->getName(); |
| 2054 | |
| 2055 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2056 | // of these steps are needed). |
| 2057 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2058 | !(Introduced <= Deprecated)) { |
| 2059 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2060 | << 1 << PlatformName << Deprecated.getAsString() |
| 2061 | << 0 << Introduced.getAsString(); |
| 2062 | return true; |
| 2063 | } |
| 2064 | |
| 2065 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2066 | !(Introduced <= Obsoleted)) { |
| 2067 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2068 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2069 | << 0 << Introduced.getAsString(); |
| 2070 | return true; |
| 2071 | } |
| 2072 | |
| 2073 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2074 | !(Deprecated <= Obsoleted)) { |
| 2075 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2076 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2077 | << 1 << Deprecated.getAsString(); |
| 2078 | return true; |
| 2079 | } |
| 2080 | |
| 2081 | return false; |
| 2082 | } |
| 2083 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2084 | /// \brief Check whether the two versions match. |
| 2085 | /// |
| 2086 | /// If either version tuple is empty, then they are assumed to match. If |
| 2087 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2088 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2089 | bool BeforeIsOkay) { |
| 2090 | if (X.empty() || Y.empty()) |
| 2091 | return true; |
| 2092 | |
| 2093 | if (X == Y) |
| 2094 | return true; |
| 2095 | |
| 2096 | if (BeforeIsOkay && X < Y) |
| 2097 | return true; |
| 2098 | |
| 2099 | return false; |
| 2100 | } |
| 2101 | |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2102 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2103 | IdentifierInfo *Platform, |
| 2104 | VersionTuple Introduced, |
| 2105 | VersionTuple Deprecated, |
| 2106 | VersionTuple Obsoleted, |
| 2107 | bool IsUnavailable, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2108 | StringRef Message, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2109 | bool Override, |
| 2110 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2111 | VersionTuple MergedIntroduced = Introduced; |
| 2112 | VersionTuple MergedDeprecated = Deprecated; |
| 2113 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2114 | bool FoundAny = false; |
| 2115 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2116 | if (D->hasAttrs()) { |
| 2117 | AttrVec &Attrs = D->getAttrs(); |
| 2118 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 2119 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 2120 | if (!OldAA) { |
| 2121 | ++i; |
| 2122 | continue; |
| 2123 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2124 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2125 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2126 | if (OldPlatform != Platform) { |
| 2127 | ++i; |
| 2128 | continue; |
| 2129 | } |
| 2130 | |
| 2131 | FoundAny = true; |
| 2132 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2133 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2134 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2135 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2136 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2137 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 2138 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 2139 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 2140 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 72daa3f | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 2141 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2142 | if (Override) { |
| 2143 | int Which = -1; |
| 2144 | VersionTuple FirstVersion; |
| 2145 | VersionTuple SecondVersion; |
| 2146 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 2147 | Which = 0; |
| 2148 | FirstVersion = OldIntroduced; |
| 2149 | SecondVersion = Introduced; |
| 2150 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 2151 | Which = 1; |
| 2152 | FirstVersion = Deprecated; |
| 2153 | SecondVersion = OldDeprecated; |
| 2154 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 2155 | Which = 2; |
| 2156 | FirstVersion = Obsoleted; |
| 2157 | SecondVersion = OldObsoleted; |
| 2158 | } |
| 2159 | |
| 2160 | if (Which == -1) { |
| 2161 | Diag(OldAA->getLocation(), |
| 2162 | diag::warn_mismatched_availability_override_unavail) |
| 2163 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2164 | } else { |
| 2165 | Diag(OldAA->getLocation(), |
| 2166 | diag::warn_mismatched_availability_override) |
| 2167 | << Which |
| 2168 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2169 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 2170 | } |
| 2171 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2172 | } else { |
| 2173 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2174 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2175 | } |
| 2176 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2177 | Attrs.erase(Attrs.begin() + i); |
| 2178 | --e; |
| 2179 | continue; |
| 2180 | } |
| 2181 | |
| 2182 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2183 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2184 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2185 | |
| 2186 | if (MergedIntroduced2.empty()) |
| 2187 | MergedIntroduced2 = OldIntroduced; |
| 2188 | if (MergedDeprecated2.empty()) |
| 2189 | MergedDeprecated2 = OldDeprecated; |
| 2190 | if (MergedObsoleted2.empty()) |
| 2191 | MergedObsoleted2 = OldObsoleted; |
| 2192 | |
| 2193 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2194 | MergedIntroduced2, MergedDeprecated2, |
| 2195 | MergedObsoleted2)) { |
| 2196 | Attrs.erase(Attrs.begin() + i); |
| 2197 | --e; |
| 2198 | continue; |
| 2199 | } |
| 2200 | |
| 2201 | MergedIntroduced = MergedIntroduced2; |
| 2202 | MergedDeprecated = MergedDeprecated2; |
| 2203 | MergedObsoleted = MergedObsoleted2; |
| 2204 | ++i; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2205 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
| 2208 | if (FoundAny && |
| 2209 | MergedIntroduced == Introduced && |
| 2210 | MergedDeprecated == Deprecated && |
| 2211 | MergedObsoleted == Obsoleted) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2212 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2213 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2214 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2215 | MergedDeprecated, MergedObsoleted)) { |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2216 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 2217 | Introduced, Deprecated, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2218 | Obsoleted, IsUnavailable, Message, |
| 2219 | AttrSpellingListIndex); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2220 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2221 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2224 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2225 | const AttributeList &Attr) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2226 | IdentifierInfo *Platform = Attr.getParameterName(); |
| 2227 | SourceLocation PlatformLoc = Attr.getParameterLoc(); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2228 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2229 | |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2230 | if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2231 | S.Diag(PlatformLoc, diag::warn_availability_unknown_platform) |
| 2232 | << Platform; |
| 2233 | |
Rafael Espindola | 8c4222a | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2234 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 2235 | if (!ND) { |
| 2236 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2237 | return; |
| 2238 | } |
| 2239 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2240 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2241 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2242 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2243 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2244 | StringRef Str; |
| 2245 | const StringLiteral *SE = |
| 2246 | dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr()); |
| 2247 | if (SE) |
| 2248 | Str = SE->getString(); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2249 | |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2250 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2251 | Platform, |
| 2252 | Introduced.Version, |
| 2253 | Deprecated.Version, |
| 2254 | Obsoleted.Version, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2255 | IsUnavailable, Str, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2256 | /*Override=*/false, |
| 2257 | Index); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2258 | if (NewAttr) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2259 | D->addAttr(NewAttr); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2262 | template <class T> |
| 2263 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2264 | typename T::VisibilityType value, |
| 2265 | unsigned attrSpellingListIndex) { |
| 2266 | T *existingAttr = D->getAttr<T>(); |
| 2267 | if (existingAttr) { |
| 2268 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2269 | if (existingValue == value) |
| 2270 | return NULL; |
| 2271 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2272 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2273 | D->dropAttr<T>(); |
| 2274 | } |
| 2275 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2276 | } |
| 2277 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2278 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2279 | VisibilityAttr::VisibilityType Vis, |
| 2280 | unsigned AttrSpellingListIndex) { |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2281 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2282 | AttrSpellingListIndex); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2283 | } |
| 2284 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2285 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2286 | TypeVisibilityAttr::VisibilityType Vis, |
| 2287 | unsigned AttrSpellingListIndex) { |
| 2288 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2289 | AttrSpellingListIndex); |
| 2290 | } |
| 2291 | |
| 2292 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2293 | bool isTypeVisibility) { |
| 2294 | // Visibility attributes don't mean anything on a typedef. |
| 2295 | if (isa<TypedefNameDecl>(D)) { |
| 2296 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2297 | << Attr.getName(); |
| 2298 | return; |
| 2299 | } |
| 2300 | |
| 2301 | // 'type_visibility' can only go on a type or namespace. |
| 2302 | if (isTypeVisibility && |
| 2303 | !(isa<TagDecl>(D) || |
| 2304 | isa<ObjCInterfaceDecl>(D) || |
| 2305 | isa<NamespaceDecl>(D))) { |
| 2306 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2307 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2308 | return; |
| 2309 | } |
| 2310 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2311 | // check the attribute arguments. |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2312 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2313 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2314 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2315 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2316 | Arg = Arg->IgnoreParenCasts(); |
| 2317 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2318 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2319 | if (!Str || !Str->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2320 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2321 | << (isTypeVisibility ? "type_visibility" : "visibility") << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2322 | return; |
| 2323 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2324 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2325 | StringRef TypeStr = Str->getString(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2326 | VisibilityAttr::VisibilityType type; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2327 | |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2328 | if (TypeStr == "default") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2329 | type = VisibilityAttr::Default; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2330 | else if (TypeStr == "hidden") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2331 | type = VisibilityAttr::Hidden; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2332 | else if (TypeStr == "internal") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2333 | type = VisibilityAttr::Hidden; // FIXME |
John McCall | 4188760 | 2012-01-29 01:20:30 +0000 | [diff] [blame] | 2334 | else if (TypeStr == "protected") { |
| 2335 | // Complain about attempts to use protected visibility on targets |
| 2336 | // (like Darwin) that don't support it. |
| 2337 | if (!S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2338 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2339 | type = VisibilityAttr::Default; |
| 2340 | } else { |
| 2341 | type = VisibilityAttr::Protected; |
| 2342 | } |
| 2343 | } else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2344 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2345 | return; |
| 2346 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2347 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2348 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2349 | clang::Attr *newAttr; |
| 2350 | if (isTypeVisibility) { |
| 2351 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2352 | (TypeVisibilityAttr::VisibilityType) type, |
| 2353 | Index); |
| 2354 | } else { |
| 2355 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2356 | } |
| 2357 | if (newAttr) |
| 2358 | D->addAttr(newAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2359 | } |
| 2360 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2361 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2362 | const AttributeList &Attr) { |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2363 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 2364 | if (!method) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2365 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2366 | << ExpectedMethod; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2367 | return; |
| 2368 | } |
| 2369 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2370 | if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) { |
| 2371 | if (!Attr.getParameterName() && Attr.getNumArgs() == 1) { |
| 2372 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2373 | << "objc_method_family" << 1; |
| 2374 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2375 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2376 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2377 | Attr.setInvalid(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2378 | return; |
| 2379 | } |
| 2380 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2381 | StringRef param = Attr.getParameterName()->getName(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2382 | ObjCMethodFamilyAttr::FamilyKind family; |
| 2383 | if (param == "none") |
| 2384 | family = ObjCMethodFamilyAttr::OMF_None; |
| 2385 | else if (param == "alloc") |
| 2386 | family = ObjCMethodFamilyAttr::OMF_alloc; |
| 2387 | else if (param == "copy") |
| 2388 | family = ObjCMethodFamilyAttr::OMF_copy; |
| 2389 | else if (param == "init") |
| 2390 | family = ObjCMethodFamilyAttr::OMF_init; |
| 2391 | else if (param == "mutableCopy") |
| 2392 | family = ObjCMethodFamilyAttr::OMF_mutableCopy; |
| 2393 | else if (param == "new") |
| 2394 | family = ObjCMethodFamilyAttr::OMF_new; |
| 2395 | else { |
| 2396 | // Just warn and ignore it. This is future-proof against new |
| 2397 | // families being used in system headers. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2398 | S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2399 | return; |
| 2400 | } |
| 2401 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2402 | if (family == ObjCMethodFamilyAttr::OMF_init && |
| 2403 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2404 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2405 | << method->getResultType(); |
| 2406 | // Ignore the attribute. |
| 2407 | return; |
| 2408 | } |
| 2409 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2410 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2411 | S.Context, family)); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2414 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 2415 | const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2416 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2417 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2418 | |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2419 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 2420 | if (OCI == 0) { |
| 2421 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 2422 | return; |
| 2423 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2424 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2425 | D->addAttr(::new (S.Context) |
| 2426 | ObjCExceptionAttr(Attr.getRange(), S.Context, |
| 2427 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2430 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2431 | if (Attr.getNumArgs() != 0) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 2432 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2433 | return; |
| 2434 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2435 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2436 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2437 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2438 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2439 | return; |
| 2440 | } |
| 2441 | } |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2442 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2443 | QualType T = PD->getType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2444 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2445 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2446 | return; |
| 2447 | } |
| 2448 | } |
| 2449 | else { |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2450 | // It is okay to include this attribute on properties, e.g.: |
| 2451 | // |
| 2452 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2453 | // |
| 2454 | // In this case it follows tradition and suppresses an error in the above |
| 2455 | // case. |
Fariborz Jahanian | 9b2eb7b | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2456 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2457 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2458 | D->addAttr(::new (S.Context) |
| 2459 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2460 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2463 | static void |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2464 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2465 | if (Attr.getNumArgs() != 0) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 2466 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2467 | return; |
| 2468 | } |
| 2469 | |
| 2470 | if (!isa<FunctionDecl>(D)) { |
| 2471 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 2472 | return; |
| 2473 | } |
| 2474 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2475 | D->addAttr(::new (S.Context) |
| 2476 | OverloadableAttr(Attr.getRange(), S.Context, |
| 2477 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2480 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2481 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2482 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2483 | << "blocks" << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2484 | return; |
| 2485 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2486 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2487 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2488 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2489 | return; |
| 2490 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2491 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2492 | BlocksAttr::BlockType type; |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 2493 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2494 | type = BlocksAttr::ByRef; |
| 2495 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2496 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2497 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2498 | return; |
| 2499 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2500 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2501 | D->addAttr(::new (S.Context) |
| 2502 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2503 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2506 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2507 | // check the attribute arguments. |
| 2508 | if (Attr.getNumArgs() > 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2509 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2510 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2511 | } |
| 2512 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2513 | unsigned sentinel = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2514 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2515 | Expr *E = Attr.getArg(0); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2516 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2517 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2518 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2519 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2520 | << "sentinel" << 1 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2521 | return; |
| 2522 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2523 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2524 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2525 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2526 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2527 | return; |
| 2528 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2529 | |
| 2530 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2533 | unsigned nullPos = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2534 | if (Attr.getNumArgs() > 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2535 | Expr *E = Attr.getArg(1); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2536 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2537 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2538 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2539 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2540 | << "sentinel" << 2 << 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 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2600 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2601 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2602 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2603 | return; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2604 | |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 2605 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2606 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Kaelyn Uhrain | d449c79 | 2012-11-13 00:18:47 +0000 | [diff] [blame] | 2607 | << Attr.getName() << ExpectedFunctionMethodOrClass; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2608 | return; |
| 2609 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2610 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2611 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2612 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2613 | << Attr.getName() << 0; |
Nuno Lopes | f857798 | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2614 | return; |
| 2615 | } |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2616 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2617 | if (MD->getResultType()->isVoidType()) { |
| 2618 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2619 | << Attr.getName() << 1; |
| 2620 | return; |
| 2621 | } |
| 2622 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2623 | D->addAttr(::new (S.Context) |
| 2624 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2625 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2628 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2629 | // check the attribute arguments. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2630 | if (Attr.hasParameterOrArguments()) { |
| 2631 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2632 | return; |
| 2633 | } |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2634 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2635 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
Fariborz Jahanian | 13c7fcc | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 2636 | if (isa<CXXRecordDecl>(D)) { |
| 2637 | D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| 2638 | return; |
| 2639 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2640 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2641 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | f23ecd9 | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 2642 | return; |
| 2643 | } |
| 2644 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2645 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 2646 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2647 | nd->addAttr(::new (S.Context) |
| 2648 | WeakAttr(Attr.getRange(), S.Context, |
| 2649 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2652 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2653 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2654 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2655 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2656 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2657 | |
| 2658 | // weak_import only applies to variable & function declarations. |
| 2659 | bool isDef = false; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2660 | if (!D->canBeWeakImported(isDef)) { |
| 2661 | if (isDef) |
| 2662 | S.Diag(Attr.getLoc(), |
| 2663 | diag::warn_attribute_weak_import_invalid_on_definition) |
| 2664 | << "weak_import" << 2 /*variable and function*/; |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2665 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2666 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 90eed21 | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2667 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2668 | // Nothing to warn about here. |
| 2669 | } else |
Fariborz Jahanian | c034974 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2670 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2671 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2672 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2673 | return; |
| 2674 | } |
| 2675 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2676 | D->addAttr(::new (S.Context) |
| 2677 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2678 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2679 | } |
| 2680 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2681 | // Handles reqd_work_group_size and work_group_size_hint. |
| 2682 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2683 | const AttributeList &Attr) { |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2684 | assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2685 | || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint); |
| 2686 | |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2687 | // Attribute has 3 arguments. |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2688 | if (!checkAttributeNumArgs(S, Attr, 3)) return; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2689 | |
| 2690 | unsigned WGSize[3]; |
| 2691 | for (unsigned i = 0; i < 3; ++i) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2692 | Expr *E = Attr.getArg(i); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2693 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2694 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2695 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2696 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2697 | << Attr.getName()->getName() << E->getSourceRange(); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2698 | return; |
| 2699 | } |
| 2700 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 2701 | } |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2702 | |
| 2703 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2704 | && D->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 2705 | ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>(); |
| 2706 | if (!(A->getXDim() == WGSize[0] && |
| 2707 | A->getYDim() == WGSize[1] && |
| 2708 | A->getZDim() == WGSize[2])) { |
| 2709 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2710 | Attr.getName(); |
| 2711 | } |
| 2712 | } |
| 2713 | |
| 2714 | if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint |
| 2715 | && D->hasAttr<WorkGroupSizeHintAttr>()) { |
| 2716 | WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>(); |
| 2717 | if (!(A->getXDim() == WGSize[0] && |
| 2718 | A->getYDim() == WGSize[1] && |
| 2719 | A->getZDim() == WGSize[2])) { |
| 2720 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2721 | Attr.getName(); |
| 2722 | } |
| 2723 | } |
| 2724 | |
| 2725 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize) |
| 2726 | D->addAttr(::new (S.Context) |
| 2727 | ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2728 | WGSize[0], WGSize[1], WGSize[2], |
| 2729 | Attr.getAttributeSpellingListIndex())); |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2730 | else |
| 2731 | D->addAttr(::new (S.Context) |
| 2732 | WorkGroupSizeHintAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2733 | WGSize[0], WGSize[1], WGSize[2], |
| 2734 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2735 | } |
| 2736 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2737 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2738 | StringRef Name, |
| 2739 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2740 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2741 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2742 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2743 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2744 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2745 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2746 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2747 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2748 | AttrSpellingListIndex); |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2749 | } |
| 2750 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2751 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2752 | // Attribute has no arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2753 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2754 | return; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2755 | |
| 2756 | // Make sure that there is a string literal as the sections's single |
| 2757 | // argument. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2758 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2759 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2760 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2761 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section"; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2762 | return; |
| 2763 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2764 | |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2765 | // If the target wants to validate the section specifier, make it happen. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2766 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString()); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2767 | if (!Error.empty()) { |
| 2768 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 2769 | << Error; |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2770 | return; |
| 2771 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2772 | |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2773 | // This attribute cannot be applied to local variables. |
| 2774 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 2775 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 2776 | return; |
| 2777 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2778 | |
| 2779 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2780 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2781 | SE->getString(), Index); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2782 | if (NewAttr) |
| 2783 | D->addAttr(NewAttr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2786 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2787 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2788 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2789 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2790 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2791 | return; |
| 2792 | } |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2793 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2794 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2795 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2796 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2797 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2798 | D->addAttr(::new (S.Context) |
| 2799 | NoThrowAttr(Attr.getRange(), S.Context, |
| 2800 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2801 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2804 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2805 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2806 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2807 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2808 | return; |
| 2809 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2810 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2811 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2812 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2813 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2814 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2815 | D->addAttr(::new (S.Context) |
| 2816 | ConstAttr(Attr.getRange(), S.Context, |
| 2817 | Attr.getAttributeSpellingListIndex() )); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2818 | } |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2821 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2822 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2823 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2824 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2825 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2826 | D->addAttr(::new (S.Context) |
| 2827 | PureAttr(Attr.getRange(), S.Context, |
| 2828 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2831 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2832 | if (!Attr.getParameterName()) { |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2833 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2834 | return; |
| 2835 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2836 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2837 | if (Attr.getNumArgs() != 0) { |
| 2838 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2839 | return; |
| 2840 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2841 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2842 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2843 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2844 | if (!VD || !VD->hasLocalStorage()) { |
| 2845 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 2846 | return; |
| 2847 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 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 |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2852 | = S.LookupSingleName(S.TUScope, Attr.getParameterName(), |
| 2853 | Attr.getParameterLoc(), Sema::LookupOrdinaryName); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2854 | if (!CleanupDecl) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2855 | S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2856 | Attr.getParameterName(); |
| 2857 | return; |
| 2858 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2859 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2860 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 2861 | if (!FD) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2862 | S.Diag(Attr.getParameterLoc(), |
| 2863 | diag::err_attribute_cleanup_arg_not_function) |
| 2864 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2865 | return; |
| 2866 | } |
| 2867 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2868 | if (FD->getNumParams() != 1) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2869 | S.Diag(Attr.getParameterLoc(), |
| 2870 | diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2871 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2872 | return; |
| 2873 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2874 | |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2875 | // We're currently more strict than GCC about what function types we accept. |
| 2876 | // If this ever proves to be a problem it should be easy to fix. |
| 2877 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2878 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2879 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2880 | ParamTy, Ty) != Sema::Compatible) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2881 | S.Diag(Attr.getParameterLoc(), |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2882 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 2883 | Attr.getParameterName() << ParamTy << Ty; |
| 2884 | return; |
| 2885 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2886 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2887 | D->addAttr(::new (S.Context) |
| 2888 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2889 | Attr.getAttributeSpellingListIndex())); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2890 | S.MarkFunctionReferenced(Attr.getParameterLoc(), FD); |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 2891 | S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc()); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2894 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2895 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2896 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2897 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2898 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2899 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2900 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2901 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2902 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2903 | return; |
| 2904 | } |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2905 | |
| 2906 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2907 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2908 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 2909 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2910 | unsigned FirstIdx = 1; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2911 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2912 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2913 | Expr *IdxExpr = Attr.getArg(0); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2914 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2915 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 2916 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2917 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 2918 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 2919 | return; |
| 2920 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2921 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2922 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
| 2923 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 2924 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 2925 | return; |
| 2926 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2927 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2928 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2929 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2930 | if (HasImplicitThisParam) { |
| 2931 | if (ArgIdx == 0) { |
| 2932 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 2933 | << "format_arg" << IdxExpr->getSourceRange(); |
| 2934 | return; |
| 2935 | } |
| 2936 | ArgIdx--; |
| 2937 | } |
| 2938 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2939 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2940 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2941 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2942 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2943 | if (not_nsstring_type && |
| 2944 | !isCFStringType(Ty, S.Context) && |
| 2945 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2946 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2947 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2948 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2949 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2950 | << IdxExpr->getSourceRange(); |
| 2951 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2952 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2953 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2954 | if (!isNSStringType(Ty, S.Context) && |
| 2955 | !isCFStringType(Ty, S.Context) && |
| 2956 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2957 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2958 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2959 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2960 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2961 | << IdxExpr->getSourceRange(); |
| 2962 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2965 | D->addAttr(::new (S.Context) |
| 2966 | FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(), |
| 2967 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2968 | } |
| 2969 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2970 | enum FormatAttrKind { |
| 2971 | CFStringFormat, |
| 2972 | NSStringFormat, |
| 2973 | StrftimeFormat, |
| 2974 | SupportedFormat, |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2975 | IgnoredFormat, |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2976 | InvalidFormat |
| 2977 | }; |
| 2978 | |
| 2979 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2980 | /// types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2981 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2982 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2983 | // Check for formats that get handled specially. |
| 2984 | .Case("NSString", NSStringFormat) |
| 2985 | .Case("CFString", CFStringFormat) |
| 2986 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2987 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2988 | // Otherwise, check for supported formats. |
| 2989 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2990 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2991 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2992 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2993 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2994 | .Default(InvalidFormat); |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2995 | } |
| 2996 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2997 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2998 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2999 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 3000 | const AttributeList &Attr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3001 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3002 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3003 | return; |
| 3004 | } |
| 3005 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3006 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3007 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3008 | Attr.setInvalid(); |
| 3009 | return; |
| 3010 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3011 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3012 | if (S.Context.getAsArrayType(T)) |
| 3013 | T = S.Context.getBaseElementType(T); |
| 3014 | if (!T->getAs<RecordType>()) { |
| 3015 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3016 | Attr.setInvalid(); |
| 3017 | return; |
| 3018 | } |
| 3019 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3020 | if (Attr.getNumArgs() != 1) { |
| 3021 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 3022 | Attr.setInvalid(); |
| 3023 | return; |
| 3024 | } |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3025 | Expr *priorityExpr = Attr.getArg(0); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3026 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3027 | llvm::APSInt priority(32); |
| 3028 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 3029 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
| 3030 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 3031 | << "init_priority" << priorityExpr->getSourceRange(); |
| 3032 | Attr.setInvalid(); |
| 3033 | return; |
| 3034 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 3035 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3036 | if (prioritynum < 101 || prioritynum > 65535) { |
| 3037 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 3038 | << priorityExpr->getSourceRange(); |
| 3039 | Attr.setInvalid(); |
| 3040 | return; |
| 3041 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3042 | D->addAttr(::new (S.Context) |
| 3043 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 3044 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3045 | } |
| 3046 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3047 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3048 | int FormatIdx, int FirstArg, |
| 3049 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3050 | // Check whether we already have an equivalent format attribute. |
| 3051 | for (specific_attr_iterator<FormatAttr> |
| 3052 | i = D->specific_attr_begin<FormatAttr>(), |
| 3053 | e = D->specific_attr_end<FormatAttr>(); |
| 3054 | i != e ; ++i) { |
| 3055 | FormatAttr *f = *i; |
| 3056 | if (f->getType() == Format && |
| 3057 | f->getFormatIdx() == FormatIdx && |
| 3058 | f->getFirstArg() == FirstArg) { |
| 3059 | // If we don't have a valid location for this attribute, adopt the |
| 3060 | // location. |
| 3061 | if (f->getLocation().isInvalid()) |
| 3062 | f->setRange(Range); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3063 | return NULL; |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3064 | } |
| 3065 | } |
| 3066 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3067 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg, |
| 3068 | AttrSpellingListIndex); |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3069 | } |
| 3070 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3071 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3072 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3073 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3074 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3075 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3076 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3077 | << "format" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3078 | return; |
| 3079 | } |
| 3080 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3081 | if (Attr.getNumArgs() != 2) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3082 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3083 | return; |
| 3084 | } |
| 3085 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3086 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3087 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3088 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3089 | return; |
| 3090 | } |
| 3091 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3092 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3093 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3094 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 3095 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3096 | unsigned FirstIdx = 1; |
| 3097 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3098 | StringRef Format = Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3099 | |
| 3100 | // Normalize the argument, __foo__ becomes foo. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3101 | if (Format.startswith("__") && Format.endswith("__")) |
| 3102 | Format = Format.substr(2, Format.size() - 4); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3103 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3104 | // Check for supported formats. |
| 3105 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3106 | |
| 3107 | if (Kind == IgnoredFormat) |
| 3108 | return; |
| 3109 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3110 | if (Kind == InvalidFormat) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3111 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3112 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3113 | return; |
| 3114 | } |
| 3115 | |
| 3116 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3117 | Expr *IdxExpr = Attr.getArg(0); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3118 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3119 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 3120 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3121 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3122 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3123 | return; |
| 3124 | } |
| 3125 | |
| 3126 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3127 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3128 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3129 | return; |
| 3130 | } |
| 3131 | |
| 3132 | // FIXME: Do we need to bounds check? |
| 3133 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3134 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3135 | if (HasImplicitThisParam) { |
| 3136 | if (ArgIdx == 0) { |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3137 | S.Diag(Attr.getLoc(), |
| 3138 | diag::err_format_attribute_implicit_this_format_string) |
| 3139 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3140 | return; |
| 3141 | } |
| 3142 | ArgIdx--; |
| 3143 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3144 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3145 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3146 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3147 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3148 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3149 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3150 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3151 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3152 | return; |
| 3153 | } |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3154 | } else if (Kind == NSStringFormat) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3155 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3156 | // semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3157 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3158 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3159 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3160 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3161 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3162 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3163 | } else if (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3164 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3165 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3166 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3167 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3168 | return; |
| 3169 | } |
| 3170 | |
| 3171 | // check the 3rd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3172 | Expr *FirstArgExpr = Attr.getArg(1); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3173 | llvm::APSInt FirstArg(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3174 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 3175 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3176 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3177 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3178 | return; |
| 3179 | } |
| 3180 | |
| 3181 | // check if the function is variadic if the 3rd argument non-zero |
| 3182 | if (FirstArg != 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3183 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3184 | ++NumArgs; // +1 for ... |
| 3185 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3186 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3187 | return; |
| 3188 | } |
| 3189 | } |
| 3190 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3191 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3192 | // variable the input is just the current time + the format string. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3193 | if (Kind == StrftimeFormat) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3194 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3195 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 3196 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3197 | return; |
| 3198 | } |
| 3199 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3200 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3201 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3202 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3203 | return; |
| 3204 | } |
| 3205 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3206 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format, |
| 3207 | Idx.getZExtValue(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3208 | FirstArg.getZExtValue(), |
| 3209 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3210 | if (NewAttr) |
| 3211 | D->addAttr(NewAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3212 | } |
| 3213 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3214 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 3215 | const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3216 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3217 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3218 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3219 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3220 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3221 | // Try to find the underlying union declaration. |
| 3222 | RecordDecl *RD = 0; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3223 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3224 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3225 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3226 | else |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3227 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3228 | |
| 3229 | if (!RD || !RD->isUnion()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3230 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3231 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3232 | return; |
| 3233 | } |
| 3234 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3235 | if (!RD->isCompleteDefinition()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3236 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3237 | diag::warn_transparent_union_attribute_not_definition); |
| 3238 | return; |
| 3239 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3240 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3241 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3242 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3243 | if (Field == FieldEnd) { |
| 3244 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3245 | return; |
| 3246 | } |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3247 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3248 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3249 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3250 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3251 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3252 | diag::warn_transparent_union_attribute_floating) |
| 3253 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3254 | return; |
| 3255 | } |
| 3256 | |
| 3257 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3258 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3259 | for (; Field != FieldEnd; ++Field) { |
| 3260 | QualType FieldType = Field->getType(); |
| 3261 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 3262 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 3263 | // Warn if we drop the attribute. |
| 3264 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3265 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3266 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3267 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3268 | diag::warn_transparent_union_attribute_field_size_align) |
| 3269 | << isSize << Field->getDeclName() << FieldBits; |
| 3270 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3271 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3272 | diag::note_transparent_union_first_field_size_align) |
| 3273 | << isSize << FirstBits; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3274 | return; |
| 3275 | } |
| 3276 | } |
| 3277 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3278 | RD->addAttr(::new (S.Context) |
| 3279 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 3280 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3283 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3284 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3285 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3286 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3287 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3288 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3289 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3290 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3291 | // Make sure that there is a string literal as the annotation's single |
| 3292 | // argument. |
| 3293 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3294 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate"; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3295 | return; |
| 3296 | } |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3297 | |
| 3298 | // Don't duplicate annotations that are already set. |
| 3299 | for (specific_attr_iterator<AnnotateAttr> |
| 3300 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 3301 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
| 3302 | if ((*i)->getAnnotation() == SE->getString()) |
| 3303 | return; |
| 3304 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3305 | |
| 3306 | D->addAttr(::new (S.Context) |
| 3307 | AnnotateAttr(Attr.getRange(), S.Context, SE->getString(), |
| 3308 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3311 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3312 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3313 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3314 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3315 | return; |
| 3316 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3317 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3318 | if (Attr.getNumArgs() == 0) { |
| 3319 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 3320 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 3321 | return; |
| 3322 | } |
| 3323 | |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame^] | 3324 | Expr *E = Attr.getArg(0); |
| 3325 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3326 | S.Diag(Attr.getEllipsisLoc(), |
| 3327 | diag::err_pack_expansion_without_parameter_packs); |
| 3328 | return; |
| 3329 | } |
| 3330 | |
| 3331 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 3332 | return; |
| 3333 | |
| 3334 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 3335 | Attr.isPackExpansion()); |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3336 | } |
| 3337 | |
| 3338 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame^] | 3339 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3340 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3341 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3342 | |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3343 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3344 | if (TmpAttr.isAlignas()) { |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3345 | // C++11 [dcl.align]p1: |
| 3346 | // An alignment-specifier may be applied to a variable or to a class |
| 3347 | // data member, but it shall not be applied to a bit-field, a function |
| 3348 | // parameter, the formal parameter of a catch clause, or a variable |
| 3349 | // declared with the register storage class specifier. An |
| 3350 | // alignment-specifier may also be applied to the declaration of a class |
| 3351 | // or enumeration type. |
| 3352 | // C11 6.7.5/2: |
| 3353 | // An alignment attribute shall not be specified in a declaration of |
| 3354 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3355 | // object declared with the register storage-class specifier. |
| 3356 | int DiagKind = -1; |
| 3357 | if (isa<ParmVarDecl>(D)) { |
| 3358 | DiagKind = 0; |
| 3359 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3360 | if (VD->getStorageClass() == SC_Register) |
| 3361 | DiagKind = 1; |
| 3362 | if (VD->isExceptionVariable()) |
| 3363 | DiagKind = 2; |
| 3364 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 3365 | if (FD->isBitField()) |
| 3366 | DiagKind = 3; |
| 3367 | } else if (!isa<TagDecl>(D)) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3368 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) |
| 3369 | << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'") |
Richard Smith | 5f838aa | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3370 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3371 | : ExpectedVariableFieldOrTag); |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3372 | return; |
| 3373 | } |
| 3374 | if (DiagKind != -1) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3375 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 3376 | << TmpAttr.isC11() << DiagKind; |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3377 | return; |
| 3378 | } |
| 3379 | } |
| 3380 | |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3381 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3382 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame^] | 3383 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3384 | AA->setPackExpansion(IsPackExpansion); |
| 3385 | D->addAttr(AA); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3386 | return; |
| 3387 | } |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3388 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3389 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3390 | llvm::APSInt Alignment(32); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3391 | ExprResult ICE |
| 3392 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3393 | diag::err_aligned_attribute_argument_not_int, |
| 3394 | /*AllowFold*/ false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3395 | if (ICE.isInvalid()) |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3396 | return; |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3397 | |
| 3398 | // C++11 [dcl.align]p2: |
| 3399 | // -- if the constant expression evaluates to zero, the alignment |
| 3400 | // specifier shall have no effect |
| 3401 | // C11 6.7.5p6: |
| 3402 | // An alignment specification of zero has no effect. |
| 3403 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 3404 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3405 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 3406 | << E->getSourceRange(); |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3407 | return; |
| 3408 | } |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3409 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3410 | if (TmpAttr.isDeclspec()) { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3411 | // We've already verified it's a power of 2, now let's make sure it's |
| 3412 | // 8192 or less. |
| 3413 | if (Alignment.getZExtValue() > 8192) { |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3414 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3415 | << E->getSourceRange(); |
| 3416 | return; |
| 3417 | } |
| 3418 | } |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3419 | |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame^] | 3420 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 3421 | ICE.take(), SpellingListIndex); |
| 3422 | AA->setPackExpansion(IsPackExpansion); |
| 3423 | D->addAttr(AA); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3426 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame^] | 3427 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3428 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3429 | // FIXME: Perform checking of type validity |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame^] | 3430 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3431 | SpellingListIndex); |
| 3432 | AA->setPackExpansion(IsPackExpansion); |
| 3433 | D->addAttr(AA); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3434 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3435 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3436 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3437 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3438 | |
| 3439 | QualType Ty; |
| 3440 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3441 | Ty = VD->getType(); |
| 3442 | else |
| 3443 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
| 3444 | if (Ty->isDependentType()) |
| 3445 | return; |
| 3446 | |
| 3447 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3448 | // The combined effect of all alignment attributes in a declaration shall |
| 3449 | // not specify an alignment that is less strict than the alignment that |
| 3450 | // would otherwise be required for the entity being declared. |
| 3451 | AlignedAttr *AlignasAttr = 0; |
| 3452 | unsigned Align = 0; |
| 3453 | for (specific_attr_iterator<AlignedAttr> |
| 3454 | I = D->specific_attr_begin<AlignedAttr>(), |
| 3455 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 3456 | if (I->isAlignmentDependent()) |
| 3457 | return; |
| 3458 | if (I->isAlignas()) |
| 3459 | AlignasAttr = *I; |
| 3460 | Align = std::max(Align, I->getAlignment(Context)); |
| 3461 | } |
| 3462 | |
| 3463 | if (AlignasAttr && Align) { |
| 3464 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 3465 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 3466 | if (NaturalAlign > RequestedAlign) |
| 3467 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 3468 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 3469 | } |
| 3470 | } |
| 3471 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3472 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3473 | /// type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3474 | /// |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3475 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3476 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3477 | /// HImode, not an intermediate pointer. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3478 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3479 | // This attribute isn't documented, but glibc uses it. It changes |
| 3480 | // the width of an int or unsigned int to the specified size. |
| 3481 | |
| 3482 | // Check that there aren't any arguments |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3483 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3484 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3485 | |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3486 | |
| 3487 | IdentifierInfo *Name = Attr.getParameterName(); |
| 3488 | if (!Name) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3489 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3490 | return; |
| 3491 | } |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3492 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3493 | StringRef Str = Attr.getParameterName()->getName(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3494 | |
| 3495 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3496 | if (Str.startswith("__") && Str.endswith("__")) |
| 3497 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3498 | |
| 3499 | unsigned DestWidth = 0; |
| 3500 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3501 | bool ComplexMode = false; |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3502 | switch (Str.size()) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3503 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3504 | switch (Str[0]) { |
| 3505 | case 'Q': DestWidth = 8; break; |
| 3506 | case 'H': DestWidth = 16; break; |
| 3507 | case 'S': DestWidth = 32; break; |
| 3508 | case 'D': DestWidth = 64; break; |
| 3509 | case 'X': DestWidth = 96; break; |
| 3510 | case 'T': DestWidth = 128; break; |
| 3511 | } |
| 3512 | if (Str[1] == 'F') { |
| 3513 | IntegerMode = false; |
| 3514 | } else if (Str[1] == 'C') { |
| 3515 | IntegerMode = false; |
| 3516 | ComplexMode = true; |
| 3517 | } else if (Str[1] != 'I') { |
| 3518 | DestWidth = 0; |
| 3519 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3520 | break; |
| 3521 | case 4: |
| 3522 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3523 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3524 | if (Str == "word") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3525 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3526 | else if (Str == "byte") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3527 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3528 | break; |
| 3529 | case 7: |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3530 | if (Str == "pointer") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3531 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3532 | break; |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3533 | case 11: |
| 3534 | if (Str == "unwind_word") |
Rafael Espindola | 0b1de54 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3535 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3536 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3537 | } |
| 3538 | |
| 3539 | QualType OldTy; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3540 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3541 | OldTy = TD->getUnderlyingType(); |
| 3542 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3543 | OldTy = VD->getType(); |
| 3544 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3545 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3546 | << "mode" << Attr.getRange(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3547 | return; |
| 3548 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3549 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3550 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3551 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3552 | else if (IntegerMode) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3553 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3554 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3555 | } else if (ComplexMode) { |
| 3556 | if (!OldTy->isComplexType()) |
| 3557 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3558 | } else { |
| 3559 | if (!OldTy->isFloatingType()) |
| 3560 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3561 | } |
| 3562 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3563 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3564 | // and friends, at least with glibc. |
| 3565 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 3566 | // width on unusual platforms. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3567 | // FIXME: Make sure floating-point mappings are accurate |
| 3568 | // FIXME: Support XF and TF types |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3569 | QualType NewTy; |
| 3570 | switch (DestWidth) { |
| 3571 | case 0: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3572 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3573 | return; |
| 3574 | default: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3575 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3576 | return; |
| 3577 | case 8: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3578 | if (!IntegerMode) { |
| 3579 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3580 | return; |
| 3581 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3582 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3583 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3584 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3585 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3586 | break; |
| 3587 | case 16: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3588 | if (!IntegerMode) { |
| 3589 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3590 | return; |
| 3591 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3592 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3593 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3594 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3595 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3596 | break; |
| 3597 | case 32: |
| 3598 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3599 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3600 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3601 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3602 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3603 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3604 | break; |
| 3605 | case 64: |
| 3606 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3607 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3608 | else if (OldTy->isSignedIntegerType()) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3609 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3610 | NewTy = S.Context.LongTy; |
| 3611 | else |
| 3612 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3613 | else |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3614 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3615 | NewTy = S.Context.UnsignedLongTy; |
| 3616 | else |
| 3617 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3618 | break; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3619 | case 96: |
| 3620 | NewTy = S.Context.LongDoubleTy; |
| 3621 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3622 | case 128: |
| 3623 | if (!IntegerMode) { |
| 3624 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3625 | return; |
| 3626 | } |
Anders Carlsson | f5f7d86 | 2009-12-29 07:07:36 +0000 | [diff] [blame] | 3627 | if (OldTy->isSignedIntegerType()) |
| 3628 | NewTy = S.Context.Int128Ty; |
| 3629 | else |
| 3630 | NewTy = S.Context.UnsignedInt128Ty; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3631 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3634 | if (ComplexMode) { |
| 3635 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3636 | } |
| 3637 | |
| 3638 | // Install the new type. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3639 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 3640 | // FIXME: preserve existing source info. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3641 | TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy)); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 3642 | } else |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3643 | cast<ValueDecl>(D)->setType(NewTy); |
| 3644 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3645 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3646 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3647 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3648 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3649 | return; |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 3650 | |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3651 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3652 | if (!VD->hasGlobalStorage()) |
| 3653 | S.Diag(Attr.getLoc(), |
| 3654 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3655 | << Attr.getName(); |
| 3656 | } else if (!isFunctionOrMethod(D)) { |
| 3657 | S.Diag(Attr.getLoc(), |
| 3658 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3659 | << Attr.getName(); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3660 | return; |
| 3661 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3662 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3663 | D->addAttr(::new (S.Context) |
| 3664 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3665 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3666 | } |
| 3667 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3668 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3669 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3670 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3671 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3672 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3673 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3674 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3675 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3676 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3677 | return; |
| 3678 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3679 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3680 | D->addAttr(::new (S.Context) |
| 3681 | NoInlineAttr(Attr.getRange(), S.Context, |
| 3682 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3683 | } |
| 3684 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3685 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 3686 | const AttributeList &Attr) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3687 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3688 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3689 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3690 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3691 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3692 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3693 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3694 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3695 | return; |
| 3696 | } |
| 3697 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3698 | D->addAttr(::new (S.Context) |
| 3699 | NoInstrumentFunctionAttr(Attr.getRange(), S.Context, |
| 3700 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3701 | } |
| 3702 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3703 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3704 | if (S.LangOpts.CUDA) { |
| 3705 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 3706 | if (Attr.hasParameterOrArguments()) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3707 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 3708 | return; |
| 3709 | } |
| 3710 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3711 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3712 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3713 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3714 | return; |
| 3715 | } |
| 3716 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3717 | D->addAttr(::new (S.Context) |
| 3718 | CUDAConstantAttr(Attr.getRange(), S.Context, |
| 3719 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3720 | } else { |
| 3721 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 3722 | } |
| 3723 | } |
| 3724 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3725 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3726 | if (S.LangOpts.CUDA) { |
| 3727 | // check the attribute arguments. |
| 3728 | if (Attr.getNumArgs() != 0) { |
| 3729 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 3730 | return; |
| 3731 | } |
| 3732 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3733 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3734 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3735 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3736 | return; |
| 3737 | } |
| 3738 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3739 | D->addAttr(::new (S.Context) |
| 3740 | CUDADeviceAttr(Attr.getRange(), S.Context, |
| 3741 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3742 | } else { |
| 3743 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 3744 | } |
| 3745 | } |
| 3746 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3747 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3748 | if (S.LangOpts.CUDA) { |
| 3749 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3750 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3751 | return; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3752 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3753 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3754 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3755 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3756 | return; |
| 3757 | } |
| 3758 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3759 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3760 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 3761 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3762 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3763 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3764 | << FD->getType() |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3765 | << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(), |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3766 | "void"); |
| 3767 | } else { |
| 3768 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3769 | << FD->getType(); |
| 3770 | } |
| 3771 | return; |
| 3772 | } |
| 3773 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3774 | D->addAttr(::new (S.Context) |
| 3775 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
| 3776 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3777 | } else { |
| 3778 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 3779 | } |
| 3780 | } |
| 3781 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3782 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3783 | if (S.LangOpts.CUDA) { |
| 3784 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3785 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3786 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3787 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3788 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3789 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3790 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3791 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3792 | return; |
| 3793 | } |
| 3794 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3795 | D->addAttr(::new (S.Context) |
| 3796 | CUDAHostAttr(Attr.getRange(), S.Context, |
| 3797 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3798 | } else { |
| 3799 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 3800 | } |
| 3801 | } |
| 3802 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3803 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3804 | if (S.LangOpts.CUDA) { |
| 3805 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3806 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3807 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3808 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3809 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3810 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3811 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3812 | return; |
| 3813 | } |
| 3814 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3815 | D->addAttr(::new (S.Context) |
| 3816 | CUDASharedAttr(Attr.getRange(), S.Context, |
| 3817 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3818 | } else { |
| 3819 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 3820 | } |
| 3821 | } |
| 3822 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3823 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3824 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3825 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3826 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3827 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3828 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3829 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3830 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3831 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3832 | return; |
| 3833 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3834 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3835 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3836 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3837 | return; |
| 3838 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3839 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3840 | D->addAttr(::new (S.Context) |
| 3841 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3842 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3843 | } |
| 3844 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3845 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3846 | if (hasDeclarator(D)) return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3847 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3848 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3849 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3850 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3851 | CallingConv CC; |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3852 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3853 | return; |
| 3854 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3855 | if (!isa<ObjCMethodDecl>(D)) { |
| 3856 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3857 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3858 | return; |
| 3859 | } |
| 3860 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3861 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3862 | case AttributeList::AT_FastCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3863 | D->addAttr(::new (S.Context) |
| 3864 | FastCallAttr(Attr.getRange(), S.Context, |
| 3865 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3866 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3867 | case AttributeList::AT_StdCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3868 | D->addAttr(::new (S.Context) |
| 3869 | StdCallAttr(Attr.getRange(), S.Context, |
| 3870 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3871 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3872 | case AttributeList::AT_ThisCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3873 | D->addAttr(::new (S.Context) |
| 3874 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3875 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 04633eb | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3876 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3877 | case AttributeList::AT_CDecl: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3878 | D->addAttr(::new (S.Context) |
| 3879 | CDeclAttr(Attr.getRange(), S.Context, |
| 3880 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3881 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3882 | case AttributeList::AT_Pascal: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3883 | D->addAttr(::new (S.Context) |
| 3884 | PascalAttr(Attr.getRange(), S.Context, |
| 3885 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3886 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3887 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3888 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3889 | switch (CC) { |
| 3890 | case CC_AAPCS: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3891 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3892 | break; |
| 3893 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3894 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3895 | break; |
| 3896 | default: |
| 3897 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3900 | D->addAttr(::new (S.Context) |
| 3901 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3902 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3903 | return; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3904 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3905 | case AttributeList::AT_PnaclCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3906 | D->addAttr(::new (S.Context) |
| 3907 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 3908 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3909 | return; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3910 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3911 | D->addAttr(::new (S.Context) |
| 3912 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3913 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3914 | return; |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3915 | |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3916 | default: |
| 3917 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3918 | } |
| 3919 | } |
| 3920 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3921 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 3922 | assert(!Attr.isInvalid()); |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3923 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3926 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3927 | const FunctionDecl *FD) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3928 | if (attr.isInvalid()) |
| 3929 | return true; |
| 3930 | |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3931 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
| 3932 | if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) { |
| 3933 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3934 | attr.setInvalid(); |
| 3935 | return true; |
| 3936 | } |
| 3937 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3938 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 3939 | // move to TargetAttributesSema one day. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3940 | switch (attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3941 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3942 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3943 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3944 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3945 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
| 3946 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3947 | Expr *Arg = attr.getArg(0); |
| 3948 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3949 | if (!Str || !Str->isAscii()) { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3950 | Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 3951 | << "pcs" << 1; |
| 3952 | attr.setInvalid(); |
| 3953 | return true; |
| 3954 | } |
| 3955 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3956 | StringRef StrRef = Str->getString(); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3957 | if (StrRef == "aapcs") { |
| 3958 | CC = CC_AAPCS; |
| 3959 | break; |
| 3960 | } else if (StrRef == "aapcs-vfp") { |
| 3961 | CC = CC_AAPCS_VFP; |
| 3962 | break; |
| 3963 | } |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3964 | |
| 3965 | attr.setInvalid(); |
| 3966 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3967 | return true; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3968 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3969 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3970 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3971 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3974 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3975 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 3976 | if (A == TargetInfo::CCCR_Warning) { |
| 3977 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3978 | |
| 3979 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3980 | if (FD) |
| 3981 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3982 | TargetInfo::CCMT_NonMember; |
| 3983 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3986 | return false; |
| 3987 | } |
| 3988 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3989 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3990 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3991 | |
| 3992 | unsigned numParams; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3993 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3994 | return; |
| 3995 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3996 | if (!isa<ObjCMethodDecl>(D)) { |
| 3997 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3998 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3999 | return; |
| 4000 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4001 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4002 | D->addAttr(::new (S.Context) |
| 4003 | RegparmAttr(Attr.getRange(), S.Context, numParams, |
| 4004 | Attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4005 | } |
| 4006 | |
| 4007 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 4008 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4009 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 4010 | if (Attr.isInvalid()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4011 | return true; |
| 4012 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4013 | if (Attr.getNumArgs() != 1) { |
| 4014 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 4015 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4016 | return true; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4017 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4018 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4019 | Expr *NumParamsExpr = Attr.getArg(0); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4020 | llvm::APSInt NumParams(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 4021 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4022 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4023 | Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4024 | << "regparm" << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4025 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4026 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4027 | } |
| 4028 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4029 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4030 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4031 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4032 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4033 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4034 | } |
| 4035 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4036 | numParams = NumParams.getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4037 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4038 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4039 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4040 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4041 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4042 | } |
| 4043 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4044 | return false; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4047 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4048 | if (S.LangOpts.CUDA) { |
| 4049 | // check the attribute arguments. |
| 4050 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 4051 | // FIXME: 0 is not okay. |
| 4052 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4053 | return; |
| 4054 | } |
| 4055 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4056 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4057 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4058 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4059 | return; |
| 4060 | } |
| 4061 | |
| 4062 | Expr *MaxThreadsExpr = Attr.getArg(0); |
| 4063 | llvm::APSInt MaxThreads(32); |
| 4064 | if (MaxThreadsExpr->isTypeDependent() || |
| 4065 | MaxThreadsExpr->isValueDependent() || |
| 4066 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
| 4067 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 4068 | << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange(); |
| 4069 | return; |
| 4070 | } |
| 4071 | |
| 4072 | llvm::APSInt MinBlocks(32); |
| 4073 | if (Attr.getNumArgs() > 1) { |
| 4074 | Expr *MinBlocksExpr = Attr.getArg(1); |
| 4075 | if (MinBlocksExpr->isTypeDependent() || |
| 4076 | MinBlocksExpr->isValueDependent() || |
| 4077 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
| 4078 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 4079 | << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange(); |
| 4080 | return; |
| 4081 | } |
| 4082 | } |
| 4083 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4084 | D->addAttr(::new (S.Context) |
| 4085 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 4086 | MaxThreads.getZExtValue(), |
| 4087 | MinBlocks.getZExtValue(), |
| 4088 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4089 | } else { |
| 4090 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 4091 | } |
| 4092 | } |
| 4093 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4094 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 4095 | const AttributeList &Attr) { |
| 4096 | StringRef AttrName = Attr.getName()->getName(); |
| 4097 | if (!Attr.getParameterName()) { |
| 4098 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier) |
| 4099 | << Attr.getName() << /* arg num = */ 1; |
| 4100 | return; |
| 4101 | } |
| 4102 | |
| 4103 | if (Attr.getNumArgs() != 2) { |
| 4104 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 4105 | << /* required args = */ 3; |
| 4106 | return; |
| 4107 | } |
| 4108 | |
| 4109 | IdentifierInfo *ArgumentKind = Attr.getParameterName(); |
| 4110 | |
| 4111 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 4112 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4113 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 4114 | return; |
| 4115 | } |
| 4116 | |
| 4117 | uint64_t ArgumentIdx; |
| 4118 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4119 | Attr.getLoc(), 2, |
| 4120 | Attr.getArg(0), ArgumentIdx)) |
| 4121 | return; |
| 4122 | |
| 4123 | uint64_t TypeTagIdx; |
| 4124 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4125 | Attr.getLoc(), 3, |
| 4126 | Attr.getArg(1), TypeTagIdx)) |
| 4127 | return; |
| 4128 | |
| 4129 | bool IsPointer = (AttrName == "pointer_with_type_tag"); |
| 4130 | if (IsPointer) { |
| 4131 | // Ensure that buffer has a pointer type. |
| 4132 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 4133 | if (!BufferTy->isPointerType()) { |
| 4134 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
| 4135 | << AttrName; |
| 4136 | } |
| 4137 | } |
| 4138 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4139 | D->addAttr(::new (S.Context) |
| 4140 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 4141 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 4142 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 4146 | const AttributeList &Attr) { |
| 4147 | IdentifierInfo *PointerKind = Attr.getParameterName(); |
| 4148 | if (!PointerKind) { |
| 4149 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier) |
| 4150 | << "type_tag_for_datatype" << 1; |
| 4151 | return; |
| 4152 | } |
| 4153 | |
| 4154 | QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL); |
| 4155 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4156 | D->addAttr(::new (S.Context) |
| 4157 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
| 4158 | MatchingCType, |
| 4159 | Attr.getLayoutCompatible(), |
| 4160 | Attr.getMustBeNull(), |
| 4161 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4164 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4165 | // Checker-specific attribute handlers. |
| 4166 | //===----------------------------------------------------------------------===// |
| 4167 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4168 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4169 | return type->isDependentType() || |
| 4170 | type->isObjCObjectPointerType() || |
| 4171 | S.Context.isObjCNSObjectType(type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4172 | } |
| 4173 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4174 | return type->isDependentType() || |
| 4175 | type->isPointerType() || |
| 4176 | isValidSubjectOfNSAttribute(S, type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4177 | } |
| 4178 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4179 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4180 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4181 | if (!param) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4182 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4183 | << Attr.getRange() << Attr.getName() << ExpectedParameter; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4184 | return; |
| 4185 | } |
| 4186 | |
| 4187 | bool typeOK, cf; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4188 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4189 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 4190 | cf = false; |
| 4191 | } else { |
| 4192 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 4193 | cf = true; |
| 4194 | } |
| 4195 | |
| 4196 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4197 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4198 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4199 | return; |
| 4200 | } |
| 4201 | |
| 4202 | if (cf) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4203 | param->addAttr(::new (S.Context) |
| 4204 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 4205 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4206 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4207 | param->addAttr(::new (S.Context) |
| 4208 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 4209 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4210 | } |
| 4211 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4212 | static void handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 4213 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4214 | if (!isa<ObjCMethodDecl>(D)) { |
| 4215 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4216 | << Attr.getRange() << Attr.getName() << ExpectedMethod; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4217 | return; |
| 4218 | } |
| 4219 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4220 | D->addAttr(::new (S.Context) |
| 4221 | NSConsumesSelfAttr(Attr.getRange(), S.Context, |
| 4222 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4223 | } |
| 4224 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4225 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 4226 | const AttributeList &Attr) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4227 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4228 | QualType returnType; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4229 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4230 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4231 | returnType = MD->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4232 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4233 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4234 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | a23bd4c | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 4235 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4236 | returnType = PD->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4237 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4238 | returnType = FD->getResultType(); |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4239 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4240 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4241 | << Attr.getRange() << Attr.getName() |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4242 | << ExpectedFunctionOrMethod; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4243 | return; |
| 4244 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4245 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4246 | bool typeOK; |
| 4247 | bool cf; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4248 | switch (Attr.getKind()) { |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4249 | default: llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4250 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4251 | case AttributeList::AT_NSReturnsRetained: |
| 4252 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4253 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 4254 | cf = false; |
| 4255 | break; |
| 4256 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4257 | case AttributeList::AT_CFReturnsRetained: |
| 4258 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4259 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 4260 | cf = true; |
| 4261 | break; |
| 4262 | } |
| 4263 | |
| 4264 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4265 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4266 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4267 | return; |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4268 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4269 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4270 | switch (Attr.getKind()) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4271 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4272 | llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4273 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4274 | D->addAttr(::new (S.Context) |
| 4275 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 4276 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4277 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4278 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4279 | D->addAttr(::new (S.Context) |
| 4280 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4281 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4282 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4283 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4284 | D->addAttr(::new (S.Context) |
| 4285 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4286 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4287 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4288 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4289 | D->addAttr(::new (S.Context) |
| 4290 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4291 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4292 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4293 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4294 | D->addAttr(::new (S.Context) |
| 4295 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4296 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4297 | return; |
| 4298 | }; |
| 4299 | } |
| 4300 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4301 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4302 | const AttributeList &attr) { |
| 4303 | SourceLocation loc = attr.getLoc(); |
| 4304 | |
| 4305 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4306 | |
Fariborz Jahanian | 94d55d7 | 2012-04-21 17:51:44 +0000 | [diff] [blame] | 4307 | if (!method) { |
Fariborz Jahanian | 0e78afb | 2012-04-20 22:00:46 +0000 | [diff] [blame] | 4308 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4309 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4310 | return; |
| 4311 | } |
| 4312 | |
| 4313 | // Check that the method returns a normal pointer. |
| 4314 | QualType resultType = method->getResultType(); |
Fariborz Jahanian | f2e5945 | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4315 | |
| 4316 | if (!resultType->isReferenceType() && |
| 4317 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4318 | S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 4319 | << SourceRange(loc) |
| 4320 | << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2; |
| 4321 | |
| 4322 | // Drop the attribute. |
| 4323 | return; |
| 4324 | } |
| 4325 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4326 | method->addAttr(::new (S.Context) |
| 4327 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 4328 | attr.getAttributeSpellingListIndex())); |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4329 | } |
| 4330 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4331 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4332 | const AttributeList &attr) { |
| 4333 | SourceLocation loc = attr.getLoc(); |
| 4334 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4335 | |
| 4336 | if (!method) { |
| 4337 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 4338 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
| 4339 | return; |
| 4340 | } |
| 4341 | DeclContext *DC = method->getDeclContext(); |
| 4342 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4343 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4344 | << attr.getName() << 0; |
| 4345 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4346 | return; |
| 4347 | } |
| 4348 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4349 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4350 | << attr.getName() << 1; |
| 4351 | return; |
| 4352 | } |
| 4353 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4354 | method->addAttr(::new (S.Context) |
| 4355 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 4356 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4357 | } |
| 4358 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4359 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 4360 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
| 4361 | if (!isa<FunctionDecl>(D)) { |
| 4362 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4363 | << A.getRange() << A.getName() << ExpectedFunction; |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4364 | return; |
| 4365 | } |
| 4366 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4367 | bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4368 | |
| 4369 | // Check whether there's a conflicting attribute already present. |
| 4370 | Attr *Existing; |
| 4371 | if (IsAudited) { |
| 4372 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 4373 | } else { |
| 4374 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 4375 | } |
| 4376 | if (Existing) { |
| 4377 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 4378 | << A.getName() |
| 4379 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 4380 | << A.getRange() << Existing->getRange(); |
| 4381 | return; |
| 4382 | } |
| 4383 | |
| 4384 | // All clear; add the attribute. |
| 4385 | if (IsAudited) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4386 | D->addAttr(::new (S.Context) |
| 4387 | CFAuditedTransferAttr(A.getRange(), S.Context, |
| 4388 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4389 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4390 | D->addAttr(::new (S.Context) |
| 4391 | CFUnknownTransferAttr(A.getRange(), S.Context, |
| 4392 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4393 | } |
| 4394 | } |
| 4395 | |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4396 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4397 | const AttributeList &Attr) { |
| 4398 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4399 | if (!RD || RD->isUnion()) { |
| 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() << ExpectedStruct; |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4402 | } |
| 4403 | |
| 4404 | IdentifierInfo *ParmName = Attr.getParameterName(); |
| 4405 | |
| 4406 | // In Objective-C, verify that the type names an Objective-C type. |
| 4407 | // We don't want to check this outside of ObjC because people sometimes |
| 4408 | // do crazy C declarations of Objective-C types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4409 | if (ParmName && S.getLangOpts().ObjC1) { |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4410 | // Check for an existing type with this name. |
| 4411 | LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(), |
| 4412 | Sema::LookupOrdinaryName); |
| 4413 | if (S.LookupName(R, Sc)) { |
| 4414 | NamedDecl *Target = R.getFoundDecl(); |
| 4415 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 4416 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 4417 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 4418 | } |
| 4419 | } |
| 4420 | } |
| 4421 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4422 | D->addAttr(::new (S.Context) |
| 4423 | NSBridgedAttr(Attr.getRange(), S.Context, ParmName, |
| 4424 | Attr.getAttributeSpellingListIndex())); |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4425 | } |
| 4426 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4427 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4428 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4429 | if (hasDeclarator(D)) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4430 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4431 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4432 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4433 | } |
| 4434 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4435 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4436 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4437 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4438 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4439 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4440 | return; |
| 4441 | } |
| 4442 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4443 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4444 | QualType type = vd->getType(); |
| 4445 | |
| 4446 | if (!type->isDependentType() && |
| 4447 | !type->isObjCLifetimeType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4448 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4449 | << type; |
| 4450 | return; |
| 4451 | } |
| 4452 | |
| 4453 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4454 | |
| 4455 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4456 | // going to infer. |
| 4457 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4458 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4459 | |
| 4460 | switch (lifetime) { |
| 4461 | case Qualifiers::OCL_None: |
| 4462 | assert(type->isDependentType() && |
| 4463 | "didn't infer lifetime for non-dependent type?"); |
| 4464 | break; |
| 4465 | |
| 4466 | case Qualifiers::OCL_Weak: // meaningful |
| 4467 | case Qualifiers::OCL_Strong: // meaningful |
| 4468 | break; |
| 4469 | |
| 4470 | case Qualifiers::OCL_ExplicitNone: |
| 4471 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4472 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4473 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4474 | break; |
| 4475 | } |
| 4476 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4477 | D->addAttr(::new (S.Context) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4478 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 4479 | Attr.getAttributeSpellingListIndex())); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4480 | } |
| 4481 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4482 | //===----------------------------------------------------------------------===// |
| 4483 | // Microsoft specific attribute handlers. |
| 4484 | //===----------------------------------------------------------------------===// |
| 4485 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4486 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 4487 | if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) { |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4488 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4489 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4490 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4491 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4492 | Expr *Arg = Attr.getArg(0); |
| 4493 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4494 | if (!Str || !Str->isAscii()) { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4495 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 4496 | << "uuid" << 1; |
| 4497 | return; |
| 4498 | } |
| 4499 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4500 | StringRef StrRef = Str->getString(); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4501 | |
| 4502 | bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' && |
| 4503 | StrRef.back() == '}'; |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4504 | |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4505 | // Validate GUID length. |
| 4506 | if (IsCurly && StrRef.size() != 38) { |
| 4507 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4508 | return; |
| 4509 | } |
| 4510 | if (!IsCurly && StrRef.size() != 36) { |
| 4511 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4512 | return; |
| 4513 | } |
| 4514 | |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4515 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4516 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4517 | StringRef::iterator I = StrRef.begin(); |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4518 | if (IsCurly) // Skip the optional '{' |
| 4519 | ++I; |
| 4520 | |
| 4521 | for (int i = 0; i < 36; ++i) { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4522 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 4523 | if (*I != '-') { |
| 4524 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4525 | return; |
| 4526 | } |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 4527 | } else if (!isHexDigit(*I)) { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4528 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4529 | return; |
| 4530 | } |
| 4531 | I++; |
| 4532 | } |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4533 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4534 | D->addAttr(::new (S.Context) |
| 4535 | UuidAttr(Attr.getRange(), S.Context, Str->getString(), |
| 4536 | Attr.getAttributeSpellingListIndex())); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4537 | } else |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4538 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid"; |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4539 | } |
| 4540 | |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4541 | static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4542 | if (!S.LangOpts.MicrosoftExt) { |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4543 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4544 | return; |
| 4545 | } |
| 4546 | |
| 4547 | AttributeList::Kind Kind = Attr.getKind(); |
| 4548 | if (Kind == AttributeList::AT_SingleInheritance) |
| 4549 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4550 | ::new (S.Context) |
| 4551 | SingleInheritanceAttr(Attr.getRange(), S.Context, |
| 4552 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4553 | else if (Kind == AttributeList::AT_MultipleInheritance) |
| 4554 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4555 | ::new (S.Context) |
| 4556 | MultipleInheritanceAttr(Attr.getRange(), S.Context, |
| 4557 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4558 | else if (Kind == AttributeList::AT_VirtualInheritance) |
| 4559 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4560 | ::new (S.Context) |
| 4561 | VirtualInheritanceAttr(Attr.getRange(), S.Context, |
| 4562 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4563 | } |
| 4564 | |
| 4565 | static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4566 | if (S.LangOpts.MicrosoftExt) { |
| 4567 | AttributeList::Kind Kind = Attr.getKind(); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4568 | if (Kind == AttributeList::AT_Ptr32) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4569 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4570 | ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context, |
| 4571 | Attr.getAttributeSpellingListIndex())); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4572 | else if (Kind == AttributeList::AT_Ptr64) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4573 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4574 | ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context, |
| 4575 | Attr.getAttributeSpellingListIndex())); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4576 | else if (Kind == AttributeList::AT_Win64) |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4577 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4578 | ::new (S.Context) Win64Attr(Attr.getRange(), S.Context, |
| 4579 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4580 | } else |
| 4581 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4582 | } |
| 4583 | |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4584 | static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4585 | if (S.LangOpts.MicrosoftExt) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4586 | D->addAttr(::new (S.Context) |
| 4587 | ForceInlineAttr(Attr.getRange(), S.Context, |
| 4588 | Attr.getAttributeSpellingListIndex())); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4589 | else |
| 4590 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4591 | } |
| 4592 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4593 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4594 | // Top Level Sema Entry Points |
| 4595 | //===----------------------------------------------------------------------===// |
| 4596 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4597 | static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4598 | const AttributeList &Attr) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4599 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4600 | case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break; |
| 4601 | case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break; |
| 4602 | case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break; |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4603 | default: |
| 4604 | break; |
| 4605 | } |
| 4606 | } |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4607 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4608 | static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4609 | const AttributeList &Attr) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4610 | switch (Attr.getKind()) { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4611 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 4612 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 4613 | case AttributeList::AT_IBOutletCollection: |
| 4614 | handleIBOutletCollection(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4615 | case AttributeList::AT_AddressSpace: |
| 4616 | case AttributeList::AT_OpenCLImageAccess: |
| 4617 | case AttributeList::AT_ObjCGC: |
| 4618 | case AttributeList::AT_VectorSize: |
| 4619 | case AttributeList::AT_NeonVectorType: |
| 4620 | case AttributeList::AT_NeonPolyVectorType: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4621 | // Ignore these, these are type attributes, handled by |
| 4622 | // ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4623 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4624 | case AttributeList::AT_CUDADevice: |
| 4625 | case AttributeList::AT_CUDAHost: |
| 4626 | case AttributeList::AT_Overloadable: |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4627 | // Ignore, this is a non-inheritable attribute, handled |
| 4628 | // by ProcessNonInheritableDeclAttr. |
| 4629 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4630 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 4631 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 4632 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 4633 | case AttributeList::AT_AlwaysInline: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4634 | handleAlwaysInlineAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4635 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4636 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 4637 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4638 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 4639 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 4640 | case AttributeList::AT_CarriesDependency: |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4641 | handleDependencyAttr(S, scope, D, Attr); |
| 4642 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4643 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
| 4644 | case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break; |
| 4645 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4646 | case AttributeList::AT_CXX11NoReturn: |
| 4647 | handleCXX11NoReturnAttr(S, D, Attr); |
| 4648 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4649 | case AttributeList::AT_Deprecated: |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4650 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated"); |
| 4651 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4652 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 4653 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4654 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4655 | break; |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4656 | case AttributeList::AT_MinSize: |
| 4657 | handleMinSizeAttr(S, D, Attr); |
| 4658 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4659 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4660 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4661 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
| 4662 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4663 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4664 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4665 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4666 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
| 4667 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
| 4668 | case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break; |
| 4669 | case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break; |
| 4670 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4671 | case AttributeList::AT_ownership_returns: |
| 4672 | case AttributeList::AT_ownership_takes: |
| 4673 | case AttributeList::AT_ownership_holds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4674 | handleOwnershipAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4675 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4676 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
| 4677 | case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break; |
| 4678 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
| 4679 | case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break; |
| 4680 | case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break; |
| 4681 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4682 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4683 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4684 | handleObjCOwnershipAttr(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4685 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4686 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4687 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4688 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4689 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4690 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4691 | case AttributeList::AT_ObjCRequiresSuper: |
| 4692 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4693 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4694 | case AttributeList::AT_NSBridged: |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4695 | handleNSBridgedAttr(S, scope, D, Attr); break; |
| 4696 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4697 | case AttributeList::AT_CFAuditedTransfer: |
| 4698 | case AttributeList::AT_CFUnknownTransfer: |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4699 | handleCFTransferAttr(S, D, Attr); break; |
| 4700 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4701 | // Checker-specific. |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4702 | case AttributeList::AT_CFConsumed: |
| 4703 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4704 | case AttributeList::AT_NSConsumesSelf: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4705 | handleNSConsumesSelfAttr(S, D, Attr); break; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4706 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4707 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4708 | case AttributeList::AT_NSReturnsNotRetained: |
| 4709 | case AttributeList::AT_CFReturnsNotRetained: |
| 4710 | case AttributeList::AT_NSReturnsRetained: |
| 4711 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4712 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4713 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4714 | case AttributeList::AT_WorkGroupSizeHint: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4715 | case AttributeList::AT_ReqdWorkGroupSize: |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4716 | handleWorkGroupSize(S, D, Attr); break; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 4717 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4718 | case AttributeList::AT_InitPriority: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4719 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4720 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4721 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4722 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4723 | case AttributeList::AT_Unavailable: |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4724 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable"); |
| 4725 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4726 | case AttributeList::AT_ArcWeakrefUnavailable: |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 4727 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 4728 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4729 | case AttributeList::AT_ObjCRootClass: |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4730 | handleObjCRootClassAttr(S, D, Attr); |
| 4731 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4732 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 4733 | handleObjCRequiresPropertyDefsAttr (S, D, Attr); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 4734 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4735 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4736 | case AttributeList::AT_ReturnsTwice: |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 4737 | handleReturnsTwiceAttr(S, D, Attr); |
| 4738 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4739 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4740 | case AttributeList::AT_Visibility: |
| 4741 | handleVisibilityAttr(S, D, Attr, false); |
| 4742 | break; |
| 4743 | case AttributeList::AT_TypeVisibility: |
| 4744 | handleVisibilityAttr(S, D, Attr, true); |
| 4745 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4746 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4747 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4748 | case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; |
| 4749 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4750 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4751 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4752 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4753 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4754 | case AttributeList::AT_ObjCException: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4755 | handleObjCExceptionAttr(S, D, Attr); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 4756 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4757 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4758 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4759 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4760 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4761 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4762 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
| 4763 | case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break; |
| 4764 | case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break; |
| 4765 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4766 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
| 4767 | case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break; |
| 4768 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4769 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4770 | // Just ignore |
| 4771 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4772 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4773 | handleNoInstrumentFunctionAttr(S, D, Attr); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 4774 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4775 | case AttributeList::AT_StdCall: |
| 4776 | case AttributeList::AT_CDecl: |
| 4777 | case AttributeList::AT_FastCall: |
| 4778 | case AttributeList::AT_ThisCall: |
| 4779 | case AttributeList::AT_Pascal: |
| 4780 | case AttributeList::AT_Pcs: |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4781 | case AttributeList::AT_PnaclCall: |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4782 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4783 | handleCallConvAttr(S, D, Attr); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4784 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4785 | case AttributeList::AT_OpenCLKernel: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4786 | handleOpenCLKernelAttr(S, D, Attr); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4787 | break; |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4788 | |
| 4789 | // Microsoft attributes: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4790 | case AttributeList::AT_MsStruct: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4791 | handleMsStructAttr(S, D, Attr); |
| 4792 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4793 | case AttributeList::AT_Uuid: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4794 | handleUuidAttr(S, D, Attr); |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4795 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4796 | case AttributeList::AT_SingleInheritance: |
| 4797 | case AttributeList::AT_MultipleInheritance: |
| 4798 | case AttributeList::AT_VirtualInheritance: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4799 | handleInheritanceAttr(S, D, Attr); |
| 4800 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4801 | case AttributeList::AT_Win64: |
| 4802 | case AttributeList::AT_Ptr32: |
| 4803 | case AttributeList::AT_Ptr64: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4804 | handlePortabilityAttr(S, D, Attr); |
| 4805 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4806 | case AttributeList::AT_ForceInline: |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4807 | handleForceInlineAttr(S, D, Attr); |
| 4808 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4809 | |
| 4810 | // Thread safety attributes: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4811 | case AttributeList::AT_GuardedVar: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4812 | handleGuardedVarAttr(S, D, Attr); |
| 4813 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4814 | case AttributeList::AT_PtGuardedVar: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4815 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4816 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4817 | case AttributeList::AT_ScopedLockable: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4818 | handleScopedLockableAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4819 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4820 | case AttributeList::AT_NoAddressSafetyAnalysis: |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4821 | handleNoAddressSafetyAttr(S, D, Attr); |
| 4822 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4823 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4824 | handleNoThreadSafetyAttr(S, D, Attr); |
| 4825 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4826 | case AttributeList::AT_Lockable: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4827 | handleLockableAttr(S, D, Attr); |
| 4828 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4829 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4830 | handleGuardedByAttr(S, D, Attr); |
| 4831 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4832 | case AttributeList::AT_PtGuardedBy: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4833 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4834 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4835 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4836 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4837 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4838 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4839 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4840 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4841 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4842 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4843 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4844 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4845 | handleLockReturnedAttr(S, D, Attr); |
| 4846 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4847 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4848 | handleLocksExcludedAttr(S, D, Attr); |
| 4849 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4850 | case AttributeList::AT_SharedLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4851 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4852 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4853 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4854 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4855 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4856 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4857 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4858 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4859 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4860 | handleUnlockFunAttr(S, D, Attr); |
| 4861 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4862 | case AttributeList::AT_AcquiredBefore: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4863 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4864 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4865 | case AttributeList::AT_AcquiredAfter: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4866 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4867 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4868 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4869 | // Type safety attributes. |
| 4870 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4871 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4872 | break; |
| 4873 | case AttributeList::AT_TypeTagForDatatype: |
| 4874 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4875 | break; |
| 4876 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4877 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4878 | // Ask target about the attribute. |
| 4879 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 4880 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4881 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 4882 | diag::warn_unhandled_ms_attribute_ignored : |
| 4883 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4884 | break; |
| 4885 | } |
| 4886 | } |
| 4887 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4888 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4889 | /// the attribute applies to decls. If the attribute is a type attribute, just |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4890 | /// silently ignore it if a GNU attribute. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4891 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4892 | const AttributeList &Attr, |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4893 | bool NonInheritable, bool Inheritable, |
| 4894 | bool IncludeCXX11Attributes) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4895 | if (Attr.isInvalid()) |
| 4896 | return; |
| 4897 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4898 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 4899 | // instead. |
| 4900 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 4901 | return; |
| 4902 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4903 | if (NonInheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4904 | ProcessNonInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4905 | |
| 4906 | if (Inheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4907 | ProcessInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4908 | } |
| 4909 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4910 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4911 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4912 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4913 | const AttributeList *AttrList, |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4914 | bool NonInheritable, bool Inheritable, |
| 4915 | bool IncludeCXX11Attributes) { |
| 4916 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
| 4917 | ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable, |
| 4918 | IncludeCXX11Attributes); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4919 | |
| 4920 | // GCC accepts |
| 4921 | // static int a9 __attribute__((weakref)); |
| 4922 | // but that looks really pointless. We reject it. |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4923 | if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4924 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Rafael Espindola | 4d8a33b | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4925 | cast<NamedDecl>(D)->getNameAsString(); |
| 4926 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4927 | return; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4928 | } |
| 4929 | } |
| 4930 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4931 | // Annotation attributes are the only attributes allowed after an access |
| 4932 | // specifier. |
| 4933 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4934 | const AttributeList *AttrList) { |
| 4935 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4936 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4937 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4938 | } else { |
| 4939 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4940 | return true; |
| 4941 | } |
| 4942 | } |
| 4943 | |
| 4944 | return false; |
| 4945 | } |
| 4946 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4947 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 4948 | /// contains any decl attributes that we should warn about. |
| 4949 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 4950 | for ( ; A; A = A->getNext()) { |
| 4951 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 4952 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4953 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 4954 | |
| 4955 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 4956 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 4957 | << A->getName() << A->getRange(); |
| 4958 | } else { |
| 4959 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 4960 | << A->getName() << A->getRange(); |
| 4961 | } |
| 4962 | } |
| 4963 | } |
| 4964 | |
| 4965 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 4966 | /// used to build a declaration, complain about any decl attributes |
| 4967 | /// which might be lying around on it. |
| 4968 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 4969 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 4970 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 4971 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 4972 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 4973 | } |
| 4974 | |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4975 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4976 | /// \#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] | 4977 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 4978 | SourceLocation Loc) { |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4979 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4980 | NamedDecl *NewD = 0; |
| 4981 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4982 | FunctionDecl *NewFD; |
| 4983 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 4984 | // FIXME: Mangling? |
| 4985 | // FIXME: Is the qualifier info correct? |
| 4986 | // FIXME: Is the DeclContext correct? |
| 4987 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 4988 | Loc, Loc, DeclarationName(II), |
| 4989 | FD->getType(), FD->getTypeSourceInfo(), |
| 4990 | SC_None, SC_None, |
| 4991 | false/*isInlineSpecified*/, |
| 4992 | FD->hasPrototype(), |
| 4993 | false/*isConstexprSpecified*/); |
| 4994 | NewD = NewFD; |
| 4995 | |
| 4996 | if (FD->getQualifier()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4997 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4998 | |
| 4999 | // Fake up parameter variables; they are declared as if this were |
| 5000 | // a typedef. |
| 5001 | QualType FDTy = FD->getType(); |
| 5002 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 5003 | SmallVector<ParmVarDecl*, 16> Params; |
| 5004 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 5005 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 5006 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 5007 | Param->setScopeInfo(0, Params.size()); |
| 5008 | Params.push_back(Param); |
| 5009 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 5010 | NewFD->setParams(Params); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5011 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5012 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 5013 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5014 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5015 | VD->getType(), VD->getTypeSourceInfo(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 5016 | VD->getStorageClass(), |
| 5017 | VD->getStorageClassAsWritten()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5018 | if (VD->getQualifier()) { |
| 5019 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5020 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5021 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5022 | } |
| 5023 | return NewD; |
| 5024 | } |
| 5025 | |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5026 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5027 | /// applied to it, possibly with an alias. |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5028 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5029 | if (W.getUsed()) return; // only do this once |
| 5030 | W.setUsed(true); |
| 5031 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 5032 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5033 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5034 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 5035 | NDId->getName())); |
| 5036 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5037 | WeakTopLevelDecl.push_back(NewD); |
| 5038 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 5039 | // to insert Decl at TU scope, sorry. |
| 5040 | DeclContext *SavedContext = CurContext; |
| 5041 | CurContext = Context.getTranslationUnitDecl(); |
| 5042 | PushOnScopeChains(NewD, S); |
| 5043 | CurContext = SavedContext; |
| 5044 | } else { // just add weak to existing |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5045 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5046 | } |
| 5047 | } |
| 5048 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5049 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 5050 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 5051 | /// specified in many different places, and we need to find and apply them all. |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5052 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD, |
| 5053 | bool NonInheritable, bool Inheritable) { |
John McCall | d4aff0e | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 5054 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 5055 | // have to do this. |
Douglas Gregor | 31e37b2 | 2011-07-28 18:09:57 +0000 | [diff] [blame] | 5056 | if (Inheritable) { |
| 5057 | LoadExternalWeakUndeclaredIdentifiers(); |
| 5058 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 5059 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
| 5060 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 5061 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 5062 | = WeakUndeclaredIdentifiers.find(Id); |
| 5063 | if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) { |
| 5064 | WeakInfo W = I->second; |
| 5065 | DeclApplyPragmaWeak(S, ND, W); |
| 5066 | WeakUndeclaredIdentifiers[Id] = W; |
| 5067 | } |
John McCall | d4aff0e | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 5068 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5069 | } |
| 5070 | } |
| 5071 | } |
| 5072 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5073 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5074 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5075 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5076 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5077 | // Walk the declarator structure, applying decl attributes that were in a type |
| 5078 | // position to the decl itself. This handles cases like: |
| 5079 | // int *__attr__(x)** D; |
| 5080 | // when X is a decl attribute. |
| 5081 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 5082 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5083 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable, |
| 5084 | /*IncludeCXX11Attributes=*/false); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5085 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5086 | // Finally, apply any attributes on the decl itself. |
| 5087 | if (const AttributeList *Attrs = PD.getAttributes()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5088 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5089 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5090 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5091 | /// Is the given declaration allowed to use a forbidden type? |
| 5092 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 5093 | // Private ivars are always okay. Unfortunately, people don't |
| 5094 | // always properly make their ivars private, even in system headers. |
| 5095 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | a6b3380 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 5096 | // Function declarations in sys headers will be marked unavailable. |
| 5097 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 5098 | !isa<FunctionDecl>(decl)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5099 | return false; |
| 5100 | |
| 5101 | // Require it to be declared in a system header. |
| 5102 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 5103 | } |
| 5104 | |
| 5105 | /// Handle a delayed forbidden-type diagnostic. |
| 5106 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 5107 | Decl *decl) { |
| 5108 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 5109 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 5110 | "this system declaration uses an unsupported type")); |
| 5111 | return; |
| 5112 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5113 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5114 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 5115 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5116 | // kind of forbidden type messages on unavailable functions. |
| 5117 | if (FD->hasAttr<UnavailableAttr>() && |
| 5118 | diag.getForbiddenTypeDiagnostic() == |
| 5119 | diag::err_arc_array_param_no_ownership) { |
| 5120 | diag.Triggered = true; |
| 5121 | return; |
| 5122 | } |
| 5123 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5124 | |
| 5125 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 5126 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 5127 | diag.Triggered = true; |
| 5128 | } |
| 5129 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5130 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 5131 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5132 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5133 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5134 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5135 | // When delaying diagnostics to run in the context of a parsed |
| 5136 | // declaration, we only want to actually emit anything if parsing |
| 5137 | // succeeds. |
| 5138 | if (!decl) return; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5139 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5140 | // We emit all the active diagnostics in this pool or any of its |
| 5141 | // parents. In general, we'll get one pool for the decl spec |
| 5142 | // and a child pool for each declarator; in a decl group like: |
| 5143 | // deprecated_typedef foo, *bar, baz(); |
| 5144 | // only the declarator pops will be passed decls. This is correct; |
| 5145 | // we really do need to consider delayed diagnostics from the decl spec |
| 5146 | // for each of the different declarations. |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5147 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5148 | do { |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5149 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5150 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 5151 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 5152 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5153 | if (diag.Triggered) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5154 | continue; |
| 5155 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5156 | switch (diag.Kind) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5157 | case DelayedDiagnostic::Deprecation: |
John McCall | e8c904f | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 5158 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 5159 | if (!decl->isInvalidDecl()) |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5160 | HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5161 | break; |
| 5162 | |
| 5163 | case DelayedDiagnostic::Access: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5164 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5165 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5166 | |
| 5167 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5168 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5169 | break; |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5170 | } |
| 5171 | } |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5172 | } while ((pool = pool->getParent())); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5173 | } |
| 5174 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5175 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 5176 | /// been delayed in the current context instead of in the given pool. |
| 5177 | /// Essentially, this just moves them to the current pool. |
| 5178 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 5179 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 5180 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 5181 | curPool->steal(pool); |
| 5182 | } |
| 5183 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5184 | static bool isDeclDeprecated(Decl *D) { |
| 5185 | do { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5186 | if (D->isDeprecated()) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5187 | return true; |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 5188 | // A category implicitly has the availability of the interface. |
| 5189 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 5190 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5191 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 5192 | return false; |
| 5193 | } |
| 5194 | |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5195 | static void |
| 5196 | DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message, |
| 5197 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5198 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5199 | const ObjCPropertyDecl *ObjCPropery) { |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5200 | DeclarationName Name = D->getDeclName(); |
| 5201 | if (!Message.empty()) { |
| 5202 | S.Diag(Loc, diag::warn_deprecated_message) << Name << Message; |
| 5203 | S.Diag(D->getLocation(), |
| 5204 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5205 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5206 | if (ObjCPropery) |
| 5207 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5208 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5209 | } else if (!UnknownObjCClass) { |
| 5210 | S.Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 5211 | S.Diag(D->getLocation(), |
| 5212 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5213 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5214 | if (ObjCPropery) |
| 5215 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5216 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5217 | } else { |
| 5218 | S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name; |
| 5219 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 5220 | } |
| 5221 | } |
| 5222 | |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 5223 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5224 | Decl *Ctx) { |
| 5225 | if (isDeclDeprecated(Ctx)) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5226 | return; |
| 5227 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5228 | DD.Triggered = true; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5229 | DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(), |
| 5230 | DD.getDeprecationMessage(), DD.Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5231 | DD.getUnknownObjCClass(), |
| 5232 | DD.getObjCProperty()); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5233 | } |
| 5234 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5235 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 5236 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5237 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5238 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5239 | // Delay if we're currently parsing a declaration. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5240 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5241 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 5242 | UnknownObjCClass, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5243 | ObjCProperty, |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5244 | Message)); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5245 | return; |
| 5246 | } |
| 5247 | |
| 5248 | // Otherwise, don't warn if our current context is deprecated. |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 5249 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5250 | return; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5251 | DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5252 | } |