Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1 | //===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements decl-related attribute processing. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 15 | #include "TargetAttributesSema.h" |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | acc5f3e | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | acc5f3e | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Rafael Espindola | d7a60ad | 2013-02-26 19:13:56 +0000 | [diff] [blame] | 22 | #include "clang/AST/Mangle.h" |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 23 | #include "clang/Basic/CharInfo.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 26 | #include "clang/Sema/DeclSpec.h" |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 27 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 28 | #include "clang/Sema/Lookup.h" |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 29 | #include "clang/Sema/Scope.h" |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 31 | using namespace clang; |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 32 | using namespace sema; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 33 | |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 34 | /// These constants match the enumerated choices of |
| 35 | /// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 36 | enum AttributeDeclKind { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 37 | ExpectedFunction, |
| 38 | ExpectedUnion, |
| 39 | ExpectedVariableOrFunction, |
| 40 | ExpectedFunctionOrMethod, |
| 41 | ExpectedParameter, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 42 | ExpectedFunctionMethodOrBlock, |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 43 | ExpectedFunctionMethodOrClass, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 44 | ExpectedFunctionMethodOrParameter, |
| 45 | ExpectedClass, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 46 | ExpectedVariable, |
| 47 | ExpectedMethod, |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 48 | ExpectedVariableFunctionOrLabel, |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 49 | ExpectedFieldOrGlobalVar, |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 50 | ExpectedStruct, |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 51 | ExpectedVariableFunctionOrTag, |
Richard Smith | 5f838aa | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 52 | ExpectedTLSVar, |
| 53 | ExpectedVariableOrField, |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 54 | ExpectedVariableFieldOrTag, |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame^] | 55 | ExpectedTypeOrNamespace, |
| 56 | ExpectedObjectiveCInterface |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 59 | //===----------------------------------------------------------------------===// |
| 60 | // Helper functions |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 63 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 64 | bool blocksToo = true) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 65 | QualType Ty; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 66 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 67 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 68 | else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 69 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 70 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 71 | Ty = decl->getUnderlyingType(); |
| 72 | else |
| 73 | return 0; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 75 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 76 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 755f9d2 | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 77 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 78 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 79 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 80 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 83 | // FIXME: We should provide an abstraction around a method or function |
| 84 | // to provide the following bits of information. |
| 85 | |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 86 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 87 | /// type (function or function-typed variable). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 88 | static bool isFunction(const Decl *D) { |
| 89 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 93 | /// type (function or function-typed variable) or an Objective-C |
| 94 | /// method. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 95 | static bool isFunctionOrMethod(const Decl *D) { |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 96 | return isFunction(D) || isa<ObjCMethodDecl>(D); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 99 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 100 | /// type (function or function-typed variable) or an Objective-C |
| 101 | /// method or a block. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 102 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 103 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 104 | return true; |
| 105 | // check for block is more involved. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 106 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 107 | QualType Ty = V->getType(); |
| 108 | return Ty->isBlockPointerType(); |
| 109 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 110 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 111 | } |
| 112 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 113 | /// Return true if the given decl has a declarator that should have |
| 114 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 115 | static bool hasDeclarator(const Decl *D) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 116 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 117 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 118 | isa<ObjCPropertyDecl>(D); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 121 | /// hasFunctionProto - Return true if the given decl has a argument |
| 122 | /// information. This decl should have already passed |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 123 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 124 | static bool hasFunctionProto(const Decl *D) { |
| 125 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 126 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 127 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 128 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 129 | return true; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 134 | /// arguments. It is an error to call this on a K&R function (use |
| 135 | /// hasFunctionProto first). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 136 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 137 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 138 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 139 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 140 | return BD->getNumParams(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 141 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 144 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 145 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 146 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 147 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 148 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 149 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 150 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 153 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 154 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 155 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 156 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 159 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 160 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 161 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 162 | return proto->isVariadic(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 163 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | db9a0ae | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 164 | return BD->isVariadic(); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 165 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 166 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 170 | static bool isInstanceMethod(const Decl *D) { |
| 171 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 172 | return MethodDecl->isInstance(); |
| 173 | return false; |
| 174 | } |
| 175 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 176 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 177 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 178 | if (!PT) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 179 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 180 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 181 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 182 | if (!Cls) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 183 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 184 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 185 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 187 | // FIXME: Should we walk the chain of classes? |
| 188 | return ClsName == &Ctx.Idents.get("NSString") || |
| 189 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 190 | } |
| 191 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 192 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 193 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 194 | if (!PT) |
| 195 | return false; |
| 196 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 197 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 198 | if (!RT) |
| 199 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 200 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 201 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 202 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 203 | return false; |
| 204 | |
| 205 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 206 | } |
| 207 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 208 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 209 | /// output an error. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 210 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 211 | unsigned int Num) { |
| 212 | if (Attr.getNumArgs() != Num) { |
| 213 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num; |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | return true; |
| 218 | } |
| 219 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 220 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 221 | /// \brief Check if the attribute has at least as many args as Num. May |
| 222 | /// output an error. |
| 223 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
| 224 | unsigned int Num) { |
| 225 | if (Attr.getNumArgs() < Num) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 226 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| 227 | return false; |
| 228 | } |
| 229 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 230 | return true; |
| 231 | } |
| 232 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 233 | /// \brief Check if IdxExpr is a valid argument index for a function or |
| 234 | /// instance method D. May output an error. |
| 235 | /// |
| 236 | /// \returns true if IdxExpr is a valid index. |
| 237 | static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D, |
| 238 | StringRef AttrName, |
| 239 | SourceLocation AttrLoc, |
| 240 | unsigned AttrArgNum, |
| 241 | const Expr *IdxExpr, |
| 242 | uint64_t &Idx) |
| 243 | { |
| 244 | assert(isFunctionOrMethod(D) && hasFunctionProto(D)); |
| 245 | |
| 246 | // In C++ the implicit 'this' function parameter also counts. |
| 247 | // Parameters are counted from one. |
| 248 | const bool HasImplicitThisParam = isInstanceMethod(D); |
| 249 | const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| 250 | const unsigned FirstIdx = 1; |
| 251 | |
| 252 | llvm::APSInt IdxInt; |
| 253 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 254 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
| 255 | S.Diag(AttrLoc, diag::err_attribute_argument_n_not_int) |
| 256 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 257 | return false; |
| 258 | } |
| 259 | |
| 260 | Idx = IdxInt.getLimitedValue(); |
| 261 | if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) { |
| 262 | S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds) |
| 263 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 264 | return false; |
| 265 | } |
| 266 | Idx--; // Convert to zero-based. |
| 267 | if (HasImplicitThisParam) { |
| 268 | if (Idx == 0) { |
| 269 | S.Diag(AttrLoc, |
| 270 | diag::err_attribute_invalid_implicit_this_argument) |
| 271 | << AttrName << IdxExpr->getSourceRange(); |
| 272 | return false; |
| 273 | } |
| 274 | --Idx; |
| 275 | } |
| 276 | |
| 277 | return true; |
| 278 | } |
| 279 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 280 | /// |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 281 | /// \brief Check if passed in Decl is a field or potentially shared global var |
| 282 | /// \return true if the Decl is a field or potentially shared global variable |
| 283 | /// |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 284 | static bool mayBeSharedVariable(const Decl *D) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 285 | if (isa<FieldDecl>(D)) |
| 286 | return true; |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 287 | if (const VarDecl *vd = dyn_cast<VarDecl>(D)) |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 288 | return vd->hasGlobalStorage() && !vd->getTLSKind(); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 289 | |
| 290 | return false; |
| 291 | } |
| 292 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 293 | /// \brief Check if the passed-in expression is of type int or bool. |
| 294 | static bool isIntOrBool(Expr *Exp) { |
| 295 | QualType QT = Exp->getType(); |
| 296 | return QT->isBooleanType() || QT->isIntegerType(); |
| 297 | } |
| 298 | |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 299 | |
| 300 | // Check to see if the type is a smart pointer of some kind. We assume |
| 301 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 302 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 303 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 304 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 305 | if (Res1.empty()) |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 306 | return false; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 307 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 308 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 309 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 310 | if (Res2.empty()) |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 311 | return false; |
| 312 | |
| 313 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 316 | /// \brief Check if passed in Decl is a pointer type. |
| 317 | /// Note that this function may produce an error message. |
| 318 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 319 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 320 | const AttributeList &Attr) { |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 321 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 322 | QualType QT = vd->getType(); |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 323 | if (QT->isAnyPointerType()) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 324 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 325 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 326 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 327 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 328 | // (We don't want to force template instantiation if we can avoid it, |
| 329 | // since that would alter the order in which templates are instantiated.) |
| 330 | if (RT->isIncompleteType()) |
| 331 | return true; |
| 332 | |
| 333 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 334 | return true; |
| 335 | } |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 336 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 337 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 338 | << Attr.getName()->getName() << QT; |
| 339 | } else { |
| 340 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 341 | << Attr.getName(); |
| 342 | } |
| 343 | return false; |
| 344 | } |
| 345 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 346 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 347 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 348 | static const RecordType *getRecordType(QualType QT) { |
| 349 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 350 | return RT; |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 351 | |
| 352 | // Now check if we point to record type. |
| 353 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 354 | return PT->getPointeeType()->getAs<RecordType>(); |
| 355 | |
| 356 | return 0; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 357 | } |
| 358 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 359 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 360 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 361 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 362 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
| 363 | if (RT->getDecl()->getAttr<LockableAttr>()) |
| 364 | return true; |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 369 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 370 | /// resolves to a lockable object. |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 371 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 372 | QualType Ty) { |
| 373 | const RecordType *RT = getRecordType(Ty); |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 374 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 375 | // Warn if could not get record type for this argument. |
Benjamin Kramer | d77ba89 | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 376 | if (!RT) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 377 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 378 | << Attr.getName() << Ty.getAsString(); |
| 379 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 380 | } |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 381 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 382 | // 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] | 383 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 384 | return; |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 385 | |
| 386 | // Allow smart pointers to be used as lockable objects. |
| 387 | // FIXME -- Check the type that the smart pointer points to. |
| 388 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 389 | return; |
| 390 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 391 | // Check if the type is lockable. |
| 392 | RecordDecl *RD = RT->getDecl(); |
| 393 | if (RD->getAttr<LockableAttr>()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 394 | return; |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 395 | |
| 396 | // Else check if any base classes are lockable. |
| 397 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 398 | CXXBasePaths BPaths(false, false); |
| 399 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 400 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 401 | } |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 402 | |
| 403 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 404 | << Attr.getName() << Ty.getAsString(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 407 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 408 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 409 | /// \param Sidx The attribute argument index to start checking with. |
| 410 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 411 | /// parameter list. |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 412 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 413 | const AttributeList &Attr, |
| 414 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 415 | int Sidx = 0, |
| 416 | bool ParamIdxOk = false) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 417 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 418 | Expr *ArgExp = Attr.getArg(Idx); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 419 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 420 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 421 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 422 | Args.push_back(ArgExp); |
| 423 | continue; |
| 424 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 425 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 426 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 427 | if (StrLit->getLength() == 0 || |
| 428 | StrLit->getString() == StringRef("*")) { |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 429 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 430 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 431 | Args.push_back(ArgExp); |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 432 | continue; |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 433 | } |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 434 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 435 | // We allow constant strings to be used as a placeholder for expressions |
| 436 | // that are not valid C++ syntax, but warn that they are ignored. |
| 437 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 438 | Attr.getName(); |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 439 | Args.push_back(ArgExp); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 440 | continue; |
| 441 | } |
| 442 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 443 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 444 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 445 | // A pointer to member expression of the form &MyClass::mu is treated |
| 446 | // specially -- we need to look at the type of the member. |
| 447 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 448 | if (UOp->getOpcode() == UO_AddrOf) |
| 449 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 450 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 451 | ArgTy = DRE->getDecl()->getType(); |
| 452 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 453 | // First see if we can just cast to record type, or point to record type. |
| 454 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 455 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 456 | // Now check if we index into a record type function param. |
| 457 | if(!RT && ParamIdxOk) { |
| 458 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 459 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 460 | if(FD && IL) { |
| 461 | unsigned int NumParams = FD->getNumParams(); |
| 462 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 463 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 464 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 465 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 466 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 467 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 468 | continue; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 469 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 470 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 474 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 475 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 476 | Args.push_back(ArgExp); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 477 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 480 | //===----------------------------------------------------------------------===// |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 481 | // Attribute Implementations |
| 482 | //===----------------------------------------------------------------------===// |
| 483 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 484 | // FIXME: All this manual attribute parsing code is gross. At the |
| 485 | // least add some helper functions to check most argument patterns (# |
| 486 | // and types of args). |
| 487 | |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 488 | enum ThreadAttributeDeclKind { |
| 489 | ThreadExpectedFieldOrGlobalVar, |
| 490 | ThreadExpectedFunctionOrMethod, |
| 491 | ThreadExpectedClassOrStruct |
| 492 | }; |
| 493 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 494 | static bool checkGuardedVarAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 495 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 496 | assert(!Attr.isInvalid()); |
| 497 | |
| 498 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 499 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 500 | |
| 501 | // D must be either a member field or global (potentially shared) variable. |
| 502 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 503 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 504 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 505 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 508 | return true; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 511 | static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 512 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 513 | return; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 514 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 515 | D->addAttr(::new (S.Context) |
| 516 | GuardedVarAttr(Attr.getRange(), S.Context, |
| 517 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 520 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 521 | const AttributeList &Attr) { |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 522 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 523 | return; |
| 524 | |
| 525 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 526 | return; |
| 527 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 528 | D->addAttr(::new (S.Context) |
| 529 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 530 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 533 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 534 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 535 | Expr* &Arg) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 536 | assert(!Attr.isInvalid()); |
| 537 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 538 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 539 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 540 | |
| 541 | // D must be either a member field or global (potentially shared) variable. |
| 542 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 543 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 544 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 545 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 546 | } |
| 547 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 548 | SmallVector<Expr*, 1> Args; |
| 549 | // check that all arguments are lockable objects |
| 550 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 551 | unsigned Size = Args.size(); |
| 552 | if (Size != 1) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 553 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 554 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 555 | Arg = Args[0]; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 556 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 557 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 560 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 561 | Expr *Arg = 0; |
| 562 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 563 | return; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 564 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 565 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| 566 | } |
| 567 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 568 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 569 | const AttributeList &Attr) { |
| 570 | Expr *Arg = 0; |
| 571 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 572 | return; |
| 573 | |
| 574 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 575 | return; |
| 576 | |
| 577 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| 578 | S.Context, Arg)); |
| 579 | } |
| 580 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 581 | static bool checkLockableAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 582 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 583 | assert(!Attr.isInvalid()); |
| 584 | |
| 585 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 586 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 587 | |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 588 | // FIXME: Lockable structs for C code. |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 589 | if (!isa<CXXRecordDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 590 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 591 | << Attr.getName() << ThreadExpectedClassOrStruct; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 592 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 595 | return true; |
| 596 | } |
| 597 | |
| 598 | static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 599 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 600 | return; |
| 601 | |
| 602 | D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); |
| 603 | } |
| 604 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 605 | static void handleScopedLockableAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 606 | const AttributeList &Attr) { |
| 607 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 608 | return; |
| 609 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 610 | D->addAttr(::new (S.Context) |
| 611 | ScopedLockableAttr(Attr.getRange(), S.Context, |
| 612 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 615 | static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D, |
| 616 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 617 | assert(!Attr.isInvalid()); |
| 618 | |
| 619 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 620 | return; |
| 621 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 622 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 623 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 624 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 625 | return; |
| 626 | } |
| 627 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 628 | D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(), |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 629 | S.Context)); |
| 630 | } |
| 631 | |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 632 | static void handleNoSanitizeAddressAttr(Sema &S, Decl *D, |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 633 | const AttributeList &Attr) { |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 634 | assert(!Attr.isInvalid()); |
| 635 | |
| 636 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 637 | return; |
| 638 | |
| 639 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 640 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 641 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 642 | return; |
| 643 | } |
| 644 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 645 | D->addAttr(::new (S.Context) |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 646 | NoSanitizeAddressAttr(Attr.getRange(), S.Context, |
| 647 | Attr.getAttributeSpellingListIndex())); |
| 648 | } |
| 649 | |
| 650 | static void handleNoSanitizeMemory(Sema &S, Decl *D, |
| 651 | const AttributeList &Attr) { |
| 652 | assert(!Attr.isInvalid()); |
| 653 | |
| 654 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 655 | return; |
| 656 | |
| 657 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 658 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 659 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 660 | return; |
| 661 | } |
| 662 | |
| 663 | D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(), |
| 664 | S.Context)); |
| 665 | } |
| 666 | |
| 667 | static void handleNoSanitizeThread(Sema &S, Decl *D, |
| 668 | const AttributeList &Attr) { |
| 669 | assert(!Attr.isInvalid()); |
| 670 | |
| 671 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 672 | return; |
| 673 | |
| 674 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 675 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 676 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 677 | return; |
| 678 | } |
| 679 | |
| 680 | D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(), |
| 681 | S.Context)); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 684 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 685 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 686 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 687 | assert(!Attr.isInvalid()); |
| 688 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 689 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 690 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 691 | |
| 692 | // D must be either a member field or global (potentially shared) variable. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 693 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 694 | if (!VD || !mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 695 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 696 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 697 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 698 | } |
| 699 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 700 | // Check that this attribute only applies to lockable types. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 701 | QualType QT = VD->getType(); |
| 702 | if (!QT->isDependentType()) { |
| 703 | const RecordType *RT = getRecordType(QT); |
| 704 | if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 705 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 706 | << Attr.getName(); |
| 707 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 711 | // Check that all arguments are lockable objects. |
| 712 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 713 | if (Args.empty()) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 714 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 715 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 716 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 719 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 720 | const AttributeList &Attr) { |
| 721 | SmallVector<Expr*, 1> Args; |
| 722 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 723 | return; |
| 724 | |
| 725 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 726 | D->addAttr(::new (S.Context) |
| 727 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 728 | StartArg, Args.size(), |
| 729 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 732 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 733 | const AttributeList &Attr) { |
| 734 | SmallVector<Expr*, 1> Args; |
| 735 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 736 | return; |
| 737 | |
| 738 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 739 | D->addAttr(::new (S.Context) |
| 740 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 741 | StartArg, Args.size(), |
| 742 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 745 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 746 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 747 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 748 | assert(!Attr.isInvalid()); |
| 749 | |
| 750 | // zero or more arguments ok |
| 751 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 752 | // check that the attribute is applied to a function |
| 753 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 754 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 755 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 756 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 759 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 760 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 761 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 762 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 765 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 766 | const AttributeList &Attr) { |
| 767 | SmallVector<Expr*, 1> Args; |
| 768 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 769 | return; |
| 770 | |
| 771 | unsigned Size = Args.size(); |
| 772 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 773 | D->addAttr(::new (S.Context) |
| 774 | SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 775 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 778 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 779 | const AttributeList &Attr) { |
| 780 | SmallVector<Expr*, 1> Args; |
| 781 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 782 | return; |
| 783 | |
| 784 | unsigned Size = Args.size(); |
| 785 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 786 | D->addAttr(::new (S.Context) |
| 787 | ExclusiveLockFunctionAttr(Attr.getRange(), S.Context, |
| 788 | StartArg, Size, |
| 789 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 790 | } |
| 791 | |
DeLesley Hutchins | 5c6134f | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 792 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 793 | const AttributeList &Attr) { |
| 794 | SmallVector<Expr*, 1> Args; |
| 795 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 796 | return; |
| 797 | |
| 798 | unsigned Size = Args.size(); |
| 799 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 800 | D->addAttr(::new (S.Context) |
| 801 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 802 | Attr.getAttributeSpellingListIndex())); |
| 803 | } |
| 804 | |
| 805 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 806 | const AttributeList &Attr) { |
| 807 | SmallVector<Expr*, 1> Args; |
| 808 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 809 | return; |
| 810 | |
| 811 | unsigned Size = Args.size(); |
| 812 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 813 | D->addAttr(::new (S.Context) |
| 814 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 815 | StartArg, Size, |
| 816 | Attr.getAttributeSpellingListIndex())); |
| 817 | } |
| 818 | |
| 819 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 820 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 821 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 822 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 823 | assert(!Attr.isInvalid()); |
| 824 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 825 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 826 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 827 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 828 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 829 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 830 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 831 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 834 | if (!isIntOrBool(Attr.getArg(0))) { |
| 835 | 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] | 836 | << Attr.getName(); |
| 837 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 841 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 842 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 843 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 846 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 847 | const AttributeList &Attr) { |
| 848 | SmallVector<Expr*, 2> Args; |
| 849 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 850 | return; |
| 851 | |
| 852 | unsigned Size = Args.size(); |
| 853 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 854 | D->addAttr(::new (S.Context) |
| 855 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
| 856 | Attr.getArg(0), StartArg, Size, |
| 857 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 860 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 861 | const AttributeList &Attr) { |
| 862 | SmallVector<Expr*, 2> Args; |
| 863 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 864 | return; |
| 865 | |
| 866 | unsigned Size = Args.size(); |
| 867 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 868 | D->addAttr(::new (S.Context) |
| 869 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
| 870 | Attr.getArg(0), StartArg, Size, |
| 871 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 874 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 875 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 876 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 877 | assert(!Attr.isInvalid()); |
| 878 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 879 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 880 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 881 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 882 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 883 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 884 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 885 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 888 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 889 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 890 | if (Args.empty()) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 891 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 892 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 893 | return true; |
| 894 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 895 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 896 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 897 | const AttributeList &Attr) { |
| 898 | SmallVector<Expr*, 1> Args; |
| 899 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 900 | return; |
| 901 | |
| 902 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 903 | D->addAttr(::new (S.Context) |
| 904 | ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context, |
| 905 | StartArg, Args.size(), |
| 906 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 909 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 910 | const AttributeList &Attr) { |
| 911 | SmallVector<Expr*, 1> Args; |
| 912 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 913 | return; |
| 914 | |
| 915 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 916 | D->addAttr(::new (S.Context) |
| 917 | SharedLocksRequiredAttr(Attr.getRange(), S.Context, |
| 918 | StartArg, Args.size(), |
| 919 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 922 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 923 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 924 | assert(!Attr.isInvalid()); |
| 925 | |
| 926 | // zero or more arguments ok |
| 927 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 928 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 929 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 930 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 931 | return; |
| 932 | } |
| 933 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 934 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 935 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 936 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 937 | unsigned Size = Args.size(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 938 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 939 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 940 | D->addAttr(::new (S.Context) |
| 941 | UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 942 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 946 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 947 | assert(!Attr.isInvalid()); |
| 948 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 949 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 950 | return; |
| 951 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 952 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 953 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 954 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 955 | return; |
| 956 | } |
| 957 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 958 | // check that the argument is lockable object |
DeLesley Hutchins | f26efd7 | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 959 | SmallVector<Expr*, 1> Args; |
| 960 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 961 | unsigned Size = Args.size(); |
| 962 | if (Size == 0) |
| 963 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 964 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 965 | D->addAttr(::new (S.Context) |
| 966 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 967 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 971 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 972 | assert(!Attr.isInvalid()); |
| 973 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 974 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 975 | return; |
| 976 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 977 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 978 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 979 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 980 | return; |
| 981 | } |
| 982 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 983 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 984 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 985 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 986 | unsigned Size = Args.size(); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 987 | if (Size == 0) |
| 988 | return; |
| 989 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 990 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 991 | D->addAttr(::new (S.Context) |
| 992 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 993 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 997 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 998 | const AttributeList &Attr) { |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 999 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 1000 | if (TD == 0) { |
| 1001 | // __attribute__((ext_vector_type(N))) can only be applied to typedefs |
| 1002 | // and type-ids. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1003 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1004 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1005 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1006 | |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1007 | // Remember this typedef decl, we will need it later for diagnostics. |
| 1008 | S.ExtVectorDecls.push_back(TD); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1011 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1012 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1013 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1014 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1015 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1016 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1017 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1018 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1019 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1020 | // has no effect. |
Eli Friedman | b68ec6b | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1021 | if (!FD->getType()->isDependentType() && |
| 1022 | !FD->getType()->isIncompleteType() && |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1023 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1024 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1025 | << Attr.getName() << FD->getType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1026 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1027 | FD->addAttr(::new (S.Context) |
| 1028 | PackedAttr(Attr.getRange(), S.Context, |
| 1029 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1030 | } else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1031 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1034 | static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 5f608ae | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 1035 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1036 | RD->addAttr(::new (S.Context) |
| 1037 | MsStructAttr(Attr.getRange(), S.Context, |
| 1038 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | c1a0a73 | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 1039 | else |
| 1040 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1041 | } |
| 1042 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1043 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1044 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1045 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1046 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1047 | |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1048 | // The IBAction attributes only apply to instance methods. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1049 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1050 | if (MD->isInstanceMethod()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1051 | D->addAttr(::new (S.Context) |
| 1052 | IBActionAttr(Attr.getRange(), S.Context, |
| 1053 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1054 | return; |
| 1055 | } |
| 1056 | |
Ted Kremenek | 4ee2bb1 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 1057 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1060 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1061 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1062 | // variables or properties of Objective-C classes. The outlet must also |
| 1063 | // have an object reference type. |
| 1064 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1065 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 0bfaf06 | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1066 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1067 | << Attr.getName() << VD->getType() << 0; |
| 1068 | return false; |
| 1069 | } |
| 1070 | } |
| 1071 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1072 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1073 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1074 | << Attr.getName() << PD->getType() << 1; |
| 1075 | return false; |
| 1076 | } |
| 1077 | } |
| 1078 | else { |
| 1079 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1080 | return false; |
| 1081 | } |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1082 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1083 | return true; |
| 1084 | } |
| 1085 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1086 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1087 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1088 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1089 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1090 | |
| 1091 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1092 | return; |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1093 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1094 | D->addAttr(::new (S.Context) |
| 1095 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1096 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1099 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1100 | const AttributeList &Attr) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1101 | |
| 1102 | // The iboutletcollection attribute can have zero or one arguments. |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1103 | if (Attr.getParameterName() && Attr.getNumArgs() > 0) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1104 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1105 | return; |
| 1106 | } |
| 1107 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1108 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1109 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1110 | |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1111 | IdentifierInfo *II = Attr.getParameterName(); |
| 1112 | if (!II) |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1113 | II = &S.Context.Idents.get("NSObject"); |
Fariborz Jahanian | 3a3400b | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 1114 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1115 | ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1116 | S.getScopeForContext(D->getDeclContext()->getParent())); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1117 | if (!TypeRep) { |
| 1118 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1119 | return; |
| 1120 | } |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1121 | QualType QT = TypeRep.get(); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1122 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1123 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1124 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1125 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1126 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1127 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1128 | return; |
| 1129 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1130 | D->addAttr(::new (S.Context) |
| 1131 | IBOutletCollectionAttr(Attr.getRange(),S.Context, |
| 1132 | QT, Attr.getParameterLoc(), |
| 1133 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1136 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1137 | if (const RecordType *UT = T->getAsUnionType()) |
| 1138 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1139 | RecordDecl *UD = UT->getDecl(); |
| 1140 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1141 | itend = UD->field_end(); it != itend; ++it) { |
| 1142 | QualType QT = it->getType(); |
| 1143 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1144 | T = QT; |
| 1145 | return; |
| 1146 | } |
| 1147 | } |
| 1148 | } |
| 1149 | } |
| 1150 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1151 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nuno Lopes | 174930d | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1152 | if (!isFunctionOrMethod(D)) { |
| 1153 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1154 | << "alloc_size" << ExpectedFunctionOrMethod; |
| 1155 | return; |
| 1156 | } |
| 1157 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1158 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1159 | return; |
| 1160 | |
| 1161 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1162 | // counted from one. |
| 1163 | bool HasImplicitThisParam = isInstanceMethod(D); |
Argyrios Kyrtzidis | 28965bf | 2013-02-22 06:58:28 +0000 | [diff] [blame] | 1164 | unsigned NumArgs; |
| 1165 | if (hasFunctionProto(D)) |
| 1166 | NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| 1167 | else |
| 1168 | NumArgs = 0; |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1169 | |
| 1170 | SmallVector<unsigned, 8> SizeArgs; |
| 1171 | |
| 1172 | for (AttributeList::arg_iterator I = Attr.arg_begin(), |
| 1173 | E = Attr.arg_end(); I!=E; ++I) { |
| 1174 | // The argument must be an integer constant expression. |
| 1175 | Expr *Ex = *I; |
| 1176 | llvm::APSInt ArgNum; |
| 1177 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 1178 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1179 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1180 | << "alloc_size" << Ex->getSourceRange(); |
| 1181 | return; |
| 1182 | } |
| 1183 | |
| 1184 | uint64_t x = ArgNum.getZExtValue(); |
| 1185 | |
| 1186 | if (x < 1 || x > NumArgs) { |
| 1187 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1188 | << "alloc_size" << I.getArgNum() << Ex->getSourceRange(); |
| 1189 | return; |
| 1190 | } |
| 1191 | |
| 1192 | --x; |
| 1193 | if (HasImplicitThisParam) { |
| 1194 | if (x == 0) { |
| 1195 | S.Diag(Attr.getLoc(), |
| 1196 | diag::err_attribute_invalid_implicit_this_argument) |
| 1197 | << "alloc_size" << Ex->getSourceRange(); |
| 1198 | return; |
| 1199 | } |
| 1200 | --x; |
| 1201 | } |
| 1202 | |
| 1203 | // check if the function argument is of an integer type |
| 1204 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
| 1205 | if (!T->isIntegerType()) { |
| 1206 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1207 | << "alloc_size" << Ex->getSourceRange(); |
| 1208 | return; |
| 1209 | } |
| 1210 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1211 | SizeArgs.push_back(x); |
| 1212 | } |
| 1213 | |
| 1214 | // check if the function returns a pointer |
| 1215 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 1216 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
| 1217 | << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
| 1218 | } |
| 1219 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1220 | D->addAttr(::new (S.Context) |
| 1221 | AllocSizeAttr(Attr.getRange(), S.Context, |
| 1222 | SizeArgs.data(), SizeArgs.size(), |
| 1223 | Attr.getAttributeSpellingListIndex())); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1226 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1227 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1228 | // ignore it as well |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1229 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1230 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1231 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1232 | return; |
| 1233 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1234 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1235 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1236 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1237 | bool HasImplicitThisParam = isInstanceMethod(D); |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1238 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1239 | |
| 1240 | // The nonnull attribute only applies to pointers. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1241 | SmallVector<unsigned, 10> NonNullArgs; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1242 | |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1243 | for (AttributeList::arg_iterator I = Attr.arg_begin(), |
| 1244 | E = Attr.arg_end(); I != E; ++I) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1245 | // The argument must be an integer constant expression. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1246 | Expr *Ex = *I; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1247 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1248 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 1249 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1250 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1251 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1252 | return; |
| 1253 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1254 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1255 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1256 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1257 | if (x < 1 || x > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1258 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 1259 | << "nonnull" << I.getArgNum() << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1260 | return; |
| 1261 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1262 | |
Ted Kremenek | 465172f | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 1263 | --x; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1264 | if (HasImplicitThisParam) { |
| 1265 | if (x == 0) { |
| 1266 | S.Diag(Attr.getLoc(), |
| 1267 | diag::err_attribute_invalid_implicit_this_argument) |
| 1268 | << "nonnull" << Ex->getSourceRange(); |
| 1269 | return; |
| 1270 | } |
| 1271 | --x; |
| 1272 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1273 | |
| 1274 | // Is the function argument a pointer type? |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1275 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1276 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1277 | |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1278 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1279 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | c9ef405 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1280 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1281 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1282 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1283 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1284 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1285 | NonNullArgs.push_back(x); |
| 1286 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1287 | |
| 1288 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1289 | // arguments have a nonnull attribute. |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1290 | if (NonNullArgs.empty()) { |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1291 | for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) { |
| 1292 | QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1293 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1294 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1295 | NonNullArgs.push_back(i); |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1296 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1297 | |
Ted Kremenek | ee1c08c | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1298 | // No pointer arguments? |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1299 | if (NonNullArgs.empty()) { |
| 1300 | // Warn the trivial case only if attribute is not coming from a |
| 1301 | // macro instantiation. |
| 1302 | if (Attr.getLoc().isFileID()) |
| 1303 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1304 | return; |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1305 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1306 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1307 | |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1308 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1309 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1310 | llvm::array_pod_sort(start, start + size); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1311 | D->addAttr(::new (S.Context) |
| 1312 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1313 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1316 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1317 | // This attribute must be applied to a function declaration. |
| 1318 | // The first argument to the attribute must be a string, |
| 1319 | // the name of the resource, for example "malloc". |
| 1320 | // The following arguments must be argument indexes, the arguments must be |
| 1321 | // of integer type for Returns, otherwise of pointer type. |
| 1322 | // 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] | 1323 | // after being held. free() should be __attribute((ownership_takes)), whereas |
| 1324 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1325 | |
| 1326 | if (!AL.getParameterName()) { |
| 1327 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1328 | << AL.getName()->getName() << 1; |
| 1329 | return; |
| 1330 | } |
| 1331 | // Figure out our Kind, and check arguments while we're at it. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1332 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1333 | switch (AL.getKind()) { |
| 1334 | case AttributeList::AT_ownership_takes: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1335 | K = OwnershipAttr::Takes; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1336 | if (AL.getNumArgs() < 1) { |
| 1337 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1338 | return; |
| 1339 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1340 | break; |
| 1341 | case AttributeList::AT_ownership_holds: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1342 | K = OwnershipAttr::Holds; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1343 | if (AL.getNumArgs() < 1) { |
| 1344 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1345 | return; |
| 1346 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1347 | break; |
| 1348 | case AttributeList::AT_ownership_returns: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1349 | K = OwnershipAttr::Returns; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1350 | if (AL.getNumArgs() > 1) { |
| 1351 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1352 | << AL.getNumArgs() + 1; |
| 1353 | return; |
| 1354 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1355 | break; |
| 1356 | default: |
| 1357 | // This should never happen given how we are called. |
| 1358 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1361 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1362 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1363 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1364 | return; |
| 1365 | } |
| 1366 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1367 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1368 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1369 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1370 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1371 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1372 | StringRef Module = AL.getParameterName()->getName(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1373 | |
| 1374 | // Normalize the argument, __foo__ becomes foo. |
| 1375 | if (Module.startswith("__") && Module.endswith("__")) |
| 1376 | Module = Module.substr(2, Module.size() - 4); |
| 1377 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1378 | SmallVector<unsigned, 10> OwnershipArgs; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1379 | |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1380 | for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E; |
| 1381 | ++I) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1382 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1383 | Expr *IdxExpr = *I; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1384 | llvm::APSInt ArgNum(32); |
| 1385 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1386 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1387 | S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int) |
| 1388 | << AL.getName()->getName() << IdxExpr->getSourceRange(); |
| 1389 | continue; |
| 1390 | } |
| 1391 | |
| 1392 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
| 1393 | |
| 1394 | if (x > NumArgs || x < 1) { |
| 1395 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1396 | << AL.getName()->getName() << x << IdxExpr->getSourceRange(); |
| 1397 | continue; |
| 1398 | } |
| 1399 | --x; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1400 | if (HasImplicitThisParam) { |
| 1401 | if (x == 0) { |
| 1402 | S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 1403 | << "ownership" << IdxExpr->getSourceRange(); |
| 1404 | return; |
| 1405 | } |
| 1406 | --x; |
| 1407 | } |
| 1408 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1409 | switch (K) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1410 | case OwnershipAttr::Takes: |
| 1411 | case OwnershipAttr::Holds: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1412 | // Is the function argument a pointer type? |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1413 | QualType T = getFunctionOrMethodArgType(D, x); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1414 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| 1415 | // FIXME: Should also highlight argument in decl. |
| 1416 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1417 | << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1418 | << "pointer" |
| 1419 | << IdxExpr->getSourceRange(); |
| 1420 | continue; |
| 1421 | } |
| 1422 | break; |
| 1423 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1424 | case OwnershipAttr::Returns: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1425 | if (AL.getNumArgs() > 1) { |
| 1426 | // Is the function argument an integer type? |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1427 | Expr *IdxExpr = AL.getArg(0); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1428 | llvm::APSInt ArgNum(32); |
| 1429 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1430 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1431 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
| 1432 | << "ownership_returns" << "integer" |
| 1433 | << IdxExpr->getSourceRange(); |
| 1434 | return; |
| 1435 | } |
| 1436 | } |
| 1437 | break; |
| 1438 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1439 | } // switch |
| 1440 | |
| 1441 | // Check we don't have a conflict with another ownership attribute. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1442 | for (specific_attr_iterator<OwnershipAttr> |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1443 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1444 | e = D->specific_attr_end<OwnershipAttr>(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1445 | i != e; ++i) { |
| 1446 | if ((*i)->getOwnKind() != K) { |
| 1447 | for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); |
| 1448 | I!=E; ++I) { |
| 1449 | if (x == *I) { |
| 1450 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1451 | << AL.getName()->getName() << "ownership_*"; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | } |
| 1456 | OwnershipArgs.push_back(x); |
| 1457 | } |
| 1458 | |
| 1459 | unsigned* start = OwnershipArgs.data(); |
| 1460 | unsigned size = OwnershipArgs.size(); |
| 1461 | llvm::array_pod_sort(start, start + size); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1462 | |
| 1463 | if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { |
| 1464 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1465 | return; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1466 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1467 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1468 | D->addAttr(::new (S.Context) |
| 1469 | OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size, |
| 1470 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1473 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1474 | // Check the attribute arguments. |
| 1475 | if (Attr.getNumArgs() > 1) { |
| 1476 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1477 | return; |
| 1478 | } |
| 1479 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1480 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1481 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1482 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1483 | return; |
| 1484 | } |
| 1485 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1486 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1487 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1488 | // gcc rejects |
| 1489 | // class c { |
| 1490 | // static int a __attribute__((weakref ("v2"))); |
| 1491 | // static int b() __attribute__((weakref ("f3"))); |
| 1492 | // }; |
| 1493 | // and ignores the attributes of |
| 1494 | // void f(void) { |
| 1495 | // static int a __attribute__((weakref ("v2"))); |
| 1496 | // } |
| 1497 | // we reject them |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1498 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1499 | if (!Ctx->isFileContext()) { |
| 1500 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1501 | nd->getNameAsString(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1502 | return; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | // The GCC manual says |
| 1506 | // |
| 1507 | // At present, a declaration to which `weakref' is attached can only |
| 1508 | // be `static'. |
| 1509 | // |
| 1510 | // It also says |
| 1511 | // |
| 1512 | // Without a TARGET, |
| 1513 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1514 | // equivalent to `weak'. |
| 1515 | // |
| 1516 | // gcc 4.4.1 will accept |
| 1517 | // int a7 __attribute__((weakref)); |
| 1518 | // as |
| 1519 | // int a7 __attribute__((weak)); |
| 1520 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1521 | // it if this behaviour is actually used. |
| 1522 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1523 | // GCC rejects |
| 1524 | // static ((alias ("y"), weakref)). |
| 1525 | // Should we? How to check that weakref is before or after alias? |
| 1526 | |
| 1527 | if (Attr.getNumArgs() == 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1528 | Expr *Arg = Attr.getArg(0); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1529 | Arg = Arg->IgnoreParenCasts(); |
| 1530 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1531 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1532 | if (!Str || !Str->isAscii()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1533 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1534 | << "weakref" << 1; |
| 1535 | return; |
| 1536 | } |
| 1537 | // GCC will accept anything as the argument of weakref. Should we |
| 1538 | // check for an existing decl? |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1539 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1540 | Str->getString())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1543 | D->addAttr(::new (S.Context) |
| 1544 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1545 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1548 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1549 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1550 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1551 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1552 | return; |
| 1553 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1554 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1555 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1556 | Arg = Arg->IgnoreParenCasts(); |
| 1557 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1558 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1559 | if (!Str || !Str->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1560 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1561 | << "alias" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1562 | return; |
| 1563 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1564 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1565 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | f5fe292 | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1566 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1567 | return; |
| 1568 | } |
| 1569 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1570 | // FIXME: check if target symbol exists in current file |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1571 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1572 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1573 | Str->getString(), |
| 1574 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1577 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1578 | // Check the attribute arguments. |
| 1579 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1580 | return; |
| 1581 | |
| 1582 | if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) { |
| 1583 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1584 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1585 | return; |
| 1586 | } |
| 1587 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1588 | D->addAttr(::new (S.Context) |
| 1589 | MinSizeAttr(Attr.getRange(), S.Context, |
| 1590 | Attr.getAttributeSpellingListIndex())); |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1593 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1594 | // Check the attribute arguments. |
| 1595 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1596 | return; |
| 1597 | |
| 1598 | if (!isa<FunctionDecl>(D)) { |
| 1599 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1600 | << Attr.getName() << ExpectedFunction; |
| 1601 | return; |
| 1602 | } |
| 1603 | |
| 1604 | if (D->hasAttr<HotAttr>()) { |
| 1605 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1606 | << Attr.getName() << "hot"; |
| 1607 | return; |
| 1608 | } |
| 1609 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1610 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1611 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1615 | // Check the attribute arguments. |
| 1616 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1617 | return; |
| 1618 | |
| 1619 | if (!isa<FunctionDecl>(D)) { |
| 1620 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1621 | << Attr.getName() << ExpectedFunction; |
| 1622 | return; |
| 1623 | } |
| 1624 | |
| 1625 | if (D->hasAttr<ColdAttr>()) { |
| 1626 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1627 | << Attr.getName() << "cold"; |
| 1628 | return; |
| 1629 | } |
| 1630 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1631 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1632 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1635 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1636 | // Check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1637 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1638 | return; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1639 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1640 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1641 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1642 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1643 | return; |
| 1644 | } |
| 1645 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1646 | D->addAttr(::new (S.Context) |
| 1647 | NakedAttr(Attr.getRange(), S.Context, |
| 1648 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1651 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 1652 | const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1653 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1654 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1655 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1656 | return; |
| 1657 | } |
| 1658 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1659 | if (!isa<FunctionDecl>(D)) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1660 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1661 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1662 | return; |
| 1663 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1664 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1665 | D->addAttr(::new (S.Context) |
| 1666 | AlwaysInlineAttr(Attr.getRange(), S.Context, |
| 1667 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1670 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1671 | const AttributeList &Attr) { |
| 1672 | // Check the attribute arguments. |
| 1673 | if (Attr.getNumArgs() != 1) { |
| 1674 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1675 | return; |
| 1676 | } |
| 1677 | |
| 1678 | Expr *Arg = Attr.getArg(0); |
| 1679 | Arg = Arg->IgnoreParenCasts(); |
| 1680 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1681 | |
| 1682 | // Check that it is a string. |
| 1683 | if (!Str) { |
| 1684 | S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model"; |
| 1685 | return; |
| 1686 | } |
| 1687 | |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 1688 | if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) { |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1689 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1690 | << Attr.getName() << ExpectedTLSVar; |
| 1691 | return; |
| 1692 | } |
| 1693 | |
| 1694 | // Check that the value. |
| 1695 | StringRef Model = Str->getString(); |
| 1696 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1697 | && Model != "initial-exec" && Model != "local-exec") { |
| 1698 | S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg); |
| 1699 | return; |
| 1700 | } |
| 1701 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1702 | D->addAttr(::new (S.Context) |
| 1703 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1704 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1707 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1708 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1709 | if (Attr.hasParameterOrArguments()) { |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1710 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1711 | return; |
| 1712 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1713 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1714 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1715 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1716 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1717 | D->addAttr(::new (S.Context) |
| 1718 | MallocAttr(Attr.getRange(), S.Context, |
| 1719 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1720 | return; |
| 1721 | } |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1724 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1727 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1728 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1729 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1730 | return; |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1731 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1732 | D->addAttr(::new (S.Context) |
| 1733 | MayAliasAttr(Attr.getRange(), S.Context, |
| 1734 | Attr.getAttributeSpellingListIndex())); |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1737 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1738 | assert(!Attr.isInvalid()); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1739 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1740 | D->addAttr(::new (S.Context) |
| 1741 | NoCommonAttr(Attr.getRange(), S.Context, |
| 1742 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1743 | else |
| 1744 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1745 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1748 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1749 | assert(!Attr.isInvalid()); |
Eli Friedman | 3e1aca2 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1750 | |
| 1751 | if (S.LangOpts.CPlusPlus) { |
| 1752 | S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus); |
| 1753 | return; |
| 1754 | } |
| 1755 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1756 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1757 | D->addAttr(::new (S.Context) |
| 1758 | CommonAttr(Attr.getRange(), S.Context, |
| 1759 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1760 | else |
| 1761 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1762 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1765 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1766 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1767 | |
| 1768 | if (S.CheckNoReturnAttr(attr)) return; |
| 1769 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1770 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1771 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1772 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1773 | return; |
| 1774 | } |
| 1775 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1776 | D->addAttr(::new (S.Context) |
| 1777 | NoReturnAttr(attr.getRange(), S.Context, |
| 1778 | attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1782 | if (attr.hasParameterOrArguments()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1783 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1784 | attr.setInvalid(); |
| 1785 | return true; |
| 1786 | } |
| 1787 | |
| 1788 | return false; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1791 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1792 | const AttributeList &Attr) { |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1793 | |
| 1794 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1795 | // because 'analyzer_noreturn' does not impact the type. |
| 1796 | |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1797 | if(!checkAttributeNumArgs(S, Attr, 0)) |
| 1798 | return; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1799 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1800 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1801 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1802 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1803 | && !VD->getType()->isFunctionPointerType())) { |
| 1804 | S.Diag(Attr.getLoc(), |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1805 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1806 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1807 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1808 | return; |
| 1809 | } |
| 1810 | } |
| 1811 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1812 | D->addAttr(::new (S.Context) |
| 1813 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1814 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1817 | static void handleCXX11NoReturnAttr(Sema &S, Decl *D, |
| 1818 | const AttributeList &Attr) { |
| 1819 | // C++11 [dcl.attr.noreturn]p1: |
| 1820 | // The attribute may be applied to the declarator-id in a function |
| 1821 | // declaration. |
| 1822 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 1823 | if (!FD) { |
| 1824 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1825 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1826 | return; |
| 1827 | } |
| 1828 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1829 | D->addAttr(::new (S.Context) |
| 1830 | CXX11NoReturnAttr(Attr.getRange(), S.Context, |
| 1831 | Attr.getAttributeSpellingListIndex())); |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1834 | // PS3 PPU-specific. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1835 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1836 | /* |
| 1837 | Returning a Vector Class in Registers |
| 1838 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1839 | According to the PPU ABI specifications, a class with a single member of |
| 1840 | vector type is returned in memory when used as the return value of a function. |
| 1841 | This results in inefficient code when implementing vector classes. To return |
| 1842 | the value in a single vector register, add the vecreturn attribute to the |
| 1843 | class definition. This attribute is also applicable to struct types. |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1844 | |
| 1845 | Example: |
| 1846 | |
| 1847 | struct Vector |
| 1848 | { |
| 1849 | __vector float xyzw; |
| 1850 | } __attribute__((vecreturn)); |
| 1851 | |
| 1852 | Vector Add(Vector lhs, Vector rhs) |
| 1853 | { |
| 1854 | Vector result; |
| 1855 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1856 | return result; // This will be returned in a register |
| 1857 | } |
| 1858 | */ |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1859 | if (!isa<RecordDecl>(D)) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1860 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1861 | << Attr.getName() << ExpectedClass; |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1862 | return; |
| 1863 | } |
| 1864 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1865 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1866 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1867 | return; |
| 1868 | } |
| 1869 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1870 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1871 | int count = 0; |
| 1872 | |
| 1873 | if (!isa<CXXRecordDecl>(record)) { |
| 1874 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1875 | return; |
| 1876 | } |
| 1877 | |
| 1878 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1879 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1880 | return; |
| 1881 | } |
| 1882 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1883 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1884 | iter != record->field_end(); iter++) { |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1885 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1886 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1887 | return; |
| 1888 | } |
| 1889 | count++; |
| 1890 | } |
| 1891 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1892 | D->addAttr(::new (S.Context) |
| 1893 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1894 | Attr.getAttributeSpellingListIndex())); |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1897 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1898 | const AttributeList &Attr) { |
| 1899 | if (isa<ParmVarDecl>(D)) { |
| 1900 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1901 | // parameter of a function declaration or lambda. |
| 1902 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1903 | S.Diag(Attr.getLoc(), |
| 1904 | diag::err_carries_dependency_param_not_function_decl); |
| 1905 | return; |
| 1906 | } |
| 1907 | } else if (!isa<FunctionDecl>(D)) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1908 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1909 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1910 | return; |
| 1911 | } |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1912 | |
| 1913 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1914 | Attr.getRange(), S.Context, |
| 1915 | Attr.getAttributeSpellingListIndex())); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1918 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1919 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1920 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1921 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1922 | return; |
| 1923 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1924 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1925 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 568eae4 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1926 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1927 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1928 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1929 | return; |
| 1930 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1931 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1932 | D->addAttr(::new (S.Context) |
| 1933 | UnusedAttr(Attr.getRange(), S.Context, |
| 1934 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1937 | static void handleReturnsTwiceAttr(Sema &S, Decl *D, |
| 1938 | const AttributeList &Attr) { |
| 1939 | // check the attribute arguments. |
| 1940 | if (Attr.hasParameterOrArguments()) { |
| 1941 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1942 | return; |
| 1943 | } |
| 1944 | |
| 1945 | if (!isa<FunctionDecl>(D)) { |
| 1946 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1947 | << Attr.getName() << ExpectedFunction; |
| 1948 | return; |
| 1949 | } |
| 1950 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1951 | D->addAttr(::new (S.Context) |
| 1952 | ReturnsTwiceAttr(Attr.getRange(), S.Context, |
| 1953 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1956 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1957 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1958 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1959 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1960 | return; |
| 1961 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1962 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1963 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Daniel Dunbar | 186204b | 2009-02-13 22:48:56 +0000 | [diff] [blame] | 1964 | if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1965 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 1966 | return; |
| 1967 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1968 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1969 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1970 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1971 | return; |
| 1972 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1973 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1974 | D->addAttr(::new (S.Context) |
| 1975 | UsedAttr(Attr.getRange(), S.Context, |
| 1976 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1979 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1980 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1981 | if (Attr.getNumArgs() > 1) { |
| 1982 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1983 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1984 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1985 | |
| 1986 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1987 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1988 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1989 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1990 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1991 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1992 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1993 | << "constructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1994 | return; |
| 1995 | } |
| 1996 | priority = Idx.getZExtValue(); |
| 1997 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1998 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1999 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2000 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2001 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2002 | return; |
| 2003 | } |
| 2004 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2005 | D->addAttr(::new (S.Context) |
| 2006 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 2007 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2010 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2011 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2012 | if (Attr.getNumArgs() > 1) { |
| 2013 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2014 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2015 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2016 | |
| 2017 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 2018 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2019 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2020 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2021 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2022 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2023 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2024 | << "destructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2025 | return; |
| 2026 | } |
| 2027 | priority = Idx.getZExtValue(); |
| 2028 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2029 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2030 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2031 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2032 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2033 | return; |
| 2034 | } |
| 2035 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2036 | D->addAttr(::new (S.Context) |
| 2037 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 2038 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2041 | template <typename AttrTy> |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 2042 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 2043 | const AttributeList &Attr) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2044 | unsigned NumArgs = Attr.getNumArgs(); |
| 2045 | if (NumArgs > 1) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2046 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2047 | return; |
| 2048 | } |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2049 | |
| 2050 | // Handle the case where the attribute has a text message. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2051 | StringRef Str; |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2052 | if (NumArgs == 1) { |
| 2053 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 2054 | if (!SE) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2055 | S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string) |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 2056 | << Attr.getName(); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 2057 | return; |
| 2058 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2059 | Str = SE->getString(); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 2060 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2061 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2062 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 2063 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2066 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 2067 | const AttributeList &Attr) { |
| 2068 | unsigned NumArgs = Attr.getNumArgs(); |
| 2069 | if (NumArgs > 0) { |
| 2070 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 2071 | return; |
| 2072 | } |
| 2073 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2074 | D->addAttr(::new (S.Context) |
| 2075 | ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context, |
| 2076 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2079 | static void handleObjCRootClassAttr(Sema &S, Decl *D, |
| 2080 | const AttributeList &Attr) { |
| 2081 | if (!isa<ObjCInterfaceDecl>(D)) { |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame^] | 2082 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2083 | << Attr.getName() << ExpectedObjectiveCInterface; |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2084 | return; |
| 2085 | } |
| 2086 | |
| 2087 | unsigned NumArgs = Attr.getNumArgs(); |
| 2088 | if (NumArgs > 0) { |
| 2089 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 2090 | return; |
| 2091 | } |
| 2092 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2093 | D->addAttr(::new (S.Context) |
| 2094 | ObjCRootClassAttr(Attr.getRange(), S.Context, |
| 2095 | Attr.getAttributeSpellingListIndex())); |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2098 | static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, |
| 2099 | const AttributeList &Attr) { |
Fariborz Jahanian | 341b8be | 2012-01-03 22:52:32 +0000 | [diff] [blame] | 2100 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 2101 | S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis); |
| 2102 | return; |
| 2103 | } |
| 2104 | |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2105 | unsigned NumArgs = Attr.getNumArgs(); |
| 2106 | if (NumArgs > 0) { |
| 2107 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 2108 | return; |
| 2109 | } |
| 2110 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2111 | D->addAttr(::new (S.Context) |
| 2112 | ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context, |
| 2113 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2116 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2117 | IdentifierInfo *Platform, |
| 2118 | VersionTuple Introduced, |
| 2119 | VersionTuple Deprecated, |
| 2120 | VersionTuple Obsoleted) { |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2121 | StringRef PlatformName |
| 2122 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2123 | if (PlatformName.empty()) |
| 2124 | PlatformName = Platform->getName(); |
| 2125 | |
| 2126 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2127 | // of these steps are needed). |
| 2128 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2129 | !(Introduced <= Deprecated)) { |
| 2130 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2131 | << 1 << PlatformName << Deprecated.getAsString() |
| 2132 | << 0 << Introduced.getAsString(); |
| 2133 | return true; |
| 2134 | } |
| 2135 | |
| 2136 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2137 | !(Introduced <= Obsoleted)) { |
| 2138 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2139 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2140 | << 0 << Introduced.getAsString(); |
| 2141 | return true; |
| 2142 | } |
| 2143 | |
| 2144 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2145 | !(Deprecated <= Obsoleted)) { |
| 2146 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2147 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2148 | << 1 << Deprecated.getAsString(); |
| 2149 | return true; |
| 2150 | } |
| 2151 | |
| 2152 | return false; |
| 2153 | } |
| 2154 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2155 | /// \brief Check whether the two versions match. |
| 2156 | /// |
| 2157 | /// If either version tuple is empty, then they are assumed to match. If |
| 2158 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2159 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2160 | bool BeforeIsOkay) { |
| 2161 | if (X.empty() || Y.empty()) |
| 2162 | return true; |
| 2163 | |
| 2164 | if (X == Y) |
| 2165 | return true; |
| 2166 | |
| 2167 | if (BeforeIsOkay && X < Y) |
| 2168 | return true; |
| 2169 | |
| 2170 | return false; |
| 2171 | } |
| 2172 | |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2173 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2174 | IdentifierInfo *Platform, |
| 2175 | VersionTuple Introduced, |
| 2176 | VersionTuple Deprecated, |
| 2177 | VersionTuple Obsoleted, |
| 2178 | bool IsUnavailable, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2179 | StringRef Message, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2180 | bool Override, |
| 2181 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2182 | VersionTuple MergedIntroduced = Introduced; |
| 2183 | VersionTuple MergedDeprecated = Deprecated; |
| 2184 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2185 | bool FoundAny = false; |
| 2186 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2187 | if (D->hasAttrs()) { |
| 2188 | AttrVec &Attrs = D->getAttrs(); |
| 2189 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 2190 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 2191 | if (!OldAA) { |
| 2192 | ++i; |
| 2193 | continue; |
| 2194 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2195 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2196 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2197 | if (OldPlatform != Platform) { |
| 2198 | ++i; |
| 2199 | continue; |
| 2200 | } |
| 2201 | |
| 2202 | FoundAny = true; |
| 2203 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2204 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2205 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2206 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2207 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2208 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 2209 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 2210 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 2211 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 72daa3f | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 2212 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2213 | if (Override) { |
| 2214 | int Which = -1; |
| 2215 | VersionTuple FirstVersion; |
| 2216 | VersionTuple SecondVersion; |
| 2217 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 2218 | Which = 0; |
| 2219 | FirstVersion = OldIntroduced; |
| 2220 | SecondVersion = Introduced; |
| 2221 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 2222 | Which = 1; |
| 2223 | FirstVersion = Deprecated; |
| 2224 | SecondVersion = OldDeprecated; |
| 2225 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 2226 | Which = 2; |
| 2227 | FirstVersion = Obsoleted; |
| 2228 | SecondVersion = OldObsoleted; |
| 2229 | } |
| 2230 | |
| 2231 | if (Which == -1) { |
| 2232 | Diag(OldAA->getLocation(), |
| 2233 | diag::warn_mismatched_availability_override_unavail) |
| 2234 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2235 | } else { |
| 2236 | Diag(OldAA->getLocation(), |
| 2237 | diag::warn_mismatched_availability_override) |
| 2238 | << Which |
| 2239 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2240 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 2241 | } |
| 2242 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2243 | } else { |
| 2244 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2245 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2246 | } |
| 2247 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2248 | Attrs.erase(Attrs.begin() + i); |
| 2249 | --e; |
| 2250 | continue; |
| 2251 | } |
| 2252 | |
| 2253 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2254 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2255 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2256 | |
| 2257 | if (MergedIntroduced2.empty()) |
| 2258 | MergedIntroduced2 = OldIntroduced; |
| 2259 | if (MergedDeprecated2.empty()) |
| 2260 | MergedDeprecated2 = OldDeprecated; |
| 2261 | if (MergedObsoleted2.empty()) |
| 2262 | MergedObsoleted2 = OldObsoleted; |
| 2263 | |
| 2264 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2265 | MergedIntroduced2, MergedDeprecated2, |
| 2266 | MergedObsoleted2)) { |
| 2267 | Attrs.erase(Attrs.begin() + i); |
| 2268 | --e; |
| 2269 | continue; |
| 2270 | } |
| 2271 | |
| 2272 | MergedIntroduced = MergedIntroduced2; |
| 2273 | MergedDeprecated = MergedDeprecated2; |
| 2274 | MergedObsoleted = MergedObsoleted2; |
| 2275 | ++i; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2276 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | if (FoundAny && |
| 2280 | MergedIntroduced == Introduced && |
| 2281 | MergedDeprecated == Deprecated && |
| 2282 | MergedObsoleted == Obsoleted) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2283 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2284 | |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2285 | // Only create a new attribute if !Override, but we want to do |
| 2286 | // the checking. |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2287 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2288 | MergedDeprecated, MergedObsoleted) && |
| 2289 | !Override) { |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2290 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 2291 | Introduced, Deprecated, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2292 | Obsoleted, IsUnavailable, Message, |
| 2293 | AttrSpellingListIndex); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2294 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2295 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2296 | } |
| 2297 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2298 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2299 | const AttributeList &Attr) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2300 | IdentifierInfo *Platform = Attr.getParameterName(); |
| 2301 | SourceLocation PlatformLoc = Attr.getParameterLoc(); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2302 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2303 | |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2304 | if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2305 | S.Diag(PlatformLoc, diag::warn_availability_unknown_platform) |
| 2306 | << Platform; |
| 2307 | |
Rafael Espindola | 8c4222a | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2308 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 2309 | if (!ND) { |
| 2310 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2311 | return; |
| 2312 | } |
| 2313 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2314 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2315 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2316 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2317 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2318 | StringRef Str; |
| 2319 | const StringLiteral *SE = |
| 2320 | dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr()); |
| 2321 | if (SE) |
| 2322 | Str = SE->getString(); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2323 | |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2324 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2325 | Platform, |
| 2326 | Introduced.Version, |
| 2327 | Deprecated.Version, |
| 2328 | Obsoleted.Version, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2329 | IsUnavailable, Str, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2330 | /*Override=*/false, |
| 2331 | Index); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2332 | if (NewAttr) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2333 | D->addAttr(NewAttr); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2336 | template <class T> |
| 2337 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2338 | typename T::VisibilityType value, |
| 2339 | unsigned attrSpellingListIndex) { |
| 2340 | T *existingAttr = D->getAttr<T>(); |
| 2341 | if (existingAttr) { |
| 2342 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2343 | if (existingValue == value) |
| 2344 | return NULL; |
| 2345 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2346 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2347 | D->dropAttr<T>(); |
| 2348 | } |
| 2349 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2350 | } |
| 2351 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2352 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2353 | VisibilityAttr::VisibilityType Vis, |
| 2354 | unsigned AttrSpellingListIndex) { |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2355 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2356 | AttrSpellingListIndex); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2359 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2360 | TypeVisibilityAttr::VisibilityType Vis, |
| 2361 | unsigned AttrSpellingListIndex) { |
| 2362 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2363 | AttrSpellingListIndex); |
| 2364 | } |
| 2365 | |
| 2366 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2367 | bool isTypeVisibility) { |
| 2368 | // Visibility attributes don't mean anything on a typedef. |
| 2369 | if (isa<TypedefNameDecl>(D)) { |
| 2370 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2371 | << Attr.getName(); |
| 2372 | return; |
| 2373 | } |
| 2374 | |
| 2375 | // 'type_visibility' can only go on a type or namespace. |
| 2376 | if (isTypeVisibility && |
| 2377 | !(isa<TagDecl>(D) || |
| 2378 | isa<ObjCInterfaceDecl>(D) || |
| 2379 | isa<NamespaceDecl>(D))) { |
| 2380 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2381 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2382 | return; |
| 2383 | } |
| 2384 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2385 | // check the attribute arguments. |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2386 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2387 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2388 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2389 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2390 | Arg = Arg->IgnoreParenCasts(); |
| 2391 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2392 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2393 | if (!Str || !Str->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2394 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2395 | << (isTypeVisibility ? "type_visibility" : "visibility") << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2396 | return; |
| 2397 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2398 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2399 | StringRef TypeStr = Str->getString(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2400 | VisibilityAttr::VisibilityType type; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2401 | |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2402 | if (TypeStr == "default") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2403 | type = VisibilityAttr::Default; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2404 | else if (TypeStr == "hidden") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2405 | type = VisibilityAttr::Hidden; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2406 | else if (TypeStr == "internal") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2407 | type = VisibilityAttr::Hidden; // FIXME |
John McCall | 4188760 | 2012-01-29 01:20:30 +0000 | [diff] [blame] | 2408 | else if (TypeStr == "protected") { |
| 2409 | // Complain about attempts to use protected visibility on targets |
| 2410 | // (like Darwin) that don't support it. |
| 2411 | if (!S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2412 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2413 | type = VisibilityAttr::Default; |
| 2414 | } else { |
| 2415 | type = VisibilityAttr::Protected; |
| 2416 | } |
| 2417 | } else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2418 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2419 | return; |
| 2420 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2421 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2422 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2423 | clang::Attr *newAttr; |
| 2424 | if (isTypeVisibility) { |
| 2425 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2426 | (TypeVisibilityAttr::VisibilityType) type, |
| 2427 | Index); |
| 2428 | } else { |
| 2429 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2430 | } |
| 2431 | if (newAttr) |
| 2432 | D->addAttr(newAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2435 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2436 | const AttributeList &Attr) { |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2437 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 2438 | if (!method) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2439 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2440 | << ExpectedMethod; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2441 | return; |
| 2442 | } |
| 2443 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2444 | if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) { |
| 2445 | if (!Attr.getParameterName() && Attr.getNumArgs() == 1) { |
| 2446 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2447 | << "objc_method_family" << 1; |
| 2448 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2449 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2450 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2451 | Attr.setInvalid(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2452 | return; |
| 2453 | } |
| 2454 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2455 | StringRef param = Attr.getParameterName()->getName(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2456 | ObjCMethodFamilyAttr::FamilyKind family; |
| 2457 | if (param == "none") |
| 2458 | family = ObjCMethodFamilyAttr::OMF_None; |
| 2459 | else if (param == "alloc") |
| 2460 | family = ObjCMethodFamilyAttr::OMF_alloc; |
| 2461 | else if (param == "copy") |
| 2462 | family = ObjCMethodFamilyAttr::OMF_copy; |
| 2463 | else if (param == "init") |
| 2464 | family = ObjCMethodFamilyAttr::OMF_init; |
| 2465 | else if (param == "mutableCopy") |
| 2466 | family = ObjCMethodFamilyAttr::OMF_mutableCopy; |
| 2467 | else if (param == "new") |
| 2468 | family = ObjCMethodFamilyAttr::OMF_new; |
| 2469 | else { |
| 2470 | // Just warn and ignore it. This is future-proof against new |
| 2471 | // families being used in system headers. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2472 | S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2473 | return; |
| 2474 | } |
| 2475 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2476 | if (family == ObjCMethodFamilyAttr::OMF_init && |
| 2477 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2478 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2479 | << method->getResultType(); |
| 2480 | // Ignore the attribute. |
| 2481 | return; |
| 2482 | } |
| 2483 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2484 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2485 | S.Context, family)); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2488 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 2489 | const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2490 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2491 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2492 | |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2493 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 2494 | if (OCI == 0) { |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame^] | 2495 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2496 | << Attr.getName() << ExpectedObjectiveCInterface; |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2497 | return; |
| 2498 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2499 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2500 | D->addAttr(::new (S.Context) |
| 2501 | ObjCExceptionAttr(Attr.getRange(), S.Context, |
| 2502 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2505 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2506 | if (Attr.getNumArgs() != 0) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 2507 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2508 | return; |
| 2509 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2510 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2511 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2512 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2513 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2514 | return; |
| 2515 | } |
| 2516 | } |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2517 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2518 | QualType T = PD->getType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2519 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2520 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2521 | return; |
| 2522 | } |
| 2523 | } |
| 2524 | else { |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2525 | // It is okay to include this attribute on properties, e.g.: |
| 2526 | // |
| 2527 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2528 | // |
| 2529 | // In this case it follows tradition and suppresses an error in the above |
| 2530 | // case. |
Fariborz Jahanian | 9b2eb7b | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2531 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2532 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2533 | D->addAttr(::new (S.Context) |
| 2534 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2535 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2536 | } |
| 2537 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2538 | static void |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2539 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2540 | if (Attr.getNumArgs() != 0) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 2541 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2542 | return; |
| 2543 | } |
| 2544 | |
| 2545 | if (!isa<FunctionDecl>(D)) { |
| 2546 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 2547 | return; |
| 2548 | } |
| 2549 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2550 | D->addAttr(::new (S.Context) |
| 2551 | OverloadableAttr(Attr.getRange(), S.Context, |
| 2552 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2555 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2556 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2557 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2558 | << "blocks" << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2559 | return; |
| 2560 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2561 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2562 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2563 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2564 | return; |
| 2565 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2566 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2567 | BlocksAttr::BlockType type; |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 2568 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2569 | type = BlocksAttr::ByRef; |
| 2570 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2571 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2572 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2573 | return; |
| 2574 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2575 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2576 | D->addAttr(::new (S.Context) |
| 2577 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2578 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2581 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2582 | // check the attribute arguments. |
| 2583 | if (Attr.getNumArgs() > 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2584 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2585 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2586 | } |
| 2587 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2588 | unsigned sentinel = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2589 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2590 | Expr *E = Attr.getArg(0); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2591 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2592 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2593 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2594 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2595 | << "sentinel" << 1 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2596 | return; |
| 2597 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2598 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2599 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2600 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2601 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2602 | return; |
| 2603 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2604 | |
| 2605 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2606 | } |
| 2607 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2608 | unsigned nullPos = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2609 | if (Attr.getNumArgs() > 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2610 | Expr *E = Attr.getArg(1); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2611 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2612 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2613 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2614 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2615 | << "sentinel" << 2 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2616 | return; |
| 2617 | } |
| 2618 | nullPos = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2619 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2620 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2621 | // FIXME: This error message could be improved, it would be nice |
| 2622 | // to say what the bounds actually are. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2623 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2624 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2625 | return; |
| 2626 | } |
| 2627 | } |
| 2628 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2629 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2630 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2631 | if (isa<FunctionNoProtoType>(FT)) { |
| 2632 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2633 | return; |
| 2634 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2635 | |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2636 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2637 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2638 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2639 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2640 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2641 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2642 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2643 | return; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2644 | } |
Eli Friedman | a0b2ba1 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2645 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2646 | if (!BD->isVariadic()) { |
| 2647 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2648 | return; |
| 2649 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2650 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2651 | QualType Ty = V->getType(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2652 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2653 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2654 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2655 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2656 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2657 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2658 | return; |
| 2659 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2660 | } else { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2661 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2662 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2663 | return; |
| 2664 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2665 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2666 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2667 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2668 | return; |
| 2669 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2670 | D->addAttr(::new (S.Context) |
| 2671 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2672 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2675 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2676 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2677 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2678 | return; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2679 | |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 2680 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2681 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Kaelyn Uhrain | d449c79 | 2012-11-13 00:18:47 +0000 | [diff] [blame] | 2682 | << Attr.getName() << ExpectedFunctionMethodOrClass; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2683 | return; |
| 2684 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2685 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2686 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2687 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2688 | << Attr.getName() << 0; |
Nuno Lopes | f857798 | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2689 | return; |
| 2690 | } |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2691 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2692 | if (MD->getResultType()->isVoidType()) { |
| 2693 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2694 | << Attr.getName() << 1; |
| 2695 | return; |
| 2696 | } |
| 2697 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2698 | D->addAttr(::new (S.Context) |
| 2699 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2700 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2703 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2704 | // check the attribute arguments. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2705 | if (Attr.hasParameterOrArguments()) { |
| 2706 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2707 | return; |
| 2708 | } |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2709 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2710 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
Fariborz Jahanian | 13c7fcc | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 2711 | if (isa<CXXRecordDecl>(D)) { |
| 2712 | D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| 2713 | return; |
| 2714 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2715 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2716 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | f23ecd9 | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 2717 | return; |
| 2718 | } |
| 2719 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2720 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 2721 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2722 | nd->addAttr(::new (S.Context) |
| 2723 | WeakAttr(Attr.getRange(), S.Context, |
| 2724 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2725 | } |
| 2726 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2727 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2728 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2729 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2730 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2731 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2732 | |
| 2733 | // weak_import only applies to variable & function declarations. |
| 2734 | bool isDef = false; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2735 | if (!D->canBeWeakImported(isDef)) { |
| 2736 | if (isDef) |
Reid Kleckner | beba3e8 | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2737 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2738 | << "weak_import"; |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2739 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2740 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 90eed21 | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2741 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2742 | // Nothing to warn about here. |
| 2743 | } else |
Fariborz Jahanian | c034974 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2744 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2745 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2746 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2747 | return; |
| 2748 | } |
| 2749 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2750 | D->addAttr(::new (S.Context) |
| 2751 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2752 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2755 | // Handles reqd_work_group_size and work_group_size_hint. |
| 2756 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2757 | const AttributeList &Attr) { |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2758 | assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2759 | || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint); |
| 2760 | |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2761 | // Attribute has 3 arguments. |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2762 | if (!checkAttributeNumArgs(S, Attr, 3)) return; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2763 | |
| 2764 | unsigned WGSize[3]; |
| 2765 | for (unsigned i = 0; i < 3; ++i) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2766 | Expr *E = Attr.getArg(i); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2767 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2768 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2769 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2770 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2771 | << Attr.getName()->getName() << E->getSourceRange(); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2772 | return; |
| 2773 | } |
| 2774 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 2775 | } |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2776 | |
| 2777 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2778 | && D->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 2779 | ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>(); |
| 2780 | if (!(A->getXDim() == WGSize[0] && |
| 2781 | A->getYDim() == WGSize[1] && |
| 2782 | A->getZDim() == WGSize[2])) { |
| 2783 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2784 | Attr.getName(); |
| 2785 | } |
| 2786 | } |
| 2787 | |
| 2788 | if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint |
| 2789 | && D->hasAttr<WorkGroupSizeHintAttr>()) { |
| 2790 | WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>(); |
| 2791 | if (!(A->getXDim() == WGSize[0] && |
| 2792 | A->getYDim() == WGSize[1] && |
| 2793 | A->getZDim() == WGSize[2])) { |
| 2794 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2795 | Attr.getName(); |
| 2796 | } |
| 2797 | } |
| 2798 | |
| 2799 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize) |
| 2800 | D->addAttr(::new (S.Context) |
| 2801 | ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2802 | WGSize[0], WGSize[1], WGSize[2], |
| 2803 | Attr.getAttributeSpellingListIndex())); |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2804 | else |
| 2805 | D->addAttr(::new (S.Context) |
| 2806 | WorkGroupSizeHintAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2807 | WGSize[0], WGSize[1], WGSize[2], |
| 2808 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2811 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2812 | assert(Attr.getKind() == AttributeList::AT_VecTypeHint); |
| 2813 | |
| 2814 | // Attribute has 1 argument. |
| 2815 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 2816 | return; |
| 2817 | |
| 2818 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg()); |
| 2819 | |
| 2820 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2821 | (ParmType->isBooleanType() || |
| 2822 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2823 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2824 | << ParmType; |
| 2825 | return; |
| 2826 | } |
| 2827 | |
| 2828 | if (Attr.getKind() == AttributeList::AT_VecTypeHint && |
| 2829 | D->hasAttr<VecTypeHintAttr>()) { |
| 2830 | VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>(); |
| 2831 | if (A->getTypeHint() != ParmType) { |
| 2832 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2833 | return; |
| 2834 | } |
| 2835 | } |
| 2836 | |
| 2837 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
| 2838 | ParmType, Attr.getLoc())); |
| 2839 | } |
| 2840 | |
Joey Gouly | 96cead5 | 2013-03-14 09:54:43 +0000 | [diff] [blame] | 2841 | static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2842 | if (!dyn_cast<VarDecl>(D)) |
| 2843 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian" |
| 2844 | << 9; |
| 2845 | StringRef EndianType = Attr.getParameterName()->getName(); |
| 2846 | if (EndianType != "host" && EndianType != "device") |
| 2847 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType; |
| 2848 | } |
| 2849 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2850 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2851 | StringRef Name, |
| 2852 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2853 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2854 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2855 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2856 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2857 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2858 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2859 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2860 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2861 | AttrSpellingListIndex); |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2864 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2865 | // Attribute has no arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2866 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2867 | return; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2868 | |
| 2869 | // Make sure that there is a string literal as the sections's single |
| 2870 | // argument. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2871 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2872 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2873 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2874 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section"; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2875 | return; |
| 2876 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2877 | |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2878 | // If the target wants to validate the section specifier, make it happen. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2879 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString()); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2880 | if (!Error.empty()) { |
| 2881 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 2882 | << Error; |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2883 | return; |
| 2884 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2885 | |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2886 | // This attribute cannot be applied to local variables. |
| 2887 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 2888 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 2889 | return; |
| 2890 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2891 | |
| 2892 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2893 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2894 | SE->getString(), Index); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2895 | if (NewAttr) |
| 2896 | D->addAttr(NewAttr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2899 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2900 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2901 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2902 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2903 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2904 | return; |
| 2905 | } |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2906 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2907 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2908 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2909 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2910 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2911 | D->addAttr(::new (S.Context) |
| 2912 | NoThrowAttr(Attr.getRange(), S.Context, |
| 2913 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2914 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2915 | } |
| 2916 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2917 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2918 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2919 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2920 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2921 | return; |
| 2922 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2923 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2924 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2925 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2926 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2927 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2928 | D->addAttr(::new (S.Context) |
| 2929 | ConstAttr(Attr.getRange(), S.Context, |
| 2930 | Attr.getAttributeSpellingListIndex() )); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2931 | } |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2934 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2935 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2936 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2937 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2938 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2939 | D->addAttr(::new (S.Context) |
| 2940 | PureAttr(Attr.getRange(), S.Context, |
| 2941 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2942 | } |
| 2943 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2944 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2945 | if (!Attr.getParameterName()) { |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2946 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2947 | return; |
| 2948 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2949 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2950 | if (Attr.getNumArgs() != 0) { |
| 2951 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2952 | return; |
| 2953 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2954 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2955 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2956 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2957 | if (!VD || !VD->hasLocalStorage()) { |
| 2958 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 2959 | return; |
| 2960 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2961 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2962 | // Look up the function |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2963 | // FIXME: Lookup probably isn't looking in the right place |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2964 | NamedDecl *CleanupDecl |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2965 | = S.LookupSingleName(S.TUScope, Attr.getParameterName(), |
| 2966 | Attr.getParameterLoc(), Sema::LookupOrdinaryName); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2967 | if (!CleanupDecl) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2968 | S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2969 | Attr.getParameterName(); |
| 2970 | return; |
| 2971 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2972 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2973 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 2974 | if (!FD) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2975 | S.Diag(Attr.getParameterLoc(), |
| 2976 | diag::err_attribute_cleanup_arg_not_function) |
| 2977 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2978 | return; |
| 2979 | } |
| 2980 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2981 | if (FD->getNumParams() != 1) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2982 | S.Diag(Attr.getParameterLoc(), |
| 2983 | diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2984 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2985 | return; |
| 2986 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2987 | |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2988 | // We're currently more strict than GCC about what function types we accept. |
| 2989 | // If this ever proves to be a problem it should be easy to fix. |
| 2990 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2991 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2992 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2993 | ParamTy, Ty) != Sema::Compatible) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2994 | S.Diag(Attr.getParameterLoc(), |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2995 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 2996 | Attr.getParameterName() << ParamTy << Ty; |
| 2997 | return; |
| 2998 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2999 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3000 | D->addAttr(::new (S.Context) |
| 3001 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 3002 | Attr.getAttributeSpellingListIndex())); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3003 | S.MarkFunctionReferenced(Attr.getParameterLoc(), FD); |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 3004 | S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc()); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3005 | } |
| 3006 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3007 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3008 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3009 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3010 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3011 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3012 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3013 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3014 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3015 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3016 | return; |
| 3017 | } |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3018 | |
| 3019 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3020 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3021 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 3022 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3023 | unsigned FirstIdx = 1; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3024 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3025 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3026 | Expr *IdxExpr = Attr.getArg(0); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3027 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3028 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 3029 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3030 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 3031 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 3032 | return; |
| 3033 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3034 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3035 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
| 3036 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 3037 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 3038 | return; |
| 3039 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3040 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3041 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3042 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3043 | if (HasImplicitThisParam) { |
| 3044 | if (ArgIdx == 0) { |
| 3045 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 3046 | << "format_arg" << IdxExpr->getSourceRange(); |
| 3047 | return; |
| 3048 | } |
| 3049 | ArgIdx--; |
| 3050 | } |
| 3051 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3052 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3053 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3054 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3055 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 3056 | if (not_nsstring_type && |
| 3057 | !isCFStringType(Ty, S.Context) && |
| 3058 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3059 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3060 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 3061 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3062 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3063 | << IdxExpr->getSourceRange(); |
| 3064 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3065 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3066 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3067 | if (!isNSStringType(Ty, S.Context) && |
| 3068 | !isCFStringType(Ty, S.Context) && |
| 3069 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3070 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3071 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 3072 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3073 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3074 | << IdxExpr->getSourceRange(); |
| 3075 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3076 | } |
| 3077 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3078 | D->addAttr(::new (S.Context) |
| 3079 | FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(), |
| 3080 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3083 | enum FormatAttrKind { |
| 3084 | CFStringFormat, |
| 3085 | NSStringFormat, |
| 3086 | StrftimeFormat, |
| 3087 | SupportedFormat, |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3088 | IgnoredFormat, |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3089 | InvalidFormat |
| 3090 | }; |
| 3091 | |
| 3092 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 3093 | /// types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3094 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3095 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 3096 | // Check for formats that get handled specially. |
| 3097 | .Case("NSString", NSStringFormat) |
| 3098 | .Case("CFString", CFStringFormat) |
| 3099 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3100 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3101 | // Otherwise, check for supported formats. |
| 3102 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 3103 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 3104 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3105 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3106 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 3107 | .Default(InvalidFormat); |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3108 | } |
| 3109 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3110 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3111 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3112 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 3113 | const AttributeList &Attr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3114 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3115 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3116 | return; |
| 3117 | } |
| 3118 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3119 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3120 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3121 | Attr.setInvalid(); |
| 3122 | return; |
| 3123 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3124 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3125 | if (S.Context.getAsArrayType(T)) |
| 3126 | T = S.Context.getBaseElementType(T); |
| 3127 | if (!T->getAs<RecordType>()) { |
| 3128 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3129 | Attr.setInvalid(); |
| 3130 | return; |
| 3131 | } |
| 3132 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3133 | if (Attr.getNumArgs() != 1) { |
| 3134 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 3135 | Attr.setInvalid(); |
| 3136 | return; |
| 3137 | } |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3138 | Expr *priorityExpr = Attr.getArg(0); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3139 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3140 | llvm::APSInt priority(32); |
| 3141 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 3142 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
| 3143 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 3144 | << "init_priority" << priorityExpr->getSourceRange(); |
| 3145 | Attr.setInvalid(); |
| 3146 | return; |
| 3147 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 3148 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3149 | if (prioritynum < 101 || prioritynum > 65535) { |
| 3150 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 3151 | << priorityExpr->getSourceRange(); |
| 3152 | Attr.setInvalid(); |
| 3153 | return; |
| 3154 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3155 | D->addAttr(::new (S.Context) |
| 3156 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 3157 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3160 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3161 | int FormatIdx, int FirstArg, |
| 3162 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3163 | // Check whether we already have an equivalent format attribute. |
| 3164 | for (specific_attr_iterator<FormatAttr> |
| 3165 | i = D->specific_attr_begin<FormatAttr>(), |
| 3166 | e = D->specific_attr_end<FormatAttr>(); |
| 3167 | i != e ; ++i) { |
| 3168 | FormatAttr *f = *i; |
| 3169 | if (f->getType() == Format && |
| 3170 | f->getFormatIdx() == FormatIdx && |
| 3171 | f->getFirstArg() == FirstArg) { |
| 3172 | // If we don't have a valid location for this attribute, adopt the |
| 3173 | // location. |
| 3174 | if (f->getLocation().isInvalid()) |
| 3175 | f->setRange(Range); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3176 | return NULL; |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3177 | } |
| 3178 | } |
| 3179 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3180 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg, |
| 3181 | AttrSpellingListIndex); |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3182 | } |
| 3183 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3184 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3185 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3186 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3187 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3188 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3189 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3190 | << "format" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3191 | return; |
| 3192 | } |
| 3193 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3194 | if (Attr.getNumArgs() != 2) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3195 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3196 | return; |
| 3197 | } |
| 3198 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3199 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3200 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3201 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3202 | return; |
| 3203 | } |
| 3204 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3205 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3206 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3207 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 3208 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3209 | unsigned FirstIdx = 1; |
| 3210 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3211 | StringRef Format = Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3212 | |
| 3213 | // Normalize the argument, __foo__ becomes foo. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3214 | if (Format.startswith("__") && Format.endswith("__")) |
| 3215 | Format = Format.substr(2, Format.size() - 4); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3216 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3217 | // Check for supported formats. |
| 3218 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3219 | |
| 3220 | if (Kind == IgnoredFormat) |
| 3221 | return; |
| 3222 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3223 | if (Kind == InvalidFormat) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3224 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3225 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3226 | return; |
| 3227 | } |
| 3228 | |
| 3229 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3230 | Expr *IdxExpr = Attr.getArg(0); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3231 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3232 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 3233 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3234 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3235 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3236 | return; |
| 3237 | } |
| 3238 | |
| 3239 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3240 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3241 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3242 | return; |
| 3243 | } |
| 3244 | |
| 3245 | // FIXME: Do we need to bounds check? |
| 3246 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3247 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3248 | if (HasImplicitThisParam) { |
| 3249 | if (ArgIdx == 0) { |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3250 | S.Diag(Attr.getLoc(), |
| 3251 | diag::err_format_attribute_implicit_this_format_string) |
| 3252 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3253 | return; |
| 3254 | } |
| 3255 | ArgIdx--; |
| 3256 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3257 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3258 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3259 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3260 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3261 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3262 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3263 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3264 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3265 | return; |
| 3266 | } |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3267 | } else if (Kind == NSStringFormat) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3268 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3269 | // semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3270 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3271 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3272 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3273 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3274 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3275 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3276 | } else if (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3277 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3278 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3279 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3280 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3281 | return; |
| 3282 | } |
| 3283 | |
| 3284 | // check the 3rd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3285 | Expr *FirstArgExpr = Attr.getArg(1); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3286 | llvm::APSInt FirstArg(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3287 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 3288 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3289 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3290 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3291 | return; |
| 3292 | } |
| 3293 | |
| 3294 | // check if the function is variadic if the 3rd argument non-zero |
| 3295 | if (FirstArg != 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3296 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3297 | ++NumArgs; // +1 for ... |
| 3298 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3299 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3300 | return; |
| 3301 | } |
| 3302 | } |
| 3303 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3304 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3305 | // variable the input is just the current time + the format string. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3306 | if (Kind == StrftimeFormat) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3307 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3308 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 3309 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3310 | return; |
| 3311 | } |
| 3312 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3313 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3314 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3315 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3316 | return; |
| 3317 | } |
| 3318 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3319 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format, |
| 3320 | Idx.getZExtValue(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3321 | FirstArg.getZExtValue(), |
| 3322 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3323 | if (NewAttr) |
| 3324 | D->addAttr(NewAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3325 | } |
| 3326 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3327 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 3328 | const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3329 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3330 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3331 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3332 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3333 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3334 | // Try to find the underlying union declaration. |
| 3335 | RecordDecl *RD = 0; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3336 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3337 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3338 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3339 | else |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3340 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3341 | |
| 3342 | if (!RD || !RD->isUnion()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3343 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3344 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3345 | return; |
| 3346 | } |
| 3347 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3348 | if (!RD->isCompleteDefinition()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3349 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3350 | diag::warn_transparent_union_attribute_not_definition); |
| 3351 | return; |
| 3352 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3353 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3354 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3355 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3356 | if (Field == FieldEnd) { |
| 3357 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3358 | return; |
| 3359 | } |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3360 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3361 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3362 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3363 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3364 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3365 | diag::warn_transparent_union_attribute_floating) |
| 3366 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3367 | return; |
| 3368 | } |
| 3369 | |
| 3370 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3371 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3372 | for (; Field != FieldEnd; ++Field) { |
| 3373 | QualType FieldType = Field->getType(); |
| 3374 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 3375 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 3376 | // Warn if we drop the attribute. |
| 3377 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3378 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3379 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3380 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3381 | diag::warn_transparent_union_attribute_field_size_align) |
| 3382 | << isSize << Field->getDeclName() << FieldBits; |
| 3383 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3384 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3385 | diag::note_transparent_union_first_field_size_align) |
| 3386 | << isSize << FirstBits; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3387 | return; |
| 3388 | } |
| 3389 | } |
| 3390 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3391 | RD->addAttr(::new (S.Context) |
| 3392 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 3393 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3394 | } |
| 3395 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3396 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3397 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3398 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3399 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3400 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3401 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3402 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3403 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3404 | // Make sure that there is a string literal as the annotation's single |
| 3405 | // argument. |
| 3406 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3407 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate"; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3408 | return; |
| 3409 | } |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3410 | |
| 3411 | // Don't duplicate annotations that are already set. |
| 3412 | for (specific_attr_iterator<AnnotateAttr> |
| 3413 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 3414 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
| 3415 | if ((*i)->getAnnotation() == SE->getString()) |
| 3416 | return; |
| 3417 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3418 | |
| 3419 | D->addAttr(::new (S.Context) |
| 3420 | AnnotateAttr(Attr.getRange(), S.Context, SE->getString(), |
| 3421 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3422 | } |
| 3423 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3424 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3425 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3426 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3427 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3428 | return; |
| 3429 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3430 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3431 | if (Attr.getNumArgs() == 0) { |
| 3432 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 3433 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 3434 | return; |
| 3435 | } |
| 3436 | |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3437 | Expr *E = Attr.getArg(0); |
| 3438 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3439 | S.Diag(Attr.getEllipsisLoc(), |
| 3440 | diag::err_pack_expansion_without_parameter_packs); |
| 3441 | return; |
| 3442 | } |
| 3443 | |
| 3444 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 3445 | return; |
| 3446 | |
| 3447 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 3448 | Attr.isPackExpansion()); |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3449 | } |
| 3450 | |
| 3451 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3452 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3453 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3454 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3455 | |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3456 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3457 | if (TmpAttr.isAlignas()) { |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3458 | // C++11 [dcl.align]p1: |
| 3459 | // An alignment-specifier may be applied to a variable or to a class |
| 3460 | // data member, but it shall not be applied to a bit-field, a function |
| 3461 | // parameter, the formal parameter of a catch clause, or a variable |
| 3462 | // declared with the register storage class specifier. An |
| 3463 | // alignment-specifier may also be applied to the declaration of a class |
| 3464 | // or enumeration type. |
| 3465 | // C11 6.7.5/2: |
| 3466 | // An alignment attribute shall not be specified in a declaration of |
| 3467 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3468 | // object declared with the register storage-class specifier. |
| 3469 | int DiagKind = -1; |
| 3470 | if (isa<ParmVarDecl>(D)) { |
| 3471 | DiagKind = 0; |
| 3472 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3473 | if (VD->getStorageClass() == SC_Register) |
| 3474 | DiagKind = 1; |
| 3475 | if (VD->isExceptionVariable()) |
| 3476 | DiagKind = 2; |
| 3477 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 3478 | if (FD->isBitField()) |
| 3479 | DiagKind = 3; |
| 3480 | } else if (!isa<TagDecl>(D)) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3481 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) |
| 3482 | << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'") |
Richard Smith | 5f838aa | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3483 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3484 | : ExpectedVariableFieldOrTag); |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3485 | return; |
| 3486 | } |
| 3487 | if (DiagKind != -1) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3488 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 3489 | << TmpAttr.isC11() << DiagKind; |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3490 | return; |
| 3491 | } |
| 3492 | } |
| 3493 | |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3494 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3495 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3496 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3497 | AA->setPackExpansion(IsPackExpansion); |
| 3498 | D->addAttr(AA); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3499 | return; |
| 3500 | } |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3501 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3502 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3503 | llvm::APSInt Alignment(32); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3504 | ExprResult ICE |
| 3505 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3506 | diag::err_aligned_attribute_argument_not_int, |
| 3507 | /*AllowFold*/ false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3508 | if (ICE.isInvalid()) |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3509 | return; |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3510 | |
| 3511 | // C++11 [dcl.align]p2: |
| 3512 | // -- if the constant expression evaluates to zero, the alignment |
| 3513 | // specifier shall have no effect |
| 3514 | // C11 6.7.5p6: |
| 3515 | // An alignment specification of zero has no effect. |
| 3516 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 3517 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3518 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 3519 | << E->getSourceRange(); |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3520 | return; |
| 3521 | } |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3522 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3523 | if (TmpAttr.isDeclspec()) { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3524 | // We've already verified it's a power of 2, now let's make sure it's |
| 3525 | // 8192 or less. |
| 3526 | if (Alignment.getZExtValue() > 8192) { |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3527 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3528 | << E->getSourceRange(); |
| 3529 | return; |
| 3530 | } |
| 3531 | } |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3532 | |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3533 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 3534 | ICE.take(), SpellingListIndex); |
| 3535 | AA->setPackExpansion(IsPackExpansion); |
| 3536 | D->addAttr(AA); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3537 | } |
| 3538 | |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3539 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3540 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3541 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3542 | // FIXME: Perform checking of type validity |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3543 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3544 | SpellingListIndex); |
| 3545 | AA->setPackExpansion(IsPackExpansion); |
| 3546 | D->addAttr(AA); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3547 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3548 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3549 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3550 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3551 | |
| 3552 | QualType Ty; |
| 3553 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3554 | Ty = VD->getType(); |
| 3555 | else |
| 3556 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
Richard Smith | 4da0903 | 2013-02-22 09:21:42 +0000 | [diff] [blame] | 3557 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3558 | return; |
| 3559 | |
| 3560 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3561 | // The combined effect of all alignment attributes in a declaration shall |
| 3562 | // not specify an alignment that is less strict than the alignment that |
| 3563 | // would otherwise be required for the entity being declared. |
| 3564 | AlignedAttr *AlignasAttr = 0; |
| 3565 | unsigned Align = 0; |
| 3566 | for (specific_attr_iterator<AlignedAttr> |
| 3567 | I = D->specific_attr_begin<AlignedAttr>(), |
| 3568 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 3569 | if (I->isAlignmentDependent()) |
| 3570 | return; |
| 3571 | if (I->isAlignas()) |
| 3572 | AlignasAttr = *I; |
| 3573 | Align = std::max(Align, I->getAlignment(Context)); |
| 3574 | } |
| 3575 | |
| 3576 | if (AlignasAttr && Align) { |
| 3577 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 3578 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 3579 | if (NaturalAlign > RequestedAlign) |
| 3580 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 3581 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 3582 | } |
| 3583 | } |
| 3584 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3585 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3586 | /// type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3587 | /// |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3588 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3589 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3590 | /// HImode, not an intermediate pointer. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3591 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3592 | // This attribute isn't documented, but glibc uses it. It changes |
| 3593 | // the width of an int or unsigned int to the specified size. |
| 3594 | |
| 3595 | // Check that there aren't any arguments |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3596 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3597 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3598 | |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3599 | |
| 3600 | IdentifierInfo *Name = Attr.getParameterName(); |
| 3601 | if (!Name) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3602 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3603 | return; |
| 3604 | } |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3605 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3606 | StringRef Str = Attr.getParameterName()->getName(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3607 | |
| 3608 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3609 | if (Str.startswith("__") && Str.endswith("__")) |
| 3610 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3611 | |
| 3612 | unsigned DestWidth = 0; |
| 3613 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3614 | bool ComplexMode = false; |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3615 | switch (Str.size()) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3616 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3617 | switch (Str[0]) { |
| 3618 | case 'Q': DestWidth = 8; break; |
| 3619 | case 'H': DestWidth = 16; break; |
| 3620 | case 'S': DestWidth = 32; break; |
| 3621 | case 'D': DestWidth = 64; break; |
| 3622 | case 'X': DestWidth = 96; break; |
| 3623 | case 'T': DestWidth = 128; break; |
| 3624 | } |
| 3625 | if (Str[1] == 'F') { |
| 3626 | IntegerMode = false; |
| 3627 | } else if (Str[1] == 'C') { |
| 3628 | IntegerMode = false; |
| 3629 | ComplexMode = true; |
| 3630 | } else if (Str[1] != 'I') { |
| 3631 | DestWidth = 0; |
| 3632 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3633 | break; |
| 3634 | case 4: |
| 3635 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3636 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3637 | if (Str == "word") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3638 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3639 | else if (Str == "byte") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3640 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3641 | break; |
| 3642 | case 7: |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3643 | if (Str == "pointer") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3644 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3645 | break; |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3646 | case 11: |
| 3647 | if (Str == "unwind_word") |
Rafael Espindola | 0b1de54 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3648 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3649 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3650 | } |
| 3651 | |
| 3652 | QualType OldTy; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3653 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3654 | OldTy = TD->getUnderlyingType(); |
| 3655 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3656 | OldTy = VD->getType(); |
| 3657 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3658 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3659 | << "mode" << Attr.getRange(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3660 | return; |
| 3661 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3662 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3663 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3664 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3665 | else if (IntegerMode) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3666 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3667 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3668 | } else if (ComplexMode) { |
| 3669 | if (!OldTy->isComplexType()) |
| 3670 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3671 | } else { |
| 3672 | if (!OldTy->isFloatingType()) |
| 3673 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3674 | } |
| 3675 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3676 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3677 | // and friends, at least with glibc. |
| 3678 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 3679 | // width on unusual platforms. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3680 | // FIXME: Make sure floating-point mappings are accurate |
| 3681 | // FIXME: Support XF and TF types |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3682 | QualType NewTy; |
| 3683 | switch (DestWidth) { |
| 3684 | case 0: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3685 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3686 | return; |
| 3687 | default: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3688 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3689 | return; |
| 3690 | case 8: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3691 | if (!IntegerMode) { |
| 3692 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3693 | return; |
| 3694 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3695 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3696 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3697 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3698 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3699 | break; |
| 3700 | case 16: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3701 | if (!IntegerMode) { |
| 3702 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3703 | return; |
| 3704 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3705 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3706 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3707 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3708 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3709 | break; |
| 3710 | case 32: |
| 3711 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3712 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3713 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3714 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3715 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3716 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3717 | break; |
| 3718 | case 64: |
| 3719 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3720 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3721 | else if (OldTy->isSignedIntegerType()) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3722 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3723 | NewTy = S.Context.LongTy; |
| 3724 | else |
| 3725 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3726 | else |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3727 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3728 | NewTy = S.Context.UnsignedLongTy; |
| 3729 | else |
| 3730 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3731 | break; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3732 | case 96: |
| 3733 | NewTy = S.Context.LongDoubleTy; |
| 3734 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3735 | case 128: |
Roman Divacky | 1964554 | 2013-07-03 21:08:41 +0000 | [diff] [blame] | 3736 | if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() != |
| 3737 | &llvm::APFloat::PPCDoubleDouble) { |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3738 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3739 | return; |
| 3740 | } |
Roman Divacky | f0d14cb | 2013-07-03 20:48:06 +0000 | [diff] [blame] | 3741 | if (IntegerMode) { |
| 3742 | if (OldTy->isSignedIntegerType()) |
| 3743 | NewTy = S.Context.Int128Ty; |
| 3744 | else |
| 3745 | NewTy = S.Context.UnsignedInt128Ty; |
| 3746 | } else |
| 3747 | NewTy = S.Context.LongDoubleTy; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3748 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3749 | } |
| 3750 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3751 | if (ComplexMode) { |
| 3752 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3753 | } |
| 3754 | |
| 3755 | // Install the new type. |
Enea Zaffanella | c2fa6b6 | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3756 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3757 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 3758 | else |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3759 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | c2fa6b6 | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3760 | |
| 3761 | D->addAttr(::new (S.Context) |
| 3762 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 3763 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3764 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3765 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3766 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3767 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3768 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3769 | return; |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 3770 | |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3771 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3772 | if (!VD->hasGlobalStorage()) |
| 3773 | S.Diag(Attr.getLoc(), |
| 3774 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3775 | << Attr.getName(); |
| 3776 | } else if (!isFunctionOrMethod(D)) { |
| 3777 | S.Diag(Attr.getLoc(), |
| 3778 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3779 | << Attr.getName(); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3780 | return; |
| 3781 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3782 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3783 | D->addAttr(::new (S.Context) |
| 3784 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3785 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3786 | } |
| 3787 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3788 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3789 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3790 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3791 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3792 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3793 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3794 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3795 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3796 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3797 | return; |
| 3798 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3799 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3800 | D->addAttr(::new (S.Context) |
| 3801 | NoInlineAttr(Attr.getRange(), S.Context, |
| 3802 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3805 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 3806 | const AttributeList &Attr) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3807 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3808 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3809 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3810 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3811 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3812 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3813 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3814 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3815 | return; |
| 3816 | } |
| 3817 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3818 | D->addAttr(::new (S.Context) |
| 3819 | NoInstrumentFunctionAttr(Attr.getRange(), S.Context, |
| 3820 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3821 | } |
| 3822 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3823 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3824 | if (S.LangOpts.CUDA) { |
| 3825 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 3826 | if (Attr.hasParameterOrArguments()) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3827 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 3828 | return; |
| 3829 | } |
| 3830 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3831 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3832 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3833 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3834 | return; |
| 3835 | } |
| 3836 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3837 | D->addAttr(::new (S.Context) |
| 3838 | CUDAConstantAttr(Attr.getRange(), S.Context, |
| 3839 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3840 | } else { |
| 3841 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 3842 | } |
| 3843 | } |
| 3844 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3845 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3846 | if (S.LangOpts.CUDA) { |
| 3847 | // check the attribute arguments. |
| 3848 | if (Attr.getNumArgs() != 0) { |
| 3849 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 3850 | return; |
| 3851 | } |
| 3852 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3853 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3854 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3855 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3856 | return; |
| 3857 | } |
| 3858 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3859 | D->addAttr(::new (S.Context) |
| 3860 | CUDADeviceAttr(Attr.getRange(), S.Context, |
| 3861 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3862 | } else { |
| 3863 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 3864 | } |
| 3865 | } |
| 3866 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3867 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3868 | if (S.LangOpts.CUDA) { |
| 3869 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3870 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3871 | return; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3872 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3873 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3874 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3875 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3876 | return; |
| 3877 | } |
| 3878 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3879 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3880 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 3881 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3882 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3883 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3884 | << FD->getType() |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3885 | << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(), |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3886 | "void"); |
| 3887 | } else { |
| 3888 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3889 | << FD->getType(); |
| 3890 | } |
| 3891 | return; |
| 3892 | } |
| 3893 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3894 | D->addAttr(::new (S.Context) |
| 3895 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
| 3896 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3897 | } else { |
| 3898 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 3899 | } |
| 3900 | } |
| 3901 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3902 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3903 | if (S.LangOpts.CUDA) { |
| 3904 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3905 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3906 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3907 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3908 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3909 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3910 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3911 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3912 | return; |
| 3913 | } |
| 3914 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3915 | D->addAttr(::new (S.Context) |
| 3916 | CUDAHostAttr(Attr.getRange(), S.Context, |
| 3917 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3918 | } else { |
| 3919 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 3920 | } |
| 3921 | } |
| 3922 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3923 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3924 | if (S.LangOpts.CUDA) { |
| 3925 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3926 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3927 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3928 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3929 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3930 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3931 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3932 | return; |
| 3933 | } |
| 3934 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3935 | D->addAttr(::new (S.Context) |
| 3936 | CUDASharedAttr(Attr.getRange(), S.Context, |
| 3937 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3938 | } else { |
| 3939 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 3940 | } |
| 3941 | } |
| 3942 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3943 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3944 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3945 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3946 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3947 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3948 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3949 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3950 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3951 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3952 | return; |
| 3953 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3954 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3955 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3956 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3957 | return; |
| 3958 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3959 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3960 | D->addAttr(::new (S.Context) |
| 3961 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3962 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3965 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3966 | if (hasDeclarator(D)) return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3967 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3968 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3969 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3970 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3971 | CallingConv CC; |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3972 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3973 | return; |
| 3974 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3975 | if (!isa<ObjCMethodDecl>(D)) { |
| 3976 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3977 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3978 | return; |
| 3979 | } |
| 3980 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3981 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3982 | case AttributeList::AT_FastCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3983 | D->addAttr(::new (S.Context) |
| 3984 | FastCallAttr(Attr.getRange(), S.Context, |
| 3985 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3986 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3987 | case AttributeList::AT_StdCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3988 | D->addAttr(::new (S.Context) |
| 3989 | StdCallAttr(Attr.getRange(), S.Context, |
| 3990 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3991 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3992 | case AttributeList::AT_ThisCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3993 | D->addAttr(::new (S.Context) |
| 3994 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3995 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 04633eb | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3996 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3997 | case AttributeList::AT_CDecl: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3998 | D->addAttr(::new (S.Context) |
| 3999 | CDeclAttr(Attr.getRange(), S.Context, |
| 4000 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4001 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4002 | case AttributeList::AT_Pascal: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4003 | D->addAttr(::new (S.Context) |
| 4004 | PascalAttr(Attr.getRange(), S.Context, |
| 4005 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4006 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4007 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4008 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4009 | switch (CC) { |
| 4010 | case CC_AAPCS: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4011 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4012 | break; |
| 4013 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4014 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4015 | break; |
| 4016 | default: |
| 4017 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4018 | } |
| 4019 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4020 | D->addAttr(::new (S.Context) |
| 4021 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 4022 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4023 | return; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4024 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4025 | case AttributeList::AT_PnaclCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4026 | D->addAttr(::new (S.Context) |
| 4027 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 4028 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4029 | return; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4030 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4031 | D->addAttr(::new (S.Context) |
| 4032 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 4033 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4034 | return; |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4035 | |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4036 | default: |
| 4037 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4038 | } |
| 4039 | } |
| 4040 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4041 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 4042 | assert(!Attr.isInvalid()); |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4043 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4044 | } |
| 4045 | |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4046 | static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
| 4047 | assert(!Attr.isInvalid()); |
| 4048 | |
| 4049 | Expr *E = Attr.getArg(0); |
| 4050 | llvm::APSInt ArgNum(32); |
| 4051 | if (E->isTypeDependent() || E->isValueDependent() || |
| 4052 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 4053 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 4054 | << Attr.getName()->getName() << E->getSourceRange(); |
| 4055 | return; |
| 4056 | } |
| 4057 | |
| 4058 | D->addAttr(::new (S.Context) OpenCLImageAccessAttr( |
| 4059 | Attr.getRange(), S.Context, ArgNum.getZExtValue())); |
| 4060 | } |
| 4061 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4062 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 4063 | const FunctionDecl *FD) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4064 | if (attr.isInvalid()) |
| 4065 | return true; |
| 4066 | |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4067 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
| 4068 | if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) { |
| 4069 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4070 | attr.setInvalid(); |
| 4071 | return true; |
| 4072 | } |
| 4073 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4074 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 4075 | // move to TargetAttributesSema one day. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4076 | switch (attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4077 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 4078 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 4079 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 4080 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 4081 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
| 4082 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4083 | Expr *Arg = attr.getArg(0); |
| 4084 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4085 | if (!Str || !Str->isAscii()) { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4086 | Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 4087 | << "pcs" << 1; |
| 4088 | attr.setInvalid(); |
| 4089 | return true; |
| 4090 | } |
| 4091 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4092 | StringRef StrRef = Str->getString(); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4093 | if (StrRef == "aapcs") { |
| 4094 | CC = CC_AAPCS; |
| 4095 | break; |
| 4096 | } else if (StrRef == "aapcs-vfp") { |
| 4097 | CC = CC_AAPCS_VFP; |
| 4098 | break; |
| 4099 | } |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4100 | |
| 4101 | attr.setInvalid(); |
| 4102 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 4103 | return true; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4104 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4105 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4106 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4107 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4108 | } |
| 4109 | |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4110 | const TargetInfo &TI = Context.getTargetInfo(); |
| 4111 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 4112 | if (A == TargetInfo::CCCR_Warning) { |
| 4113 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4114 | |
| 4115 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 4116 | if (FD) |
| 4117 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 4118 | TargetInfo::CCMT_NonMember; |
| 4119 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4122 | return false; |
| 4123 | } |
| 4124 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4125 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4126 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4127 | |
| 4128 | unsigned numParams; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4129 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4130 | return; |
| 4131 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4132 | if (!isa<ObjCMethodDecl>(D)) { |
| 4133 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4134 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4135 | return; |
| 4136 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4137 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4138 | D->addAttr(::new (S.Context) |
| 4139 | RegparmAttr(Attr.getRange(), S.Context, numParams, |
| 4140 | Attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4141 | } |
| 4142 | |
| 4143 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 4144 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4145 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 4146 | if (Attr.isInvalid()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4147 | return true; |
| 4148 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4149 | if (Attr.getNumArgs() != 1) { |
| 4150 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 4151 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4152 | return true; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4153 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4154 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4155 | Expr *NumParamsExpr = Attr.getArg(0); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4156 | llvm::APSInt NumParams(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 4157 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4158 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4159 | Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4160 | << "regparm" << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4161 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4162 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4163 | } |
| 4164 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4165 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4166 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4167 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4168 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4169 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4172 | numParams = NumParams.getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4173 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4174 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4175 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4176 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4177 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4178 | } |
| 4179 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4180 | return false; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4181 | } |
| 4182 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4183 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4184 | if (S.LangOpts.CUDA) { |
| 4185 | // check the attribute arguments. |
| 4186 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 4187 | // FIXME: 0 is not okay. |
| 4188 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4189 | return; |
| 4190 | } |
| 4191 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4192 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4193 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4194 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4195 | return; |
| 4196 | } |
| 4197 | |
| 4198 | Expr *MaxThreadsExpr = Attr.getArg(0); |
| 4199 | llvm::APSInt MaxThreads(32); |
| 4200 | if (MaxThreadsExpr->isTypeDependent() || |
| 4201 | MaxThreadsExpr->isValueDependent() || |
| 4202 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
| 4203 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 4204 | << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange(); |
| 4205 | return; |
| 4206 | } |
| 4207 | |
| 4208 | llvm::APSInt MinBlocks(32); |
| 4209 | if (Attr.getNumArgs() > 1) { |
| 4210 | Expr *MinBlocksExpr = Attr.getArg(1); |
| 4211 | if (MinBlocksExpr->isTypeDependent() || |
| 4212 | MinBlocksExpr->isValueDependent() || |
| 4213 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
| 4214 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 4215 | << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange(); |
| 4216 | return; |
| 4217 | } |
| 4218 | } |
| 4219 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4220 | D->addAttr(::new (S.Context) |
| 4221 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 4222 | MaxThreads.getZExtValue(), |
| 4223 | MinBlocks.getZExtValue(), |
| 4224 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4225 | } else { |
| 4226 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 4227 | } |
| 4228 | } |
| 4229 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4230 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 4231 | const AttributeList &Attr) { |
| 4232 | StringRef AttrName = Attr.getName()->getName(); |
| 4233 | if (!Attr.getParameterName()) { |
| 4234 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier) |
| 4235 | << Attr.getName() << /* arg num = */ 1; |
| 4236 | return; |
| 4237 | } |
| 4238 | |
| 4239 | if (Attr.getNumArgs() != 2) { |
| 4240 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 4241 | << /* required args = */ 3; |
| 4242 | return; |
| 4243 | } |
| 4244 | |
| 4245 | IdentifierInfo *ArgumentKind = Attr.getParameterName(); |
| 4246 | |
| 4247 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 4248 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4249 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 4250 | return; |
| 4251 | } |
| 4252 | |
| 4253 | uint64_t ArgumentIdx; |
| 4254 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4255 | Attr.getLoc(), 2, |
| 4256 | Attr.getArg(0), ArgumentIdx)) |
| 4257 | return; |
| 4258 | |
| 4259 | uint64_t TypeTagIdx; |
| 4260 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4261 | Attr.getLoc(), 3, |
| 4262 | Attr.getArg(1), TypeTagIdx)) |
| 4263 | return; |
| 4264 | |
| 4265 | bool IsPointer = (AttrName == "pointer_with_type_tag"); |
| 4266 | if (IsPointer) { |
| 4267 | // Ensure that buffer has a pointer type. |
| 4268 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 4269 | if (!BufferTy->isPointerType()) { |
| 4270 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4271 | << Attr.getName(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4272 | } |
| 4273 | } |
| 4274 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4275 | D->addAttr(::new (S.Context) |
| 4276 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 4277 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 4278 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4279 | } |
| 4280 | |
| 4281 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 4282 | const AttributeList &Attr) { |
| 4283 | IdentifierInfo *PointerKind = Attr.getParameterName(); |
| 4284 | if (!PointerKind) { |
| 4285 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier) |
| 4286 | << "type_tag_for_datatype" << 1; |
| 4287 | return; |
| 4288 | } |
| 4289 | |
| 4290 | QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL); |
| 4291 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4292 | D->addAttr(::new (S.Context) |
| 4293 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
| 4294 | MatchingCType, |
| 4295 | Attr.getLayoutCompatible(), |
| 4296 | Attr.getMustBeNull(), |
| 4297 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4298 | } |
| 4299 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4300 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4301 | // Checker-specific attribute handlers. |
| 4302 | //===----------------------------------------------------------------------===// |
| 4303 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4304 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4305 | return type->isDependentType() || |
| 4306 | type->isObjCObjectPointerType() || |
| 4307 | S.Context.isObjCNSObjectType(type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4308 | } |
| 4309 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4310 | return type->isDependentType() || |
| 4311 | type->isPointerType() || |
| 4312 | isValidSubjectOfNSAttribute(S, type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4313 | } |
| 4314 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4315 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4316 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4317 | if (!param) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4318 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4319 | << Attr.getRange() << Attr.getName() << ExpectedParameter; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4320 | return; |
| 4321 | } |
| 4322 | |
| 4323 | bool typeOK, cf; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4324 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4325 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 4326 | cf = false; |
| 4327 | } else { |
| 4328 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 4329 | cf = true; |
| 4330 | } |
| 4331 | |
| 4332 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4333 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4334 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4335 | return; |
| 4336 | } |
| 4337 | |
| 4338 | if (cf) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4339 | param->addAttr(::new (S.Context) |
| 4340 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 4341 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4342 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4343 | param->addAttr(::new (S.Context) |
| 4344 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 4345 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4346 | } |
| 4347 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4348 | static void handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 4349 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4350 | if (!isa<ObjCMethodDecl>(D)) { |
| 4351 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4352 | << Attr.getRange() << Attr.getName() << ExpectedMethod; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4353 | return; |
| 4354 | } |
| 4355 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4356 | D->addAttr(::new (S.Context) |
| 4357 | NSConsumesSelfAttr(Attr.getRange(), S.Context, |
| 4358 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4361 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 4362 | const AttributeList &Attr) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4363 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4364 | QualType returnType; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4365 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4366 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4367 | returnType = MD->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4368 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4369 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4370 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | a23bd4c | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 4371 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4372 | returnType = PD->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4373 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4374 | returnType = FD->getResultType(); |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4375 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4376 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4377 | << Attr.getRange() << Attr.getName() |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4378 | << ExpectedFunctionOrMethod; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4379 | return; |
| 4380 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4381 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4382 | bool typeOK; |
| 4383 | bool cf; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4384 | switch (Attr.getKind()) { |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4385 | default: llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4386 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4387 | case AttributeList::AT_NSReturnsRetained: |
| 4388 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4389 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 4390 | cf = false; |
| 4391 | break; |
| 4392 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4393 | case AttributeList::AT_CFReturnsRetained: |
| 4394 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4395 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 4396 | cf = true; |
| 4397 | break; |
| 4398 | } |
| 4399 | |
| 4400 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4401 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4402 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4403 | return; |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4404 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4405 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4406 | switch (Attr.getKind()) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4407 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4408 | llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4409 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4410 | D->addAttr(::new (S.Context) |
| 4411 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 4412 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4413 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4414 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4415 | D->addAttr(::new (S.Context) |
| 4416 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4417 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4418 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4419 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4420 | D->addAttr(::new (S.Context) |
| 4421 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4422 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4423 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4424 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4425 | D->addAttr(::new (S.Context) |
| 4426 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4427 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4428 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4429 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4430 | D->addAttr(::new (S.Context) |
| 4431 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4432 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4433 | return; |
| 4434 | }; |
| 4435 | } |
| 4436 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4437 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4438 | const AttributeList &attr) { |
| 4439 | SourceLocation loc = attr.getLoc(); |
| 4440 | |
| 4441 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4442 | |
Fariborz Jahanian | 94d55d7 | 2012-04-21 17:51:44 +0000 | [diff] [blame] | 4443 | if (!method) { |
Fariborz Jahanian | 0e78afb | 2012-04-20 22:00:46 +0000 | [diff] [blame] | 4444 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4445 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4446 | return; |
| 4447 | } |
| 4448 | |
| 4449 | // Check that the method returns a normal pointer. |
| 4450 | QualType resultType = method->getResultType(); |
Fariborz Jahanian | f2e5945 | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4451 | |
| 4452 | if (!resultType->isReferenceType() && |
| 4453 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4454 | S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 4455 | << SourceRange(loc) |
| 4456 | << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2; |
| 4457 | |
| 4458 | // Drop the attribute. |
| 4459 | return; |
| 4460 | } |
| 4461 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4462 | method->addAttr(::new (S.Context) |
| 4463 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 4464 | attr.getAttributeSpellingListIndex())); |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4465 | } |
| 4466 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4467 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4468 | const AttributeList &attr) { |
| 4469 | SourceLocation loc = attr.getLoc(); |
| 4470 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4471 | |
| 4472 | if (!method) { |
| 4473 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 4474 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
| 4475 | return; |
| 4476 | } |
| 4477 | DeclContext *DC = method->getDeclContext(); |
| 4478 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4479 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4480 | << attr.getName() << 0; |
| 4481 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4482 | return; |
| 4483 | } |
| 4484 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4485 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4486 | << attr.getName() << 1; |
| 4487 | return; |
| 4488 | } |
| 4489 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4490 | method->addAttr(::new (S.Context) |
| 4491 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 4492 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4493 | } |
| 4494 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4495 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 4496 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
| 4497 | if (!isa<FunctionDecl>(D)) { |
| 4498 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4499 | << A.getRange() << A.getName() << ExpectedFunction; |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4500 | return; |
| 4501 | } |
| 4502 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4503 | bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4504 | |
| 4505 | // Check whether there's a conflicting attribute already present. |
| 4506 | Attr *Existing; |
| 4507 | if (IsAudited) { |
| 4508 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 4509 | } else { |
| 4510 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 4511 | } |
| 4512 | if (Existing) { |
| 4513 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 4514 | << A.getName() |
| 4515 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 4516 | << A.getRange() << Existing->getRange(); |
| 4517 | return; |
| 4518 | } |
| 4519 | |
| 4520 | // All clear; add the attribute. |
| 4521 | if (IsAudited) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4522 | D->addAttr(::new (S.Context) |
| 4523 | CFAuditedTransferAttr(A.getRange(), S.Context, |
| 4524 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4525 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4526 | D->addAttr(::new (S.Context) |
| 4527 | CFUnknownTransferAttr(A.getRange(), S.Context, |
| 4528 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4529 | } |
| 4530 | } |
| 4531 | |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4532 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4533 | const AttributeList &Attr) { |
| 4534 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4535 | if (!RD || RD->isUnion()) { |
| 4536 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4537 | << Attr.getRange() << Attr.getName() << ExpectedStruct; |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4538 | } |
| 4539 | |
| 4540 | IdentifierInfo *ParmName = Attr.getParameterName(); |
| 4541 | |
| 4542 | // In Objective-C, verify that the type names an Objective-C type. |
| 4543 | // We don't want to check this outside of ObjC because people sometimes |
| 4544 | // do crazy C declarations of Objective-C types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4545 | if (ParmName && S.getLangOpts().ObjC1) { |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4546 | // Check for an existing type with this name. |
| 4547 | LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(), |
| 4548 | Sema::LookupOrdinaryName); |
| 4549 | if (S.LookupName(R, Sc)) { |
| 4550 | NamedDecl *Target = R.getFoundDecl(); |
| 4551 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 4552 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 4553 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 4554 | } |
| 4555 | } |
| 4556 | } |
| 4557 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4558 | D->addAttr(::new (S.Context) |
| 4559 | NSBridgedAttr(Attr.getRange(), S.Context, ParmName, |
| 4560 | Attr.getAttributeSpellingListIndex())); |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4561 | } |
| 4562 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4563 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4564 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4565 | if (hasDeclarator(D)) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4566 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4567 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4568 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4569 | } |
| 4570 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4571 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4572 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4573 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4574 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4575 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4576 | return; |
| 4577 | } |
| 4578 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4579 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4580 | QualType type = vd->getType(); |
| 4581 | |
| 4582 | if (!type->isDependentType() && |
| 4583 | !type->isObjCLifetimeType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4584 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4585 | << type; |
| 4586 | return; |
| 4587 | } |
| 4588 | |
| 4589 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4590 | |
| 4591 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4592 | // going to infer. |
| 4593 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4594 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4595 | |
| 4596 | switch (lifetime) { |
| 4597 | case Qualifiers::OCL_None: |
| 4598 | assert(type->isDependentType() && |
| 4599 | "didn't infer lifetime for non-dependent type?"); |
| 4600 | break; |
| 4601 | |
| 4602 | case Qualifiers::OCL_Weak: // meaningful |
| 4603 | case Qualifiers::OCL_Strong: // meaningful |
| 4604 | break; |
| 4605 | |
| 4606 | case Qualifiers::OCL_ExplicitNone: |
| 4607 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4608 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4609 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4610 | break; |
| 4611 | } |
| 4612 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4613 | D->addAttr(::new (S.Context) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4614 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 4615 | Attr.getAttributeSpellingListIndex())); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4616 | } |
| 4617 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4618 | //===----------------------------------------------------------------------===// |
| 4619 | // Microsoft specific attribute handlers. |
| 4620 | //===----------------------------------------------------------------------===// |
| 4621 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4622 | // Check if MS extensions or some other language extensions are enabled. If |
| 4623 | // not, issue a diagnostic that the given attribute is unused. |
| 4624 | static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr, |
| 4625 | bool OtherExtension = false) { |
| 4626 | if (S.LangOpts.MicrosoftExt || OtherExtension) |
| 4627 | return true; |
| 4628 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4629 | return false; |
| 4630 | } |
| 4631 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4632 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4633 | if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland)) |
| 4634 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4635 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4636 | // check the attribute arguments. |
| 4637 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4638 | return; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4639 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4640 | Expr *Arg = Attr.getArg(0); |
| 4641 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 4642 | if (!Str || !Str->isAscii()) { |
| 4643 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 4644 | << "uuid" << 1; |
| 4645 | return; |
| 4646 | } |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4647 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4648 | StringRef StrRef = Str->getString(); |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4649 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4650 | bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' && |
| 4651 | StrRef.back() == '}'; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4652 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4653 | // Validate GUID length. |
| 4654 | if (IsCurly && StrRef.size() != 38) { |
| 4655 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4656 | return; |
| 4657 | } |
| 4658 | if (!IsCurly && StrRef.size() != 36) { |
| 4659 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4660 | return; |
| 4661 | } |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4662 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4663 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 4664 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" |
| 4665 | StringRef::iterator I = StrRef.begin(); |
| 4666 | if (IsCurly) // Skip the optional '{' |
| 4667 | ++I; |
| 4668 | |
| 4669 | for (int i = 0; i < 36; ++i) { |
| 4670 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 4671 | if (*I != '-') { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4672 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4673 | return; |
| 4674 | } |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4675 | } else if (!isHexDigit(*I)) { |
| 4676 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4677 | return; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4678 | } |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4679 | I++; |
| 4680 | } |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4681 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4682 | D->addAttr(::new (S.Context) |
| 4683 | UuidAttr(Attr.getRange(), S.Context, Str->getString(), |
| 4684 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4685 | } |
| 4686 | |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4687 | static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4688 | if (!checkMicrosoftExt(S, Attr)) |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4689 | return; |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4690 | |
| 4691 | AttributeList::Kind Kind = Attr.getKind(); |
| 4692 | if (Kind == AttributeList::AT_SingleInheritance) |
| 4693 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4694 | ::new (S.Context) |
| 4695 | SingleInheritanceAttr(Attr.getRange(), S.Context, |
| 4696 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4697 | else if (Kind == AttributeList::AT_MultipleInheritance) |
| 4698 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4699 | ::new (S.Context) |
| 4700 | MultipleInheritanceAttr(Attr.getRange(), S.Context, |
| 4701 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4702 | else if (Kind == AttributeList::AT_VirtualInheritance) |
| 4703 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4704 | ::new (S.Context) |
| 4705 | VirtualInheritanceAttr(Attr.getRange(), S.Context, |
| 4706 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4707 | } |
| 4708 | |
| 4709 | static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4710 | if (!checkMicrosoftExt(S, Attr)) |
| 4711 | return; |
| 4712 | |
| 4713 | AttributeList::Kind Kind = Attr.getKind(); |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4714 | if (Kind == AttributeList::AT_Win64) |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4715 | D->addAttr( |
| 4716 | ::new (S.Context) Win64Attr(Attr.getRange(), S.Context, |
| 4717 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4718 | } |
| 4719 | |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4720 | static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4721 | if (!checkMicrosoftExt(S, Attr)) |
| 4722 | return; |
| 4723 | D->addAttr(::new (S.Context) |
| 4724 | ForceInlineAttr(Attr.getRange(), S.Context, |
| 4725 | Attr.getAttributeSpellingListIndex())); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4726 | } |
| 4727 | |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4728 | static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4729 | if (!checkMicrosoftExt(S, Attr)) |
| 4730 | return; |
| 4731 | // Check linkage after possibly merging declaratinos. See |
| 4732 | // checkAttributesAfterMerging(). |
| 4733 | D->addAttr(::new (S.Context) |
| 4734 | SelectAnyAttr(Attr.getRange(), S.Context, |
| 4735 | Attr.getAttributeSpellingListIndex())); |
| 4736 | } |
| 4737 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4738 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4739 | // Top Level Sema Entry Points |
| 4740 | //===----------------------------------------------------------------------===// |
| 4741 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4742 | static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4743 | const AttributeList &Attr) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4744 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4745 | case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break; |
| 4746 | case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break; |
| 4747 | case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break; |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4748 | default: |
| 4749 | break; |
| 4750 | } |
| 4751 | } |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4752 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4753 | static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4754 | const AttributeList &Attr) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4755 | switch (Attr.getKind()) { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4756 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 4757 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 4758 | case AttributeList::AT_IBOutletCollection: |
| 4759 | handleIBOutletCollection(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4760 | case AttributeList::AT_AddressSpace: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4761 | case AttributeList::AT_ObjCGC: |
| 4762 | case AttributeList::AT_VectorSize: |
| 4763 | case AttributeList::AT_NeonVectorType: |
| 4764 | case AttributeList::AT_NeonPolyVectorType: |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4765 | case AttributeList::AT_Ptr32: |
| 4766 | case AttributeList::AT_Ptr64: |
| 4767 | case AttributeList::AT_SPtr: |
| 4768 | case AttributeList::AT_UPtr: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4769 | // Ignore these, these are type attributes, handled by |
| 4770 | // ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4771 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4772 | case AttributeList::AT_CUDADevice: |
| 4773 | case AttributeList::AT_CUDAHost: |
| 4774 | case AttributeList::AT_Overloadable: |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4775 | // Ignore, this is a non-inheritable attribute, handled |
| 4776 | // by ProcessNonInheritableDeclAttr. |
| 4777 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4778 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 4779 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 4780 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 4781 | case AttributeList::AT_AlwaysInline: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4782 | handleAlwaysInlineAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4783 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4784 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 4785 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4786 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 4787 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 4788 | case AttributeList::AT_CarriesDependency: |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4789 | handleDependencyAttr(S, scope, D, Attr); |
| 4790 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4791 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
| 4792 | case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break; |
| 4793 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4794 | case AttributeList::AT_CXX11NoReturn: |
| 4795 | handleCXX11NoReturnAttr(S, D, Attr); |
| 4796 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4797 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4798 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4799 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4800 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 4801 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4802 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4803 | break; |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4804 | case AttributeList::AT_MinSize: |
| 4805 | handleMinSizeAttr(S, D, Attr); |
| 4806 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4807 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4808 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4809 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
| 4810 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4811 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4812 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4813 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4814 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
| 4815 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
| 4816 | case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break; |
| 4817 | case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break; |
| 4818 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4819 | case AttributeList::AT_ownership_returns: |
| 4820 | case AttributeList::AT_ownership_takes: |
| 4821 | case AttributeList::AT_ownership_holds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4822 | handleOwnershipAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4823 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4824 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
| 4825 | case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break; |
| 4826 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
| 4827 | case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break; |
| 4828 | case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break; |
| 4829 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4830 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4831 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4832 | handleObjCOwnershipAttr(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4833 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4834 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4835 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4836 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4837 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4838 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4839 | case AttributeList::AT_ObjCRequiresSuper: |
| 4840 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4841 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4842 | case AttributeList::AT_NSBridged: |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4843 | handleNSBridgedAttr(S, scope, D, Attr); break; |
| 4844 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4845 | case AttributeList::AT_CFAuditedTransfer: |
| 4846 | case AttributeList::AT_CFUnknownTransfer: |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4847 | handleCFTransferAttr(S, D, Attr); break; |
| 4848 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4849 | // Checker-specific. |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4850 | case AttributeList::AT_CFConsumed: |
| 4851 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4852 | case AttributeList::AT_NSConsumesSelf: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4853 | handleNSConsumesSelfAttr(S, D, Attr); break; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4854 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4855 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4856 | case AttributeList::AT_NSReturnsNotRetained: |
| 4857 | case AttributeList::AT_CFReturnsNotRetained: |
| 4858 | case AttributeList::AT_NSReturnsRetained: |
| 4859 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4860 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4861 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4862 | case AttributeList::AT_WorkGroupSizeHint: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4863 | case AttributeList::AT_ReqdWorkGroupSize: |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4864 | handleWorkGroupSize(S, D, Attr); break; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 4865 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4866 | case AttributeList::AT_VecTypeHint: |
| 4867 | handleVecTypeHint(S, D, Attr); break; |
| 4868 | |
Joey Gouly | 96cead5 | 2013-03-14 09:54:43 +0000 | [diff] [blame] | 4869 | case AttributeList::AT_Endian: |
| 4870 | handleEndianAttr(S, D, Attr); |
| 4871 | break; |
| 4872 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4873 | case AttributeList::AT_InitPriority: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4874 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4875 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4876 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4877 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4878 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4879 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4880 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4881 | case AttributeList::AT_ArcWeakrefUnavailable: |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 4882 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 4883 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4884 | case AttributeList::AT_ObjCRootClass: |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4885 | handleObjCRootClassAttr(S, D, Attr); |
| 4886 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4887 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 4888 | handleObjCRequiresPropertyDefsAttr (S, D, Attr); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 4889 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4890 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4891 | case AttributeList::AT_ReturnsTwice: |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 4892 | handleReturnsTwiceAttr(S, D, Attr); |
| 4893 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4894 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4895 | case AttributeList::AT_Visibility: |
| 4896 | handleVisibilityAttr(S, D, Attr, false); |
| 4897 | break; |
| 4898 | case AttributeList::AT_TypeVisibility: |
| 4899 | handleVisibilityAttr(S, D, Attr, true); |
| 4900 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4901 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4902 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4903 | case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; |
| 4904 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4905 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4906 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4907 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4908 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4909 | case AttributeList::AT_ObjCException: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4910 | handleObjCExceptionAttr(S, D, Attr); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 4911 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4912 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4913 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4914 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4915 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4916 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4917 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
| 4918 | case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break; |
| 4919 | case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break; |
| 4920 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4921 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
| 4922 | case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break; |
| 4923 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4924 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4925 | // Just ignore |
| 4926 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4927 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4928 | handleNoInstrumentFunctionAttr(S, D, Attr); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 4929 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4930 | case AttributeList::AT_StdCall: |
| 4931 | case AttributeList::AT_CDecl: |
| 4932 | case AttributeList::AT_FastCall: |
| 4933 | case AttributeList::AT_ThisCall: |
| 4934 | case AttributeList::AT_Pascal: |
| 4935 | case AttributeList::AT_Pcs: |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4936 | case AttributeList::AT_PnaclCall: |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4937 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4938 | handleCallConvAttr(S, D, Attr); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4939 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4940 | case AttributeList::AT_OpenCLKernel: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4941 | handleOpenCLKernelAttr(S, D, Attr); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4942 | break; |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4943 | case AttributeList::AT_OpenCLImageAccess: |
| 4944 | handleOpenCLImageAccessAttr(S, D, Attr); |
| 4945 | break; |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4946 | |
| 4947 | // Microsoft attributes: |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4948 | case AttributeList::AT_MsProperty: break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4949 | case AttributeList::AT_MsStruct: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4950 | handleMsStructAttr(S, D, Attr); |
| 4951 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4952 | case AttributeList::AT_Uuid: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4953 | handleUuidAttr(S, D, Attr); |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4954 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4955 | case AttributeList::AT_SingleInheritance: |
| 4956 | case AttributeList::AT_MultipleInheritance: |
| 4957 | case AttributeList::AT_VirtualInheritance: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4958 | handleInheritanceAttr(S, D, Attr); |
| 4959 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4960 | case AttributeList::AT_Win64: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4961 | handlePortabilityAttr(S, D, Attr); |
| 4962 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4963 | case AttributeList::AT_ForceInline: |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4964 | handleForceInlineAttr(S, D, Attr); |
| 4965 | break; |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4966 | case AttributeList::AT_SelectAny: |
| 4967 | handleSelectAnyAttr(S, D, Attr); |
| 4968 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4969 | |
| 4970 | // Thread safety attributes: |
DeLesley Hutchins | 5c6134f | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 4971 | case AttributeList::AT_AssertExclusiveLock: |
| 4972 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 4973 | break; |
| 4974 | case AttributeList::AT_AssertSharedLock: |
| 4975 | handleAssertSharedLockAttr(S, D, Attr); |
| 4976 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4977 | case AttributeList::AT_GuardedVar: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4978 | handleGuardedVarAttr(S, D, Attr); |
| 4979 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4980 | case AttributeList::AT_PtGuardedVar: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4981 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4982 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4983 | case AttributeList::AT_ScopedLockable: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4984 | handleScopedLockableAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4985 | break; |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4986 | case AttributeList::AT_NoSanitizeAddress: |
| 4987 | handleNoSanitizeAddressAttr(S, D, Attr); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4988 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4989 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4990 | handleNoThreadSafetyAnalysis(S, D, Attr); |
| 4991 | break; |
| 4992 | case AttributeList::AT_NoSanitizeThread: |
| 4993 | handleNoSanitizeThread(S, D, Attr); |
| 4994 | break; |
| 4995 | case AttributeList::AT_NoSanitizeMemory: |
| 4996 | handleNoSanitizeMemory(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4997 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4998 | case AttributeList::AT_Lockable: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4999 | handleLockableAttr(S, D, Attr); |
| 5000 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5001 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5002 | handleGuardedByAttr(S, D, Attr); |
| 5003 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5004 | case AttributeList::AT_PtGuardedBy: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5005 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5006 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5007 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5008 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5009 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5010 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5011 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5012 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5013 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5014 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5015 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5016 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5017 | handleLockReturnedAttr(S, D, Attr); |
| 5018 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5019 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5020 | handleLocksExcludedAttr(S, D, Attr); |
| 5021 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5022 | case AttributeList::AT_SharedLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5023 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5024 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5025 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5026 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5027 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5028 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5029 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5030 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5031 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5032 | handleUnlockFunAttr(S, D, Attr); |
| 5033 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5034 | case AttributeList::AT_AcquiredBefore: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5035 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5036 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5037 | case AttributeList::AT_AcquiredAfter: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5038 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5039 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5040 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 5041 | // Type safety attributes. |
| 5042 | case AttributeList::AT_ArgumentWithTypeTag: |
| 5043 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 5044 | break; |
| 5045 | case AttributeList::AT_TypeTagForDatatype: |
| 5046 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 5047 | break; |
| 5048 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5049 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5050 | // Ask target about the attribute. |
| 5051 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 5052 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 5053 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 5054 | diag::warn_unhandled_ms_attribute_ignored : |
| 5055 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5056 | break; |
| 5057 | } |
| 5058 | } |
| 5059 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5060 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 5061 | /// 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] | 5062 | /// silently ignore it if a GNU attribute. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5063 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 5064 | const AttributeList &Attr, |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5065 | bool NonInheritable, bool Inheritable, |
| 5066 | bool IncludeCXX11Attributes) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5067 | if (Attr.isInvalid()) |
| 5068 | return; |
| 5069 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5070 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 5071 | // instead. |
| 5072 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 5073 | return; |
| 5074 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5075 | if (NonInheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5076 | ProcessNonInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5077 | |
| 5078 | if (Inheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5079 | ProcessInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5080 | } |
| 5081 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5082 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 5083 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 5084 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5085 | const AttributeList *AttrList, |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5086 | bool NonInheritable, bool Inheritable, |
| 5087 | bool IncludeCXX11Attributes) { |
| 5088 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
| 5089 | ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable, |
| 5090 | IncludeCXX11Attributes); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5091 | |
| 5092 | // GCC accepts |
| 5093 | // static int a9 __attribute__((weakref)); |
| 5094 | // but that looks really pointless. We reject it. |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5095 | if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5096 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Rafael Espindola | 4d8a33b | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 5097 | cast<NamedDecl>(D)->getNameAsString(); |
| 5098 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5099 | return; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5100 | } |
| 5101 | } |
| 5102 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 5103 | // Annotation attributes are the only attributes allowed after an access |
| 5104 | // specifier. |
| 5105 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 5106 | const AttributeList *AttrList) { |
| 5107 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5108 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 5109 | handleAnnotateAttr(*this, ASDecl, *l); |
| 5110 | } else { |
| 5111 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 5112 | return true; |
| 5113 | } |
| 5114 | } |
| 5115 | |
| 5116 | return false; |
| 5117 | } |
| 5118 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5119 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 5120 | /// contains any decl attributes that we should warn about. |
| 5121 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 5122 | for ( ; A; A = A->getNext()) { |
| 5123 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 5124 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5125 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 5126 | |
| 5127 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 5128 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 5129 | << A->getName() << A->getRange(); |
| 5130 | } else { |
| 5131 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 5132 | << A->getName() << A->getRange(); |
| 5133 | } |
| 5134 | } |
| 5135 | } |
| 5136 | |
| 5137 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 5138 | /// used to build a declaration, complain about any decl attributes |
| 5139 | /// which might be lying around on it. |
| 5140 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 5141 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 5142 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 5143 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 5144 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 5145 | } |
| 5146 | |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5147 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5148 | /// \#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] | 5149 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 5150 | SourceLocation Loc) { |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5151 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5152 | NamedDecl *NewD = 0; |
| 5153 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5154 | FunctionDecl *NewFD; |
| 5155 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 5156 | // FIXME: Mangling? |
| 5157 | // FIXME: Is the qualifier info correct? |
| 5158 | // FIXME: Is the DeclContext correct? |
| 5159 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 5160 | Loc, Loc, DeclarationName(II), |
| 5161 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5162 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5163 | FD->hasPrototype(), |
| 5164 | false/*isConstexprSpecified*/); |
| 5165 | NewD = NewFD; |
| 5166 | |
| 5167 | if (FD->getQualifier()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5168 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5169 | |
| 5170 | // Fake up parameter variables; they are declared as if this were |
| 5171 | // a typedef. |
| 5172 | QualType FDTy = FD->getType(); |
| 5173 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 5174 | SmallVector<ParmVarDecl*, 16> Params; |
| 5175 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 5176 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 5177 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 5178 | Param->setScopeInfo(0, Params.size()); |
| 5179 | Params.push_back(Param); |
| 5180 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 5181 | NewFD->setParams(Params); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5182 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5183 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 5184 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5185 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5186 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5187 | VD->getStorageClass()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5188 | if (VD->getQualifier()) { |
| 5189 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5190 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5191 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5192 | } |
| 5193 | return NewD; |
| 5194 | } |
| 5195 | |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5196 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5197 | /// applied to it, possibly with an alias. |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5198 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5199 | if (W.getUsed()) return; // only do this once |
| 5200 | W.setUsed(true); |
| 5201 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 5202 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5203 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5204 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 5205 | NDId->getName())); |
| 5206 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5207 | WeakTopLevelDecl.push_back(NewD); |
| 5208 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 5209 | // to insert Decl at TU scope, sorry. |
| 5210 | DeclContext *SavedContext = CurContext; |
| 5211 | CurContext = Context.getTranslationUnitDecl(); |
| 5212 | PushOnScopeChains(NewD, S); |
| 5213 | CurContext = SavedContext; |
| 5214 | } else { // just add weak to existing |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5215 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5216 | } |
| 5217 | } |
| 5218 | |
Rafael Espindola | 65611bf | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5219 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 5220 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 5221 | // have to do this. |
| 5222 | LoadExternalWeakUndeclaredIdentifiers(); |
| 5223 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 5224 | NamedDecl *ND = NULL; |
| 5225 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 5226 | if (VD->isExternC()) |
| 5227 | ND = VD; |
| 5228 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5229 | if (FD->isExternC()) |
| 5230 | ND = FD; |
| 5231 | if (ND) { |
| 5232 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 5233 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 5234 | = WeakUndeclaredIdentifiers.find(Id); |
| 5235 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 5236 | WeakInfo W = I->second; |
| 5237 | DeclApplyPragmaWeak(S, ND, W); |
| 5238 | WeakUndeclaredIdentifiers[Id] = W; |
| 5239 | } |
| 5240 | } |
| 5241 | } |
| 5242 | } |
| 5243 | } |
| 5244 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5245 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 5246 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 5247 | /// 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] | 5248 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD, |
| 5249 | bool NonInheritable, bool Inheritable) { |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5250 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5251 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5252 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5253 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5254 | // Walk the declarator structure, applying decl attributes that were in a type |
| 5255 | // position to the decl itself. This handles cases like: |
| 5256 | // int *__attr__(x)** D; |
| 5257 | // when X is a decl attribute. |
| 5258 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 5259 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5260 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable, |
| 5261 | /*IncludeCXX11Attributes=*/false); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5262 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5263 | // Finally, apply any attributes on the decl itself. |
| 5264 | if (const AttributeList *Attrs = PD.getAttributes()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5265 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5266 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5267 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5268 | /// Is the given declaration allowed to use a forbidden type? |
| 5269 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 5270 | // Private ivars are always okay. Unfortunately, people don't |
| 5271 | // always properly make their ivars private, even in system headers. |
| 5272 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | a6b3380 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 5273 | // Function declarations in sys headers will be marked unavailable. |
| 5274 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 5275 | !isa<FunctionDecl>(decl)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5276 | return false; |
| 5277 | |
| 5278 | // Require it to be declared in a system header. |
| 5279 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 5280 | } |
| 5281 | |
| 5282 | /// Handle a delayed forbidden-type diagnostic. |
| 5283 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 5284 | Decl *decl) { |
| 5285 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 5286 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 5287 | "this system declaration uses an unsupported type")); |
| 5288 | return; |
| 5289 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5290 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5291 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 5292 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5293 | // kind of forbidden type messages on unavailable functions. |
| 5294 | if (FD->hasAttr<UnavailableAttr>() && |
| 5295 | diag.getForbiddenTypeDiagnostic() == |
| 5296 | diag::err_arc_array_param_no_ownership) { |
| 5297 | diag.Triggered = true; |
| 5298 | return; |
| 5299 | } |
| 5300 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5301 | |
| 5302 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 5303 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 5304 | diag.Triggered = true; |
| 5305 | } |
| 5306 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5307 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 5308 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5309 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5310 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5311 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5312 | // When delaying diagnostics to run in the context of a parsed |
| 5313 | // declaration, we only want to actually emit anything if parsing |
| 5314 | // succeeds. |
| 5315 | if (!decl) return; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5316 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5317 | // We emit all the active diagnostics in this pool or any of its |
| 5318 | // parents. In general, we'll get one pool for the decl spec |
| 5319 | // and a child pool for each declarator; in a decl group like: |
| 5320 | // deprecated_typedef foo, *bar, baz(); |
| 5321 | // only the declarator pops will be passed decls. This is correct; |
| 5322 | // we really do need to consider delayed diagnostics from the decl spec |
| 5323 | // for each of the different declarations. |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5324 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5325 | do { |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5326 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5327 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 5328 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 5329 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5330 | if (diag.Triggered) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5331 | continue; |
| 5332 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5333 | switch (diag.Kind) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5334 | case DelayedDiagnostic::Deprecation: |
John McCall | e8c904f | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 5335 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 5336 | if (!decl->isInvalidDecl()) |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5337 | HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5338 | break; |
| 5339 | |
| 5340 | case DelayedDiagnostic::Access: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5341 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5342 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5343 | |
| 5344 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5345 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5346 | break; |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5347 | } |
| 5348 | } |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5349 | } while ((pool = pool->getParent())); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5350 | } |
| 5351 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5352 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 5353 | /// been delayed in the current context instead of in the given pool. |
| 5354 | /// Essentially, this just moves them to the current pool. |
| 5355 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 5356 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 5357 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 5358 | curPool->steal(pool); |
| 5359 | } |
| 5360 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5361 | static bool isDeclDeprecated(Decl *D) { |
| 5362 | do { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5363 | if (D->isDeprecated()) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5364 | return true; |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 5365 | // A category implicitly has the availability of the interface. |
| 5366 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 5367 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5368 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 5369 | return false; |
| 5370 | } |
| 5371 | |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5372 | static void |
| 5373 | DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message, |
| 5374 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5375 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5376 | const ObjCPropertyDecl *ObjCPropery) { |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5377 | DeclarationName Name = D->getDeclName(); |
| 5378 | if (!Message.empty()) { |
| 5379 | S.Diag(Loc, diag::warn_deprecated_message) << Name << Message; |
| 5380 | S.Diag(D->getLocation(), |
| 5381 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5382 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5383 | if (ObjCPropery) |
| 5384 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5385 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5386 | } else if (!UnknownObjCClass) { |
| 5387 | S.Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 5388 | S.Diag(D->getLocation(), |
| 5389 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5390 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5391 | if (ObjCPropery) |
| 5392 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5393 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5394 | } else { |
| 5395 | S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name; |
| 5396 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 5397 | } |
| 5398 | } |
| 5399 | |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 5400 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5401 | Decl *Ctx) { |
| 5402 | if (isDeclDeprecated(Ctx)) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5403 | return; |
| 5404 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5405 | DD.Triggered = true; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5406 | DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(), |
| 5407 | DD.getDeprecationMessage(), DD.Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5408 | DD.getUnknownObjCClass(), |
| 5409 | DD.getObjCProperty()); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5410 | } |
| 5411 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5412 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 5413 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5414 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5415 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5416 | // Delay if we're currently parsing a declaration. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5417 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5418 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 5419 | UnknownObjCClass, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5420 | ObjCProperty, |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5421 | Message)); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5422 | return; |
| 5423 | } |
| 5424 | |
| 5425 | // Otherwise, don't warn if our current context is deprecated. |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 5426 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5427 | return; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5428 | DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5429 | } |