Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1 | //===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements decl-related attribute processing. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 15 | #include "TargetAttributesSema.h" |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | acc5f3e | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | acc5f3e | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Rafael Espindola | d7a60ad | 2013-02-26 19:13:56 +0000 | [diff] [blame] | 22 | #include "clang/AST/Mangle.h" |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 23 | #include "clang/Basic/CharInfo.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 26 | #include "clang/Sema/DeclSpec.h" |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 27 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 28 | #include "clang/Sema/Lookup.h" |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 29 | #include "clang/Sema/Scope.h" |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 31 | using namespace clang; |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 32 | using namespace sema; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 33 | |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 34 | /// These constants match the enumerated choices of |
| 35 | /// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 36 | enum AttributeDeclKind { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 37 | ExpectedFunction, |
| 38 | ExpectedUnion, |
| 39 | ExpectedVariableOrFunction, |
| 40 | ExpectedFunctionOrMethod, |
| 41 | ExpectedParameter, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 42 | ExpectedFunctionMethodOrBlock, |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 43 | ExpectedFunctionMethodOrClass, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 44 | ExpectedFunctionMethodOrParameter, |
| 45 | ExpectedClass, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 46 | ExpectedVariable, |
| 47 | ExpectedMethod, |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 48 | ExpectedVariableFunctionOrLabel, |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 49 | ExpectedFieldOrGlobalVar, |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 50 | ExpectedStruct, |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 51 | ExpectedVariableFunctionOrTag, |
Richard Smith | 5f838aa | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 52 | ExpectedTLSVar, |
| 53 | ExpectedVariableOrField, |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 54 | ExpectedVariableFieldOrTag, |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 55 | ExpectedTypeOrNamespace, |
| 56 | ExpectedObjectiveCInterface |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 59 | //===----------------------------------------------------------------------===// |
| 60 | // Helper functions |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 63 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 64 | bool blocksToo = true) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 65 | QualType Ty; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 66 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 67 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 68 | else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 69 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 70 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 71 | Ty = decl->getUnderlyingType(); |
| 72 | else |
| 73 | return 0; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 75 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 76 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 755f9d2 | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 77 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 78 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 79 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 80 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 83 | // FIXME: We should provide an abstraction around a method or function |
| 84 | // to provide the following bits of information. |
| 85 | |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 86 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 87 | /// type (function or function-typed variable). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 88 | static bool isFunction(const Decl *D) { |
| 89 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 93 | /// type (function or function-typed variable) or an Objective-C |
| 94 | /// method. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 95 | static bool isFunctionOrMethod(const Decl *D) { |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 96 | return isFunction(D) || isa<ObjCMethodDecl>(D); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 99 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 100 | /// type (function or function-typed variable) or an Objective-C |
| 101 | /// method or a block. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 102 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 103 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 104 | return true; |
| 105 | // check for block is more involved. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 106 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 107 | QualType Ty = V->getType(); |
| 108 | return Ty->isBlockPointerType(); |
| 109 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 110 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 111 | } |
| 112 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 113 | /// Return true if the given decl has a declarator that should have |
| 114 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 115 | static bool hasDeclarator(const Decl *D) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 116 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 117 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 118 | isa<ObjCPropertyDecl>(D); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 121 | /// hasFunctionProto - Return true if the given decl has a argument |
| 122 | /// information. This decl should have already passed |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 123 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 124 | static bool hasFunctionProto(const Decl *D) { |
| 125 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 126 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 127 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 128 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 129 | return true; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 134 | /// arguments. It is an error to call this on a K&R function (use |
| 135 | /// hasFunctionProto first). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 136 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 137 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 138 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 139 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 140 | return BD->getNumParams(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 141 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 144 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 145 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 146 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 147 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 148 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 149 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 150 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 153 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 154 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 155 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 156 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 159 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 160 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 161 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 162 | return proto->isVariadic(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 163 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | db9a0ae | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 164 | return BD->isVariadic(); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 165 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 166 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 170 | static bool isInstanceMethod(const Decl *D) { |
| 171 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 172 | return MethodDecl->isInstance(); |
| 173 | return false; |
| 174 | } |
| 175 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 176 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 177 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 178 | if (!PT) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 179 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 180 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 181 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 182 | if (!Cls) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 183 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 184 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 185 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 187 | // FIXME: Should we walk the chain of classes? |
| 188 | return ClsName == &Ctx.Idents.get("NSString") || |
| 189 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 190 | } |
| 191 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 192 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 193 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 194 | if (!PT) |
| 195 | return false; |
| 196 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 197 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 198 | if (!RT) |
| 199 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 200 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 201 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 202 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 203 | return false; |
| 204 | |
| 205 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 206 | } |
| 207 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 208 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 209 | /// output an error. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 210 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 211 | unsigned int Num) { |
| 212 | if (Attr.getNumArgs() != Num) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 213 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 214 | << Attr.getName() << Num; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 215 | return false; |
| 216 | } |
| 217 | |
| 218 | return true; |
| 219 | } |
| 220 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 221 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 222 | /// \brief Check if the attribute has at least as many args as Num. May |
| 223 | /// output an error. |
| 224 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
| 225 | unsigned int Num) { |
| 226 | if (Attr.getNumArgs() < Num) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 227 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| 228 | return false; |
| 229 | } |
| 230 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 231 | return true; |
| 232 | } |
| 233 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 234 | /// \brief Check if IdxExpr is a valid argument index for a function or |
| 235 | /// instance method D. May output an error. |
| 236 | /// |
| 237 | /// \returns true if IdxExpr is a valid index. |
| 238 | static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D, |
| 239 | StringRef AttrName, |
| 240 | SourceLocation AttrLoc, |
| 241 | unsigned AttrArgNum, |
| 242 | const Expr *IdxExpr, |
| 243 | uint64_t &Idx) |
| 244 | { |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 245 | assert(isFunctionOrMethod(D)); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 246 | |
| 247 | // In C++ the implicit 'this' function parameter also counts. |
| 248 | // Parameters are counted from one. |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 249 | bool HP = hasFunctionProto(D); |
| 250 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 251 | bool IV = HP && isFunctionOrMethodVariadic(D); |
| 252 | unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) + |
| 253 | HasImplicitThisParam; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 254 | |
| 255 | llvm::APSInt IdxInt; |
| 256 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 257 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 258 | std::string Name = std::string("'") + AttrName.str() + std::string("'"); |
| 259 | S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str() |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 260 | << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | |
| 264 | Idx = IdxInt.getLimitedValue(); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 265 | if (Idx < 1 || (!IV && Idx > NumArgs)) { |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 266 | S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds) |
| 267 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 268 | return false; |
| 269 | } |
| 270 | Idx--; // Convert to zero-based. |
| 271 | if (HasImplicitThisParam) { |
| 272 | if (Idx == 0) { |
| 273 | S.Diag(AttrLoc, |
| 274 | diag::err_attribute_invalid_implicit_this_argument) |
| 275 | << AttrName << IdxExpr->getSourceRange(); |
| 276 | return false; |
| 277 | } |
| 278 | --Idx; |
| 279 | } |
| 280 | |
| 281 | return true; |
| 282 | } |
| 283 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 284 | /// |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 285 | /// \brief Check if passed in Decl is a field or potentially shared global var |
| 286 | /// \return true if the Decl is a field or potentially shared global variable |
| 287 | /// |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 288 | static bool mayBeSharedVariable(const Decl *D) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 289 | if (isa<FieldDecl>(D)) |
| 290 | return true; |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 291 | if (const VarDecl *vd = dyn_cast<VarDecl>(D)) |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 292 | return vd->hasGlobalStorage() && !vd->getTLSKind(); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 293 | |
| 294 | return false; |
| 295 | } |
| 296 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 297 | /// \brief Check if the passed-in expression is of type int or bool. |
| 298 | static bool isIntOrBool(Expr *Exp) { |
| 299 | QualType QT = Exp->getType(); |
| 300 | return QT->isBooleanType() || QT->isIntegerType(); |
| 301 | } |
| 302 | |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 303 | |
| 304 | // Check to see if the type is a smart pointer of some kind. We assume |
| 305 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 306 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 307 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 308 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 309 | if (Res1.empty()) |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 310 | return false; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 311 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 312 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 313 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 314 | if (Res2.empty()) |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 315 | return false; |
| 316 | |
| 317 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 320 | /// \brief Check if passed in Decl is a pointer type. |
| 321 | /// Note that this function may produce an error message. |
| 322 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 323 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 324 | const AttributeList &Attr) { |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 325 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 326 | QualType QT = vd->getType(); |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 327 | if (QT->isAnyPointerType()) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 328 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 329 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 330 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 331 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 332 | // (We don't want to force template instantiation if we can avoid it, |
| 333 | // since that would alter the order in which templates are instantiated.) |
| 334 | if (RT->isIncompleteType()) |
| 335 | return true; |
| 336 | |
| 337 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 338 | return true; |
| 339 | } |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 340 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 341 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 342 | << Attr.getName()->getName() << QT; |
| 343 | } else { |
| 344 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 345 | << Attr.getName(); |
| 346 | } |
| 347 | return false; |
| 348 | } |
| 349 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 350 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 351 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 352 | static const RecordType *getRecordType(QualType QT) { |
| 353 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 354 | return RT; |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 355 | |
| 356 | // Now check if we point to record type. |
| 357 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 358 | return PT->getPointeeType()->getAs<RecordType>(); |
| 359 | |
| 360 | return 0; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 361 | } |
| 362 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 363 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 364 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 365 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 366 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
| 367 | if (RT->getDecl()->getAttr<LockableAttr>()) |
| 368 | return true; |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 373 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 374 | /// resolves to a lockable object. |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 375 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 376 | QualType Ty) { |
| 377 | const RecordType *RT = getRecordType(Ty); |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 378 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 379 | // Warn if could not get record type for this argument. |
Benjamin Kramer | d77ba89 | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 380 | if (!RT) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 381 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 382 | << Attr.getName() << Ty.getAsString(); |
| 383 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 384 | } |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 385 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 386 | // Don't check for lockable if the class hasn't been defined yet. |
DeLesley Hutchins | 634b293 | 2012-02-16 17:15:51 +0000 | [diff] [blame] | 387 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 388 | return; |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 389 | |
| 390 | // Allow smart pointers to be used as lockable objects. |
| 391 | // FIXME -- Check the type that the smart pointer points to. |
| 392 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 393 | return; |
| 394 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 395 | // Check if the type is lockable. |
| 396 | RecordDecl *RD = RT->getDecl(); |
| 397 | if (RD->getAttr<LockableAttr>()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 398 | return; |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 399 | |
| 400 | // Else check if any base classes are lockable. |
| 401 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 402 | CXXBasePaths BPaths(false, false); |
| 403 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 404 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 405 | } |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 406 | |
| 407 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 408 | << Attr.getName() << Ty.getAsString(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 411 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 412 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 413 | /// \param Sidx The attribute argument index to start checking with. |
| 414 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 415 | /// parameter list. |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 416 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 417 | const AttributeList &Attr, |
| 418 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 419 | int Sidx = 0, |
| 420 | bool ParamIdxOk = false) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 421 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 422 | Expr *ArgExp = Attr.getArg(Idx); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 423 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 424 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 425 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 426 | Args.push_back(ArgExp); |
| 427 | continue; |
| 428 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 429 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 430 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 431 | if (StrLit->getLength() == 0 || |
| 432 | StrLit->getString() == StringRef("*")) { |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 433 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 434 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 435 | Args.push_back(ArgExp); |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 436 | continue; |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 437 | } |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 438 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 439 | // We allow constant strings to be used as a placeholder for expressions |
| 440 | // that are not valid C++ syntax, but warn that they are ignored. |
| 441 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 442 | Attr.getName(); |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 443 | Args.push_back(ArgExp); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 444 | continue; |
| 445 | } |
| 446 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 447 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 448 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 449 | // A pointer to member expression of the form &MyClass::mu is treated |
| 450 | // specially -- we need to look at the type of the member. |
| 451 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 452 | if (UOp->getOpcode() == UO_AddrOf) |
| 453 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 454 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 455 | ArgTy = DRE->getDecl()->getType(); |
| 456 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 457 | // First see if we can just cast to record type, or point to record type. |
| 458 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 459 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 460 | // Now check if we index into a record type function param. |
| 461 | if(!RT && ParamIdxOk) { |
| 462 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 463 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 464 | if(FD && IL) { |
| 465 | unsigned int NumParams = FD->getNumParams(); |
| 466 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 467 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 468 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 469 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 470 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 471 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 472 | continue; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 473 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 474 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 478 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 479 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 480 | Args.push_back(ArgExp); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 481 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 484 | //===----------------------------------------------------------------------===// |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 485 | // Attribute Implementations |
| 486 | //===----------------------------------------------------------------------===// |
| 487 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 488 | // FIXME: All this manual attribute parsing code is gross. At the |
| 489 | // least add some helper functions to check most argument patterns (# |
| 490 | // and types of args). |
| 491 | |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 492 | enum ThreadAttributeDeclKind { |
| 493 | ThreadExpectedFieldOrGlobalVar, |
| 494 | ThreadExpectedFunctionOrMethod, |
| 495 | ThreadExpectedClassOrStruct |
| 496 | }; |
| 497 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 498 | static bool checkGuardedVarAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 499 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 500 | assert(!Attr.isInvalid()); |
| 501 | |
| 502 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 503 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 504 | |
| 505 | // D must be either a member field or global (potentially shared) variable. |
| 506 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 507 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 508 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 509 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 512 | return true; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 515 | static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 516 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 517 | return; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 518 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 519 | D->addAttr(::new (S.Context) |
| 520 | GuardedVarAttr(Attr.getRange(), S.Context, |
| 521 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 524 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 525 | const AttributeList &Attr) { |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 526 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 527 | return; |
| 528 | |
| 529 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 530 | return; |
| 531 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 532 | D->addAttr(::new (S.Context) |
| 533 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 534 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 537 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 538 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 539 | Expr* &Arg) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 540 | assert(!Attr.isInvalid()); |
| 541 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 542 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 543 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 544 | |
| 545 | // D must be either a member field or global (potentially shared) variable. |
| 546 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 547 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 548 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 549 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 550 | } |
| 551 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 552 | SmallVector<Expr*, 1> Args; |
| 553 | // check that all arguments are lockable objects |
| 554 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 555 | unsigned Size = Args.size(); |
| 556 | if (Size != 1) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 557 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 558 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 559 | Arg = Args[0]; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 560 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 561 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 564 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 565 | Expr *Arg = 0; |
| 566 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 567 | return; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 568 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 569 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| 570 | } |
| 571 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 572 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 573 | const AttributeList &Attr) { |
| 574 | Expr *Arg = 0; |
| 575 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 576 | return; |
| 577 | |
| 578 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 579 | return; |
| 580 | |
| 581 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| 582 | S.Context, Arg)); |
| 583 | } |
| 584 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 585 | static bool checkLockableAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 586 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 587 | assert(!Attr.isInvalid()); |
| 588 | |
| 589 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 590 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 591 | |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 592 | // FIXME: Lockable structs for C code. |
David Blaikie | 88c4b5e | 2013-07-29 18:24:03 +0000 | [diff] [blame] | 593 | if (!isa<RecordDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 594 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 595 | << Attr.getName() << ThreadExpectedClassOrStruct; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 596 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 599 | return true; |
| 600 | } |
| 601 | |
| 602 | static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 603 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 604 | return; |
| 605 | |
| 606 | D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); |
| 607 | } |
| 608 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 609 | static void handleScopedLockableAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 610 | const AttributeList &Attr) { |
| 611 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 612 | return; |
| 613 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 614 | D->addAttr(::new (S.Context) |
| 615 | ScopedLockableAttr(Attr.getRange(), S.Context, |
| 616 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 619 | static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D, |
| 620 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 621 | assert(!Attr.isInvalid()); |
| 622 | |
| 623 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 624 | return; |
| 625 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 626 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 627 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 628 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 629 | return; |
| 630 | } |
| 631 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 632 | D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(), |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 633 | S.Context)); |
| 634 | } |
| 635 | |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 636 | static void handleNoSanitizeAddressAttr(Sema &S, Decl *D, |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 637 | const AttributeList &Attr) { |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 638 | assert(!Attr.isInvalid()); |
| 639 | |
| 640 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 641 | return; |
| 642 | |
| 643 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 644 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 645 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 646 | return; |
| 647 | } |
| 648 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 649 | D->addAttr(::new (S.Context) |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 650 | NoSanitizeAddressAttr(Attr.getRange(), S.Context, |
| 651 | Attr.getAttributeSpellingListIndex())); |
| 652 | } |
| 653 | |
| 654 | static void handleNoSanitizeMemory(Sema &S, Decl *D, |
| 655 | const AttributeList &Attr) { |
| 656 | assert(!Attr.isInvalid()); |
| 657 | |
| 658 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 659 | return; |
| 660 | |
| 661 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 662 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 663 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 664 | return; |
| 665 | } |
| 666 | |
| 667 | D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(), |
| 668 | S.Context)); |
| 669 | } |
| 670 | |
| 671 | static void handleNoSanitizeThread(Sema &S, Decl *D, |
| 672 | const AttributeList &Attr) { |
| 673 | assert(!Attr.isInvalid()); |
| 674 | |
| 675 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 676 | return; |
| 677 | |
| 678 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 679 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 680 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 681 | return; |
| 682 | } |
| 683 | |
| 684 | D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(), |
| 685 | S.Context)); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 688 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 689 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 690 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 691 | assert(!Attr.isInvalid()); |
| 692 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 693 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 694 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 695 | |
| 696 | // D must be either a member field or global (potentially shared) variable. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 697 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 698 | if (!VD || !mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 699 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 700 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 701 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 702 | } |
| 703 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 704 | // Check that this attribute only applies to lockable types. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 705 | QualType QT = VD->getType(); |
| 706 | if (!QT->isDependentType()) { |
| 707 | const RecordType *RT = getRecordType(QT); |
| 708 | if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 709 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 710 | << Attr.getName(); |
| 711 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 715 | // Check that all arguments are lockable objects. |
| 716 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 717 | if (Args.empty()) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 718 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 719 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 720 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 723 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 724 | const AttributeList &Attr) { |
| 725 | SmallVector<Expr*, 1> Args; |
| 726 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 727 | return; |
| 728 | |
| 729 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 730 | D->addAttr(::new (S.Context) |
| 731 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 732 | StartArg, Args.size(), |
| 733 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 736 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 737 | const AttributeList &Attr) { |
| 738 | SmallVector<Expr*, 1> Args; |
| 739 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 740 | return; |
| 741 | |
| 742 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 743 | D->addAttr(::new (S.Context) |
| 744 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 745 | StartArg, Args.size(), |
| 746 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 749 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 750 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 751 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 752 | assert(!Attr.isInvalid()); |
| 753 | |
| 754 | // zero or more arguments ok |
| 755 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 756 | // check that the attribute is applied to a function |
| 757 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 758 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 759 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 760 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 763 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 764 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 765 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 766 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 769 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 770 | const AttributeList &Attr) { |
| 771 | SmallVector<Expr*, 1> Args; |
| 772 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 773 | return; |
| 774 | |
| 775 | unsigned Size = Args.size(); |
| 776 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 777 | D->addAttr(::new (S.Context) |
| 778 | SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 779 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 782 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 783 | const AttributeList &Attr) { |
| 784 | SmallVector<Expr*, 1> Args; |
| 785 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 786 | return; |
| 787 | |
| 788 | unsigned Size = Args.size(); |
| 789 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 790 | D->addAttr(::new (S.Context) |
| 791 | ExclusiveLockFunctionAttr(Attr.getRange(), S.Context, |
| 792 | StartArg, Size, |
| 793 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 794 | } |
| 795 | |
DeLesley Hutchins | 5c6134f | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 796 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 797 | const AttributeList &Attr) { |
| 798 | SmallVector<Expr*, 1> Args; |
| 799 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 800 | return; |
| 801 | |
| 802 | unsigned Size = Args.size(); |
| 803 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 804 | D->addAttr(::new (S.Context) |
| 805 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 806 | Attr.getAttributeSpellingListIndex())); |
| 807 | } |
| 808 | |
| 809 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 810 | const AttributeList &Attr) { |
| 811 | SmallVector<Expr*, 1> Args; |
| 812 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 813 | return; |
| 814 | |
| 815 | unsigned Size = Args.size(); |
| 816 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 817 | D->addAttr(::new (S.Context) |
| 818 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 819 | StartArg, Size, |
| 820 | Attr.getAttributeSpellingListIndex())); |
| 821 | } |
| 822 | |
| 823 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 824 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 825 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 826 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 827 | assert(!Attr.isInvalid()); |
| 828 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 829 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 830 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 831 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 832 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 833 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 834 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 835 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 838 | if (!isIntOrBool(Attr.getArg(0))) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 839 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 840 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 841 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 845 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 846 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 847 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 850 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 851 | const AttributeList &Attr) { |
| 852 | SmallVector<Expr*, 2> Args; |
| 853 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 854 | return; |
| 855 | |
| 856 | unsigned Size = Args.size(); |
| 857 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 858 | D->addAttr(::new (S.Context) |
| 859 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
| 860 | Attr.getArg(0), StartArg, Size, |
| 861 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 864 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 865 | const AttributeList &Attr) { |
| 866 | SmallVector<Expr*, 2> Args; |
| 867 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 868 | return; |
| 869 | |
| 870 | unsigned Size = Args.size(); |
| 871 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 872 | D->addAttr(::new (S.Context) |
| 873 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
| 874 | Attr.getArg(0), StartArg, Size, |
| 875 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 878 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 879 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 880 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 881 | assert(!Attr.isInvalid()); |
| 882 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 883 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 884 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 885 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 886 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 887 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 888 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 889 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 892 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 893 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 894 | if (Args.empty()) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 895 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 896 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 897 | return true; |
| 898 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 899 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 900 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 901 | const AttributeList &Attr) { |
| 902 | SmallVector<Expr*, 1> Args; |
| 903 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 904 | return; |
| 905 | |
| 906 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 907 | D->addAttr(::new (S.Context) |
| 908 | ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context, |
| 909 | StartArg, Args.size(), |
| 910 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 913 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 914 | const AttributeList &Attr) { |
| 915 | SmallVector<Expr*, 1> Args; |
| 916 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 917 | return; |
| 918 | |
| 919 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 920 | D->addAttr(::new (S.Context) |
| 921 | SharedLocksRequiredAttr(Attr.getRange(), S.Context, |
| 922 | StartArg, Args.size(), |
| 923 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 926 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 927 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 928 | assert(!Attr.isInvalid()); |
| 929 | |
| 930 | // zero or more arguments ok |
| 931 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 932 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 933 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 934 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 935 | return; |
| 936 | } |
| 937 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 938 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 939 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 940 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 941 | unsigned Size = Args.size(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 942 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 943 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 944 | D->addAttr(::new (S.Context) |
| 945 | UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 946 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 950 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 951 | assert(!Attr.isInvalid()); |
| 952 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 953 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 954 | return; |
| 955 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 956 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 957 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 958 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 959 | return; |
| 960 | } |
| 961 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 962 | // check that the argument is lockable object |
DeLesley Hutchins | f26efd7 | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 963 | SmallVector<Expr*, 1> Args; |
| 964 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 965 | unsigned Size = Args.size(); |
| 966 | if (Size == 0) |
| 967 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 968 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 969 | D->addAttr(::new (S.Context) |
| 970 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 971 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 975 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 976 | assert(!Attr.isInvalid()); |
| 977 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 978 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 979 | return; |
| 980 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 981 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 982 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 983 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 984 | return; |
| 985 | } |
| 986 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 987 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 988 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 989 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 990 | unsigned Size = Args.size(); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 991 | if (Size == 0) |
| 992 | return; |
| 993 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 994 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 995 | D->addAttr(::new (S.Context) |
| 996 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 997 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1001 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1002 | const AttributeList &Attr) { |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1003 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 1004 | if (TD == 0) { |
| 1005 | // __attribute__((ext_vector_type(N))) can only be applied to typedefs |
| 1006 | // and type-ids. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1007 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1008 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1009 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1010 | |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1011 | // Remember this typedef decl, we will need it later for diagnostics. |
| 1012 | S.ExtVectorDecls.push_back(TD); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1015 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1016 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1017 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1018 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1019 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1020 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1021 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1022 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1023 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1024 | // has no effect. |
Eli Friedman | b68ec6b | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1025 | if (!FD->getType()->isDependentType() && |
| 1026 | !FD->getType()->isIncompleteType() && |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1027 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1028 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1029 | << Attr.getName() << FD->getType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1030 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1031 | FD->addAttr(::new (S.Context) |
| 1032 | PackedAttr(Attr.getRange(), S.Context, |
| 1033 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1034 | } else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1035 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1038 | static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 5f608ae | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 1039 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1040 | RD->addAttr(::new (S.Context) |
| 1041 | MsStructAttr(Attr.getRange(), S.Context, |
| 1042 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | c1a0a73 | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 1043 | else |
| 1044 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1045 | } |
| 1046 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1047 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1048 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1049 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1050 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1051 | |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1052 | // The IBAction attributes only apply to instance methods. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1053 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1054 | if (MD->isInstanceMethod()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1055 | D->addAttr(::new (S.Context) |
| 1056 | IBActionAttr(Attr.getRange(), S.Context, |
| 1057 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1058 | return; |
| 1059 | } |
| 1060 | |
Ted Kremenek | 4ee2bb1 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 1061 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1064 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1065 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1066 | // variables or properties of Objective-C classes. The outlet must also |
| 1067 | // have an object reference type. |
| 1068 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1069 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 0bfaf06 | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1070 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1071 | << Attr.getName() << VD->getType() << 0; |
| 1072 | return false; |
| 1073 | } |
| 1074 | } |
| 1075 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1076 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1077 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1078 | << Attr.getName() << PD->getType() << 1; |
| 1079 | return false; |
| 1080 | } |
| 1081 | } |
| 1082 | else { |
| 1083 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1084 | return false; |
| 1085 | } |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1086 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1087 | return true; |
| 1088 | } |
| 1089 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1090 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1091 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1092 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1093 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1094 | |
| 1095 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1096 | return; |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1097 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1098 | D->addAttr(::new (S.Context) |
| 1099 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1100 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1103 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1104 | const AttributeList &Attr) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1105 | |
| 1106 | // The iboutletcollection attribute can have zero or one arguments. |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1107 | if (Attr.getParameterName() && Attr.getNumArgs() > 0) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1108 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1109 | << Attr.getName() << 1; |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1110 | return; |
| 1111 | } |
| 1112 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1113 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1114 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1115 | |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1116 | IdentifierInfo *II = Attr.getParameterName(); |
| 1117 | if (!II) |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1118 | II = &S.Context.Idents.get("NSObject"); |
Fariborz Jahanian | 3a3400b | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 1119 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1120 | ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1121 | S.getScopeForContext(D->getDeclContext()->getParent())); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1122 | if (!TypeRep) { |
| 1123 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1124 | return; |
| 1125 | } |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1126 | QualType QT = TypeRep.get(); |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1127 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1128 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1129 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1130 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1131 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1132 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1133 | return; |
| 1134 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1135 | D->addAttr(::new (S.Context) |
| 1136 | IBOutletCollectionAttr(Attr.getRange(),S.Context, |
| 1137 | QT, Attr.getParameterLoc(), |
| 1138 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1141 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1142 | if (const RecordType *UT = T->getAsUnionType()) |
| 1143 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1144 | RecordDecl *UD = UT->getDecl(); |
| 1145 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1146 | itend = UD->field_end(); it != itend; ++it) { |
| 1147 | QualType QT = it->getType(); |
| 1148 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1149 | T = QT; |
| 1150 | return; |
| 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | } |
| 1155 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1156 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nuno Lopes | 174930d | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1157 | if (!isFunctionOrMethod(D)) { |
| 1158 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1159 | << Attr.getName() << ExpectedFunctionOrMethod; |
Nuno Lopes | 174930d | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1160 | return; |
| 1161 | } |
| 1162 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1163 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1164 | return; |
| 1165 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1166 | SmallVector<unsigned, 8> SizeArgs; |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1167 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
| 1168 | Expr *Ex = Attr.getArg(i); |
| 1169 | uint64_t Idx; |
| 1170 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1171 | Attr.getLoc(), i + 1, Ex, Idx)) |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1172 | return; |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1173 | |
| 1174 | // check if the function argument is of an integer type |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1175 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1176 | if (!T->isIntegerType()) { |
| 1177 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1178 | << "alloc_size" << Ex->getSourceRange(); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1179 | return; |
| 1180 | } |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1181 | SizeArgs.push_back(Idx); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | // check if the function returns a pointer |
| 1185 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 1186 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1187 | << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1190 | D->addAttr(::new (S.Context) |
| 1191 | AllocSizeAttr(Attr.getRange(), S.Context, |
| 1192 | SizeArgs.data(), SizeArgs.size(), |
| 1193 | Attr.getAttributeSpellingListIndex())); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1196 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1197 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1198 | // ignore it as well |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1199 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1200 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1201 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1202 | return; |
| 1203 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1204 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1205 | SmallVector<unsigned, 8> NonNullArgs; |
| 1206 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
| 1207 | Expr *Ex = Attr.getArg(i); |
| 1208 | uint64_t Idx; |
| 1209 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1210 | Attr.getLoc(), i + 1, Ex, Idx)) |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1211 | return; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1212 | |
| 1213 | // Is the function argument a pointer type? |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1214 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1215 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1216 | |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1217 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1218 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | c9ef405 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1219 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1220 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1221 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1222 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1223 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1224 | NonNullArgs.push_back(Idx); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1225 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1226 | |
| 1227 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1228 | // arguments have a nonnull attribute. |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1229 | if (NonNullArgs.empty()) { |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1230 | for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) { |
| 1231 | QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1232 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1233 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1234 | NonNullArgs.push_back(i); |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1235 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1236 | |
Ted Kremenek | ee1c08c | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1237 | // No pointer arguments? |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1238 | if (NonNullArgs.empty()) { |
| 1239 | // Warn the trivial case only if attribute is not coming from a |
| 1240 | // macro instantiation. |
| 1241 | if (Attr.getLoc().isFileID()) |
| 1242 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1243 | return; |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1244 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1245 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1246 | |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1247 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1248 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1249 | llvm::array_pod_sort(start, start + size); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1250 | D->addAttr(::new (S.Context) |
| 1251 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1252 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1255 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1256 | // This attribute must be applied to a function declaration. |
| 1257 | // The first argument to the attribute must be a string, |
| 1258 | // the name of the resource, for example "malloc". |
| 1259 | // The following arguments must be argument indexes, the arguments must be |
| 1260 | // of integer type for Returns, otherwise of pointer type. |
| 1261 | // 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] | 1262 | // after being held. free() should be __attribute((ownership_takes)), whereas |
| 1263 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1264 | |
| 1265 | if (!AL.getParameterName()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1266 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 1267 | << AL.getName()->getName() << 1 << AANT_ArgumentString; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1268 | return; |
| 1269 | } |
| 1270 | // Figure out our Kind, and check arguments while we're at it. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1271 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1272 | switch (AL.getKind()) { |
| 1273 | case AttributeList::AT_ownership_takes: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1274 | K = OwnershipAttr::Takes; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1275 | if (AL.getNumArgs() < 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1276 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1277 | << AL.getName() << 2; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1278 | return; |
| 1279 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1280 | break; |
| 1281 | case AttributeList::AT_ownership_holds: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1282 | K = OwnershipAttr::Holds; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1283 | if (AL.getNumArgs() < 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1284 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1285 | << AL.getName() << 2; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1286 | return; |
| 1287 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1288 | break; |
| 1289 | case AttributeList::AT_ownership_returns: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1290 | K = OwnershipAttr::Returns; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1291 | if (AL.getNumArgs() > 1) { |
| 1292 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1293 | << AL.getName() << AL.getNumArgs() + 1; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1294 | return; |
| 1295 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1296 | break; |
| 1297 | default: |
| 1298 | // This should never happen given how we are called. |
| 1299 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1302 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1303 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1304 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1305 | return; |
| 1306 | } |
| 1307 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1308 | StringRef Module = AL.getParameterName()->getName(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1309 | |
| 1310 | // Normalize the argument, __foo__ becomes foo. |
| 1311 | if (Module.startswith("__") && Module.endswith("__")) |
| 1312 | Module = Module.substr(2, Module.size() - 4); |
| 1313 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1314 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1315 | SmallVector<unsigned, 8> OwnershipArgs; |
| 1316 | for (unsigned i = 0; i < AL.getNumArgs(); ++i) { |
| 1317 | Expr *Ex = AL.getArg(i); |
| 1318 | uint64_t Idx; |
| 1319 | if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(), |
| 1320 | AL.getLoc(), i + 1, Ex, Idx)) |
| 1321 | return; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1322 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1323 | switch (K) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1324 | case OwnershipAttr::Takes: |
| 1325 | case OwnershipAttr::Holds: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1326 | // Is the function argument a pointer type? |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1327 | QualType T = getFunctionOrMethodArgType(D, Idx); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1328 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| 1329 | // FIXME: Should also highlight argument in decl. |
| 1330 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1331 | << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1332 | << "pointer" |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1333 | << Ex->getSourceRange(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1334 | continue; |
| 1335 | } |
| 1336 | break; |
| 1337 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1338 | case OwnershipAttr::Returns: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1339 | if (AL.getNumArgs() > 1) { |
| 1340 | // Is the function argument an integer type? |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1341 | Expr *IdxExpr = AL.getArg(0); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1342 | llvm::APSInt ArgNum(32); |
| 1343 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1344 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1345 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
| 1346 | << "ownership_returns" << "integer" |
| 1347 | << IdxExpr->getSourceRange(); |
| 1348 | return; |
| 1349 | } |
| 1350 | } |
| 1351 | break; |
| 1352 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1353 | } // switch |
| 1354 | |
| 1355 | // Check we don't have a conflict with another ownership attribute. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1356 | for (specific_attr_iterator<OwnershipAttr> |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1357 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1358 | e = D->specific_attr_end<OwnershipAttr>(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1359 | i != e; ++i) { |
| 1360 | if ((*i)->getOwnKind() != K) { |
| 1361 | for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); |
| 1362 | I!=E; ++I) { |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1363 | if (Idx == *I) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1364 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1365 | << AL.getName()->getName() << "ownership_*"; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1366 | } |
| 1367 | } |
| 1368 | } |
| 1369 | } |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1370 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | unsigned* start = OwnershipArgs.data(); |
| 1374 | unsigned size = OwnershipArgs.size(); |
| 1375 | llvm::array_pod_sort(start, start + size); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1376 | |
| 1377 | if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1378 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1379 | << AL.getName() << 2; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1380 | return; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1381 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1382 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1383 | D->addAttr(::new (S.Context) |
| 1384 | OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size, |
| 1385 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1388 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1389 | // Check the attribute arguments. |
| 1390 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1391 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1392 | << Attr.getName() << 1; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1393 | return; |
| 1394 | } |
| 1395 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1396 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1397 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1398 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1399 | return; |
| 1400 | } |
| 1401 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1402 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1403 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1404 | // gcc rejects |
| 1405 | // class c { |
| 1406 | // static int a __attribute__((weakref ("v2"))); |
| 1407 | // static int b() __attribute__((weakref ("f3"))); |
| 1408 | // }; |
| 1409 | // and ignores the attributes of |
| 1410 | // void f(void) { |
| 1411 | // static int a __attribute__((weakref ("v2"))); |
| 1412 | // } |
| 1413 | // we reject them |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1414 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1415 | if (!Ctx->isFileContext()) { |
| 1416 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1417 | nd->getNameAsString(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1418 | return; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | // The GCC manual says |
| 1422 | // |
| 1423 | // At present, a declaration to which `weakref' is attached can only |
| 1424 | // be `static'. |
| 1425 | // |
| 1426 | // It also says |
| 1427 | // |
| 1428 | // Without a TARGET, |
| 1429 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1430 | // equivalent to `weak'. |
| 1431 | // |
| 1432 | // gcc 4.4.1 will accept |
| 1433 | // int a7 __attribute__((weakref)); |
| 1434 | // as |
| 1435 | // int a7 __attribute__((weak)); |
| 1436 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1437 | // it if this behaviour is actually used. |
| 1438 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1439 | // GCC rejects |
| 1440 | // static ((alias ("y"), weakref)). |
| 1441 | // Should we? How to check that weakref is before or after alias? |
| 1442 | |
| 1443 | if (Attr.getNumArgs() == 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1444 | Expr *Arg = Attr.getArg(0); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1445 | Arg = Arg->IgnoreParenCasts(); |
| 1446 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1447 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1448 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1449 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 1450 | << Attr.getName() << 1 << AANT_ArgumentString; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1451 | return; |
| 1452 | } |
| 1453 | // GCC will accept anything as the argument of weakref. Should we |
| 1454 | // check for an existing decl? |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1455 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1456 | Str->getString())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1459 | D->addAttr(::new (S.Context) |
| 1460 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1461 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1464 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1465 | // check the attribute arguments. |
Aaron Ballman | ffa9d57 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 1466 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1467 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1468 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1469 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1470 | Arg = Arg->IgnoreParenCasts(); |
| 1471 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1472 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1473 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 1474 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1475 | << Attr.getName() << AANT_ArgumentString; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1476 | return; |
| 1477 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1478 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1479 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | f5fe292 | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1480 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1481 | return; |
| 1482 | } |
| 1483 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1484 | // FIXME: check if target symbol exists in current file |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1485 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1486 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1487 | Str->getString(), |
| 1488 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1491 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1492 | // Check the attribute arguments. |
| 1493 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1494 | return; |
| 1495 | |
| 1496 | if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) { |
| 1497 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1498 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1499 | return; |
| 1500 | } |
| 1501 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1502 | D->addAttr(::new (S.Context) |
| 1503 | MinSizeAttr(Attr.getRange(), S.Context, |
| 1504 | Attr.getAttributeSpellingListIndex())); |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1507 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1508 | // Check the attribute arguments. |
| 1509 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1510 | return; |
| 1511 | |
| 1512 | if (!isa<FunctionDecl>(D)) { |
| 1513 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1514 | << Attr.getName() << ExpectedFunction; |
| 1515 | return; |
| 1516 | } |
| 1517 | |
| 1518 | if (D->hasAttr<HotAttr>()) { |
| 1519 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1520 | << Attr.getName() << "hot"; |
| 1521 | return; |
| 1522 | } |
| 1523 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1524 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1525 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1529 | // Check the attribute arguments. |
| 1530 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1531 | return; |
| 1532 | |
| 1533 | if (!isa<FunctionDecl>(D)) { |
| 1534 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1535 | << Attr.getName() << ExpectedFunction; |
| 1536 | return; |
| 1537 | } |
| 1538 | |
| 1539 | if (D->hasAttr<ColdAttr>()) { |
| 1540 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1541 | << Attr.getName() << "cold"; |
| 1542 | return; |
| 1543 | } |
| 1544 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1545 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1546 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1549 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1550 | // Check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1551 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1552 | return; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1553 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1554 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1555 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1556 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1557 | return; |
| 1558 | } |
| 1559 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1560 | D->addAttr(::new (S.Context) |
| 1561 | NakedAttr(Attr.getRange(), S.Context, |
| 1562 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1565 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 1566 | const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1567 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1568 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1569 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1570 | << Attr.getName() << 0; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1571 | return; |
| 1572 | } |
| 1573 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1574 | if (!isa<FunctionDecl>(D)) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1575 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1576 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1577 | return; |
| 1578 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1579 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1580 | D->addAttr(::new (S.Context) |
| 1581 | AlwaysInlineAttr(Attr.getRange(), S.Context, |
| 1582 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1585 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1586 | const AttributeList &Attr) { |
| 1587 | // Check the attribute arguments. |
Aaron Ballman | ffa9d57 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 1588 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1589 | return; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1590 | |
| 1591 | Expr *Arg = Attr.getArg(0); |
| 1592 | Arg = Arg->IgnoreParenCasts(); |
| 1593 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1594 | |
| 1595 | // Check that it is a string. |
| 1596 | if (!Str) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 1597 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1598 | << Attr.getName() << AANT_ArgumentString; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1599 | return; |
| 1600 | } |
| 1601 | |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 1602 | if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) { |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1603 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1604 | << Attr.getName() << ExpectedTLSVar; |
| 1605 | return; |
| 1606 | } |
| 1607 | |
| 1608 | // Check that the value. |
| 1609 | StringRef Model = Str->getString(); |
| 1610 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1611 | && Model != "initial-exec" && Model != "local-exec") { |
| 1612 | S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg); |
| 1613 | return; |
| 1614 | } |
| 1615 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1616 | D->addAttr(::new (S.Context) |
| 1617 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1618 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1621 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1622 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1623 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1624 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1625 | << Attr.getName() << 0; |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1626 | return; |
| 1627 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1628 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1629 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1630 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1631 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1632 | D->addAttr(::new (S.Context) |
| 1633 | MallocAttr(Attr.getRange(), S.Context, |
| 1634 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1635 | return; |
| 1636 | } |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1639 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1642 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1643 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1644 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1645 | return; |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1646 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1647 | D->addAttr(::new (S.Context) |
| 1648 | MayAliasAttr(Attr.getRange(), S.Context, |
| 1649 | Attr.getAttributeSpellingListIndex())); |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1652 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1653 | assert(!Attr.isInvalid()); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1654 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1655 | D->addAttr(::new (S.Context) |
| 1656 | NoCommonAttr(Attr.getRange(), S.Context, |
| 1657 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1658 | else |
| 1659 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1660 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1663 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1664 | assert(!Attr.isInvalid()); |
Eli Friedman | 3e1aca2 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1665 | |
| 1666 | if (S.LangOpts.CPlusPlus) { |
| 1667 | S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus); |
| 1668 | return; |
| 1669 | } |
| 1670 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1671 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1672 | D->addAttr(::new (S.Context) |
| 1673 | CommonAttr(Attr.getRange(), S.Context, |
| 1674 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1675 | else |
| 1676 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1677 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1680 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1681 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1682 | |
| 1683 | if (S.CheckNoReturnAttr(attr)) return; |
| 1684 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1685 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1686 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1687 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1688 | return; |
| 1689 | } |
| 1690 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1691 | D->addAttr(::new (S.Context) |
| 1692 | NoReturnAttr(attr.getRange(), S.Context, |
| 1693 | attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1697 | if (attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1698 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1699 | << attr.getName() << 0; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1700 | attr.setInvalid(); |
| 1701 | return true; |
| 1702 | } |
| 1703 | |
| 1704 | return false; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1707 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1708 | const AttributeList &Attr) { |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1709 | |
| 1710 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1711 | // because 'analyzer_noreturn' does not impact the type. |
| 1712 | |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1713 | if(!checkAttributeNumArgs(S, Attr, 0)) |
| 1714 | return; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1715 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1716 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1717 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1718 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1719 | && !VD->getType()->isFunctionPointerType())) { |
| 1720 | S.Diag(Attr.getLoc(), |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1721 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1722 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1723 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1724 | return; |
| 1725 | } |
| 1726 | } |
| 1727 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1728 | D->addAttr(::new (S.Context) |
| 1729 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1730 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1733 | static void handleCXX11NoReturnAttr(Sema &S, Decl *D, |
| 1734 | const AttributeList &Attr) { |
| 1735 | // C++11 [dcl.attr.noreturn]p1: |
| 1736 | // The attribute may be applied to the declarator-id in a function |
| 1737 | // declaration. |
| 1738 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 1739 | if (!FD) { |
| 1740 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1741 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1742 | return; |
| 1743 | } |
| 1744 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1745 | D->addAttr(::new (S.Context) |
| 1746 | CXX11NoReturnAttr(Attr.getRange(), S.Context, |
| 1747 | Attr.getAttributeSpellingListIndex())); |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1750 | // PS3 PPU-specific. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1751 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1752 | /* |
| 1753 | Returning a Vector Class in Registers |
| 1754 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1755 | According to the PPU ABI specifications, a class with a single member of |
| 1756 | vector type is returned in memory when used as the return value of a function. |
| 1757 | This results in inefficient code when implementing vector classes. To return |
| 1758 | the value in a single vector register, add the vecreturn attribute to the |
| 1759 | class definition. This attribute is also applicable to struct types. |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1760 | |
| 1761 | Example: |
| 1762 | |
| 1763 | struct Vector |
| 1764 | { |
| 1765 | __vector float xyzw; |
| 1766 | } __attribute__((vecreturn)); |
| 1767 | |
| 1768 | Vector Add(Vector lhs, Vector rhs) |
| 1769 | { |
| 1770 | Vector result; |
| 1771 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1772 | return result; // This will be returned in a register |
| 1773 | } |
| 1774 | */ |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1775 | if (!isa<RecordDecl>(D)) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1776 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1777 | << Attr.getName() << ExpectedClass; |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1778 | return; |
| 1779 | } |
| 1780 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1781 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1782 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1783 | return; |
| 1784 | } |
| 1785 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1786 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1787 | int count = 0; |
| 1788 | |
| 1789 | if (!isa<CXXRecordDecl>(record)) { |
| 1790 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1791 | return; |
| 1792 | } |
| 1793 | |
| 1794 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1795 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1796 | return; |
| 1797 | } |
| 1798 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1799 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1800 | iter != record->field_end(); iter++) { |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1801 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1802 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1803 | return; |
| 1804 | } |
| 1805 | count++; |
| 1806 | } |
| 1807 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1808 | D->addAttr(::new (S.Context) |
| 1809 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1810 | Attr.getAttributeSpellingListIndex())); |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1813 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1814 | const AttributeList &Attr) { |
| 1815 | if (isa<ParmVarDecl>(D)) { |
| 1816 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1817 | // parameter of a function declaration or lambda. |
| 1818 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1819 | S.Diag(Attr.getLoc(), |
| 1820 | diag::err_carries_dependency_param_not_function_decl); |
| 1821 | return; |
| 1822 | } |
| 1823 | } else if (!isa<FunctionDecl>(D)) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1824 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1825 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1826 | return; |
| 1827 | } |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1828 | |
| 1829 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1830 | Attr.getRange(), S.Context, |
| 1831 | Attr.getAttributeSpellingListIndex())); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1834 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1835 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1836 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1837 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1838 | << Attr.getName() << 0; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1839 | return; |
| 1840 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1841 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1842 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 568eae4 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1843 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1844 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1845 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1846 | return; |
| 1847 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1848 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1849 | D->addAttr(::new (S.Context) |
| 1850 | UnusedAttr(Attr.getRange(), S.Context, |
| 1851 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1854 | static void handleReturnsTwiceAttr(Sema &S, Decl *D, |
| 1855 | const AttributeList &Attr) { |
| 1856 | // check the attribute arguments. |
| 1857 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1858 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1859 | << Attr.getName() << 0; |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1860 | return; |
| 1861 | } |
| 1862 | |
| 1863 | if (!isa<FunctionDecl>(D)) { |
| 1864 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1865 | << Attr.getName() << ExpectedFunction; |
| 1866 | return; |
| 1867 | } |
| 1868 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1869 | D->addAttr(::new (S.Context) |
| 1870 | ReturnsTwiceAttr(Attr.getRange(), S.Context, |
| 1871 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1874 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1875 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1876 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1877 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1878 | << Attr.getName() << 0; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1879 | return; |
| 1880 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1881 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1882 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Daniel Dunbar | 186204b | 2009-02-13 22:48:56 +0000 | [diff] [blame] | 1883 | if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1884 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 1885 | return; |
| 1886 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1887 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1888 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1889 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1890 | return; |
| 1891 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1892 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1893 | D->addAttr(::new (S.Context) |
| 1894 | UsedAttr(Attr.getRange(), S.Context, |
| 1895 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1898 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1899 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1900 | if (Attr.getNumArgs() > 1) { |
| 1901 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1902 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1903 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1904 | |
| 1905 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1906 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1907 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1908 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1909 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1910 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1911 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 1912 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1913 | << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1914 | return; |
| 1915 | } |
| 1916 | priority = Idx.getZExtValue(); |
| 1917 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1918 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1919 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1920 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1921 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1922 | return; |
| 1923 | } |
| 1924 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1925 | D->addAttr(::new (S.Context) |
| 1926 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1927 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1930 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1931 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1932 | if (Attr.getNumArgs() > 1) { |
| 1933 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1934 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1935 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1936 | |
| 1937 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1938 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1939 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1940 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1941 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1942 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1943 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 1944 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1945 | << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1946 | return; |
| 1947 | } |
| 1948 | priority = Idx.getZExtValue(); |
| 1949 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1950 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1951 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1952 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1953 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1954 | return; |
| 1955 | } |
| 1956 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1957 | D->addAttr(::new (S.Context) |
| 1958 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 1959 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1962 | template <typename AttrTy> |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 1963 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 1964 | const AttributeList &Attr) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1965 | unsigned NumArgs = Attr.getNumArgs(); |
| 1966 | if (NumArgs > 1) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1967 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1968 | return; |
| 1969 | } |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1970 | |
| 1971 | // Handle the case where the attribute has a text message. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1972 | StringRef Str; |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1973 | if (NumArgs == 1) { |
| 1974 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1975 | if (!SE) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 1976 | S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_argument_type) |
| 1977 | << Attr.getName() << AANT_ArgumentString; |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1978 | return; |
| 1979 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1980 | Str = SE->getString(); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1981 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1982 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1983 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 1984 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1987 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 1988 | const AttributeList &Attr) { |
Aaron Ballman | faf71a8 | 2013-07-23 15:16:00 +0000 | [diff] [blame] | 1989 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1990 | return; |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1991 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1992 | D->addAttr(::new (S.Context) |
| 1993 | ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context, |
| 1994 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 1997 | static void handleObjCRootClassAttr(Sema &S, Decl *D, |
| 1998 | const AttributeList &Attr) { |
| 1999 | if (!isa<ObjCInterfaceDecl>(D)) { |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 2000 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2001 | << Attr.getName() << ExpectedObjectiveCInterface; |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2002 | return; |
| 2003 | } |
| 2004 | |
Aaron Ballman | faf71a8 | 2013-07-23 15:16:00 +0000 | [diff] [blame] | 2005 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2006 | return; |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2007 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2008 | D->addAttr(::new (S.Context) |
| 2009 | ObjCRootClassAttr(Attr.getRange(), S.Context, |
| 2010 | Attr.getAttributeSpellingListIndex())); |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2013 | static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, |
| 2014 | const AttributeList &Attr) { |
Fariborz Jahanian | 341b8be | 2012-01-03 22:52:32 +0000 | [diff] [blame] | 2015 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 2016 | S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis); |
| 2017 | return; |
| 2018 | } |
| 2019 | |
Aaron Ballman | faf71a8 | 2013-07-23 15:16:00 +0000 | [diff] [blame] | 2020 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2021 | return; |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2022 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2023 | D->addAttr(::new (S.Context) |
| 2024 | ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context, |
| 2025 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2028 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2029 | IdentifierInfo *Platform, |
| 2030 | VersionTuple Introduced, |
| 2031 | VersionTuple Deprecated, |
| 2032 | VersionTuple Obsoleted) { |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2033 | StringRef PlatformName |
| 2034 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2035 | if (PlatformName.empty()) |
| 2036 | PlatformName = Platform->getName(); |
| 2037 | |
| 2038 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2039 | // of these steps are needed). |
| 2040 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2041 | !(Introduced <= Deprecated)) { |
| 2042 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2043 | << 1 << PlatformName << Deprecated.getAsString() |
| 2044 | << 0 << Introduced.getAsString(); |
| 2045 | return true; |
| 2046 | } |
| 2047 | |
| 2048 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2049 | !(Introduced <= Obsoleted)) { |
| 2050 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2051 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2052 | << 0 << Introduced.getAsString(); |
| 2053 | return true; |
| 2054 | } |
| 2055 | |
| 2056 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2057 | !(Deprecated <= Obsoleted)) { |
| 2058 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2059 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2060 | << 1 << Deprecated.getAsString(); |
| 2061 | return true; |
| 2062 | } |
| 2063 | |
| 2064 | return false; |
| 2065 | } |
| 2066 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2067 | /// \brief Check whether the two versions match. |
| 2068 | /// |
| 2069 | /// If either version tuple is empty, then they are assumed to match. If |
| 2070 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2071 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2072 | bool BeforeIsOkay) { |
| 2073 | if (X.empty() || Y.empty()) |
| 2074 | return true; |
| 2075 | |
| 2076 | if (X == Y) |
| 2077 | return true; |
| 2078 | |
| 2079 | if (BeforeIsOkay && X < Y) |
| 2080 | return true; |
| 2081 | |
| 2082 | return false; |
| 2083 | } |
| 2084 | |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2085 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2086 | IdentifierInfo *Platform, |
| 2087 | VersionTuple Introduced, |
| 2088 | VersionTuple Deprecated, |
| 2089 | VersionTuple Obsoleted, |
| 2090 | bool IsUnavailable, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2091 | StringRef Message, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2092 | bool Override, |
| 2093 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2094 | VersionTuple MergedIntroduced = Introduced; |
| 2095 | VersionTuple MergedDeprecated = Deprecated; |
| 2096 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2097 | bool FoundAny = false; |
| 2098 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2099 | if (D->hasAttrs()) { |
| 2100 | AttrVec &Attrs = D->getAttrs(); |
| 2101 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 2102 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 2103 | if (!OldAA) { |
| 2104 | ++i; |
| 2105 | continue; |
| 2106 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2107 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2108 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2109 | if (OldPlatform != Platform) { |
| 2110 | ++i; |
| 2111 | continue; |
| 2112 | } |
| 2113 | |
| 2114 | FoundAny = true; |
| 2115 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2116 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2117 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2118 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2119 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2120 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 2121 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 2122 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 2123 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 72daa3f | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 2124 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2125 | if (Override) { |
| 2126 | int Which = -1; |
| 2127 | VersionTuple FirstVersion; |
| 2128 | VersionTuple SecondVersion; |
| 2129 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 2130 | Which = 0; |
| 2131 | FirstVersion = OldIntroduced; |
| 2132 | SecondVersion = Introduced; |
| 2133 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 2134 | Which = 1; |
| 2135 | FirstVersion = Deprecated; |
| 2136 | SecondVersion = OldDeprecated; |
| 2137 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 2138 | Which = 2; |
| 2139 | FirstVersion = Obsoleted; |
| 2140 | SecondVersion = OldObsoleted; |
| 2141 | } |
| 2142 | |
| 2143 | if (Which == -1) { |
| 2144 | Diag(OldAA->getLocation(), |
| 2145 | diag::warn_mismatched_availability_override_unavail) |
| 2146 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2147 | } else { |
| 2148 | Diag(OldAA->getLocation(), |
| 2149 | diag::warn_mismatched_availability_override) |
| 2150 | << Which |
| 2151 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2152 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 2153 | } |
| 2154 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2155 | } else { |
| 2156 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2157 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2158 | } |
| 2159 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2160 | Attrs.erase(Attrs.begin() + i); |
| 2161 | --e; |
| 2162 | continue; |
| 2163 | } |
| 2164 | |
| 2165 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2166 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2167 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2168 | |
| 2169 | if (MergedIntroduced2.empty()) |
| 2170 | MergedIntroduced2 = OldIntroduced; |
| 2171 | if (MergedDeprecated2.empty()) |
| 2172 | MergedDeprecated2 = OldDeprecated; |
| 2173 | if (MergedObsoleted2.empty()) |
| 2174 | MergedObsoleted2 = OldObsoleted; |
| 2175 | |
| 2176 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2177 | MergedIntroduced2, MergedDeprecated2, |
| 2178 | MergedObsoleted2)) { |
| 2179 | Attrs.erase(Attrs.begin() + i); |
| 2180 | --e; |
| 2181 | continue; |
| 2182 | } |
| 2183 | |
| 2184 | MergedIntroduced = MergedIntroduced2; |
| 2185 | MergedDeprecated = MergedDeprecated2; |
| 2186 | MergedObsoleted = MergedObsoleted2; |
| 2187 | ++i; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2188 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | if (FoundAny && |
| 2192 | MergedIntroduced == Introduced && |
| 2193 | MergedDeprecated == Deprecated && |
| 2194 | MergedObsoleted == Obsoleted) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2195 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2196 | |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2197 | // Only create a new attribute if !Override, but we want to do |
| 2198 | // the checking. |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2199 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2200 | MergedDeprecated, MergedObsoleted) && |
| 2201 | !Override) { |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2202 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 2203 | Introduced, Deprecated, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2204 | Obsoleted, IsUnavailable, Message, |
| 2205 | AttrSpellingListIndex); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2206 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2207 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2210 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2211 | const AttributeList &Attr) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2212 | IdentifierInfo *Platform = Attr.getParameterName(); |
| 2213 | SourceLocation PlatformLoc = Attr.getParameterLoc(); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2214 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2215 | |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2216 | if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2217 | S.Diag(PlatformLoc, diag::warn_availability_unknown_platform) |
| 2218 | << Platform; |
| 2219 | |
Rafael Espindola | 8c4222a | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2220 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 2221 | if (!ND) { |
| 2222 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2223 | return; |
| 2224 | } |
| 2225 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2226 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2227 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2228 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2229 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2230 | StringRef Str; |
| 2231 | const StringLiteral *SE = |
| 2232 | dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr()); |
| 2233 | if (SE) |
| 2234 | Str = SE->getString(); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2235 | |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2236 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2237 | Platform, |
| 2238 | Introduced.Version, |
| 2239 | Deprecated.Version, |
| 2240 | Obsoleted.Version, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2241 | IsUnavailable, Str, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2242 | /*Override=*/false, |
| 2243 | Index); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2244 | if (NewAttr) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2245 | D->addAttr(NewAttr); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2248 | template <class T> |
| 2249 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2250 | typename T::VisibilityType value, |
| 2251 | unsigned attrSpellingListIndex) { |
| 2252 | T *existingAttr = D->getAttr<T>(); |
| 2253 | if (existingAttr) { |
| 2254 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2255 | if (existingValue == value) |
| 2256 | return NULL; |
| 2257 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2258 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2259 | D->dropAttr<T>(); |
| 2260 | } |
| 2261 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2262 | } |
| 2263 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2264 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2265 | VisibilityAttr::VisibilityType Vis, |
| 2266 | unsigned AttrSpellingListIndex) { |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2267 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2268 | AttrSpellingListIndex); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2269 | } |
| 2270 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2271 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2272 | TypeVisibilityAttr::VisibilityType Vis, |
| 2273 | unsigned AttrSpellingListIndex) { |
| 2274 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2275 | AttrSpellingListIndex); |
| 2276 | } |
| 2277 | |
| 2278 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2279 | bool isTypeVisibility) { |
| 2280 | // Visibility attributes don't mean anything on a typedef. |
| 2281 | if (isa<TypedefNameDecl>(D)) { |
| 2282 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2283 | << Attr.getName(); |
| 2284 | return; |
| 2285 | } |
| 2286 | |
| 2287 | // 'type_visibility' can only go on a type or namespace. |
| 2288 | if (isTypeVisibility && |
| 2289 | !(isa<TagDecl>(D) || |
| 2290 | isa<ObjCInterfaceDecl>(D) || |
| 2291 | isa<NamespaceDecl>(D))) { |
| 2292 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2293 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2294 | return; |
| 2295 | } |
| 2296 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2297 | // check the attribute arguments. |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2298 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2299 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2300 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2301 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2302 | Arg = Arg->IgnoreParenCasts(); |
| 2303 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2304 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2305 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 2306 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 2307 | << Attr.getName() << AANT_ArgumentString; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2308 | return; |
| 2309 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2310 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2311 | StringRef TypeStr = Str->getString(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2312 | VisibilityAttr::VisibilityType type; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2313 | |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2314 | if (TypeStr == "default") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2315 | type = VisibilityAttr::Default; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2316 | else if (TypeStr == "hidden") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2317 | type = VisibilityAttr::Hidden; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2318 | else if (TypeStr == "internal") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2319 | type = VisibilityAttr::Hidden; // FIXME |
John McCall | 4188760 | 2012-01-29 01:20:30 +0000 | [diff] [blame] | 2320 | else if (TypeStr == "protected") { |
| 2321 | // Complain about attempts to use protected visibility on targets |
| 2322 | // (like Darwin) that don't support it. |
| 2323 | if (!S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2324 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2325 | type = VisibilityAttr::Default; |
| 2326 | } else { |
| 2327 | type = VisibilityAttr::Protected; |
| 2328 | } |
| 2329 | } else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2330 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2331 | return; |
| 2332 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2333 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2334 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2335 | clang::Attr *newAttr; |
| 2336 | if (isTypeVisibility) { |
| 2337 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2338 | (TypeVisibilityAttr::VisibilityType) type, |
| 2339 | Index); |
| 2340 | } else { |
| 2341 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2342 | } |
| 2343 | if (newAttr) |
| 2344 | D->addAttr(newAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2347 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2348 | const AttributeList &Attr) { |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2349 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 2350 | if (!method) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2351 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2352 | << ExpectedMethod; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2353 | return; |
| 2354 | } |
| 2355 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2356 | if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) { |
| 2357 | if (!Attr.getParameterName() && Attr.getNumArgs() == 1) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2358 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 2359 | << Attr.getName() << 1 << AANT_ArgumentString; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2360 | } else { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2361 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2362 | << Attr.getName() << 0; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2363 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2364 | Attr.setInvalid(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2365 | return; |
| 2366 | } |
| 2367 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2368 | StringRef param = Attr.getParameterName()->getName(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2369 | ObjCMethodFamilyAttr::FamilyKind family; |
| 2370 | if (param == "none") |
| 2371 | family = ObjCMethodFamilyAttr::OMF_None; |
| 2372 | else if (param == "alloc") |
| 2373 | family = ObjCMethodFamilyAttr::OMF_alloc; |
| 2374 | else if (param == "copy") |
| 2375 | family = ObjCMethodFamilyAttr::OMF_copy; |
| 2376 | else if (param == "init") |
| 2377 | family = ObjCMethodFamilyAttr::OMF_init; |
| 2378 | else if (param == "mutableCopy") |
| 2379 | family = ObjCMethodFamilyAttr::OMF_mutableCopy; |
| 2380 | else if (param == "new") |
| 2381 | family = ObjCMethodFamilyAttr::OMF_new; |
| 2382 | else { |
| 2383 | // Just warn and ignore it. This is future-proof against new |
| 2384 | // families being used in system headers. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2385 | S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2386 | return; |
| 2387 | } |
| 2388 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2389 | if (family == ObjCMethodFamilyAttr::OMF_init && |
| 2390 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2391 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2392 | << method->getResultType(); |
| 2393 | // Ignore the attribute. |
| 2394 | return; |
| 2395 | } |
| 2396 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2397 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2398 | S.Context, family)); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2399 | } |
| 2400 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2401 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 2402 | const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2403 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2404 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2405 | |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2406 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 2407 | if (OCI == 0) { |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 2408 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2409 | << Attr.getName() << ExpectedObjectiveCInterface; |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2410 | return; |
| 2411 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2412 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2413 | D->addAttr(::new (S.Context) |
| 2414 | ObjCExceptionAttr(Attr.getRange(), S.Context, |
| 2415 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2418 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 081c883 | 2013-07-23 12:13:14 +0000 | [diff] [blame] | 2419 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2420 | return; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2421 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2422 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2423 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2424 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2425 | return; |
| 2426 | } |
| 2427 | } |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2428 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2429 | QualType T = PD->getType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2430 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2431 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2432 | return; |
| 2433 | } |
| 2434 | } |
| 2435 | else { |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2436 | // It is okay to include this attribute on properties, e.g.: |
| 2437 | // |
| 2438 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2439 | // |
| 2440 | // In this case it follows tradition and suppresses an error in the above |
| 2441 | // case. |
Fariborz Jahanian | 9b2eb7b | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2442 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2443 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2444 | D->addAttr(::new (S.Context) |
| 2445 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2446 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2449 | static void |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2450 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 081c883 | 2013-07-23 12:13:14 +0000 | [diff] [blame] | 2451 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2452 | return; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2453 | |
| 2454 | if (!isa<FunctionDecl>(D)) { |
| 2455 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 2456 | return; |
| 2457 | } |
| 2458 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2459 | D->addAttr(::new (S.Context) |
| 2460 | OverloadableAttr(Attr.getRange(), S.Context, |
| 2461 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2464 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2465 | if (!Attr.getParameterName()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2466 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 2467 | << Attr.getName() << 1 << AANT_ArgumentString; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2468 | return; |
| 2469 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2470 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2471 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2472 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2473 | << Attr.getName() << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2474 | return; |
| 2475 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2476 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2477 | BlocksAttr::BlockType type; |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 2478 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2479 | type = BlocksAttr::ByRef; |
| 2480 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2481 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2482 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2483 | return; |
| 2484 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2485 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2486 | D->addAttr(::new (S.Context) |
| 2487 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2488 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2489 | } |
| 2490 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2491 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2492 | // check the attribute arguments. |
| 2493 | if (Attr.getNumArgs() > 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2494 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2495 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2496 | } |
| 2497 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2498 | unsigned sentinel = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2499 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2500 | Expr *E = Attr.getArg(0); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2501 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2502 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2503 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2504 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 2505 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2506 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2507 | return; |
| 2508 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2509 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2510 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2511 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2512 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2513 | return; |
| 2514 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2515 | |
| 2516 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2519 | unsigned nullPos = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2520 | if (Attr.getNumArgs() > 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2521 | Expr *E = Attr.getArg(1); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2522 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2523 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2524 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2525 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 2526 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2527 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2528 | return; |
| 2529 | } |
| 2530 | nullPos = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2531 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2532 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2533 | // FIXME: This error message could be improved, it would be nice |
| 2534 | // to say what the bounds actually are. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2535 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2536 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2537 | return; |
| 2538 | } |
| 2539 | } |
| 2540 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2541 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2542 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2543 | if (isa<FunctionNoProtoType>(FT)) { |
| 2544 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2545 | return; |
| 2546 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2547 | |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2548 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2549 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2550 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2551 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2552 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2553 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2554 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2555 | return; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2556 | } |
Eli Friedman | a0b2ba1 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2557 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2558 | if (!BD->isVariadic()) { |
| 2559 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2560 | return; |
| 2561 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2562 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2563 | QualType Ty = V->getType(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2564 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2565 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2566 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2567 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2568 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2569 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2570 | return; |
| 2571 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2572 | } else { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2573 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2574 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2575 | return; |
| 2576 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2577 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2578 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2579 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2580 | return; |
| 2581 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2582 | D->addAttr(::new (S.Context) |
| 2583 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2584 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 2587 | static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2588 | // Check the attribute arguments. |
| 2589 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 2590 | return; |
| 2591 | |
| 2592 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
| 2593 | RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context)); |
| 2594 | else |
| 2595 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2596 | } |
| 2597 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2598 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2599 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2600 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2601 | return; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2602 | |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 2603 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2604 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Kaelyn Uhrain | d449c79 | 2012-11-13 00:18:47 +0000 | [diff] [blame] | 2605 | << Attr.getName() << ExpectedFunctionMethodOrClass; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2606 | return; |
| 2607 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2608 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2609 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2610 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2611 | << Attr.getName() << 0; |
Nuno Lopes | f857798 | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2612 | return; |
| 2613 | } |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2614 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2615 | if (MD->getResultType()->isVoidType()) { |
| 2616 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2617 | << Attr.getName() << 1; |
| 2618 | return; |
| 2619 | } |
| 2620 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2621 | D->addAttr(::new (S.Context) |
| 2622 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2623 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2626 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2627 | // check the attribute arguments. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2628 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2629 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2630 | << Attr.getName() << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2631 | return; |
| 2632 | } |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2633 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2634 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
Fariborz Jahanian | 13c7fcc | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 2635 | if (isa<CXXRecordDecl>(D)) { |
| 2636 | D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| 2637 | return; |
| 2638 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2639 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2640 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | f23ecd9 | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 2641 | return; |
| 2642 | } |
| 2643 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2644 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 2645 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2646 | nd->addAttr(::new (S.Context) |
| 2647 | WeakAttr(Attr.getRange(), S.Context, |
| 2648 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2651 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2652 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2653 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2654 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2655 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2656 | |
| 2657 | // weak_import only applies to variable & function declarations. |
| 2658 | bool isDef = false; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2659 | if (!D->canBeWeakImported(isDef)) { |
| 2660 | if (isDef) |
Reid Kleckner | beba3e8 | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2661 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2662 | << "weak_import"; |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2663 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2664 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 90eed21 | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2665 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2666 | // Nothing to warn about here. |
| 2667 | } else |
Fariborz Jahanian | c034974 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2668 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2669 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2670 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2671 | return; |
| 2672 | } |
| 2673 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2674 | D->addAttr(::new (S.Context) |
| 2675 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2676 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2677 | } |
| 2678 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2679 | // Handles reqd_work_group_size and work_group_size_hint. |
| 2680 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2681 | const AttributeList &Attr) { |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2682 | assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2683 | || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint); |
| 2684 | |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2685 | // Attribute has 3 arguments. |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2686 | if (!checkAttributeNumArgs(S, Attr, 3)) return; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2687 | |
| 2688 | unsigned WGSize[3]; |
| 2689 | for (unsigned i = 0; i < 3; ++i) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2690 | Expr *E = Attr.getArg(i); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2691 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2692 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2693 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2694 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2695 | << Attr.getName()->getName() << E->getSourceRange(); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2696 | return; |
| 2697 | } |
| 2698 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 2699 | } |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2700 | |
| 2701 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2702 | && D->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 2703 | ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>(); |
| 2704 | if (!(A->getXDim() == WGSize[0] && |
| 2705 | A->getYDim() == WGSize[1] && |
| 2706 | A->getZDim() == WGSize[2])) { |
| 2707 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2708 | Attr.getName(); |
| 2709 | } |
| 2710 | } |
| 2711 | |
| 2712 | if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint |
| 2713 | && D->hasAttr<WorkGroupSizeHintAttr>()) { |
| 2714 | WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>(); |
| 2715 | if (!(A->getXDim() == WGSize[0] && |
| 2716 | A->getYDim() == WGSize[1] && |
| 2717 | A->getZDim() == WGSize[2])) { |
| 2718 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2719 | Attr.getName(); |
| 2720 | } |
| 2721 | } |
| 2722 | |
| 2723 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize) |
| 2724 | D->addAttr(::new (S.Context) |
| 2725 | ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2726 | WGSize[0], WGSize[1], WGSize[2], |
| 2727 | Attr.getAttributeSpellingListIndex())); |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2728 | else |
| 2729 | D->addAttr(::new (S.Context) |
| 2730 | WorkGroupSizeHintAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2731 | WGSize[0], WGSize[1], WGSize[2], |
| 2732 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2733 | } |
| 2734 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2735 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2736 | assert(Attr.getKind() == AttributeList::AT_VecTypeHint); |
| 2737 | |
| 2738 | // Attribute has 1 argument. |
| 2739 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 2740 | return; |
| 2741 | |
| 2742 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg()); |
| 2743 | |
| 2744 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2745 | (ParmType->isBooleanType() || |
| 2746 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2747 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2748 | << ParmType; |
| 2749 | return; |
| 2750 | } |
| 2751 | |
| 2752 | if (Attr.getKind() == AttributeList::AT_VecTypeHint && |
| 2753 | D->hasAttr<VecTypeHintAttr>()) { |
| 2754 | VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>(); |
| 2755 | if (A->getTypeHint() != ParmType) { |
| 2756 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2757 | return; |
| 2758 | } |
| 2759 | } |
| 2760 | |
| 2761 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
| 2762 | ParmType, Attr.getLoc())); |
| 2763 | } |
| 2764 | |
Joey Gouly | 96cead5 | 2013-03-14 09:54:43 +0000 | [diff] [blame] | 2765 | static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2766 | if (!dyn_cast<VarDecl>(D)) |
| 2767 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian" |
| 2768 | << 9; |
| 2769 | StringRef EndianType = Attr.getParameterName()->getName(); |
| 2770 | if (EndianType != "host" && EndianType != "device") |
| 2771 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType; |
| 2772 | } |
| 2773 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2774 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2775 | StringRef Name, |
| 2776 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2777 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2778 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2779 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2780 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2781 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2782 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2783 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2784 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2785 | AttrSpellingListIndex); |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2788 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2789 | // Attribute has no arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2790 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2791 | return; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2792 | |
| 2793 | // Make sure that there is a string literal as the sections's single |
| 2794 | // argument. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2795 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2796 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2797 | if (!SE) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 2798 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
| 2799 | << Attr.getName() << AANT_ArgumentString; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2800 | return; |
| 2801 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2802 | |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2803 | // If the target wants to validate the section specifier, make it happen. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2804 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString()); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2805 | if (!Error.empty()) { |
| 2806 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 2807 | << Error; |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2808 | return; |
| 2809 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2810 | |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2811 | // This attribute cannot be applied to local variables. |
| 2812 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 2813 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 2814 | return; |
| 2815 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2816 | |
| 2817 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2818 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2819 | SE->getString(), Index); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2820 | if (NewAttr) |
| 2821 | D->addAttr(NewAttr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2822 | } |
| 2823 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2824 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2825 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2826 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2827 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2828 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2829 | << Attr.getName() << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2830 | return; |
| 2831 | } |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2832 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2833 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2834 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2835 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2836 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2837 | D->addAttr(::new (S.Context) |
| 2838 | NoThrowAttr(Attr.getRange(), S.Context, |
| 2839 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2840 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2841 | } |
| 2842 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2843 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2844 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2845 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2846 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2847 | << Attr.getName() << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2848 | return; |
| 2849 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2850 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2851 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2852 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2853 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2854 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2855 | D->addAttr(::new (S.Context) |
| 2856 | ConstAttr(Attr.getRange(), S.Context, |
| 2857 | Attr.getAttributeSpellingListIndex() )); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2858 | } |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2861 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2862 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2863 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2864 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2865 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2866 | D->addAttr(::new (S.Context) |
| 2867 | PureAttr(Attr.getRange(), S.Context, |
| 2868 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2869 | } |
| 2870 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2871 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2872 | if (!Attr.getParameterName()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2873 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2874 | << Attr.getName() << 1; |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2875 | return; |
| 2876 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2877 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2878 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2879 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2880 | << Attr.getName() << 1; |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2881 | return; |
| 2882 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2883 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2884 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2885 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2886 | if (!VD || !VD->hasLocalStorage()) { |
| 2887 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 2888 | return; |
| 2889 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2890 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2891 | // Look up the function |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2892 | // FIXME: Lookup probably isn't looking in the right place |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2893 | NamedDecl *CleanupDecl |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2894 | = S.LookupSingleName(S.TUScope, Attr.getParameterName(), |
| 2895 | Attr.getParameterLoc(), Sema::LookupOrdinaryName); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2896 | if (!CleanupDecl) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2897 | S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2898 | Attr.getParameterName(); |
| 2899 | return; |
| 2900 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2901 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2902 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 2903 | if (!FD) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2904 | S.Diag(Attr.getParameterLoc(), |
| 2905 | diag::err_attribute_cleanup_arg_not_function) |
| 2906 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2907 | return; |
| 2908 | } |
| 2909 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2910 | if (FD->getNumParams() != 1) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2911 | S.Diag(Attr.getParameterLoc(), |
| 2912 | diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2913 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2914 | return; |
| 2915 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2916 | |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2917 | // We're currently more strict than GCC about what function types we accept. |
| 2918 | // If this ever proves to be a problem it should be easy to fix. |
| 2919 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2920 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2921 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2922 | ParamTy, Ty) != Sema::Compatible) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2923 | S.Diag(Attr.getParameterLoc(), |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2924 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 2925 | Attr.getParameterName() << ParamTy << Ty; |
| 2926 | return; |
| 2927 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2928 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2929 | D->addAttr(::new (S.Context) |
| 2930 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2931 | Attr.getAttributeSpellingListIndex())); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2932 | S.MarkFunctionReferenced(Attr.getParameterLoc(), FD); |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 2933 | S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc()); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2936 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2937 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2938 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2939 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2940 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2941 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2942 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2943 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2944 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2945 | return; |
| 2946 | } |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2947 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2948 | Expr *IdxExpr = Attr.getArg(0); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2949 | uint64_t ArgIdx; |
| 2950 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 2951 | Attr.getLoc(), 1, IdxExpr, ArgIdx)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2952 | return; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2953 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2954 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2955 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2956 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2957 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2958 | if (not_nsstring_type && |
| 2959 | !isCFStringType(Ty, S.Context) && |
| 2960 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2961 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2962 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2963 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2964 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2965 | << IdxExpr->getSourceRange(); |
| 2966 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2967 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2968 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2969 | if (!isNSStringType(Ty, S.Context) && |
| 2970 | !isCFStringType(Ty, S.Context) && |
| 2971 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2972 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2973 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2974 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2975 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2976 | << IdxExpr->getSourceRange(); |
| 2977 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2978 | } |
| 2979 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2980 | // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex |
| 2981 | // because that has corrected for the implicit this parameter, and is zero- |
| 2982 | // based. The attribute expects what the user wrote explicitly. |
| 2983 | llvm::APSInt Val; |
| 2984 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2985 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2986 | D->addAttr(::new (S.Context) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2987 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2988 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2989 | } |
| 2990 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2991 | enum FormatAttrKind { |
| 2992 | CFStringFormat, |
| 2993 | NSStringFormat, |
| 2994 | StrftimeFormat, |
| 2995 | SupportedFormat, |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2996 | IgnoredFormat, |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2997 | InvalidFormat |
| 2998 | }; |
| 2999 | |
| 3000 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 3001 | /// types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3002 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3003 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 3004 | // Check for formats that get handled specially. |
| 3005 | .Case("NSString", NSStringFormat) |
| 3006 | .Case("CFString", CFStringFormat) |
| 3007 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3008 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3009 | // Otherwise, check for supported formats. |
| 3010 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 3011 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 3012 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3013 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3014 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 3015 | .Default(InvalidFormat); |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3016 | } |
| 3017 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3018 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3019 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3020 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 3021 | const AttributeList &Attr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3022 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3023 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3024 | return; |
| 3025 | } |
| 3026 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3027 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3028 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3029 | Attr.setInvalid(); |
| 3030 | return; |
| 3031 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3032 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3033 | if (S.Context.getAsArrayType(T)) |
| 3034 | T = S.Context.getBaseElementType(T); |
| 3035 | if (!T->getAs<RecordType>()) { |
| 3036 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3037 | Attr.setInvalid(); |
| 3038 | return; |
| 3039 | } |
| 3040 | |
Aaron Ballman | ffa9d57 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3041 | if (!checkAttributeNumArgs(S, Attr, 1)) { |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3042 | Attr.setInvalid(); |
| 3043 | return; |
| 3044 | } |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3045 | Expr *priorityExpr = Attr.getArg(0); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3046 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3047 | llvm::APSInt priority(32); |
| 3048 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 3049 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
| 3050 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 3051 | << "init_priority" << priorityExpr->getSourceRange(); |
| 3052 | Attr.setInvalid(); |
| 3053 | return; |
| 3054 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 3055 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3056 | if (prioritynum < 101 || prioritynum > 65535) { |
| 3057 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 3058 | << priorityExpr->getSourceRange(); |
| 3059 | Attr.setInvalid(); |
| 3060 | return; |
| 3061 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3062 | D->addAttr(::new (S.Context) |
| 3063 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 3064 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3065 | } |
| 3066 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3067 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3068 | int FormatIdx, int FirstArg, |
| 3069 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3070 | // Check whether we already have an equivalent format attribute. |
| 3071 | for (specific_attr_iterator<FormatAttr> |
| 3072 | i = D->specific_attr_begin<FormatAttr>(), |
| 3073 | e = D->specific_attr_end<FormatAttr>(); |
| 3074 | i != e ; ++i) { |
| 3075 | FormatAttr *f = *i; |
| 3076 | if (f->getType() == Format && |
| 3077 | f->getFormatIdx() == FormatIdx && |
| 3078 | f->getFirstArg() == FirstArg) { |
| 3079 | // If we don't have a valid location for this attribute, adopt the |
| 3080 | // location. |
| 3081 | if (f->getLocation().isInvalid()) |
| 3082 | f->setRange(Range); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3083 | return NULL; |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3084 | } |
| 3085 | } |
| 3086 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3087 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg, |
| 3088 | AttrSpellingListIndex); |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3091 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3092 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3093 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3094 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3095 | if (!Attr.getParameterName()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3096 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 3097 | << Attr.getName() << 1 << AANT_ArgumentString; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3098 | return; |
| 3099 | } |
| 3100 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3101 | if (Attr.getNumArgs() != 2) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3102 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3103 | << Attr.getName() << 3; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3104 | return; |
| 3105 | } |
| 3106 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3107 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3108 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3109 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3110 | return; |
| 3111 | } |
| 3112 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3113 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3114 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3115 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 3116 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3117 | unsigned FirstIdx = 1; |
| 3118 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3119 | StringRef Format = Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3120 | |
| 3121 | // Normalize the argument, __foo__ becomes foo. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3122 | if (Format.startswith("__") && Format.endswith("__")) |
| 3123 | Format = Format.substr(2, Format.size() - 4); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3124 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3125 | // Check for supported formats. |
| 3126 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3127 | |
| 3128 | if (Kind == IgnoredFormat) |
| 3129 | return; |
| 3130 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3131 | if (Kind == InvalidFormat) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3132 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3133 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3134 | return; |
| 3135 | } |
| 3136 | |
| 3137 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3138 | Expr *IdxExpr = Attr.getArg(0); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3139 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3140 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 3141 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3142 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 3143 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3144 | << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3145 | return; |
| 3146 | } |
| 3147 | |
| 3148 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3149 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3150 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3151 | return; |
| 3152 | } |
| 3153 | |
| 3154 | // FIXME: Do we need to bounds check? |
| 3155 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3156 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3157 | if (HasImplicitThisParam) { |
| 3158 | if (ArgIdx == 0) { |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3159 | S.Diag(Attr.getLoc(), |
| 3160 | diag::err_format_attribute_implicit_this_format_string) |
| 3161 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3162 | return; |
| 3163 | } |
| 3164 | ArgIdx--; |
| 3165 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3166 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3167 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3168 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3169 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3170 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3171 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3172 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3173 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3174 | return; |
| 3175 | } |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3176 | } else if (Kind == NSStringFormat) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3177 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3178 | // semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3179 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3180 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3181 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3182 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3183 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3184 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3185 | } else if (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3186 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3187 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3188 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3189 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3190 | return; |
| 3191 | } |
| 3192 | |
| 3193 | // check the 3rd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3194 | Expr *FirstArgExpr = Attr.getArg(1); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3195 | llvm::APSInt FirstArg(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3196 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 3197 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3198 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 3199 | << Attr.getName() << 3 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3200 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3201 | return; |
| 3202 | } |
| 3203 | |
| 3204 | // check if the function is variadic if the 3rd argument non-zero |
| 3205 | if (FirstArg != 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3206 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3207 | ++NumArgs; // +1 for ... |
| 3208 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3209 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3210 | return; |
| 3211 | } |
| 3212 | } |
| 3213 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3214 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3215 | // variable the input is just the current time + the format string. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3216 | if (Kind == StrftimeFormat) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3217 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3218 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 3219 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3220 | return; |
| 3221 | } |
| 3222 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3223 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3224 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3225 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3226 | return; |
| 3227 | } |
| 3228 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3229 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format, |
| 3230 | Idx.getZExtValue(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3231 | FirstArg.getZExtValue(), |
| 3232 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3233 | if (NewAttr) |
| 3234 | D->addAttr(NewAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3237 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 3238 | const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3239 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3240 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3241 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3242 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3243 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3244 | // Try to find the underlying union declaration. |
| 3245 | RecordDecl *RD = 0; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3246 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3247 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3248 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3249 | else |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3250 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3251 | |
| 3252 | if (!RD || !RD->isUnion()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3253 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3254 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3255 | return; |
| 3256 | } |
| 3257 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3258 | if (!RD->isCompleteDefinition()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3259 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3260 | diag::warn_transparent_union_attribute_not_definition); |
| 3261 | return; |
| 3262 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3263 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3264 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3265 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3266 | if (Field == FieldEnd) { |
| 3267 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3268 | return; |
| 3269 | } |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3270 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3271 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3272 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3273 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3274 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3275 | diag::warn_transparent_union_attribute_floating) |
| 3276 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3277 | return; |
| 3278 | } |
| 3279 | |
| 3280 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3281 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3282 | for (; Field != FieldEnd; ++Field) { |
| 3283 | QualType FieldType = Field->getType(); |
| 3284 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 3285 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 3286 | // Warn if we drop the attribute. |
| 3287 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3288 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3289 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3290 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3291 | diag::warn_transparent_union_attribute_field_size_align) |
| 3292 | << isSize << Field->getDeclName() << FieldBits; |
| 3293 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3294 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3295 | diag::note_transparent_union_first_field_size_align) |
| 3296 | << isSize << FirstBits; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3297 | return; |
| 3298 | } |
| 3299 | } |
| 3300 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3301 | RD->addAttr(::new (S.Context) |
| 3302 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 3303 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3304 | } |
| 3305 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3306 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3307 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3308 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3309 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3310 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3311 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3312 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3313 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3314 | // Make sure that there is a string literal as the annotation's single |
| 3315 | // argument. |
| 3316 | if (!SE) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 3317 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
| 3318 | << Attr.getName() << AANT_ArgumentString; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3319 | return; |
| 3320 | } |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3321 | |
| 3322 | // Don't duplicate annotations that are already set. |
| 3323 | for (specific_attr_iterator<AnnotateAttr> |
| 3324 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 3325 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
| 3326 | if ((*i)->getAnnotation() == SE->getString()) |
| 3327 | return; |
| 3328 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3329 | |
| 3330 | D->addAttr(::new (S.Context) |
| 3331 | AnnotateAttr(Attr.getRange(), S.Context, SE->getString(), |
| 3332 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3333 | } |
| 3334 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3335 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3336 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3337 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3338 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3339 | << Attr.getName() << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3340 | return; |
| 3341 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3342 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3343 | if (Attr.getNumArgs() == 0) { |
| 3344 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 3345 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 3346 | return; |
| 3347 | } |
| 3348 | |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3349 | Expr *E = Attr.getArg(0); |
| 3350 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3351 | S.Diag(Attr.getEllipsisLoc(), |
| 3352 | diag::err_pack_expansion_without_parameter_packs); |
| 3353 | return; |
| 3354 | } |
| 3355 | |
| 3356 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 3357 | return; |
| 3358 | |
| 3359 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 3360 | Attr.isPackExpansion()); |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3361 | } |
| 3362 | |
| 3363 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3364 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3365 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3366 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3367 | |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3368 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3369 | if (TmpAttr.isAlignas()) { |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3370 | // C++11 [dcl.align]p1: |
| 3371 | // An alignment-specifier may be applied to a variable or to a class |
| 3372 | // data member, but it shall not be applied to a bit-field, a function |
| 3373 | // parameter, the formal parameter of a catch clause, or a variable |
| 3374 | // declared with the register storage class specifier. An |
| 3375 | // alignment-specifier may also be applied to the declaration of a class |
| 3376 | // or enumeration type. |
| 3377 | // C11 6.7.5/2: |
| 3378 | // An alignment attribute shall not be specified in a declaration of |
| 3379 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3380 | // object declared with the register storage-class specifier. |
| 3381 | int DiagKind = -1; |
| 3382 | if (isa<ParmVarDecl>(D)) { |
| 3383 | DiagKind = 0; |
| 3384 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3385 | if (VD->getStorageClass() == SC_Register) |
| 3386 | DiagKind = 1; |
| 3387 | if (VD->isExceptionVariable()) |
| 3388 | DiagKind = 2; |
| 3389 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 3390 | if (FD->isBitField()) |
| 3391 | DiagKind = 3; |
| 3392 | } else if (!isa<TagDecl>(D)) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3393 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) |
| 3394 | << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'") |
Richard Smith | 5f838aa | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3395 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3396 | : ExpectedVariableFieldOrTag); |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3397 | return; |
| 3398 | } |
| 3399 | if (DiagKind != -1) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3400 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 3401 | << TmpAttr.isC11() << DiagKind; |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3402 | return; |
| 3403 | } |
| 3404 | } |
| 3405 | |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3406 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3407 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3408 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3409 | AA->setPackExpansion(IsPackExpansion); |
| 3410 | D->addAttr(AA); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3411 | return; |
| 3412 | } |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3413 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3414 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3415 | llvm::APSInt Alignment(32); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3416 | ExprResult ICE |
| 3417 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3418 | diag::err_aligned_attribute_argument_not_int, |
| 3419 | /*AllowFold*/ false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3420 | if (ICE.isInvalid()) |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3421 | return; |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3422 | |
| 3423 | // C++11 [dcl.align]p2: |
| 3424 | // -- if the constant expression evaluates to zero, the alignment |
| 3425 | // specifier shall have no effect |
| 3426 | // C11 6.7.5p6: |
| 3427 | // An alignment specification of zero has no effect. |
| 3428 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 3429 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3430 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 3431 | << E->getSourceRange(); |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3432 | return; |
| 3433 | } |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3434 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3435 | if (TmpAttr.isDeclspec()) { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3436 | // We've already verified it's a power of 2, now let's make sure it's |
| 3437 | // 8192 or less. |
| 3438 | if (Alignment.getZExtValue() > 8192) { |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3439 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3440 | << E->getSourceRange(); |
| 3441 | return; |
| 3442 | } |
| 3443 | } |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3444 | |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3445 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 3446 | ICE.take(), SpellingListIndex); |
| 3447 | AA->setPackExpansion(IsPackExpansion); |
| 3448 | D->addAttr(AA); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3449 | } |
| 3450 | |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3451 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3452 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3453 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3454 | // FIXME: Perform checking of type validity |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3455 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3456 | SpellingListIndex); |
| 3457 | AA->setPackExpansion(IsPackExpansion); |
| 3458 | D->addAttr(AA); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3459 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3460 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3461 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3462 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3463 | |
| 3464 | QualType Ty; |
| 3465 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3466 | Ty = VD->getType(); |
| 3467 | else |
| 3468 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
Richard Smith | 4da0903 | 2013-02-22 09:21:42 +0000 | [diff] [blame] | 3469 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3470 | return; |
| 3471 | |
| 3472 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3473 | // The combined effect of all alignment attributes in a declaration shall |
| 3474 | // not specify an alignment that is less strict than the alignment that |
| 3475 | // would otherwise be required for the entity being declared. |
| 3476 | AlignedAttr *AlignasAttr = 0; |
| 3477 | unsigned Align = 0; |
| 3478 | for (specific_attr_iterator<AlignedAttr> |
| 3479 | I = D->specific_attr_begin<AlignedAttr>(), |
| 3480 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 3481 | if (I->isAlignmentDependent()) |
| 3482 | return; |
| 3483 | if (I->isAlignas()) |
| 3484 | AlignasAttr = *I; |
| 3485 | Align = std::max(Align, I->getAlignment(Context)); |
| 3486 | } |
| 3487 | |
| 3488 | if (AlignasAttr && Align) { |
| 3489 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 3490 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 3491 | if (NaturalAlign > RequestedAlign) |
| 3492 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 3493 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 3494 | } |
| 3495 | } |
| 3496 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3497 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3498 | /// type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3499 | /// |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3500 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3501 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3502 | /// HImode, not an intermediate pointer. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3503 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3504 | // This attribute isn't documented, but glibc uses it. It changes |
| 3505 | // the width of an int or unsigned int to the specified size. |
| 3506 | |
| 3507 | // Check that there aren't any arguments |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3508 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3509 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3510 | |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3511 | |
| 3512 | IdentifierInfo *Name = Attr.getParameterName(); |
| 3513 | if (!Name) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3514 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3515 | return; |
| 3516 | } |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3517 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3518 | StringRef Str = Attr.getParameterName()->getName(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3519 | |
| 3520 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3521 | if (Str.startswith("__") && Str.endswith("__")) |
| 3522 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3523 | |
| 3524 | unsigned DestWidth = 0; |
| 3525 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3526 | bool ComplexMode = false; |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3527 | switch (Str.size()) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3528 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3529 | switch (Str[0]) { |
| 3530 | case 'Q': DestWidth = 8; break; |
| 3531 | case 'H': DestWidth = 16; break; |
| 3532 | case 'S': DestWidth = 32; break; |
| 3533 | case 'D': DestWidth = 64; break; |
| 3534 | case 'X': DestWidth = 96; break; |
| 3535 | case 'T': DestWidth = 128; break; |
| 3536 | } |
| 3537 | if (Str[1] == 'F') { |
| 3538 | IntegerMode = false; |
| 3539 | } else if (Str[1] == 'C') { |
| 3540 | IntegerMode = false; |
| 3541 | ComplexMode = true; |
| 3542 | } else if (Str[1] != 'I') { |
| 3543 | DestWidth = 0; |
| 3544 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3545 | break; |
| 3546 | case 4: |
| 3547 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3548 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3549 | if (Str == "word") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3550 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3551 | else if (Str == "byte") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3552 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3553 | break; |
| 3554 | case 7: |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3555 | if (Str == "pointer") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3556 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3557 | break; |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3558 | case 11: |
| 3559 | if (Str == "unwind_word") |
Rafael Espindola | 0b1de54 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3560 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3561 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3562 | } |
| 3563 | |
| 3564 | QualType OldTy; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3565 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3566 | OldTy = TD->getUnderlyingType(); |
| 3567 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3568 | OldTy = VD->getType(); |
| 3569 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3570 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3571 | << "mode" << Attr.getRange(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3572 | return; |
| 3573 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3574 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3575 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3576 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3577 | else if (IntegerMode) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3578 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3579 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3580 | } else if (ComplexMode) { |
| 3581 | if (!OldTy->isComplexType()) |
| 3582 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3583 | } else { |
| 3584 | if (!OldTy->isFloatingType()) |
| 3585 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3586 | } |
| 3587 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3588 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3589 | // and friends, at least with glibc. |
| 3590 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 3591 | // width on unusual platforms. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3592 | // FIXME: Make sure floating-point mappings are accurate |
| 3593 | // FIXME: Support XF and TF types |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3594 | QualType NewTy; |
| 3595 | switch (DestWidth) { |
| 3596 | case 0: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3597 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3598 | return; |
| 3599 | default: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3600 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3601 | return; |
| 3602 | case 8: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3603 | if (!IntegerMode) { |
| 3604 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3605 | return; |
| 3606 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3607 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3608 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3609 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3610 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3611 | break; |
| 3612 | case 16: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3613 | if (!IntegerMode) { |
| 3614 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3615 | return; |
| 3616 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3617 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3618 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3619 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3620 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3621 | break; |
| 3622 | case 32: |
| 3623 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3624 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3625 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3626 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3627 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3628 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3629 | break; |
| 3630 | case 64: |
| 3631 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3632 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3633 | else if (OldTy->isSignedIntegerType()) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3634 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3635 | NewTy = S.Context.LongTy; |
| 3636 | else |
| 3637 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3638 | else |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3639 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3640 | NewTy = S.Context.UnsignedLongTy; |
| 3641 | else |
| 3642 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3643 | break; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3644 | case 96: |
| 3645 | NewTy = S.Context.LongDoubleTy; |
| 3646 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3647 | case 128: |
Roman Divacky | 1964554 | 2013-07-03 21:08:41 +0000 | [diff] [blame] | 3648 | if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() != |
| 3649 | &llvm::APFloat::PPCDoubleDouble) { |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3650 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3651 | return; |
| 3652 | } |
Roman Divacky | f0d14cb | 2013-07-03 20:48:06 +0000 | [diff] [blame] | 3653 | if (IntegerMode) { |
| 3654 | if (OldTy->isSignedIntegerType()) |
| 3655 | NewTy = S.Context.Int128Ty; |
| 3656 | else |
| 3657 | NewTy = S.Context.UnsignedInt128Ty; |
| 3658 | } else |
| 3659 | NewTy = S.Context.LongDoubleTy; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3660 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3661 | } |
| 3662 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3663 | if (ComplexMode) { |
| 3664 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3665 | } |
| 3666 | |
| 3667 | // Install the new type. |
Enea Zaffanella | c2fa6b6 | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3668 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3669 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 3670 | else |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3671 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | c2fa6b6 | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3672 | |
| 3673 | D->addAttr(::new (S.Context) |
| 3674 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 3675 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3676 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3677 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3678 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3679 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3680 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3681 | return; |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 3682 | |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3683 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3684 | if (!VD->hasGlobalStorage()) |
| 3685 | S.Diag(Attr.getLoc(), |
| 3686 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3687 | << Attr.getName(); |
| 3688 | } else if (!isFunctionOrMethod(D)) { |
| 3689 | S.Diag(Attr.getLoc(), |
| 3690 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3691 | << Attr.getName(); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3692 | return; |
| 3693 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3694 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3695 | D->addAttr(::new (S.Context) |
| 3696 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3697 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3698 | } |
| 3699 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3700 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3701 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3702 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3703 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3704 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3705 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3706 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3707 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3708 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3709 | return; |
| 3710 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3711 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3712 | D->addAttr(::new (S.Context) |
| 3713 | NoInlineAttr(Attr.getRange(), S.Context, |
| 3714 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3715 | } |
| 3716 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3717 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 3718 | const AttributeList &Attr) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3719 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3720 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3721 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3722 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3723 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3724 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3725 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3726 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3727 | return; |
| 3728 | } |
| 3729 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3730 | D->addAttr(::new (S.Context) |
| 3731 | NoInstrumentFunctionAttr(Attr.getRange(), S.Context, |
| 3732 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3733 | } |
| 3734 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3735 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3736 | if (S.LangOpts.CUDA) { |
| 3737 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 3738 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3739 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3740 | << Attr.getName() << 0; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3741 | return; |
| 3742 | } |
| 3743 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3744 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3745 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3746 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3747 | return; |
| 3748 | } |
| 3749 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3750 | D->addAttr(::new (S.Context) |
| 3751 | CUDAConstantAttr(Attr.getRange(), S.Context, |
| 3752 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3753 | } else { |
| 3754 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 3755 | } |
| 3756 | } |
| 3757 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3758 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3759 | if (S.LangOpts.CUDA) { |
| 3760 | // check the attribute arguments. |
| 3761 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3762 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3763 | << Attr.getName() << 0; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3764 | return; |
| 3765 | } |
| 3766 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3767 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3768 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3769 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3770 | return; |
| 3771 | } |
| 3772 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3773 | D->addAttr(::new (S.Context) |
| 3774 | CUDADeviceAttr(Attr.getRange(), S.Context, |
| 3775 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3776 | } else { |
| 3777 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 3778 | } |
| 3779 | } |
| 3780 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3781 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3782 | if (S.LangOpts.CUDA) { |
| 3783 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3784 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3785 | return; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3786 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3787 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3788 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3789 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3790 | return; |
| 3791 | } |
| 3792 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3793 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3794 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 3795 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3796 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3797 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3798 | << FD->getType() |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3799 | << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(), |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3800 | "void"); |
| 3801 | } else { |
| 3802 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3803 | << FD->getType(); |
| 3804 | } |
| 3805 | return; |
| 3806 | } |
| 3807 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3808 | D->addAttr(::new (S.Context) |
| 3809 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
| 3810 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3811 | } else { |
| 3812 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 3813 | } |
| 3814 | } |
| 3815 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3816 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3817 | if (S.LangOpts.CUDA) { |
| 3818 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3819 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3820 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3821 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3822 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3823 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3824 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3825 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3826 | return; |
| 3827 | } |
| 3828 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3829 | D->addAttr(::new (S.Context) |
| 3830 | CUDAHostAttr(Attr.getRange(), S.Context, |
| 3831 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3832 | } else { |
| 3833 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 3834 | } |
| 3835 | } |
| 3836 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3837 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3838 | if (S.LangOpts.CUDA) { |
| 3839 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3840 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3841 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3842 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3843 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3844 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3845 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3846 | return; |
| 3847 | } |
| 3848 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3849 | D->addAttr(::new (S.Context) |
| 3850 | CUDASharedAttr(Attr.getRange(), S.Context, |
| 3851 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3852 | } else { |
| 3853 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 3854 | } |
| 3855 | } |
| 3856 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3857 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3858 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3859 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3860 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3861 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3862 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3863 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3864 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3865 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3866 | return; |
| 3867 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3868 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3869 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3870 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3871 | return; |
| 3872 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3873 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3874 | D->addAttr(::new (S.Context) |
| 3875 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3876 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3877 | } |
| 3878 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3879 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3880 | if (hasDeclarator(D)) return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3881 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3882 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3883 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3884 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3885 | CallingConv CC; |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3886 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3887 | return; |
| 3888 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3889 | if (!isa<ObjCMethodDecl>(D)) { |
| 3890 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3891 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3892 | return; |
| 3893 | } |
| 3894 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3895 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3896 | case AttributeList::AT_FastCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3897 | D->addAttr(::new (S.Context) |
| 3898 | FastCallAttr(Attr.getRange(), S.Context, |
| 3899 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3900 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3901 | case AttributeList::AT_StdCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3902 | D->addAttr(::new (S.Context) |
| 3903 | StdCallAttr(Attr.getRange(), S.Context, |
| 3904 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3905 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3906 | case AttributeList::AT_ThisCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3907 | D->addAttr(::new (S.Context) |
| 3908 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3909 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 04633eb | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3910 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3911 | case AttributeList::AT_CDecl: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3912 | D->addAttr(::new (S.Context) |
| 3913 | CDeclAttr(Attr.getRange(), S.Context, |
| 3914 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3915 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3916 | case AttributeList::AT_Pascal: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3917 | D->addAttr(::new (S.Context) |
| 3918 | PascalAttr(Attr.getRange(), S.Context, |
| 3919 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3920 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3921 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3922 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3923 | switch (CC) { |
| 3924 | case CC_AAPCS: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3925 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3926 | break; |
| 3927 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3928 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3929 | break; |
| 3930 | default: |
| 3931 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3934 | D->addAttr(::new (S.Context) |
| 3935 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3936 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3937 | return; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3938 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3939 | case AttributeList::AT_PnaclCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3940 | D->addAttr(::new (S.Context) |
| 3941 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 3942 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3943 | return; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3944 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3945 | D->addAttr(::new (S.Context) |
| 3946 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3947 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3948 | return; |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3949 | |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3950 | default: |
| 3951 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3952 | } |
| 3953 | } |
| 3954 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3955 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 3956 | assert(!Attr.isInvalid()); |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3957 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3960 | static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
| 3961 | assert(!Attr.isInvalid()); |
| 3962 | |
| 3963 | Expr *E = Attr.getArg(0); |
| 3964 | llvm::APSInt ArgNum(32); |
| 3965 | if (E->isTypeDependent() || E->isValueDependent() || |
| 3966 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 3967 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 3968 | << Attr.getName()->getName() << E->getSourceRange(); |
| 3969 | return; |
| 3970 | } |
| 3971 | |
| 3972 | D->addAttr(::new (S.Context) OpenCLImageAccessAttr( |
| 3973 | Attr.getRange(), S.Context, ArgNum.getZExtValue())); |
| 3974 | } |
| 3975 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3976 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3977 | const FunctionDecl *FD) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3978 | if (attr.isInvalid()) |
| 3979 | return true; |
| 3980 | |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3981 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
| 3982 | if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3983 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3984 | << attr.getName() << ReqArgs; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3985 | attr.setInvalid(); |
| 3986 | return true; |
| 3987 | } |
| 3988 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3989 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 3990 | // move to TargetAttributesSema one day. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3991 | switch (attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3992 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3993 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3994 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3995 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3996 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
| 3997 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3998 | Expr *Arg = attr.getArg(0); |
| 3999 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4000 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 4001 | Diag(attr.getLoc(), diag::err_attribute_argument_type) << attr.getName() |
| 4002 | << AANT_ArgumentString; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4003 | attr.setInvalid(); |
| 4004 | return true; |
| 4005 | } |
| 4006 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4007 | StringRef StrRef = Str->getString(); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4008 | if (StrRef == "aapcs") { |
| 4009 | CC = CC_AAPCS; |
| 4010 | break; |
| 4011 | } else if (StrRef == "aapcs-vfp") { |
| 4012 | CC = CC_AAPCS_VFP; |
| 4013 | break; |
| 4014 | } |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4015 | |
| 4016 | attr.setInvalid(); |
| 4017 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 4018 | return true; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4019 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4020 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4021 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4022 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4025 | const TargetInfo &TI = Context.getTargetInfo(); |
| 4026 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 4027 | if (A == TargetInfo::CCCR_Warning) { |
| 4028 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4029 | |
| 4030 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 4031 | if (FD) |
| 4032 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 4033 | TargetInfo::CCMT_NonMember; |
| 4034 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4035 | } |
| 4036 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4037 | return false; |
| 4038 | } |
| 4039 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4040 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4041 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4042 | |
| 4043 | unsigned numParams; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4044 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4045 | return; |
| 4046 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4047 | if (!isa<ObjCMethodDecl>(D)) { |
| 4048 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4049 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4050 | return; |
| 4051 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4052 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4053 | D->addAttr(::new (S.Context) |
| 4054 | RegparmAttr(Attr.getRange(), S.Context, numParams, |
| 4055 | Attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4056 | } |
| 4057 | |
| 4058 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 4059 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4060 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 4061 | if (Attr.isInvalid()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4062 | return true; |
| 4063 | |
Aaron Ballman | ffa9d57 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 4064 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4065 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4066 | return true; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4067 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4068 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4069 | Expr *NumParamsExpr = Attr.getArg(0); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4070 | llvm::APSInt NumParams(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 4071 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4072 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4073 | Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4074 | << "regparm" << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4075 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4076 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4077 | } |
| 4078 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4079 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4080 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4081 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4082 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4083 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4084 | } |
| 4085 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4086 | numParams = NumParams.getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4087 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4088 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4089 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4090 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4091 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4092 | } |
| 4093 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4094 | return false; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4097 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4098 | if (S.LangOpts.CUDA) { |
| 4099 | // check the attribute arguments. |
| 4100 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 4101 | // FIXME: 0 is not okay. |
| 4102 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4103 | return; |
| 4104 | } |
| 4105 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4106 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4107 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4108 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4109 | return; |
| 4110 | } |
| 4111 | |
| 4112 | Expr *MaxThreadsExpr = Attr.getArg(0); |
| 4113 | llvm::APSInt MaxThreads(32); |
| 4114 | if (MaxThreadsExpr->isTypeDependent() || |
| 4115 | MaxThreadsExpr->isValueDependent() || |
| 4116 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4117 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 4118 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4119 | << MaxThreadsExpr->getSourceRange(); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4120 | return; |
| 4121 | } |
| 4122 | |
| 4123 | llvm::APSInt MinBlocks(32); |
| 4124 | if (Attr.getNumArgs() > 1) { |
| 4125 | Expr *MinBlocksExpr = Attr.getArg(1); |
| 4126 | if (MinBlocksExpr->isTypeDependent() || |
| 4127 | MinBlocksExpr->isValueDependent() || |
| 4128 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4129 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 4130 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4131 | << MinBlocksExpr->getSourceRange(); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4132 | return; |
| 4133 | } |
| 4134 | } |
| 4135 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4136 | D->addAttr(::new (S.Context) |
| 4137 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 4138 | MaxThreads.getZExtValue(), |
| 4139 | MinBlocks.getZExtValue(), |
| 4140 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4141 | } else { |
| 4142 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 4143 | } |
| 4144 | } |
| 4145 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4146 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 4147 | const AttributeList &Attr) { |
| 4148 | StringRef AttrName = Attr.getName()->getName(); |
| 4149 | if (!Attr.getParameterName()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4150 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 4151 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4152 | return; |
| 4153 | } |
| 4154 | |
| 4155 | if (Attr.getNumArgs() != 2) { |
| 4156 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 4157 | << Attr.getName() << /* required args = */ 3; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4158 | return; |
| 4159 | } |
| 4160 | |
| 4161 | IdentifierInfo *ArgumentKind = Attr.getParameterName(); |
| 4162 | |
| 4163 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 4164 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4165 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 4166 | return; |
| 4167 | } |
| 4168 | |
| 4169 | uint64_t ArgumentIdx; |
| 4170 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4171 | Attr.getLoc(), 2, |
| 4172 | Attr.getArg(0), ArgumentIdx)) |
| 4173 | return; |
| 4174 | |
| 4175 | uint64_t TypeTagIdx; |
| 4176 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4177 | Attr.getLoc(), 3, |
| 4178 | Attr.getArg(1), TypeTagIdx)) |
| 4179 | return; |
| 4180 | |
| 4181 | bool IsPointer = (AttrName == "pointer_with_type_tag"); |
| 4182 | if (IsPointer) { |
| 4183 | // Ensure that buffer has a pointer type. |
| 4184 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 4185 | if (!BufferTy->isPointerType()) { |
| 4186 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4187 | << Attr.getName(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4188 | } |
| 4189 | } |
| 4190 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4191 | D->addAttr(::new (S.Context) |
| 4192 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 4193 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 4194 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4195 | } |
| 4196 | |
| 4197 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 4198 | const AttributeList &Attr) { |
| 4199 | IdentifierInfo *PointerKind = Attr.getParameterName(); |
| 4200 | if (!PointerKind) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4201 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 4202 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4203 | return; |
| 4204 | } |
| 4205 | |
| 4206 | QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL); |
| 4207 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4208 | D->addAttr(::new (S.Context) |
| 4209 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
| 4210 | MatchingCType, |
| 4211 | Attr.getLayoutCompatible(), |
| 4212 | Attr.getMustBeNull(), |
| 4213 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4216 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4217 | // Checker-specific attribute handlers. |
| 4218 | //===----------------------------------------------------------------------===// |
| 4219 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4220 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4221 | return type->isDependentType() || |
| 4222 | type->isObjCObjectPointerType() || |
| 4223 | S.Context.isObjCNSObjectType(type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4224 | } |
| 4225 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4226 | return type->isDependentType() || |
| 4227 | type->isPointerType() || |
| 4228 | isValidSubjectOfNSAttribute(S, type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4229 | } |
| 4230 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4231 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4232 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4233 | if (!param) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4234 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4235 | << Attr.getRange() << Attr.getName() << ExpectedParameter; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4236 | return; |
| 4237 | } |
| 4238 | |
| 4239 | bool typeOK, cf; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4240 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4241 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 4242 | cf = false; |
| 4243 | } else { |
| 4244 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 4245 | cf = true; |
| 4246 | } |
| 4247 | |
| 4248 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4249 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4250 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4251 | return; |
| 4252 | } |
| 4253 | |
| 4254 | if (cf) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4255 | param->addAttr(::new (S.Context) |
| 4256 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 4257 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4258 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4259 | param->addAttr(::new (S.Context) |
| 4260 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 4261 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4262 | } |
| 4263 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4264 | static void handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 4265 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4266 | if (!isa<ObjCMethodDecl>(D)) { |
| 4267 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4268 | << Attr.getRange() << Attr.getName() << ExpectedMethod; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4269 | return; |
| 4270 | } |
| 4271 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4272 | D->addAttr(::new (S.Context) |
| 4273 | NSConsumesSelfAttr(Attr.getRange(), S.Context, |
| 4274 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4275 | } |
| 4276 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4277 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 4278 | const AttributeList &Attr) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4279 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4280 | QualType returnType; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4281 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4282 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4283 | returnType = MD->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4284 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4285 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4286 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | a23bd4c | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 4287 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4288 | returnType = PD->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4289 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4290 | returnType = FD->getResultType(); |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4291 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4292 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4293 | << Attr.getRange() << Attr.getName() |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4294 | << ExpectedFunctionOrMethod; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4295 | return; |
| 4296 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4297 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4298 | bool typeOK; |
| 4299 | bool cf; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4300 | switch (Attr.getKind()) { |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4301 | default: llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4302 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4303 | case AttributeList::AT_NSReturnsRetained: |
| 4304 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4305 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 4306 | cf = false; |
| 4307 | break; |
| 4308 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4309 | case AttributeList::AT_CFReturnsRetained: |
| 4310 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4311 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 4312 | cf = true; |
| 4313 | break; |
| 4314 | } |
| 4315 | |
| 4316 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4317 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4318 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4319 | return; |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4320 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4321 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4322 | switch (Attr.getKind()) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4323 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4324 | llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4325 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4326 | D->addAttr(::new (S.Context) |
| 4327 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 4328 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4329 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4330 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4331 | D->addAttr(::new (S.Context) |
| 4332 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4333 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4334 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4335 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4336 | D->addAttr(::new (S.Context) |
| 4337 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4338 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4339 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4340 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4341 | D->addAttr(::new (S.Context) |
| 4342 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4343 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4344 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4345 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4346 | D->addAttr(::new (S.Context) |
| 4347 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4348 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4349 | return; |
| 4350 | }; |
| 4351 | } |
| 4352 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4353 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4354 | const AttributeList &attr) { |
| 4355 | SourceLocation loc = attr.getLoc(); |
| 4356 | |
| 4357 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4358 | |
Fariborz Jahanian | 94d55d7 | 2012-04-21 17:51:44 +0000 | [diff] [blame] | 4359 | if (!method) { |
Fariborz Jahanian | 0e78afb | 2012-04-20 22:00:46 +0000 | [diff] [blame] | 4360 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4361 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4362 | return; |
| 4363 | } |
| 4364 | |
| 4365 | // Check that the method returns a normal pointer. |
| 4366 | QualType resultType = method->getResultType(); |
Fariborz Jahanian | f2e5945 | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4367 | |
| 4368 | if (!resultType->isReferenceType() && |
| 4369 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4370 | S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 4371 | << SourceRange(loc) |
| 4372 | << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2; |
| 4373 | |
| 4374 | // Drop the attribute. |
| 4375 | return; |
| 4376 | } |
| 4377 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4378 | method->addAttr(::new (S.Context) |
| 4379 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 4380 | attr.getAttributeSpellingListIndex())); |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4381 | } |
| 4382 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4383 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4384 | const AttributeList &attr) { |
| 4385 | SourceLocation loc = attr.getLoc(); |
| 4386 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4387 | |
| 4388 | if (!method) { |
| 4389 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 4390 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
| 4391 | return; |
| 4392 | } |
| 4393 | DeclContext *DC = method->getDeclContext(); |
| 4394 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4395 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4396 | << attr.getName() << 0; |
| 4397 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4398 | return; |
| 4399 | } |
| 4400 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4401 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4402 | << attr.getName() << 1; |
| 4403 | return; |
| 4404 | } |
| 4405 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4406 | method->addAttr(::new (S.Context) |
| 4407 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 4408 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4409 | } |
| 4410 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4411 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 4412 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
| 4413 | if (!isa<FunctionDecl>(D)) { |
| 4414 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4415 | << A.getRange() << A.getName() << ExpectedFunction; |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4416 | return; |
| 4417 | } |
| 4418 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4419 | bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4420 | |
| 4421 | // Check whether there's a conflicting attribute already present. |
| 4422 | Attr *Existing; |
| 4423 | if (IsAudited) { |
| 4424 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 4425 | } else { |
| 4426 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 4427 | } |
| 4428 | if (Existing) { |
| 4429 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 4430 | << A.getName() |
| 4431 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 4432 | << A.getRange() << Existing->getRange(); |
| 4433 | return; |
| 4434 | } |
| 4435 | |
| 4436 | // All clear; add the attribute. |
| 4437 | if (IsAudited) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4438 | D->addAttr(::new (S.Context) |
| 4439 | CFAuditedTransferAttr(A.getRange(), S.Context, |
| 4440 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4441 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4442 | D->addAttr(::new (S.Context) |
| 4443 | CFUnknownTransferAttr(A.getRange(), S.Context, |
| 4444 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4445 | } |
| 4446 | } |
| 4447 | |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4448 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4449 | const AttributeList &Attr) { |
| 4450 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4451 | if (!RD || RD->isUnion()) { |
| 4452 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4453 | << Attr.getRange() << Attr.getName() << ExpectedStruct; |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4454 | } |
| 4455 | |
| 4456 | IdentifierInfo *ParmName = Attr.getParameterName(); |
| 4457 | |
| 4458 | // In Objective-C, verify that the type names an Objective-C type. |
| 4459 | // We don't want to check this outside of ObjC because people sometimes |
| 4460 | // do crazy C declarations of Objective-C types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4461 | if (ParmName && S.getLangOpts().ObjC1) { |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4462 | // Check for an existing type with this name. |
| 4463 | LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(), |
| 4464 | Sema::LookupOrdinaryName); |
| 4465 | if (S.LookupName(R, Sc)) { |
| 4466 | NamedDecl *Target = R.getFoundDecl(); |
| 4467 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 4468 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 4469 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 4470 | } |
| 4471 | } |
| 4472 | } |
| 4473 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4474 | D->addAttr(::new (S.Context) |
| 4475 | NSBridgedAttr(Attr.getRange(), S.Context, ParmName, |
| 4476 | Attr.getAttributeSpellingListIndex())); |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4477 | } |
| 4478 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4479 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4480 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4481 | if (hasDeclarator(D)) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4482 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4483 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4484 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4485 | } |
| 4486 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4487 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4488 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4489 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4490 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4491 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4492 | return; |
| 4493 | } |
| 4494 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4495 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4496 | QualType type = vd->getType(); |
| 4497 | |
| 4498 | if (!type->isDependentType() && |
| 4499 | !type->isObjCLifetimeType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4500 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4501 | << type; |
| 4502 | return; |
| 4503 | } |
| 4504 | |
| 4505 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4506 | |
| 4507 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4508 | // going to infer. |
| 4509 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4510 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4511 | |
| 4512 | switch (lifetime) { |
| 4513 | case Qualifiers::OCL_None: |
| 4514 | assert(type->isDependentType() && |
| 4515 | "didn't infer lifetime for non-dependent type?"); |
| 4516 | break; |
| 4517 | |
| 4518 | case Qualifiers::OCL_Weak: // meaningful |
| 4519 | case Qualifiers::OCL_Strong: // meaningful |
| 4520 | break; |
| 4521 | |
| 4522 | case Qualifiers::OCL_ExplicitNone: |
| 4523 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4524 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4525 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4526 | break; |
| 4527 | } |
| 4528 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4529 | D->addAttr(::new (S.Context) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4530 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 4531 | Attr.getAttributeSpellingListIndex())); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4532 | } |
| 4533 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4534 | //===----------------------------------------------------------------------===// |
| 4535 | // Microsoft specific attribute handlers. |
| 4536 | //===----------------------------------------------------------------------===// |
| 4537 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4538 | // Check if MS extensions or some other language extensions are enabled. If |
| 4539 | // not, issue a diagnostic that the given attribute is unused. |
| 4540 | static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr, |
| 4541 | bool OtherExtension = false) { |
| 4542 | if (S.LangOpts.MicrosoftExt || OtherExtension) |
| 4543 | return true; |
| 4544 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4545 | return false; |
| 4546 | } |
| 4547 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4548 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4549 | if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland)) |
| 4550 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4551 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4552 | // check the attribute arguments. |
| 4553 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4554 | return; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4555 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4556 | Expr *Arg = Attr.getArg(0); |
| 4557 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 4558 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame^] | 4559 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 4560 | << Attr.getName() << AANT_ArgumentString; |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4561 | return; |
| 4562 | } |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4563 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4564 | StringRef StrRef = Str->getString(); |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4565 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4566 | bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' && |
| 4567 | StrRef.back() == '}'; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4568 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4569 | // Validate GUID length. |
| 4570 | if (IsCurly && StrRef.size() != 38) { |
| 4571 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4572 | return; |
| 4573 | } |
| 4574 | if (!IsCurly && StrRef.size() != 36) { |
| 4575 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4576 | return; |
| 4577 | } |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4578 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4579 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 4580 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" |
| 4581 | StringRef::iterator I = StrRef.begin(); |
| 4582 | if (IsCurly) // Skip the optional '{' |
| 4583 | ++I; |
| 4584 | |
| 4585 | for (int i = 0; i < 36; ++i) { |
| 4586 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 4587 | if (*I != '-') { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4588 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4589 | return; |
| 4590 | } |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4591 | } else if (!isHexDigit(*I)) { |
| 4592 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4593 | return; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4594 | } |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4595 | I++; |
| 4596 | } |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4597 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4598 | D->addAttr(::new (S.Context) |
| 4599 | UuidAttr(Attr.getRange(), S.Context, Str->getString(), |
| 4600 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4603 | static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4604 | if (!checkMicrosoftExt(S, Attr)) |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4605 | return; |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4606 | |
| 4607 | AttributeList::Kind Kind = Attr.getKind(); |
| 4608 | if (Kind == AttributeList::AT_SingleInheritance) |
| 4609 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4610 | ::new (S.Context) |
| 4611 | SingleInheritanceAttr(Attr.getRange(), S.Context, |
| 4612 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4613 | else if (Kind == AttributeList::AT_MultipleInheritance) |
| 4614 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4615 | ::new (S.Context) |
| 4616 | MultipleInheritanceAttr(Attr.getRange(), S.Context, |
| 4617 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4618 | else if (Kind == AttributeList::AT_VirtualInheritance) |
| 4619 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4620 | ::new (S.Context) |
| 4621 | VirtualInheritanceAttr(Attr.getRange(), S.Context, |
| 4622 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4623 | } |
| 4624 | |
| 4625 | static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4626 | if (!checkMicrosoftExt(S, Attr)) |
| 4627 | return; |
| 4628 | |
| 4629 | AttributeList::Kind Kind = Attr.getKind(); |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4630 | if (Kind == AttributeList::AT_Win64) |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4631 | D->addAttr( |
| 4632 | ::new (S.Context) Win64Attr(Attr.getRange(), S.Context, |
| 4633 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4634 | } |
| 4635 | |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4636 | static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4637 | if (!checkMicrosoftExt(S, Attr)) |
| 4638 | return; |
| 4639 | D->addAttr(::new (S.Context) |
| 4640 | ForceInlineAttr(Attr.getRange(), S.Context, |
| 4641 | Attr.getAttributeSpellingListIndex())); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4644 | static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4645 | if (!checkMicrosoftExt(S, Attr)) |
| 4646 | return; |
| 4647 | // Check linkage after possibly merging declaratinos. See |
| 4648 | // checkAttributesAfterMerging(). |
| 4649 | D->addAttr(::new (S.Context) |
| 4650 | SelectAnyAttr(Attr.getRange(), S.Context, |
| 4651 | Attr.getAttributeSpellingListIndex())); |
| 4652 | } |
| 4653 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4654 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4655 | // Top Level Sema Entry Points |
| 4656 | //===----------------------------------------------------------------------===// |
| 4657 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4658 | static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4659 | const AttributeList &Attr) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4660 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4661 | case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break; |
| 4662 | case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break; |
| 4663 | case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break; |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4664 | default: |
| 4665 | break; |
| 4666 | } |
| 4667 | } |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4668 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4669 | static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4670 | const AttributeList &Attr) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4671 | switch (Attr.getKind()) { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4672 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 4673 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 4674 | case AttributeList::AT_IBOutletCollection: |
| 4675 | handleIBOutletCollection(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4676 | case AttributeList::AT_AddressSpace: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4677 | case AttributeList::AT_ObjCGC: |
| 4678 | case AttributeList::AT_VectorSize: |
| 4679 | case AttributeList::AT_NeonVectorType: |
| 4680 | case AttributeList::AT_NeonPolyVectorType: |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4681 | case AttributeList::AT_Ptr32: |
| 4682 | case AttributeList::AT_Ptr64: |
| 4683 | case AttributeList::AT_SPtr: |
| 4684 | case AttributeList::AT_UPtr: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4685 | // Ignore these, these are type attributes, handled by |
| 4686 | // ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4687 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4688 | case AttributeList::AT_CUDADevice: |
| 4689 | case AttributeList::AT_CUDAHost: |
| 4690 | case AttributeList::AT_Overloadable: |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4691 | // Ignore, this is a non-inheritable attribute, handled |
| 4692 | // by ProcessNonInheritableDeclAttr. |
| 4693 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4694 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 4695 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 4696 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 4697 | case AttributeList::AT_AlwaysInline: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4698 | handleAlwaysInlineAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4699 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4700 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 4701 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4702 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 4703 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 4704 | case AttributeList::AT_CarriesDependency: |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4705 | handleDependencyAttr(S, scope, D, Attr); |
| 4706 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4707 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
| 4708 | case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break; |
| 4709 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4710 | case AttributeList::AT_CXX11NoReturn: |
| 4711 | handleCXX11NoReturnAttr(S, D, Attr); |
| 4712 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4713 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4714 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4715 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4716 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 4717 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4718 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4719 | break; |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4720 | case AttributeList::AT_MinSize: |
| 4721 | handleMinSizeAttr(S, D, Attr); |
| 4722 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4723 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4724 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4725 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
| 4726 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4727 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4728 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4729 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4730 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
| 4731 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
| 4732 | case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break; |
| 4733 | case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break; |
| 4734 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4735 | case AttributeList::AT_ownership_returns: |
| 4736 | case AttributeList::AT_ownership_takes: |
| 4737 | case AttributeList::AT_ownership_holds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4738 | handleOwnershipAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4739 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4740 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
| 4741 | case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break; |
| 4742 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
| 4743 | case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break; |
| 4744 | case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break; |
| 4745 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4746 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4747 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4748 | handleObjCOwnershipAttr(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4749 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4750 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4751 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4752 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4753 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4754 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4755 | case AttributeList::AT_ObjCRequiresSuper: |
| 4756 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4757 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4758 | case AttributeList::AT_NSBridged: |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4759 | handleNSBridgedAttr(S, scope, D, Attr); break; |
| 4760 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4761 | case AttributeList::AT_CFAuditedTransfer: |
| 4762 | case AttributeList::AT_CFUnknownTransfer: |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4763 | handleCFTransferAttr(S, D, Attr); break; |
| 4764 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4765 | // Checker-specific. |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4766 | case AttributeList::AT_CFConsumed: |
| 4767 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4768 | case AttributeList::AT_NSConsumesSelf: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4769 | handleNSConsumesSelfAttr(S, D, Attr); break; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4770 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4771 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4772 | case AttributeList::AT_NSReturnsNotRetained: |
| 4773 | case AttributeList::AT_CFReturnsNotRetained: |
| 4774 | case AttributeList::AT_NSReturnsRetained: |
| 4775 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4776 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4777 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4778 | case AttributeList::AT_WorkGroupSizeHint: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4779 | case AttributeList::AT_ReqdWorkGroupSize: |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4780 | handleWorkGroupSize(S, D, Attr); break; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 4781 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4782 | case AttributeList::AT_VecTypeHint: |
| 4783 | handleVecTypeHint(S, D, Attr); break; |
| 4784 | |
Joey Gouly | 96cead5 | 2013-03-14 09:54:43 +0000 | [diff] [blame] | 4785 | case AttributeList::AT_Endian: |
| 4786 | handleEndianAttr(S, D, Attr); |
| 4787 | break; |
| 4788 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4789 | case AttributeList::AT_InitPriority: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4790 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4791 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4792 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4793 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4794 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4795 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4796 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4797 | case AttributeList::AT_ArcWeakrefUnavailable: |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 4798 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 4799 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4800 | case AttributeList::AT_ObjCRootClass: |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4801 | handleObjCRootClassAttr(S, D, Attr); |
| 4802 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4803 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 4804 | handleObjCRequiresPropertyDefsAttr (S, D, Attr); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 4805 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4806 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4807 | case AttributeList::AT_ReturnsTwice: |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 4808 | handleReturnsTwiceAttr(S, D, Attr); |
| 4809 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4810 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4811 | case AttributeList::AT_Visibility: |
| 4812 | handleVisibilityAttr(S, D, Attr, false); |
| 4813 | break; |
| 4814 | case AttributeList::AT_TypeVisibility: |
| 4815 | handleVisibilityAttr(S, D, Attr, true); |
| 4816 | break; |
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 4817 | case AttributeList::AT_WarnUnused: |
| 4818 | handleWarnUnusedAttr(S, D, Attr); |
| 4819 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4820 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4821 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4822 | case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; |
| 4823 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4824 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4825 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4826 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4827 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4828 | case AttributeList::AT_ObjCException: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4829 | handleObjCExceptionAttr(S, D, Attr); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 4830 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4831 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4832 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4833 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4834 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4835 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4836 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
| 4837 | case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break; |
| 4838 | case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break; |
| 4839 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4840 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
| 4841 | case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break; |
| 4842 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4843 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4844 | // Just ignore |
| 4845 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4846 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4847 | handleNoInstrumentFunctionAttr(S, D, Attr); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 4848 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4849 | case AttributeList::AT_StdCall: |
| 4850 | case AttributeList::AT_CDecl: |
| 4851 | case AttributeList::AT_FastCall: |
| 4852 | case AttributeList::AT_ThisCall: |
| 4853 | case AttributeList::AT_Pascal: |
| 4854 | case AttributeList::AT_Pcs: |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4855 | case AttributeList::AT_PnaclCall: |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4856 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4857 | handleCallConvAttr(S, D, Attr); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4858 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4859 | case AttributeList::AT_OpenCLKernel: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4860 | handleOpenCLKernelAttr(S, D, Attr); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4861 | break; |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4862 | case AttributeList::AT_OpenCLImageAccess: |
| 4863 | handleOpenCLImageAccessAttr(S, D, Attr); |
| 4864 | break; |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4865 | |
| 4866 | // Microsoft attributes: |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4867 | case AttributeList::AT_MsProperty: break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4868 | case AttributeList::AT_MsStruct: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4869 | handleMsStructAttr(S, D, Attr); |
| 4870 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4871 | case AttributeList::AT_Uuid: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4872 | handleUuidAttr(S, D, Attr); |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4873 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4874 | case AttributeList::AT_SingleInheritance: |
| 4875 | case AttributeList::AT_MultipleInheritance: |
| 4876 | case AttributeList::AT_VirtualInheritance: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4877 | handleInheritanceAttr(S, D, Attr); |
| 4878 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4879 | case AttributeList::AT_Win64: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4880 | handlePortabilityAttr(S, D, Attr); |
| 4881 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4882 | case AttributeList::AT_ForceInline: |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4883 | handleForceInlineAttr(S, D, Attr); |
| 4884 | break; |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4885 | case AttributeList::AT_SelectAny: |
| 4886 | handleSelectAnyAttr(S, D, Attr); |
| 4887 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4888 | |
| 4889 | // Thread safety attributes: |
DeLesley Hutchins | 5c6134f | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 4890 | case AttributeList::AT_AssertExclusiveLock: |
| 4891 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 4892 | break; |
| 4893 | case AttributeList::AT_AssertSharedLock: |
| 4894 | handleAssertSharedLockAttr(S, D, Attr); |
| 4895 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4896 | case AttributeList::AT_GuardedVar: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4897 | handleGuardedVarAttr(S, D, Attr); |
| 4898 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4899 | case AttributeList::AT_PtGuardedVar: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4900 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4901 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4902 | case AttributeList::AT_ScopedLockable: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4903 | handleScopedLockableAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4904 | break; |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4905 | case AttributeList::AT_NoSanitizeAddress: |
| 4906 | handleNoSanitizeAddressAttr(S, D, Attr); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4907 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4908 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4909 | handleNoThreadSafetyAnalysis(S, D, Attr); |
| 4910 | break; |
| 4911 | case AttributeList::AT_NoSanitizeThread: |
| 4912 | handleNoSanitizeThread(S, D, Attr); |
| 4913 | break; |
| 4914 | case AttributeList::AT_NoSanitizeMemory: |
| 4915 | handleNoSanitizeMemory(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4916 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4917 | case AttributeList::AT_Lockable: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4918 | handleLockableAttr(S, D, Attr); |
| 4919 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4920 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4921 | handleGuardedByAttr(S, D, Attr); |
| 4922 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4923 | case AttributeList::AT_PtGuardedBy: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4924 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4925 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4926 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4927 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4928 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4929 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4930 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4931 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4932 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4933 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4934 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4935 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4936 | handleLockReturnedAttr(S, D, Attr); |
| 4937 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4938 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4939 | handleLocksExcludedAttr(S, D, Attr); |
| 4940 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4941 | case AttributeList::AT_SharedLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4942 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4943 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4944 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4945 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4946 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4947 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4948 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4949 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4950 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4951 | handleUnlockFunAttr(S, D, Attr); |
| 4952 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4953 | case AttributeList::AT_AcquiredBefore: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4954 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4955 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4956 | case AttributeList::AT_AcquiredAfter: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4957 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4958 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4959 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4960 | // Type safety attributes. |
| 4961 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4962 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4963 | break; |
| 4964 | case AttributeList::AT_TypeTagForDatatype: |
| 4965 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4966 | break; |
| 4967 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4968 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4969 | // Ask target about the attribute. |
| 4970 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 4971 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4972 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 4973 | diag::warn_unhandled_ms_attribute_ignored : |
| 4974 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4975 | break; |
| 4976 | } |
| 4977 | } |
| 4978 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4979 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4980 | /// 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] | 4981 | /// silently ignore it if a GNU attribute. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4982 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4983 | const AttributeList &Attr, |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4984 | bool NonInheritable, bool Inheritable, |
| 4985 | bool IncludeCXX11Attributes) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4986 | if (Attr.isInvalid()) |
| 4987 | return; |
| 4988 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4989 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 4990 | // instead. |
| 4991 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 4992 | return; |
| 4993 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4994 | if (NonInheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4995 | ProcessNonInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4996 | |
| 4997 | if (Inheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4998 | ProcessInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4999 | } |
| 5000 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5001 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 5002 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 5003 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5004 | const AttributeList *AttrList, |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5005 | bool NonInheritable, bool Inheritable, |
| 5006 | bool IncludeCXX11Attributes) { |
| 5007 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
| 5008 | ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable, |
| 5009 | IncludeCXX11Attributes); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5010 | |
| 5011 | // GCC accepts |
| 5012 | // static int a9 __attribute__((weakref)); |
| 5013 | // but that looks really pointless. We reject it. |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5014 | if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5015 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Rafael Espindola | 4d8a33b | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 5016 | cast<NamedDecl>(D)->getNameAsString(); |
| 5017 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5018 | return; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5019 | } |
| 5020 | } |
| 5021 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 5022 | // Annotation attributes are the only attributes allowed after an access |
| 5023 | // specifier. |
| 5024 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 5025 | const AttributeList *AttrList) { |
| 5026 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5027 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 5028 | handleAnnotateAttr(*this, ASDecl, *l); |
| 5029 | } else { |
| 5030 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 5031 | return true; |
| 5032 | } |
| 5033 | } |
| 5034 | |
| 5035 | return false; |
| 5036 | } |
| 5037 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5038 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 5039 | /// contains any decl attributes that we should warn about. |
| 5040 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 5041 | for ( ; A; A = A->getNext()) { |
| 5042 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 5043 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5044 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 5045 | |
| 5046 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 5047 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 5048 | << A->getName() << A->getRange(); |
| 5049 | } else { |
| 5050 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 5051 | << A->getName() << A->getRange(); |
| 5052 | } |
| 5053 | } |
| 5054 | } |
| 5055 | |
| 5056 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 5057 | /// used to build a declaration, complain about any decl attributes |
| 5058 | /// which might be lying around on it. |
| 5059 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 5060 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 5061 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 5062 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 5063 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 5064 | } |
| 5065 | |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5066 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5067 | /// \#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] | 5068 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 5069 | SourceLocation Loc) { |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5070 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5071 | NamedDecl *NewD = 0; |
| 5072 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5073 | FunctionDecl *NewFD; |
| 5074 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 5075 | // FIXME: Mangling? |
| 5076 | // FIXME: Is the qualifier info correct? |
| 5077 | // FIXME: Is the DeclContext correct? |
| 5078 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 5079 | Loc, Loc, DeclarationName(II), |
| 5080 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5081 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5082 | FD->hasPrototype(), |
| 5083 | false/*isConstexprSpecified*/); |
| 5084 | NewD = NewFD; |
| 5085 | |
| 5086 | if (FD->getQualifier()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5087 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5088 | |
| 5089 | // Fake up parameter variables; they are declared as if this were |
| 5090 | // a typedef. |
| 5091 | QualType FDTy = FD->getType(); |
| 5092 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 5093 | SmallVector<ParmVarDecl*, 16> Params; |
| 5094 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 5095 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 5096 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 5097 | Param->setScopeInfo(0, Params.size()); |
| 5098 | Params.push_back(Param); |
| 5099 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 5100 | NewFD->setParams(Params); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5101 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5102 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 5103 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5104 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5105 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5106 | VD->getStorageClass()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5107 | if (VD->getQualifier()) { |
| 5108 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5109 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5110 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5111 | } |
| 5112 | return NewD; |
| 5113 | } |
| 5114 | |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5115 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5116 | /// applied to it, possibly with an alias. |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5117 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5118 | if (W.getUsed()) return; // only do this once |
| 5119 | W.setUsed(true); |
| 5120 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 5121 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5122 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5123 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 5124 | NDId->getName())); |
| 5125 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5126 | WeakTopLevelDecl.push_back(NewD); |
| 5127 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 5128 | // to insert Decl at TU scope, sorry. |
| 5129 | DeclContext *SavedContext = CurContext; |
| 5130 | CurContext = Context.getTranslationUnitDecl(); |
| 5131 | PushOnScopeChains(NewD, S); |
| 5132 | CurContext = SavedContext; |
| 5133 | } else { // just add weak to existing |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5134 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5135 | } |
| 5136 | } |
| 5137 | |
Rafael Espindola | 65611bf | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5138 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 5139 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 5140 | // have to do this. |
| 5141 | LoadExternalWeakUndeclaredIdentifiers(); |
| 5142 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 5143 | NamedDecl *ND = NULL; |
| 5144 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 5145 | if (VD->isExternC()) |
| 5146 | ND = VD; |
| 5147 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5148 | if (FD->isExternC()) |
| 5149 | ND = FD; |
| 5150 | if (ND) { |
| 5151 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 5152 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 5153 | = WeakUndeclaredIdentifiers.find(Id); |
| 5154 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 5155 | WeakInfo W = I->second; |
| 5156 | DeclApplyPragmaWeak(S, ND, W); |
| 5157 | WeakUndeclaredIdentifiers[Id] = W; |
| 5158 | } |
| 5159 | } |
| 5160 | } |
| 5161 | } |
| 5162 | } |
| 5163 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5164 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 5165 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 5166 | /// 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] | 5167 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD, |
| 5168 | bool NonInheritable, bool Inheritable) { |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5169 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5170 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5171 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5172 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5173 | // Walk the declarator structure, applying decl attributes that were in a type |
| 5174 | // position to the decl itself. This handles cases like: |
| 5175 | // int *__attr__(x)** D; |
| 5176 | // when X is a decl attribute. |
| 5177 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 5178 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5179 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable, |
| 5180 | /*IncludeCXX11Attributes=*/false); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5181 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5182 | // Finally, apply any attributes on the decl itself. |
| 5183 | if (const AttributeList *Attrs = PD.getAttributes()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5184 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5185 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5186 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5187 | /// Is the given declaration allowed to use a forbidden type? |
| 5188 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 5189 | // Private ivars are always okay. Unfortunately, people don't |
| 5190 | // always properly make their ivars private, even in system headers. |
| 5191 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | a6b3380 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 5192 | // Function declarations in sys headers will be marked unavailable. |
| 5193 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 5194 | !isa<FunctionDecl>(decl)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5195 | return false; |
| 5196 | |
| 5197 | // Require it to be declared in a system header. |
| 5198 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 5199 | } |
| 5200 | |
| 5201 | /// Handle a delayed forbidden-type diagnostic. |
| 5202 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 5203 | Decl *decl) { |
| 5204 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 5205 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 5206 | "this system declaration uses an unsupported type")); |
| 5207 | return; |
| 5208 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5209 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5210 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 5211 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5212 | // kind of forbidden type messages on unavailable functions. |
| 5213 | if (FD->hasAttr<UnavailableAttr>() && |
| 5214 | diag.getForbiddenTypeDiagnostic() == |
| 5215 | diag::err_arc_array_param_no_ownership) { |
| 5216 | diag.Triggered = true; |
| 5217 | return; |
| 5218 | } |
| 5219 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5220 | |
| 5221 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 5222 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 5223 | diag.Triggered = true; |
| 5224 | } |
| 5225 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5226 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 5227 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5228 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5229 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5230 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5231 | // When delaying diagnostics to run in the context of a parsed |
| 5232 | // declaration, we only want to actually emit anything if parsing |
| 5233 | // succeeds. |
| 5234 | if (!decl) return; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5235 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5236 | // We emit all the active diagnostics in this pool or any of its |
| 5237 | // parents. In general, we'll get one pool for the decl spec |
| 5238 | // and a child pool for each declarator; in a decl group like: |
| 5239 | // deprecated_typedef foo, *bar, baz(); |
| 5240 | // only the declarator pops will be passed decls. This is correct; |
| 5241 | // we really do need to consider delayed diagnostics from the decl spec |
| 5242 | // for each of the different declarations. |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5243 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5244 | do { |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5245 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5246 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 5247 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 5248 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5249 | if (diag.Triggered) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5250 | continue; |
| 5251 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5252 | switch (diag.Kind) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5253 | case DelayedDiagnostic::Deprecation: |
John McCall | e8c904f | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 5254 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 5255 | if (!decl->isInvalidDecl()) |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5256 | HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5257 | break; |
| 5258 | |
| 5259 | case DelayedDiagnostic::Access: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5260 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5261 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5262 | |
| 5263 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5264 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5265 | break; |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5266 | } |
| 5267 | } |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5268 | } while ((pool = pool->getParent())); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5269 | } |
| 5270 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5271 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 5272 | /// been delayed in the current context instead of in the given pool. |
| 5273 | /// Essentially, this just moves them to the current pool. |
| 5274 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 5275 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 5276 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 5277 | curPool->steal(pool); |
| 5278 | } |
| 5279 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5280 | static bool isDeclDeprecated(Decl *D) { |
| 5281 | do { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5282 | if (D->isDeprecated()) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5283 | return true; |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 5284 | // A category implicitly has the availability of the interface. |
| 5285 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 5286 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5287 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 5288 | return false; |
| 5289 | } |
| 5290 | |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5291 | static void |
| 5292 | DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message, |
| 5293 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5294 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5295 | const ObjCPropertyDecl *ObjCPropery) { |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5296 | DeclarationName Name = D->getDeclName(); |
| 5297 | if (!Message.empty()) { |
| 5298 | S.Diag(Loc, diag::warn_deprecated_message) << Name << Message; |
| 5299 | S.Diag(D->getLocation(), |
| 5300 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5301 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5302 | if (ObjCPropery) |
| 5303 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5304 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5305 | } else if (!UnknownObjCClass) { |
| 5306 | S.Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 5307 | S.Diag(D->getLocation(), |
| 5308 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5309 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5310 | if (ObjCPropery) |
| 5311 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5312 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5313 | } else { |
| 5314 | S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name; |
| 5315 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 5316 | } |
| 5317 | } |
| 5318 | |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 5319 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5320 | Decl *Ctx) { |
| 5321 | if (isDeclDeprecated(Ctx)) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5322 | return; |
| 5323 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5324 | DD.Triggered = true; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5325 | DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(), |
| 5326 | DD.getDeprecationMessage(), DD.Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5327 | DD.getUnknownObjCClass(), |
| 5328 | DD.getObjCProperty()); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5329 | } |
| 5330 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5331 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 5332 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5333 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5334 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5335 | // Delay if we're currently parsing a declaration. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5336 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5337 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 5338 | UnknownObjCClass, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5339 | ObjCProperty, |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5340 | Message)); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5341 | return; |
| 5342 | } |
| 5343 | |
| 5344 | // Otherwise, don't warn if our current context is deprecated. |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 5345 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5346 | return; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5347 | DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5348 | } |