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()) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame^] | 1177 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1178 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 1179 | << Ex->getSourceRange(); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1180 | return; |
| 1181 | } |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1182 | SizeArgs.push_back(Idx); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | // check if the function returns a pointer |
| 1186 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 1187 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1188 | << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1191 | D->addAttr(::new (S.Context) |
| 1192 | AllocSizeAttr(Attr.getRange(), S.Context, |
| 1193 | SizeArgs.data(), SizeArgs.size(), |
| 1194 | Attr.getAttributeSpellingListIndex())); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1197 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1198 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1199 | // ignore it as well |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1200 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1201 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1202 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1203 | return; |
| 1204 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1205 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1206 | SmallVector<unsigned, 8> NonNullArgs; |
| 1207 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
| 1208 | Expr *Ex = Attr.getArg(i); |
| 1209 | uint64_t Idx; |
| 1210 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1211 | Attr.getLoc(), i + 1, Ex, Idx)) |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1212 | return; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1213 | |
| 1214 | // Is the function argument a pointer type? |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1215 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1216 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1217 | |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1218 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1219 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | c9ef405 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1220 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1221 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1222 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1223 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1224 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1225 | NonNullArgs.push_back(Idx); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1226 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1227 | |
| 1228 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1229 | // arguments have a nonnull attribute. |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1230 | if (NonNullArgs.empty()) { |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1231 | for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) { |
| 1232 | QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1233 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1234 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1235 | NonNullArgs.push_back(i); |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1236 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1237 | |
Ted Kremenek | ee1c08c | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1238 | // No pointer arguments? |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1239 | if (NonNullArgs.empty()) { |
| 1240 | // Warn the trivial case only if attribute is not coming from a |
| 1241 | // macro instantiation. |
| 1242 | if (Attr.getLoc().isFileID()) |
| 1243 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1244 | return; |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1245 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1246 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1247 | |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1248 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1249 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1250 | llvm::array_pod_sort(start, start + size); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1251 | D->addAttr(::new (S.Context) |
| 1252 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1253 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1256 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1257 | // This attribute must be applied to a function declaration. |
| 1258 | // The first argument to the attribute must be a string, |
| 1259 | // the name of the resource, for example "malloc". |
| 1260 | // The following arguments must be argument indexes, the arguments must be |
| 1261 | // of integer type for Returns, otherwise of pointer type. |
| 1262 | // 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] | 1263 | // after being held. free() should be __attribute((ownership_takes)), whereas |
| 1264 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1265 | |
| 1266 | if (!AL.getParameterName()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1267 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1268 | << AL.getName()->getName() << 1 << AANT_ArgumentString; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1269 | return; |
| 1270 | } |
| 1271 | // Figure out our Kind, and check arguments while we're at it. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1272 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1273 | switch (AL.getKind()) { |
| 1274 | case AttributeList::AT_ownership_takes: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1275 | K = OwnershipAttr::Takes; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1276 | if (AL.getNumArgs() < 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1277 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1278 | << AL.getName() << 2; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1279 | return; |
| 1280 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1281 | break; |
| 1282 | case AttributeList::AT_ownership_holds: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1283 | K = OwnershipAttr::Holds; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1284 | if (AL.getNumArgs() < 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1285 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1286 | << AL.getName() << 2; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1287 | return; |
| 1288 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1289 | break; |
| 1290 | case AttributeList::AT_ownership_returns: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1291 | K = OwnershipAttr::Returns; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1292 | if (AL.getNumArgs() > 1) { |
| 1293 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1294 | << AL.getName() << AL.getNumArgs() + 1; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1295 | return; |
| 1296 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1297 | break; |
| 1298 | default: |
| 1299 | // This should never happen given how we are called. |
| 1300 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1303 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1304 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1305 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1306 | return; |
| 1307 | } |
| 1308 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1309 | StringRef Module = AL.getParameterName()->getName(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1310 | |
| 1311 | // Normalize the argument, __foo__ becomes foo. |
| 1312 | if (Module.startswith("__") && Module.endswith("__")) |
| 1313 | Module = Module.substr(2, Module.size() - 4); |
| 1314 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1315 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1316 | SmallVector<unsigned, 8> OwnershipArgs; |
| 1317 | for (unsigned i = 0; i < AL.getNumArgs(); ++i) { |
| 1318 | Expr *Ex = AL.getArg(i); |
| 1319 | uint64_t Idx; |
| 1320 | if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(), |
| 1321 | AL.getLoc(), i + 1, Ex, Idx)) |
| 1322 | return; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1323 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1324 | switch (K) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1325 | case OwnershipAttr::Takes: |
| 1326 | case OwnershipAttr::Holds: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1327 | // Is the function argument a pointer type? |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1328 | QualType T = getFunctionOrMethodArgType(D, Idx); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1329 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| 1330 | // FIXME: Should also highlight argument in decl. |
| 1331 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1332 | << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1333 | << "pointer" |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1334 | << Ex->getSourceRange(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1335 | continue; |
| 1336 | } |
| 1337 | break; |
| 1338 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1339 | case OwnershipAttr::Returns: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1340 | if (AL.getNumArgs() > 1) { |
| 1341 | // Is the function argument an integer type? |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1342 | Expr *IdxExpr = AL.getArg(0); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1343 | llvm::APSInt ArgNum(32); |
| 1344 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1345 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1346 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
| 1347 | << "ownership_returns" << "integer" |
| 1348 | << IdxExpr->getSourceRange(); |
| 1349 | return; |
| 1350 | } |
| 1351 | } |
| 1352 | break; |
| 1353 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1354 | } // switch |
| 1355 | |
| 1356 | // Check we don't have a conflict with another ownership attribute. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1357 | for (specific_attr_iterator<OwnershipAttr> |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1358 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1359 | e = D->specific_attr_end<OwnershipAttr>(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1360 | i != e; ++i) { |
| 1361 | if ((*i)->getOwnKind() != K) { |
| 1362 | for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); |
| 1363 | I!=E; ++I) { |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1364 | if (Idx == *I) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1365 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1366 | << AL.getName()->getName() << "ownership_*"; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1367 | } |
| 1368 | } |
| 1369 | } |
| 1370 | } |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1371 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | unsigned* start = OwnershipArgs.data(); |
| 1375 | unsigned size = OwnershipArgs.size(); |
| 1376 | llvm::array_pod_sort(start, start + size); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1377 | |
| 1378 | if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1379 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1380 | << AL.getName() << 2; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1381 | return; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1382 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1383 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1384 | D->addAttr(::new (S.Context) |
| 1385 | OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size, |
| 1386 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1389 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1390 | // Check the attribute arguments. |
| 1391 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1392 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1393 | << Attr.getName() << 1; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1394 | return; |
| 1395 | } |
| 1396 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1397 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1398 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1399 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1400 | return; |
| 1401 | } |
| 1402 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1403 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1404 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1405 | // gcc rejects |
| 1406 | // class c { |
| 1407 | // static int a __attribute__((weakref ("v2"))); |
| 1408 | // static int b() __attribute__((weakref ("f3"))); |
| 1409 | // }; |
| 1410 | // and ignores the attributes of |
| 1411 | // void f(void) { |
| 1412 | // static int a __attribute__((weakref ("v2"))); |
| 1413 | // } |
| 1414 | // we reject them |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1415 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1416 | if (!Ctx->isFileContext()) { |
| 1417 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1418 | nd->getNameAsString(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1419 | return; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | // The GCC manual says |
| 1423 | // |
| 1424 | // At present, a declaration to which `weakref' is attached can only |
| 1425 | // be `static'. |
| 1426 | // |
| 1427 | // It also says |
| 1428 | // |
| 1429 | // Without a TARGET, |
| 1430 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1431 | // equivalent to `weak'. |
| 1432 | // |
| 1433 | // gcc 4.4.1 will accept |
| 1434 | // int a7 __attribute__((weakref)); |
| 1435 | // as |
| 1436 | // int a7 __attribute__((weak)); |
| 1437 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1438 | // it if this behaviour is actually used. |
| 1439 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1440 | // GCC rejects |
| 1441 | // static ((alias ("y"), weakref)). |
| 1442 | // Should we? How to check that weakref is before or after alias? |
| 1443 | |
| 1444 | if (Attr.getNumArgs() == 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1445 | Expr *Arg = Attr.getArg(0); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1446 | Arg = Arg->IgnoreParenCasts(); |
| 1447 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1448 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1449 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1450 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1451 | << Attr.getName() << 1 << AANT_ArgumentString; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1452 | return; |
| 1453 | } |
| 1454 | // GCC will accept anything as the argument of weakref. Should we |
| 1455 | // check for an existing decl? |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1456 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1457 | Str->getString())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1460 | D->addAttr(::new (S.Context) |
| 1461 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1462 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1465 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1466 | // check the attribute arguments. |
Aaron Ballman | ffa9d57 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 1467 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1468 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1469 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1470 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1471 | Arg = Arg->IgnoreParenCasts(); |
| 1472 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1473 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1474 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1475 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1476 | << Attr.getName() << AANT_ArgumentString; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1477 | return; |
| 1478 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1479 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1480 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | f5fe292 | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1481 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1482 | return; |
| 1483 | } |
| 1484 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1485 | // FIXME: check if target symbol exists in current file |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1486 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1487 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1488 | Str->getString(), |
| 1489 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1492 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1493 | // Check the attribute arguments. |
| 1494 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1495 | return; |
| 1496 | |
| 1497 | if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) { |
| 1498 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1499 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1500 | return; |
| 1501 | } |
| 1502 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1503 | D->addAttr(::new (S.Context) |
| 1504 | MinSizeAttr(Attr.getRange(), S.Context, |
| 1505 | Attr.getAttributeSpellingListIndex())); |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1508 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1509 | // Check the attribute arguments. |
| 1510 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1511 | return; |
| 1512 | |
| 1513 | if (!isa<FunctionDecl>(D)) { |
| 1514 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1515 | << Attr.getName() << ExpectedFunction; |
| 1516 | return; |
| 1517 | } |
| 1518 | |
| 1519 | if (D->hasAttr<HotAttr>()) { |
| 1520 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1521 | << Attr.getName() << "hot"; |
| 1522 | return; |
| 1523 | } |
| 1524 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1525 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1526 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1530 | // Check the attribute arguments. |
| 1531 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1532 | return; |
| 1533 | |
| 1534 | if (!isa<FunctionDecl>(D)) { |
| 1535 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1536 | << Attr.getName() << ExpectedFunction; |
| 1537 | return; |
| 1538 | } |
| 1539 | |
| 1540 | if (D->hasAttr<ColdAttr>()) { |
| 1541 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1542 | << Attr.getName() << "cold"; |
| 1543 | return; |
| 1544 | } |
| 1545 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1546 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1547 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1550 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1551 | // Check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1552 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1553 | return; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1554 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1555 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1556 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1557 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1558 | return; |
| 1559 | } |
| 1560 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1561 | D->addAttr(::new (S.Context) |
| 1562 | NakedAttr(Attr.getRange(), S.Context, |
| 1563 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1566 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 1567 | const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1568 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1569 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1570 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1571 | << Attr.getName() << 0; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1572 | return; |
| 1573 | } |
| 1574 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1575 | if (!isa<FunctionDecl>(D)) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1576 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1577 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1578 | return; |
| 1579 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1580 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1581 | D->addAttr(::new (S.Context) |
| 1582 | AlwaysInlineAttr(Attr.getRange(), S.Context, |
| 1583 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1586 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1587 | const AttributeList &Attr) { |
| 1588 | // Check the attribute arguments. |
Aaron Ballman | ffa9d57 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 1589 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1590 | return; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1591 | |
| 1592 | Expr *Arg = Attr.getArg(0); |
| 1593 | Arg = Arg->IgnoreParenCasts(); |
| 1594 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1595 | |
| 1596 | // Check that it is a string. |
| 1597 | if (!Str) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1598 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1599 | << Attr.getName() << AANT_ArgumentString; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1600 | return; |
| 1601 | } |
| 1602 | |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 1603 | if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) { |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1604 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1605 | << Attr.getName() << ExpectedTLSVar; |
| 1606 | return; |
| 1607 | } |
| 1608 | |
| 1609 | // Check that the value. |
| 1610 | StringRef Model = Str->getString(); |
| 1611 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1612 | && Model != "initial-exec" && Model != "local-exec") { |
| 1613 | S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg); |
| 1614 | return; |
| 1615 | } |
| 1616 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1617 | D->addAttr(::new (S.Context) |
| 1618 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1619 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1622 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1623 | // Check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1624 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1625 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1626 | << Attr.getName() << 0; |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1627 | return; |
| 1628 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1629 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1630 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1631 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1632 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1633 | D->addAttr(::new (S.Context) |
| 1634 | MallocAttr(Attr.getRange(), S.Context, |
| 1635 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1636 | return; |
| 1637 | } |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1640 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1643 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1644 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1645 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1646 | return; |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1647 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1648 | D->addAttr(::new (S.Context) |
| 1649 | MayAliasAttr(Attr.getRange(), S.Context, |
| 1650 | Attr.getAttributeSpellingListIndex())); |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1653 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1654 | assert(!Attr.isInvalid()); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1655 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1656 | D->addAttr(::new (S.Context) |
| 1657 | NoCommonAttr(Attr.getRange(), S.Context, |
| 1658 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1659 | else |
| 1660 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1661 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1664 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1665 | assert(!Attr.isInvalid()); |
Eli Friedman | 3e1aca2 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1666 | |
| 1667 | if (S.LangOpts.CPlusPlus) { |
| 1668 | S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus); |
| 1669 | return; |
| 1670 | } |
| 1671 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1672 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1673 | D->addAttr(::new (S.Context) |
| 1674 | CommonAttr(Attr.getRange(), S.Context, |
| 1675 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1676 | else |
| 1677 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1678 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1681 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1682 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1683 | |
| 1684 | if (S.CheckNoReturnAttr(attr)) return; |
| 1685 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1686 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1687 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1688 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1689 | return; |
| 1690 | } |
| 1691 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1692 | D->addAttr(::new (S.Context) |
| 1693 | NoReturnAttr(attr.getRange(), S.Context, |
| 1694 | attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1698 | if (attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1699 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1700 | << attr.getName() << 0; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1701 | attr.setInvalid(); |
| 1702 | return true; |
| 1703 | } |
| 1704 | |
| 1705 | return false; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1708 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1709 | const AttributeList &Attr) { |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1710 | |
| 1711 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1712 | // because 'analyzer_noreturn' does not impact the type. |
| 1713 | |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1714 | if(!checkAttributeNumArgs(S, Attr, 0)) |
| 1715 | return; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1716 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1717 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1718 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1719 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1720 | && !VD->getType()->isFunctionPointerType())) { |
| 1721 | S.Diag(Attr.getLoc(), |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1722 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1723 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1724 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1725 | return; |
| 1726 | } |
| 1727 | } |
| 1728 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1729 | D->addAttr(::new (S.Context) |
| 1730 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1731 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1734 | static void handleCXX11NoReturnAttr(Sema &S, Decl *D, |
| 1735 | const AttributeList &Attr) { |
| 1736 | // C++11 [dcl.attr.noreturn]p1: |
| 1737 | // The attribute may be applied to the declarator-id in a function |
| 1738 | // declaration. |
| 1739 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 1740 | if (!FD) { |
| 1741 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1742 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1743 | return; |
| 1744 | } |
| 1745 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1746 | D->addAttr(::new (S.Context) |
| 1747 | CXX11NoReturnAttr(Attr.getRange(), S.Context, |
| 1748 | Attr.getAttributeSpellingListIndex())); |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1751 | // PS3 PPU-specific. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1752 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1753 | /* |
| 1754 | Returning a Vector Class in Registers |
| 1755 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1756 | According to the PPU ABI specifications, a class with a single member of |
| 1757 | vector type is returned in memory when used as the return value of a function. |
| 1758 | This results in inefficient code when implementing vector classes. To return |
| 1759 | the value in a single vector register, add the vecreturn attribute to the |
| 1760 | class definition. This attribute is also applicable to struct types. |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1761 | |
| 1762 | Example: |
| 1763 | |
| 1764 | struct Vector |
| 1765 | { |
| 1766 | __vector float xyzw; |
| 1767 | } __attribute__((vecreturn)); |
| 1768 | |
| 1769 | Vector Add(Vector lhs, Vector rhs) |
| 1770 | { |
| 1771 | Vector result; |
| 1772 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1773 | return result; // This will be returned in a register |
| 1774 | } |
| 1775 | */ |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1776 | if (!isa<RecordDecl>(D)) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1777 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1778 | << Attr.getName() << ExpectedClass; |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1779 | return; |
| 1780 | } |
| 1781 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1782 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1783 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1784 | return; |
| 1785 | } |
| 1786 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1787 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1788 | int count = 0; |
| 1789 | |
| 1790 | if (!isa<CXXRecordDecl>(record)) { |
| 1791 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1792 | return; |
| 1793 | } |
| 1794 | |
| 1795 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1796 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1797 | return; |
| 1798 | } |
| 1799 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1800 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1801 | iter != record->field_end(); iter++) { |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1802 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1803 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1804 | return; |
| 1805 | } |
| 1806 | count++; |
| 1807 | } |
| 1808 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1809 | D->addAttr(::new (S.Context) |
| 1810 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1811 | Attr.getAttributeSpellingListIndex())); |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1814 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1815 | const AttributeList &Attr) { |
| 1816 | if (isa<ParmVarDecl>(D)) { |
| 1817 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1818 | // parameter of a function declaration or lambda. |
| 1819 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1820 | S.Diag(Attr.getLoc(), |
| 1821 | diag::err_carries_dependency_param_not_function_decl); |
| 1822 | return; |
| 1823 | } |
| 1824 | } else if (!isa<FunctionDecl>(D)) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1825 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1826 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1827 | return; |
| 1828 | } |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1829 | |
| 1830 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1831 | Attr.getRange(), S.Context, |
| 1832 | Attr.getAttributeSpellingListIndex())); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1835 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1836 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1837 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1838 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1839 | << Attr.getName() << 0; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1840 | return; |
| 1841 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1842 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1843 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 568eae4 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1844 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1845 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1846 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1847 | return; |
| 1848 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1849 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1850 | D->addAttr(::new (S.Context) |
| 1851 | UnusedAttr(Attr.getRange(), S.Context, |
| 1852 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1855 | static void handleReturnsTwiceAttr(Sema &S, Decl *D, |
| 1856 | const AttributeList &Attr) { |
| 1857 | // check the attribute arguments. |
| 1858 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1859 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1860 | << Attr.getName() << 0; |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1861 | return; |
| 1862 | } |
| 1863 | |
| 1864 | if (!isa<FunctionDecl>(D)) { |
| 1865 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1866 | << Attr.getName() << ExpectedFunction; |
| 1867 | return; |
| 1868 | } |
| 1869 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1870 | D->addAttr(::new (S.Context) |
| 1871 | ReturnsTwiceAttr(Attr.getRange(), S.Context, |
| 1872 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1875 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1876 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1877 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1878 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1879 | << Attr.getName() << 0; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1880 | return; |
| 1881 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1882 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1883 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Daniel Dunbar | 186204b | 2009-02-13 22:48:56 +0000 | [diff] [blame] | 1884 | if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1885 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 1886 | return; |
| 1887 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1888 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1889 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1890 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1891 | return; |
| 1892 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1893 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1894 | D->addAttr(::new (S.Context) |
| 1895 | UsedAttr(Attr.getRange(), S.Context, |
| 1896 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1899 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1900 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1901 | if (Attr.getNumArgs() > 1) { |
| 1902 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1903 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1904 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1905 | |
| 1906 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1907 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1908 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1909 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1910 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1911 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1912 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1913 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1914 | << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1915 | return; |
| 1916 | } |
| 1917 | priority = Idx.getZExtValue(); |
| 1918 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1919 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1920 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1921 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1922 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1923 | return; |
| 1924 | } |
| 1925 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1926 | D->addAttr(::new (S.Context) |
| 1927 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1928 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1931 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1932 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1933 | if (Attr.getNumArgs() > 1) { |
| 1934 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1935 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1936 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1937 | |
| 1938 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1939 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1940 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1941 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1942 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1943 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1944 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1945 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1946 | << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1947 | return; |
| 1948 | } |
| 1949 | priority = Idx.getZExtValue(); |
| 1950 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1951 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1952 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1953 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1954 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1955 | return; |
| 1956 | } |
| 1957 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1958 | D->addAttr(::new (S.Context) |
| 1959 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 1960 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1963 | template <typename AttrTy> |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 1964 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 1965 | const AttributeList &Attr) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1966 | unsigned NumArgs = Attr.getNumArgs(); |
| 1967 | if (NumArgs > 1) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1968 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1969 | return; |
| 1970 | } |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1971 | |
| 1972 | // Handle the case where the attribute has a text message. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1973 | StringRef Str; |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1974 | if (NumArgs == 1) { |
| 1975 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1976 | if (!SE) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 1977 | S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_argument_type) |
| 1978 | << Attr.getName() << AANT_ArgumentString; |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1979 | return; |
| 1980 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1981 | Str = SE->getString(); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1982 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1983 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1984 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 1985 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1988 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 1989 | const AttributeList &Attr) { |
Aaron Ballman | faf71a8 | 2013-07-23 15:16:00 +0000 | [diff] [blame] | 1990 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1991 | return; |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1992 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1993 | D->addAttr(::new (S.Context) |
| 1994 | ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context, |
| 1995 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 1998 | static void handleObjCRootClassAttr(Sema &S, Decl *D, |
| 1999 | const AttributeList &Attr) { |
| 2000 | if (!isa<ObjCInterfaceDecl>(D)) { |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 2001 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2002 | << Attr.getName() << ExpectedObjectiveCInterface; |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2003 | return; |
| 2004 | } |
| 2005 | |
Aaron Ballman | faf71a8 | 2013-07-23 15:16:00 +0000 | [diff] [blame] | 2006 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2007 | return; |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2008 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2009 | D->addAttr(::new (S.Context) |
| 2010 | ObjCRootClassAttr(Attr.getRange(), S.Context, |
| 2011 | Attr.getAttributeSpellingListIndex())); |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2014 | static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, |
| 2015 | const AttributeList &Attr) { |
Fariborz Jahanian | 341b8be | 2012-01-03 22:52:32 +0000 | [diff] [blame] | 2016 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 2017 | S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis); |
| 2018 | return; |
| 2019 | } |
| 2020 | |
Aaron Ballman | faf71a8 | 2013-07-23 15:16:00 +0000 | [diff] [blame] | 2021 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2022 | return; |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2023 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2024 | D->addAttr(::new (S.Context) |
| 2025 | ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context, |
| 2026 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2029 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2030 | IdentifierInfo *Platform, |
| 2031 | VersionTuple Introduced, |
| 2032 | VersionTuple Deprecated, |
| 2033 | VersionTuple Obsoleted) { |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2034 | StringRef PlatformName |
| 2035 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2036 | if (PlatformName.empty()) |
| 2037 | PlatformName = Platform->getName(); |
| 2038 | |
| 2039 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2040 | // of these steps are needed). |
| 2041 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2042 | !(Introduced <= Deprecated)) { |
| 2043 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2044 | << 1 << PlatformName << Deprecated.getAsString() |
| 2045 | << 0 << Introduced.getAsString(); |
| 2046 | return true; |
| 2047 | } |
| 2048 | |
| 2049 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2050 | !(Introduced <= Obsoleted)) { |
| 2051 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2052 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2053 | << 0 << Introduced.getAsString(); |
| 2054 | return true; |
| 2055 | } |
| 2056 | |
| 2057 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2058 | !(Deprecated <= Obsoleted)) { |
| 2059 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2060 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2061 | << 1 << Deprecated.getAsString(); |
| 2062 | return true; |
| 2063 | } |
| 2064 | |
| 2065 | return false; |
| 2066 | } |
| 2067 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2068 | /// \brief Check whether the two versions match. |
| 2069 | /// |
| 2070 | /// If either version tuple is empty, then they are assumed to match. If |
| 2071 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2072 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2073 | bool BeforeIsOkay) { |
| 2074 | if (X.empty() || Y.empty()) |
| 2075 | return true; |
| 2076 | |
| 2077 | if (X == Y) |
| 2078 | return true; |
| 2079 | |
| 2080 | if (BeforeIsOkay && X < Y) |
| 2081 | return true; |
| 2082 | |
| 2083 | return false; |
| 2084 | } |
| 2085 | |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2086 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2087 | IdentifierInfo *Platform, |
| 2088 | VersionTuple Introduced, |
| 2089 | VersionTuple Deprecated, |
| 2090 | VersionTuple Obsoleted, |
| 2091 | bool IsUnavailable, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2092 | StringRef Message, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2093 | bool Override, |
| 2094 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2095 | VersionTuple MergedIntroduced = Introduced; |
| 2096 | VersionTuple MergedDeprecated = Deprecated; |
| 2097 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2098 | bool FoundAny = false; |
| 2099 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2100 | if (D->hasAttrs()) { |
| 2101 | AttrVec &Attrs = D->getAttrs(); |
| 2102 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 2103 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 2104 | if (!OldAA) { |
| 2105 | ++i; |
| 2106 | continue; |
| 2107 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2108 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2109 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2110 | if (OldPlatform != Platform) { |
| 2111 | ++i; |
| 2112 | continue; |
| 2113 | } |
| 2114 | |
| 2115 | FoundAny = true; |
| 2116 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2117 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2118 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2119 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2120 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2121 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 2122 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 2123 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 2124 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 72daa3f | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 2125 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2126 | if (Override) { |
| 2127 | int Which = -1; |
| 2128 | VersionTuple FirstVersion; |
| 2129 | VersionTuple SecondVersion; |
| 2130 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 2131 | Which = 0; |
| 2132 | FirstVersion = OldIntroduced; |
| 2133 | SecondVersion = Introduced; |
| 2134 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 2135 | Which = 1; |
| 2136 | FirstVersion = Deprecated; |
| 2137 | SecondVersion = OldDeprecated; |
| 2138 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 2139 | Which = 2; |
| 2140 | FirstVersion = Obsoleted; |
| 2141 | SecondVersion = OldObsoleted; |
| 2142 | } |
| 2143 | |
| 2144 | if (Which == -1) { |
| 2145 | Diag(OldAA->getLocation(), |
| 2146 | diag::warn_mismatched_availability_override_unavail) |
| 2147 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2148 | } else { |
| 2149 | Diag(OldAA->getLocation(), |
| 2150 | diag::warn_mismatched_availability_override) |
| 2151 | << Which |
| 2152 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2153 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 2154 | } |
| 2155 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2156 | } else { |
| 2157 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2158 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2159 | } |
| 2160 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2161 | Attrs.erase(Attrs.begin() + i); |
| 2162 | --e; |
| 2163 | continue; |
| 2164 | } |
| 2165 | |
| 2166 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2167 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2168 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2169 | |
| 2170 | if (MergedIntroduced2.empty()) |
| 2171 | MergedIntroduced2 = OldIntroduced; |
| 2172 | if (MergedDeprecated2.empty()) |
| 2173 | MergedDeprecated2 = OldDeprecated; |
| 2174 | if (MergedObsoleted2.empty()) |
| 2175 | MergedObsoleted2 = OldObsoleted; |
| 2176 | |
| 2177 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2178 | MergedIntroduced2, MergedDeprecated2, |
| 2179 | MergedObsoleted2)) { |
| 2180 | Attrs.erase(Attrs.begin() + i); |
| 2181 | --e; |
| 2182 | continue; |
| 2183 | } |
| 2184 | |
| 2185 | MergedIntroduced = MergedIntroduced2; |
| 2186 | MergedDeprecated = MergedDeprecated2; |
| 2187 | MergedObsoleted = MergedObsoleted2; |
| 2188 | ++i; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2189 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | if (FoundAny && |
| 2193 | MergedIntroduced == Introduced && |
| 2194 | MergedDeprecated == Deprecated && |
| 2195 | MergedObsoleted == Obsoleted) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2196 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2197 | |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2198 | // Only create a new attribute if !Override, but we want to do |
| 2199 | // the checking. |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2200 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2201 | MergedDeprecated, MergedObsoleted) && |
| 2202 | !Override) { |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2203 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 2204 | Introduced, Deprecated, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2205 | Obsoleted, IsUnavailable, Message, |
| 2206 | AttrSpellingListIndex); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2207 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2208 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2211 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2212 | const AttributeList &Attr) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2213 | IdentifierInfo *Platform = Attr.getParameterName(); |
| 2214 | SourceLocation PlatformLoc = Attr.getParameterLoc(); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2215 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2216 | |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2217 | if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2218 | S.Diag(PlatformLoc, diag::warn_availability_unknown_platform) |
| 2219 | << Platform; |
| 2220 | |
Rafael Espindola | 8c4222a | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2221 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 2222 | if (!ND) { |
| 2223 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2224 | return; |
| 2225 | } |
| 2226 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2227 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2228 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2229 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2230 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2231 | StringRef Str; |
| 2232 | const StringLiteral *SE = |
| 2233 | dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr()); |
| 2234 | if (SE) |
| 2235 | Str = SE->getString(); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2236 | |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2237 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2238 | Platform, |
| 2239 | Introduced.Version, |
| 2240 | Deprecated.Version, |
| 2241 | Obsoleted.Version, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2242 | IsUnavailable, Str, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2243 | /*Override=*/false, |
| 2244 | Index); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2245 | if (NewAttr) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2246 | D->addAttr(NewAttr); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2249 | template <class T> |
| 2250 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2251 | typename T::VisibilityType value, |
| 2252 | unsigned attrSpellingListIndex) { |
| 2253 | T *existingAttr = D->getAttr<T>(); |
| 2254 | if (existingAttr) { |
| 2255 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2256 | if (existingValue == value) |
| 2257 | return NULL; |
| 2258 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2259 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2260 | D->dropAttr<T>(); |
| 2261 | } |
| 2262 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2263 | } |
| 2264 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2265 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2266 | VisibilityAttr::VisibilityType Vis, |
| 2267 | unsigned AttrSpellingListIndex) { |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2268 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2269 | AttrSpellingListIndex); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2272 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2273 | TypeVisibilityAttr::VisibilityType Vis, |
| 2274 | unsigned AttrSpellingListIndex) { |
| 2275 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2276 | AttrSpellingListIndex); |
| 2277 | } |
| 2278 | |
| 2279 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2280 | bool isTypeVisibility) { |
| 2281 | // Visibility attributes don't mean anything on a typedef. |
| 2282 | if (isa<TypedefNameDecl>(D)) { |
| 2283 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2284 | << Attr.getName(); |
| 2285 | return; |
| 2286 | } |
| 2287 | |
| 2288 | // 'type_visibility' can only go on a type or namespace. |
| 2289 | if (isTypeVisibility && |
| 2290 | !(isa<TagDecl>(D) || |
| 2291 | isa<ObjCInterfaceDecl>(D) || |
| 2292 | isa<NamespaceDecl>(D))) { |
| 2293 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2294 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2295 | return; |
| 2296 | } |
| 2297 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2298 | // check the attribute arguments. |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2299 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2300 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2301 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2302 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2303 | Arg = Arg->IgnoreParenCasts(); |
| 2304 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2305 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2306 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2307 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 2308 | << Attr.getName() << AANT_ArgumentString; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2309 | return; |
| 2310 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2311 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2312 | StringRef TypeStr = Str->getString(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2313 | VisibilityAttr::VisibilityType type; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2314 | |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2315 | if (TypeStr == "default") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2316 | type = VisibilityAttr::Default; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2317 | else if (TypeStr == "hidden") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2318 | type = VisibilityAttr::Hidden; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2319 | else if (TypeStr == "internal") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2320 | type = VisibilityAttr::Hidden; // FIXME |
John McCall | 4188760 | 2012-01-29 01:20:30 +0000 | [diff] [blame] | 2321 | else if (TypeStr == "protected") { |
| 2322 | // Complain about attempts to use protected visibility on targets |
| 2323 | // (like Darwin) that don't support it. |
| 2324 | if (!S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2325 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2326 | type = VisibilityAttr::Default; |
| 2327 | } else { |
| 2328 | type = VisibilityAttr::Protected; |
| 2329 | } |
| 2330 | } else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2331 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2332 | return; |
| 2333 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2334 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2335 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2336 | clang::Attr *newAttr; |
| 2337 | if (isTypeVisibility) { |
| 2338 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2339 | (TypeVisibilityAttr::VisibilityType) type, |
| 2340 | Index); |
| 2341 | } else { |
| 2342 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2343 | } |
| 2344 | if (newAttr) |
| 2345 | D->addAttr(newAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2348 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2349 | const AttributeList &Attr) { |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2350 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 2351 | if (!method) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2352 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2353 | << ExpectedMethod; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2354 | return; |
| 2355 | } |
| 2356 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2357 | if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) { |
| 2358 | if (!Attr.getParameterName() && Attr.getNumArgs() == 1) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2359 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2360 | << Attr.getName() << 1 << AANT_ArgumentString; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2361 | } else { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2362 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2363 | << Attr.getName() << 0; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2364 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2365 | Attr.setInvalid(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2366 | return; |
| 2367 | } |
| 2368 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2369 | StringRef param = Attr.getParameterName()->getName(); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2370 | ObjCMethodFamilyAttr::FamilyKind family; |
| 2371 | if (param == "none") |
| 2372 | family = ObjCMethodFamilyAttr::OMF_None; |
| 2373 | else if (param == "alloc") |
| 2374 | family = ObjCMethodFamilyAttr::OMF_alloc; |
| 2375 | else if (param == "copy") |
| 2376 | family = ObjCMethodFamilyAttr::OMF_copy; |
| 2377 | else if (param == "init") |
| 2378 | family = ObjCMethodFamilyAttr::OMF_init; |
| 2379 | else if (param == "mutableCopy") |
| 2380 | family = ObjCMethodFamilyAttr::OMF_mutableCopy; |
| 2381 | else if (param == "new") |
| 2382 | family = ObjCMethodFamilyAttr::OMF_new; |
| 2383 | else { |
| 2384 | // Just warn and ignore it. This is future-proof against new |
| 2385 | // families being used in system headers. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2386 | S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2387 | return; |
| 2388 | } |
| 2389 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2390 | if (family == ObjCMethodFamilyAttr::OMF_init && |
| 2391 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2392 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2393 | << method->getResultType(); |
| 2394 | // Ignore the attribute. |
| 2395 | return; |
| 2396 | } |
| 2397 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2398 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2399 | S.Context, family)); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2402 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 2403 | const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2404 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2405 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2406 | |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2407 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 2408 | if (OCI == 0) { |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 2409 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2410 | << Attr.getName() << ExpectedObjectiveCInterface; |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2411 | return; |
| 2412 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2413 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2414 | D->addAttr(::new (S.Context) |
| 2415 | ObjCExceptionAttr(Attr.getRange(), S.Context, |
| 2416 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2419 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 081c883 | 2013-07-23 12:13:14 +0000 | [diff] [blame] | 2420 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2421 | return; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2422 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2423 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2424 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2425 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2426 | return; |
| 2427 | } |
| 2428 | } |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2429 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2430 | QualType T = PD->getType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2431 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2432 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2433 | return; |
| 2434 | } |
| 2435 | } |
| 2436 | else { |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2437 | // It is okay to include this attribute on properties, e.g.: |
| 2438 | // |
| 2439 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2440 | // |
| 2441 | // In this case it follows tradition and suppresses an error in the above |
| 2442 | // case. |
Fariborz Jahanian | 9b2eb7b | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2443 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2444 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2445 | D->addAttr(::new (S.Context) |
| 2446 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2447 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2448 | } |
| 2449 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2450 | static void |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2451 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 081c883 | 2013-07-23 12:13:14 +0000 | [diff] [blame] | 2452 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2453 | return; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2454 | |
| 2455 | if (!isa<FunctionDecl>(D)) { |
| 2456 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 2457 | return; |
| 2458 | } |
| 2459 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2460 | D->addAttr(::new (S.Context) |
| 2461 | OverloadableAttr(Attr.getRange(), S.Context, |
| 2462 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2463 | } |
| 2464 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2465 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2466 | if (!Attr.getParameterName()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2467 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2468 | << Attr.getName() << 1 << AANT_ArgumentString; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2469 | return; |
| 2470 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2471 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2472 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2473 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2474 | << Attr.getName() << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2475 | return; |
| 2476 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2477 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2478 | BlocksAttr::BlockType type; |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 2479 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2480 | type = BlocksAttr::ByRef; |
| 2481 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2482 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2483 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2484 | return; |
| 2485 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2486 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2487 | D->addAttr(::new (S.Context) |
| 2488 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2489 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2492 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2493 | // check the attribute arguments. |
| 2494 | if (Attr.getNumArgs() > 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2495 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2496 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2499 | unsigned sentinel = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2500 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2501 | Expr *E = Attr.getArg(0); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2502 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2503 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2504 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2505 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2506 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2507 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2508 | return; |
| 2509 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2510 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2511 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2512 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2513 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2514 | return; |
| 2515 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2516 | |
| 2517 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2520 | unsigned nullPos = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2521 | if (Attr.getNumArgs() > 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2522 | Expr *E = Attr.getArg(1); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2523 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2524 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2525 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2526 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2527 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2528 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2529 | return; |
| 2530 | } |
| 2531 | nullPos = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2532 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2533 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2534 | // FIXME: This error message could be improved, it would be nice |
| 2535 | // to say what the bounds actually are. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2536 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2537 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2538 | return; |
| 2539 | } |
| 2540 | } |
| 2541 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2542 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2543 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2544 | if (isa<FunctionNoProtoType>(FT)) { |
| 2545 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2546 | return; |
| 2547 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2548 | |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2549 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2550 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2551 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2552 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2553 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2554 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2555 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2556 | return; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2557 | } |
Eli Friedman | a0b2ba1 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2558 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2559 | if (!BD->isVariadic()) { |
| 2560 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2561 | return; |
| 2562 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2563 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2564 | QualType Ty = V->getType(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2565 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2566 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2567 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2568 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2569 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2570 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2571 | return; |
| 2572 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2573 | } else { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2574 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2575 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2576 | return; |
| 2577 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2578 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2579 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2580 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2581 | return; |
| 2582 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2583 | D->addAttr(::new (S.Context) |
| 2584 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2585 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2586 | } |
| 2587 | |
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 2588 | static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2589 | // Check the attribute arguments. |
| 2590 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 2591 | return; |
| 2592 | |
| 2593 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
| 2594 | RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context)); |
| 2595 | else |
| 2596 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2597 | } |
| 2598 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2599 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2600 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2601 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2602 | return; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2603 | |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 2604 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2605 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Kaelyn Uhrain | d449c79 | 2012-11-13 00:18:47 +0000 | [diff] [blame] | 2606 | << Attr.getName() << ExpectedFunctionMethodOrClass; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2607 | return; |
| 2608 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2609 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2610 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2611 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2612 | << Attr.getName() << 0; |
Nuno Lopes | f857798 | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2613 | return; |
| 2614 | } |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2615 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2616 | if (MD->getResultType()->isVoidType()) { |
| 2617 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2618 | << Attr.getName() << 1; |
| 2619 | return; |
| 2620 | } |
| 2621 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2622 | D->addAttr(::new (S.Context) |
| 2623 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2624 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2625 | } |
| 2626 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2627 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2628 | // check the attribute arguments. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2629 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2630 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2631 | << Attr.getName() << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2632 | return; |
| 2633 | } |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2634 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2635 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
Fariborz Jahanian | 13c7fcc | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 2636 | if (isa<CXXRecordDecl>(D)) { |
| 2637 | D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| 2638 | return; |
| 2639 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2640 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2641 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | f23ecd9 | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 2642 | return; |
| 2643 | } |
| 2644 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2645 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 2646 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2647 | nd->addAttr(::new (S.Context) |
| 2648 | WeakAttr(Attr.getRange(), S.Context, |
| 2649 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2652 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2653 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2654 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2655 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2656 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2657 | |
| 2658 | // weak_import only applies to variable & function declarations. |
| 2659 | bool isDef = false; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2660 | if (!D->canBeWeakImported(isDef)) { |
| 2661 | if (isDef) |
Reid Kleckner | beba3e8 | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2662 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2663 | << "weak_import"; |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2664 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2665 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 90eed21 | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2666 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2667 | // Nothing to warn about here. |
| 2668 | } else |
Fariborz Jahanian | c034974 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2669 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2670 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2671 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2672 | return; |
| 2673 | } |
| 2674 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2675 | D->addAttr(::new (S.Context) |
| 2676 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2677 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2678 | } |
| 2679 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2680 | // Handles reqd_work_group_size and work_group_size_hint. |
| 2681 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2682 | const AttributeList &Attr) { |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2683 | assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2684 | || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint); |
| 2685 | |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2686 | // Attribute has 3 arguments. |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2687 | if (!checkAttributeNumArgs(S, Attr, 3)) return; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2688 | |
| 2689 | unsigned WGSize[3]; |
| 2690 | for (unsigned i = 0; i < 3; ++i) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2691 | Expr *E = Attr.getArg(i); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2692 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2693 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2694 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame^] | 2695 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 2696 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 2697 | << E->getSourceRange(); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2698 | return; |
| 2699 | } |
| 2700 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 2701 | } |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2702 | |
| 2703 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2704 | && D->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 2705 | ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>(); |
| 2706 | if (!(A->getXDim() == WGSize[0] && |
| 2707 | A->getYDim() == WGSize[1] && |
| 2708 | A->getZDim() == WGSize[2])) { |
| 2709 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2710 | Attr.getName(); |
| 2711 | } |
| 2712 | } |
| 2713 | |
| 2714 | if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint |
| 2715 | && D->hasAttr<WorkGroupSizeHintAttr>()) { |
| 2716 | WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>(); |
| 2717 | if (!(A->getXDim() == WGSize[0] && |
| 2718 | A->getYDim() == WGSize[1] && |
| 2719 | A->getZDim() == WGSize[2])) { |
| 2720 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2721 | Attr.getName(); |
| 2722 | } |
| 2723 | } |
| 2724 | |
| 2725 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize) |
| 2726 | D->addAttr(::new (S.Context) |
| 2727 | ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2728 | WGSize[0], WGSize[1], WGSize[2], |
| 2729 | Attr.getAttributeSpellingListIndex())); |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2730 | else |
| 2731 | D->addAttr(::new (S.Context) |
| 2732 | WorkGroupSizeHintAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2733 | WGSize[0], WGSize[1], WGSize[2], |
| 2734 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2735 | } |
| 2736 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2737 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2738 | assert(Attr.getKind() == AttributeList::AT_VecTypeHint); |
| 2739 | |
| 2740 | // Attribute has 1 argument. |
| 2741 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 2742 | return; |
| 2743 | |
| 2744 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg()); |
| 2745 | |
| 2746 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2747 | (ParmType->isBooleanType() || |
| 2748 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2749 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2750 | << ParmType; |
| 2751 | return; |
| 2752 | } |
| 2753 | |
| 2754 | if (Attr.getKind() == AttributeList::AT_VecTypeHint && |
| 2755 | D->hasAttr<VecTypeHintAttr>()) { |
| 2756 | VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>(); |
| 2757 | if (A->getTypeHint() != ParmType) { |
| 2758 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2759 | return; |
| 2760 | } |
| 2761 | } |
| 2762 | |
| 2763 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
| 2764 | ParmType, Attr.getLoc())); |
| 2765 | } |
| 2766 | |
Joey Gouly | 96cead5 | 2013-03-14 09:54:43 +0000 | [diff] [blame] | 2767 | static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2768 | if (!dyn_cast<VarDecl>(D)) |
| 2769 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian" |
| 2770 | << 9; |
| 2771 | StringRef EndianType = Attr.getParameterName()->getName(); |
| 2772 | if (EndianType != "host" && EndianType != "device") |
| 2773 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType; |
| 2774 | } |
| 2775 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2776 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2777 | StringRef Name, |
| 2778 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2779 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2780 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2781 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2782 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2783 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2784 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2785 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2786 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2787 | AttrSpellingListIndex); |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2790 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2791 | // Attribute has no arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2792 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2793 | return; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2794 | |
| 2795 | // Make sure that there is a string literal as the sections's single |
| 2796 | // argument. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2797 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2798 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2799 | if (!SE) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2800 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
| 2801 | << Attr.getName() << AANT_ArgumentString; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2802 | return; |
| 2803 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2804 | |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2805 | // If the target wants to validate the section specifier, make it happen. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2806 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString()); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2807 | if (!Error.empty()) { |
| 2808 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 2809 | << Error; |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2810 | return; |
| 2811 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2812 | |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2813 | // This attribute cannot be applied to local variables. |
| 2814 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 2815 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 2816 | return; |
| 2817 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2818 | |
| 2819 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2820 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2821 | SE->getString(), Index); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2822 | if (NewAttr) |
| 2823 | D->addAttr(NewAttr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2826 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2827 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2828 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2829 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2830 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2831 | << Attr.getName() << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2832 | return; |
| 2833 | } |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2834 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2835 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2836 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2837 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2838 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2839 | D->addAttr(::new (S.Context) |
| 2840 | NoThrowAttr(Attr.getRange(), S.Context, |
| 2841 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2842 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2845 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2846 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2847 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2848 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2849 | << Attr.getName() << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2850 | return; |
| 2851 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2852 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2853 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2854 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2855 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2856 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2857 | D->addAttr(::new (S.Context) |
| 2858 | ConstAttr(Attr.getRange(), S.Context, |
| 2859 | Attr.getAttributeSpellingListIndex() )); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2860 | } |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2863 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2864 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2865 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2866 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2867 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2868 | D->addAttr(::new (S.Context) |
| 2869 | PureAttr(Attr.getRange(), S.Context, |
| 2870 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2873 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2874 | if (!Attr.getParameterName()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2875 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2876 | << Attr.getName() << 1; |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2877 | return; |
| 2878 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2879 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2880 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2881 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2882 | << Attr.getName() << 1; |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2883 | return; |
| 2884 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2885 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2886 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2887 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2888 | if (!VD || !VD->hasLocalStorage()) { |
| 2889 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 2890 | return; |
| 2891 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2892 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2893 | // Look up the function |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2894 | // FIXME: Lookup probably isn't looking in the right place |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2895 | NamedDecl *CleanupDecl |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2896 | = S.LookupSingleName(S.TUScope, Attr.getParameterName(), |
| 2897 | Attr.getParameterLoc(), Sema::LookupOrdinaryName); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2898 | if (!CleanupDecl) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2899 | S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2900 | Attr.getParameterName(); |
| 2901 | return; |
| 2902 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2903 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2904 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 2905 | if (!FD) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2906 | S.Diag(Attr.getParameterLoc(), |
| 2907 | diag::err_attribute_cleanup_arg_not_function) |
| 2908 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2909 | return; |
| 2910 | } |
| 2911 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2912 | if (FD->getNumParams() != 1) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2913 | S.Diag(Attr.getParameterLoc(), |
| 2914 | diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2915 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2916 | return; |
| 2917 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2918 | |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2919 | // We're currently more strict than GCC about what function types we accept. |
| 2920 | // If this ever proves to be a problem it should be easy to fix. |
| 2921 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2922 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2923 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2924 | ParamTy, Ty) != Sema::Compatible) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2925 | S.Diag(Attr.getParameterLoc(), |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2926 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 2927 | Attr.getParameterName() << ParamTy << Ty; |
| 2928 | return; |
| 2929 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2930 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2931 | D->addAttr(::new (S.Context) |
| 2932 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2933 | Attr.getAttributeSpellingListIndex())); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2934 | S.MarkFunctionReferenced(Attr.getParameterLoc(), FD); |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 2935 | S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc()); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2938 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2939 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2940 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2941 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2942 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2943 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2944 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2945 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2946 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2947 | return; |
| 2948 | } |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2949 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2950 | Expr *IdxExpr = Attr.getArg(0); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2951 | uint64_t ArgIdx; |
| 2952 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 2953 | Attr.getLoc(), 1, IdxExpr, ArgIdx)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2954 | return; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2955 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2956 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2957 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2958 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2959 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2960 | if (not_nsstring_type && |
| 2961 | !isCFStringType(Ty, S.Context) && |
| 2962 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2963 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2964 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2965 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2966 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2967 | << IdxExpr->getSourceRange(); |
| 2968 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2969 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2970 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2971 | if (!isNSStringType(Ty, S.Context) && |
| 2972 | !isCFStringType(Ty, S.Context) && |
| 2973 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2974 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2975 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2976 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2977 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2978 | << IdxExpr->getSourceRange(); |
| 2979 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2982 | // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex |
| 2983 | // because that has corrected for the implicit this parameter, and is zero- |
| 2984 | // based. The attribute expects what the user wrote explicitly. |
| 2985 | llvm::APSInt Val; |
| 2986 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2987 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2988 | D->addAttr(::new (S.Context) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2989 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2990 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2993 | enum FormatAttrKind { |
| 2994 | CFStringFormat, |
| 2995 | NSStringFormat, |
| 2996 | StrftimeFormat, |
| 2997 | SupportedFormat, |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2998 | IgnoredFormat, |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2999 | InvalidFormat |
| 3000 | }; |
| 3001 | |
| 3002 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 3003 | /// types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3004 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3005 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 3006 | // Check for formats that get handled specially. |
| 3007 | .Case("NSString", NSStringFormat) |
| 3008 | .Case("CFString", CFStringFormat) |
| 3009 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3010 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3011 | // Otherwise, check for supported formats. |
| 3012 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 3013 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 3014 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3015 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3016 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 3017 | .Default(InvalidFormat); |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3018 | } |
| 3019 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3020 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3021 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3022 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 3023 | const AttributeList &Attr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3024 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3025 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3026 | return; |
| 3027 | } |
| 3028 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3029 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3030 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3031 | Attr.setInvalid(); |
| 3032 | return; |
| 3033 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3034 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3035 | if (S.Context.getAsArrayType(T)) |
| 3036 | T = S.Context.getBaseElementType(T); |
| 3037 | if (!T->getAs<RecordType>()) { |
| 3038 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3039 | Attr.setInvalid(); |
| 3040 | return; |
| 3041 | } |
| 3042 | |
Aaron Ballman | ffa9d57 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3043 | if (!checkAttributeNumArgs(S, Attr, 1)) { |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3044 | Attr.setInvalid(); |
| 3045 | return; |
| 3046 | } |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3047 | Expr *priorityExpr = Attr.getArg(0); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3048 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3049 | llvm::APSInt priority(32); |
| 3050 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 3051 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame^] | 3052 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3053 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3054 | << priorityExpr->getSourceRange(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3055 | Attr.setInvalid(); |
| 3056 | return; |
| 3057 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 3058 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3059 | if (prioritynum < 101 || prioritynum > 65535) { |
| 3060 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 3061 | << priorityExpr->getSourceRange(); |
| 3062 | Attr.setInvalid(); |
| 3063 | return; |
| 3064 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3065 | D->addAttr(::new (S.Context) |
| 3066 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 3067 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3070 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3071 | int FormatIdx, int FirstArg, |
| 3072 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3073 | // Check whether we already have an equivalent format attribute. |
| 3074 | for (specific_attr_iterator<FormatAttr> |
| 3075 | i = D->specific_attr_begin<FormatAttr>(), |
| 3076 | e = D->specific_attr_end<FormatAttr>(); |
| 3077 | i != e ; ++i) { |
| 3078 | FormatAttr *f = *i; |
| 3079 | if (f->getType() == Format && |
| 3080 | f->getFormatIdx() == FormatIdx && |
| 3081 | f->getFirstArg() == FirstArg) { |
| 3082 | // If we don't have a valid location for this attribute, adopt the |
| 3083 | // location. |
| 3084 | if (f->getLocation().isInvalid()) |
| 3085 | f->setRange(Range); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3086 | return NULL; |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3087 | } |
| 3088 | } |
| 3089 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3090 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg, |
| 3091 | AttrSpellingListIndex); |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3092 | } |
| 3093 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3094 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3095 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3096 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3097 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3098 | if (!Attr.getParameterName()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3099 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3100 | << Attr.getName() << 1 << AANT_ArgumentString; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3101 | return; |
| 3102 | } |
| 3103 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3104 | if (Attr.getNumArgs() != 2) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3105 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3106 | << Attr.getName() << 3; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3107 | return; |
| 3108 | } |
| 3109 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3110 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3111 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3112 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3113 | return; |
| 3114 | } |
| 3115 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3116 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3117 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3118 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 3119 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3120 | unsigned FirstIdx = 1; |
| 3121 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3122 | StringRef Format = Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3123 | |
| 3124 | // Normalize the argument, __foo__ becomes foo. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3125 | if (Format.startswith("__") && Format.endswith("__")) |
| 3126 | Format = Format.substr(2, Format.size() - 4); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3127 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3128 | // Check for supported formats. |
| 3129 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3130 | |
| 3131 | if (Kind == IgnoredFormat) |
| 3132 | return; |
| 3133 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3134 | if (Kind == InvalidFormat) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3135 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3136 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3137 | return; |
| 3138 | } |
| 3139 | |
| 3140 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3141 | Expr *IdxExpr = Attr.getArg(0); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3142 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3143 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 3144 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3145 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3146 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3147 | << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3148 | return; |
| 3149 | } |
| 3150 | |
| 3151 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3152 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3153 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3154 | return; |
| 3155 | } |
| 3156 | |
| 3157 | // FIXME: Do we need to bounds check? |
| 3158 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3159 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3160 | if (HasImplicitThisParam) { |
| 3161 | if (ArgIdx == 0) { |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3162 | S.Diag(Attr.getLoc(), |
| 3163 | diag::err_format_attribute_implicit_this_format_string) |
| 3164 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3165 | return; |
| 3166 | } |
| 3167 | ArgIdx--; |
| 3168 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3169 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3170 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3171 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3172 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3173 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3174 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3175 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3176 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3177 | return; |
| 3178 | } |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3179 | } else if (Kind == NSStringFormat) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3180 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3181 | // semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3182 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3183 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3184 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3185 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3186 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3187 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3188 | } else if (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3189 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3190 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3191 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3192 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3193 | return; |
| 3194 | } |
| 3195 | |
| 3196 | // check the 3rd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3197 | Expr *FirstArgExpr = Attr.getArg(1); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3198 | llvm::APSInt FirstArg(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3199 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 3200 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3201 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3202 | << Attr.getName() << 3 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3203 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3204 | return; |
| 3205 | } |
| 3206 | |
| 3207 | // check if the function is variadic if the 3rd argument non-zero |
| 3208 | if (FirstArg != 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3209 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3210 | ++NumArgs; // +1 for ... |
| 3211 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3212 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3213 | return; |
| 3214 | } |
| 3215 | } |
| 3216 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3217 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3218 | // variable the input is just the current time + the format string. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3219 | if (Kind == StrftimeFormat) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3220 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3221 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 3222 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3223 | return; |
| 3224 | } |
| 3225 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3226 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3227 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3228 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3229 | return; |
| 3230 | } |
| 3231 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3232 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format, |
| 3233 | Idx.getZExtValue(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3234 | FirstArg.getZExtValue(), |
| 3235 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3236 | if (NewAttr) |
| 3237 | D->addAttr(NewAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3238 | } |
| 3239 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3240 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 3241 | const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3242 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3243 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3244 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3245 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3246 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3247 | // Try to find the underlying union declaration. |
| 3248 | RecordDecl *RD = 0; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3249 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3250 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3251 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3252 | else |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3253 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3254 | |
| 3255 | if (!RD || !RD->isUnion()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3256 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3257 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3258 | return; |
| 3259 | } |
| 3260 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3261 | if (!RD->isCompleteDefinition()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3262 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3263 | diag::warn_transparent_union_attribute_not_definition); |
| 3264 | return; |
| 3265 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3266 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3267 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3268 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3269 | if (Field == FieldEnd) { |
| 3270 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3271 | return; |
| 3272 | } |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3273 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3274 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3275 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3276 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3277 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3278 | diag::warn_transparent_union_attribute_floating) |
| 3279 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3280 | return; |
| 3281 | } |
| 3282 | |
| 3283 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3284 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3285 | for (; Field != FieldEnd; ++Field) { |
| 3286 | QualType FieldType = Field->getType(); |
| 3287 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 3288 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 3289 | // Warn if we drop the attribute. |
| 3290 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3291 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3292 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3293 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3294 | diag::warn_transparent_union_attribute_field_size_align) |
| 3295 | << isSize << Field->getDeclName() << FieldBits; |
| 3296 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3297 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3298 | diag::note_transparent_union_first_field_size_align) |
| 3299 | << isSize << FirstBits; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3300 | return; |
| 3301 | } |
| 3302 | } |
| 3303 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3304 | RD->addAttr(::new (S.Context) |
| 3305 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 3306 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3309 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3310 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3311 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3312 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3313 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3314 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3315 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3316 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3317 | // Make sure that there is a string literal as the annotation's single |
| 3318 | // argument. |
| 3319 | if (!SE) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3320 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
| 3321 | << Attr.getName() << AANT_ArgumentString; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3322 | return; |
| 3323 | } |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3324 | |
| 3325 | // Don't duplicate annotations that are already set. |
| 3326 | for (specific_attr_iterator<AnnotateAttr> |
| 3327 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 3328 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
| 3329 | if ((*i)->getAnnotation() == SE->getString()) |
| 3330 | return; |
| 3331 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3332 | |
| 3333 | D->addAttr(::new (S.Context) |
| 3334 | AnnotateAttr(Attr.getRange(), S.Context, SE->getString(), |
| 3335 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3336 | } |
| 3337 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3338 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3339 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3340 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3341 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3342 | << Attr.getName() << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3343 | return; |
| 3344 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3345 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3346 | if (Attr.getNumArgs() == 0) { |
| 3347 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 3348 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 3349 | return; |
| 3350 | } |
| 3351 | |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3352 | Expr *E = Attr.getArg(0); |
| 3353 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3354 | S.Diag(Attr.getEllipsisLoc(), |
| 3355 | diag::err_pack_expansion_without_parameter_packs); |
| 3356 | return; |
| 3357 | } |
| 3358 | |
| 3359 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 3360 | return; |
| 3361 | |
| 3362 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 3363 | Attr.isPackExpansion()); |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3364 | } |
| 3365 | |
| 3366 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3367 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3368 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3369 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3370 | |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3371 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3372 | if (TmpAttr.isAlignas()) { |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3373 | // C++11 [dcl.align]p1: |
| 3374 | // An alignment-specifier may be applied to a variable or to a class |
| 3375 | // data member, but it shall not be applied to a bit-field, a function |
| 3376 | // parameter, the formal parameter of a catch clause, or a variable |
| 3377 | // declared with the register storage class specifier. An |
| 3378 | // alignment-specifier may also be applied to the declaration of a class |
| 3379 | // or enumeration type. |
| 3380 | // C11 6.7.5/2: |
| 3381 | // An alignment attribute shall not be specified in a declaration of |
| 3382 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3383 | // object declared with the register storage-class specifier. |
| 3384 | int DiagKind = -1; |
| 3385 | if (isa<ParmVarDecl>(D)) { |
| 3386 | DiagKind = 0; |
| 3387 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3388 | if (VD->getStorageClass() == SC_Register) |
| 3389 | DiagKind = 1; |
| 3390 | if (VD->isExceptionVariable()) |
| 3391 | DiagKind = 2; |
| 3392 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 3393 | if (FD->isBitField()) |
| 3394 | DiagKind = 3; |
| 3395 | } else if (!isa<TagDecl>(D)) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3396 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) |
| 3397 | << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'") |
Richard Smith | 5f838aa | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3398 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3399 | : ExpectedVariableFieldOrTag); |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3400 | return; |
| 3401 | } |
| 3402 | if (DiagKind != -1) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3403 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 3404 | << TmpAttr.isC11() << DiagKind; |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3405 | return; |
| 3406 | } |
| 3407 | } |
| 3408 | |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3409 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3410 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3411 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3412 | AA->setPackExpansion(IsPackExpansion); |
| 3413 | D->addAttr(AA); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3414 | return; |
| 3415 | } |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3416 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3417 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3418 | llvm::APSInt Alignment(32); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3419 | ExprResult ICE |
| 3420 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3421 | diag::err_aligned_attribute_argument_not_int, |
| 3422 | /*AllowFold*/ false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3423 | if (ICE.isInvalid()) |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3424 | return; |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3425 | |
| 3426 | // C++11 [dcl.align]p2: |
| 3427 | // -- if the constant expression evaluates to zero, the alignment |
| 3428 | // specifier shall have no effect |
| 3429 | // C11 6.7.5p6: |
| 3430 | // An alignment specification of zero has no effect. |
| 3431 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 3432 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3433 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 3434 | << E->getSourceRange(); |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3435 | return; |
| 3436 | } |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3437 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3438 | if (TmpAttr.isDeclspec()) { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3439 | // We've already verified it's a power of 2, now let's make sure it's |
| 3440 | // 8192 or less. |
| 3441 | if (Alignment.getZExtValue() > 8192) { |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3442 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3443 | << E->getSourceRange(); |
| 3444 | return; |
| 3445 | } |
| 3446 | } |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3447 | |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3448 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 3449 | ICE.take(), SpellingListIndex); |
| 3450 | AA->setPackExpansion(IsPackExpansion); |
| 3451 | D->addAttr(AA); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3452 | } |
| 3453 | |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3454 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3455 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3456 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3457 | // FIXME: Perform checking of type validity |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3458 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3459 | SpellingListIndex); |
| 3460 | AA->setPackExpansion(IsPackExpansion); |
| 3461 | D->addAttr(AA); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3462 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3463 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3464 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3465 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3466 | |
| 3467 | QualType Ty; |
| 3468 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3469 | Ty = VD->getType(); |
| 3470 | else |
| 3471 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
Richard Smith | 4da0903 | 2013-02-22 09:21:42 +0000 | [diff] [blame] | 3472 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3473 | return; |
| 3474 | |
| 3475 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3476 | // The combined effect of all alignment attributes in a declaration shall |
| 3477 | // not specify an alignment that is less strict than the alignment that |
| 3478 | // would otherwise be required for the entity being declared. |
| 3479 | AlignedAttr *AlignasAttr = 0; |
| 3480 | unsigned Align = 0; |
| 3481 | for (specific_attr_iterator<AlignedAttr> |
| 3482 | I = D->specific_attr_begin<AlignedAttr>(), |
| 3483 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 3484 | if (I->isAlignmentDependent()) |
| 3485 | return; |
| 3486 | if (I->isAlignas()) |
| 3487 | AlignasAttr = *I; |
| 3488 | Align = std::max(Align, I->getAlignment(Context)); |
| 3489 | } |
| 3490 | |
| 3491 | if (AlignasAttr && Align) { |
| 3492 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 3493 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 3494 | if (NaturalAlign > RequestedAlign) |
| 3495 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 3496 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 3497 | } |
| 3498 | } |
| 3499 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3500 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3501 | /// type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3502 | /// |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3503 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3504 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3505 | /// HImode, not an intermediate pointer. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3506 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3507 | // This attribute isn't documented, but glibc uses it. It changes |
| 3508 | // the width of an int or unsigned int to the specified size. |
| 3509 | |
| 3510 | // Check that there aren't any arguments |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3511 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3512 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3513 | |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3514 | |
| 3515 | IdentifierInfo *Name = Attr.getParameterName(); |
| 3516 | if (!Name) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3517 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3518 | return; |
| 3519 | } |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3520 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3521 | StringRef Str = Attr.getParameterName()->getName(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3522 | |
| 3523 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3524 | if (Str.startswith("__") && Str.endswith("__")) |
| 3525 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3526 | |
| 3527 | unsigned DestWidth = 0; |
| 3528 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3529 | bool ComplexMode = false; |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3530 | switch (Str.size()) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3531 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3532 | switch (Str[0]) { |
| 3533 | case 'Q': DestWidth = 8; break; |
| 3534 | case 'H': DestWidth = 16; break; |
| 3535 | case 'S': DestWidth = 32; break; |
| 3536 | case 'D': DestWidth = 64; break; |
| 3537 | case 'X': DestWidth = 96; break; |
| 3538 | case 'T': DestWidth = 128; break; |
| 3539 | } |
| 3540 | if (Str[1] == 'F') { |
| 3541 | IntegerMode = false; |
| 3542 | } else if (Str[1] == 'C') { |
| 3543 | IntegerMode = false; |
| 3544 | ComplexMode = true; |
| 3545 | } else if (Str[1] != 'I') { |
| 3546 | DestWidth = 0; |
| 3547 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3548 | break; |
| 3549 | case 4: |
| 3550 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3551 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3552 | if (Str == "word") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3553 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3554 | else if (Str == "byte") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3555 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3556 | break; |
| 3557 | case 7: |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3558 | if (Str == "pointer") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3559 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3560 | break; |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3561 | case 11: |
| 3562 | if (Str == "unwind_word") |
Rafael Espindola | 0b1de54 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3563 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3564 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3565 | } |
| 3566 | |
| 3567 | QualType OldTy; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3568 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3569 | OldTy = TD->getUnderlyingType(); |
| 3570 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3571 | OldTy = VD->getType(); |
| 3572 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3573 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3574 | << "mode" << Attr.getRange(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3575 | return; |
| 3576 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3577 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3578 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3579 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3580 | else if (IntegerMode) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3581 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3582 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3583 | } else if (ComplexMode) { |
| 3584 | if (!OldTy->isComplexType()) |
| 3585 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3586 | } else { |
| 3587 | if (!OldTy->isFloatingType()) |
| 3588 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3589 | } |
| 3590 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3591 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3592 | // and friends, at least with glibc. |
| 3593 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 3594 | // width on unusual platforms. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3595 | // FIXME: Make sure floating-point mappings are accurate |
| 3596 | // FIXME: Support XF and TF types |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3597 | QualType NewTy; |
| 3598 | switch (DestWidth) { |
| 3599 | case 0: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3600 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3601 | return; |
| 3602 | default: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3603 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3604 | return; |
| 3605 | case 8: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3606 | if (!IntegerMode) { |
| 3607 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3608 | return; |
| 3609 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3610 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3611 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3612 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3613 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3614 | break; |
| 3615 | case 16: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3616 | if (!IntegerMode) { |
| 3617 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3618 | return; |
| 3619 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3620 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3621 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3622 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3623 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3624 | break; |
| 3625 | case 32: |
| 3626 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3627 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3628 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3629 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3630 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3631 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3632 | break; |
| 3633 | case 64: |
| 3634 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3635 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3636 | else if (OldTy->isSignedIntegerType()) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3637 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3638 | NewTy = S.Context.LongTy; |
| 3639 | else |
| 3640 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3641 | else |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3642 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3643 | NewTy = S.Context.UnsignedLongTy; |
| 3644 | else |
| 3645 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3646 | break; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3647 | case 96: |
| 3648 | NewTy = S.Context.LongDoubleTy; |
| 3649 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3650 | case 128: |
Roman Divacky | 1964554 | 2013-07-03 21:08:41 +0000 | [diff] [blame] | 3651 | if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() != |
| 3652 | &llvm::APFloat::PPCDoubleDouble) { |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3653 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3654 | return; |
| 3655 | } |
Roman Divacky | f0d14cb | 2013-07-03 20:48:06 +0000 | [diff] [blame] | 3656 | if (IntegerMode) { |
| 3657 | if (OldTy->isSignedIntegerType()) |
| 3658 | NewTy = S.Context.Int128Ty; |
| 3659 | else |
| 3660 | NewTy = S.Context.UnsignedInt128Ty; |
| 3661 | } else |
| 3662 | NewTy = S.Context.LongDoubleTy; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3663 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3664 | } |
| 3665 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3666 | if (ComplexMode) { |
| 3667 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | // Install the new type. |
Enea Zaffanella | c2fa6b6 | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3671 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3672 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 3673 | else |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3674 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | c2fa6b6 | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3675 | |
| 3676 | D->addAttr(::new (S.Context) |
| 3677 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 3678 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3679 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3680 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3681 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3682 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3683 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3684 | return; |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 3685 | |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3686 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3687 | if (!VD->hasGlobalStorage()) |
| 3688 | S.Diag(Attr.getLoc(), |
| 3689 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3690 | << Attr.getName(); |
| 3691 | } else if (!isFunctionOrMethod(D)) { |
| 3692 | S.Diag(Attr.getLoc(), |
| 3693 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3694 | << Attr.getName(); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3695 | return; |
| 3696 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3697 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3698 | D->addAttr(::new (S.Context) |
| 3699 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3700 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3701 | } |
| 3702 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3703 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3704 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3705 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3706 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3707 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3708 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3709 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3710 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3711 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3712 | return; |
| 3713 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3714 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3715 | D->addAttr(::new (S.Context) |
| 3716 | NoInlineAttr(Attr.getRange(), S.Context, |
| 3717 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3718 | } |
| 3719 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3720 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 3721 | const AttributeList &Attr) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3722 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3723 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3724 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3725 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3726 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3727 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3728 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3729 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3730 | return; |
| 3731 | } |
| 3732 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3733 | D->addAttr(::new (S.Context) |
| 3734 | NoInstrumentFunctionAttr(Attr.getRange(), S.Context, |
| 3735 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3736 | } |
| 3737 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3738 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3739 | if (S.LangOpts.CUDA) { |
| 3740 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 3741 | if (Attr.hasParameterOrArguments()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3742 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3743 | << Attr.getName() << 0; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3744 | return; |
| 3745 | } |
| 3746 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3747 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3748 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3749 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3750 | return; |
| 3751 | } |
| 3752 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3753 | D->addAttr(::new (S.Context) |
| 3754 | CUDAConstantAttr(Attr.getRange(), S.Context, |
| 3755 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3756 | } else { |
| 3757 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 3758 | } |
| 3759 | } |
| 3760 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3761 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3762 | if (S.LangOpts.CUDA) { |
| 3763 | // check the attribute arguments. |
| 3764 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3765 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3766 | << Attr.getName() << 0; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3767 | return; |
| 3768 | } |
| 3769 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3770 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3771 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3772 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3773 | return; |
| 3774 | } |
| 3775 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3776 | D->addAttr(::new (S.Context) |
| 3777 | CUDADeviceAttr(Attr.getRange(), S.Context, |
| 3778 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3779 | } else { |
| 3780 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 3781 | } |
| 3782 | } |
| 3783 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3784 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3785 | if (S.LangOpts.CUDA) { |
| 3786 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3787 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3788 | return; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3789 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3790 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3791 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3792 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3793 | return; |
| 3794 | } |
| 3795 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3796 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3797 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 3798 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3799 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3800 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3801 | << FD->getType() |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3802 | << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(), |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3803 | "void"); |
| 3804 | } else { |
| 3805 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3806 | << FD->getType(); |
| 3807 | } |
| 3808 | return; |
| 3809 | } |
| 3810 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3811 | D->addAttr(::new (S.Context) |
| 3812 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
| 3813 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3814 | } else { |
| 3815 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 3816 | } |
| 3817 | } |
| 3818 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3819 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3820 | if (S.LangOpts.CUDA) { |
| 3821 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3822 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3823 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3824 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3825 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3826 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3827 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3828 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3829 | return; |
| 3830 | } |
| 3831 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3832 | D->addAttr(::new (S.Context) |
| 3833 | CUDAHostAttr(Attr.getRange(), S.Context, |
| 3834 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3835 | } else { |
| 3836 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 3837 | } |
| 3838 | } |
| 3839 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3840 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3841 | if (S.LangOpts.CUDA) { |
| 3842 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3843 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3844 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3845 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3846 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3847 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3848 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3849 | return; |
| 3850 | } |
| 3851 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3852 | D->addAttr(::new (S.Context) |
| 3853 | CUDASharedAttr(Attr.getRange(), S.Context, |
| 3854 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3855 | } else { |
| 3856 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 3857 | } |
| 3858 | } |
| 3859 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3860 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3861 | // check the attribute arguments. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3862 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3863 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3864 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3865 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3866 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3867 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3868 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3869 | return; |
| 3870 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3871 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3872 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3873 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3874 | return; |
| 3875 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3876 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3877 | D->addAttr(::new (S.Context) |
| 3878 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3879 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3880 | } |
| 3881 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3882 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3883 | if (hasDeclarator(D)) return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3884 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3885 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3886 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3887 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3888 | CallingConv CC; |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3889 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3890 | return; |
| 3891 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3892 | if (!isa<ObjCMethodDecl>(D)) { |
| 3893 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3894 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3895 | return; |
| 3896 | } |
| 3897 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3898 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3899 | case AttributeList::AT_FastCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3900 | D->addAttr(::new (S.Context) |
| 3901 | FastCallAttr(Attr.getRange(), S.Context, |
| 3902 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3903 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3904 | case AttributeList::AT_StdCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3905 | D->addAttr(::new (S.Context) |
| 3906 | StdCallAttr(Attr.getRange(), S.Context, |
| 3907 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3908 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3909 | case AttributeList::AT_ThisCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3910 | D->addAttr(::new (S.Context) |
| 3911 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3912 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 04633eb | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3913 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3914 | case AttributeList::AT_CDecl: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3915 | D->addAttr(::new (S.Context) |
| 3916 | CDeclAttr(Attr.getRange(), S.Context, |
| 3917 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3918 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3919 | case AttributeList::AT_Pascal: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3920 | D->addAttr(::new (S.Context) |
| 3921 | PascalAttr(Attr.getRange(), S.Context, |
| 3922 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3923 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3924 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3925 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3926 | switch (CC) { |
| 3927 | case CC_AAPCS: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3928 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3929 | break; |
| 3930 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3931 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3932 | break; |
| 3933 | default: |
| 3934 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3937 | D->addAttr(::new (S.Context) |
| 3938 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3939 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3940 | return; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3941 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3942 | case AttributeList::AT_PnaclCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3943 | D->addAttr(::new (S.Context) |
| 3944 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 3945 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3946 | return; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3947 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3948 | D->addAttr(::new (S.Context) |
| 3949 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3950 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3951 | return; |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3952 | |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3953 | default: |
| 3954 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3955 | } |
| 3956 | } |
| 3957 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3958 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Chandler Carruth | 56aeb40 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 3959 | assert(!Attr.isInvalid()); |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3960 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3963 | static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
| 3964 | assert(!Attr.isInvalid()); |
| 3965 | |
| 3966 | Expr *E = Attr.getArg(0); |
| 3967 | llvm::APSInt ArgNum(32); |
| 3968 | if (E->isTypeDependent() || E->isValueDependent() || |
| 3969 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame^] | 3970 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3971 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3972 | << E->getSourceRange(); |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3973 | return; |
| 3974 | } |
| 3975 | |
| 3976 | D->addAttr(::new (S.Context) OpenCLImageAccessAttr( |
| 3977 | Attr.getRange(), S.Context, ArgNum.getZExtValue())); |
| 3978 | } |
| 3979 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3980 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3981 | const FunctionDecl *FD) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3982 | if (attr.isInvalid()) |
| 3983 | return true; |
| 3984 | |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3985 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
| 3986 | if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3987 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3988 | << attr.getName() << ReqArgs; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3989 | attr.setInvalid(); |
| 3990 | return true; |
| 3991 | } |
| 3992 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3993 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 3994 | // move to TargetAttributesSema one day. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3995 | switch (attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3996 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3997 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3998 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3999 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 4000 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
| 4001 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4002 | Expr *Arg = attr.getArg(0); |
| 4003 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4004 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4005 | Diag(attr.getLoc(), diag::err_attribute_argument_type) << attr.getName() |
| 4006 | << AANT_ArgumentString; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4007 | attr.setInvalid(); |
| 4008 | return true; |
| 4009 | } |
| 4010 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4011 | StringRef StrRef = Str->getString(); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4012 | if (StrRef == "aapcs") { |
| 4013 | CC = CC_AAPCS; |
| 4014 | break; |
| 4015 | } else if (StrRef == "aapcs-vfp") { |
| 4016 | CC = CC_AAPCS_VFP; |
| 4017 | break; |
| 4018 | } |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4019 | |
| 4020 | attr.setInvalid(); |
| 4021 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 4022 | return true; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4023 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4024 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4025 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4026 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4027 | } |
| 4028 | |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4029 | const TargetInfo &TI = Context.getTargetInfo(); |
| 4030 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 4031 | if (A == TargetInfo::CCCR_Warning) { |
| 4032 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4033 | |
| 4034 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 4035 | if (FD) |
| 4036 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 4037 | TargetInfo::CCMT_NonMember; |
| 4038 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4039 | } |
| 4040 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4041 | return false; |
| 4042 | } |
| 4043 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4044 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4045 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4046 | |
| 4047 | unsigned numParams; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4048 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4049 | return; |
| 4050 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4051 | if (!isa<ObjCMethodDecl>(D)) { |
| 4052 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4053 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4054 | return; |
| 4055 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4056 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4057 | D->addAttr(::new (S.Context) |
| 4058 | RegparmAttr(Attr.getRange(), S.Context, numParams, |
| 4059 | Attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4060 | } |
| 4061 | |
| 4062 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 4063 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4064 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 4065 | if (Attr.isInvalid()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4066 | return true; |
| 4067 | |
Aaron Ballman | ffa9d57 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 4068 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4069 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4070 | return true; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4071 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4072 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4073 | Expr *NumParamsExpr = Attr.getArg(0); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4074 | llvm::APSInt NumParams(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 4075 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4076 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame^] | 4077 | Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 4078 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 4079 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4080 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4081 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4082 | } |
| 4083 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4084 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4085 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4086 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4087 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4088 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4089 | } |
| 4090 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4091 | numParams = NumParams.getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4092 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4093 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4094 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4095 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4096 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4097 | } |
| 4098 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4099 | return false; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4100 | } |
| 4101 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4102 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4103 | if (S.LangOpts.CUDA) { |
| 4104 | // check the attribute arguments. |
| 4105 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 4106 | // FIXME: 0 is not okay. |
| 4107 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4108 | return; |
| 4109 | } |
| 4110 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4111 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4112 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4113 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4114 | return; |
| 4115 | } |
| 4116 | |
| 4117 | Expr *MaxThreadsExpr = Attr.getArg(0); |
| 4118 | llvm::APSInt MaxThreads(32); |
| 4119 | if (MaxThreadsExpr->isTypeDependent() || |
| 4120 | MaxThreadsExpr->isValueDependent() || |
| 4121 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4122 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4123 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4124 | << MaxThreadsExpr->getSourceRange(); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4125 | return; |
| 4126 | } |
| 4127 | |
| 4128 | llvm::APSInt MinBlocks(32); |
| 4129 | if (Attr.getNumArgs() > 1) { |
| 4130 | Expr *MinBlocksExpr = Attr.getArg(1); |
| 4131 | if (MinBlocksExpr->isTypeDependent() || |
| 4132 | MinBlocksExpr->isValueDependent() || |
| 4133 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4134 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4135 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4136 | << MinBlocksExpr->getSourceRange(); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4137 | return; |
| 4138 | } |
| 4139 | } |
| 4140 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4141 | D->addAttr(::new (S.Context) |
| 4142 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 4143 | MaxThreads.getZExtValue(), |
| 4144 | MinBlocks.getZExtValue(), |
| 4145 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4146 | } else { |
| 4147 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 4148 | } |
| 4149 | } |
| 4150 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4151 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 4152 | const AttributeList &Attr) { |
| 4153 | StringRef AttrName = Attr.getName()->getName(); |
| 4154 | if (!Attr.getParameterName()) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4155 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4156 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4157 | return; |
| 4158 | } |
| 4159 | |
| 4160 | if (Attr.getNumArgs() != 2) { |
| 4161 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 4162 | << Attr.getName() << /* required args = */ 3; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4163 | return; |
| 4164 | } |
| 4165 | |
| 4166 | IdentifierInfo *ArgumentKind = Attr.getParameterName(); |
| 4167 | |
| 4168 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 4169 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4170 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 4171 | return; |
| 4172 | } |
| 4173 | |
| 4174 | uint64_t ArgumentIdx; |
| 4175 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4176 | Attr.getLoc(), 2, |
| 4177 | Attr.getArg(0), ArgumentIdx)) |
| 4178 | return; |
| 4179 | |
| 4180 | uint64_t TypeTagIdx; |
| 4181 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4182 | Attr.getLoc(), 3, |
| 4183 | Attr.getArg(1), TypeTagIdx)) |
| 4184 | return; |
| 4185 | |
| 4186 | bool IsPointer = (AttrName == "pointer_with_type_tag"); |
| 4187 | if (IsPointer) { |
| 4188 | // Ensure that buffer has a pointer type. |
| 4189 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 4190 | if (!BufferTy->isPointerType()) { |
| 4191 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4192 | << Attr.getName(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4193 | } |
| 4194 | } |
| 4195 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4196 | D->addAttr(::new (S.Context) |
| 4197 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 4198 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 4199 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4200 | } |
| 4201 | |
| 4202 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 4203 | const AttributeList &Attr) { |
| 4204 | IdentifierInfo *PointerKind = Attr.getParameterName(); |
| 4205 | if (!PointerKind) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4206 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4207 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4208 | return; |
| 4209 | } |
| 4210 | |
| 4211 | QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL); |
| 4212 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4213 | D->addAttr(::new (S.Context) |
| 4214 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
| 4215 | MatchingCType, |
| 4216 | Attr.getLayoutCompatible(), |
| 4217 | Attr.getMustBeNull(), |
| 4218 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4221 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4222 | // Checker-specific attribute handlers. |
| 4223 | //===----------------------------------------------------------------------===// |
| 4224 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4225 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4226 | return type->isDependentType() || |
| 4227 | type->isObjCObjectPointerType() || |
| 4228 | S.Context.isObjCNSObjectType(type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4229 | } |
| 4230 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4231 | return type->isDependentType() || |
| 4232 | type->isPointerType() || |
| 4233 | isValidSubjectOfNSAttribute(S, type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4234 | } |
| 4235 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4236 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4237 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4238 | if (!param) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4239 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4240 | << Attr.getRange() << Attr.getName() << ExpectedParameter; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4241 | return; |
| 4242 | } |
| 4243 | |
| 4244 | bool typeOK, cf; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4245 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4246 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 4247 | cf = false; |
| 4248 | } else { |
| 4249 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 4250 | cf = true; |
| 4251 | } |
| 4252 | |
| 4253 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4254 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4255 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4256 | return; |
| 4257 | } |
| 4258 | |
| 4259 | if (cf) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4260 | param->addAttr(::new (S.Context) |
| 4261 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 4262 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4263 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4264 | param->addAttr(::new (S.Context) |
| 4265 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 4266 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4269 | static void handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 4270 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4271 | if (!isa<ObjCMethodDecl>(D)) { |
| 4272 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4273 | << Attr.getRange() << Attr.getName() << ExpectedMethod; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4274 | return; |
| 4275 | } |
| 4276 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4277 | D->addAttr(::new (S.Context) |
| 4278 | NSConsumesSelfAttr(Attr.getRange(), S.Context, |
| 4279 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4280 | } |
| 4281 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4282 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 4283 | const AttributeList &Attr) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4284 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4285 | QualType returnType; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4286 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4287 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4288 | returnType = MD->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4289 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4290 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4291 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | a23bd4c | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 4292 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4293 | returnType = PD->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4294 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4295 | returnType = FD->getResultType(); |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4296 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4297 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4298 | << Attr.getRange() << Attr.getName() |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4299 | << ExpectedFunctionOrMethod; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4300 | return; |
| 4301 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4302 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4303 | bool typeOK; |
| 4304 | bool cf; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4305 | switch (Attr.getKind()) { |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4306 | default: llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4307 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4308 | case AttributeList::AT_NSReturnsRetained: |
| 4309 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4310 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 4311 | cf = false; |
| 4312 | break; |
| 4313 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4314 | case AttributeList::AT_CFReturnsRetained: |
| 4315 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4316 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 4317 | cf = true; |
| 4318 | break; |
| 4319 | } |
| 4320 | |
| 4321 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4322 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4323 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4324 | return; |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4325 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4326 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4327 | switch (Attr.getKind()) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4328 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4329 | llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4330 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4331 | D->addAttr(::new (S.Context) |
| 4332 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 4333 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4334 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4335 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4336 | D->addAttr(::new (S.Context) |
| 4337 | CFReturnsNotRetainedAttr(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_NSReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4341 | D->addAttr(::new (S.Context) |
| 4342 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4343 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4344 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4345 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4346 | D->addAttr(::new (S.Context) |
| 4347 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4348 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4349 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4350 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4351 | D->addAttr(::new (S.Context) |
| 4352 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4353 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4354 | return; |
| 4355 | }; |
| 4356 | } |
| 4357 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4358 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4359 | const AttributeList &attr) { |
| 4360 | SourceLocation loc = attr.getLoc(); |
| 4361 | |
| 4362 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4363 | |
Fariborz Jahanian | 94d55d7 | 2012-04-21 17:51:44 +0000 | [diff] [blame] | 4364 | if (!method) { |
Fariborz Jahanian | 0e78afb | 2012-04-20 22:00:46 +0000 | [diff] [blame] | 4365 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4366 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4367 | return; |
| 4368 | } |
| 4369 | |
| 4370 | // Check that the method returns a normal pointer. |
| 4371 | QualType resultType = method->getResultType(); |
Fariborz Jahanian | f2e5945 | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4372 | |
| 4373 | if (!resultType->isReferenceType() && |
| 4374 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4375 | S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 4376 | << SourceRange(loc) |
| 4377 | << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2; |
| 4378 | |
| 4379 | // Drop the attribute. |
| 4380 | return; |
| 4381 | } |
| 4382 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4383 | method->addAttr(::new (S.Context) |
| 4384 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 4385 | attr.getAttributeSpellingListIndex())); |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4386 | } |
| 4387 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4388 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4389 | const AttributeList &attr) { |
| 4390 | SourceLocation loc = attr.getLoc(); |
| 4391 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4392 | |
| 4393 | if (!method) { |
| 4394 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 4395 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
| 4396 | return; |
| 4397 | } |
| 4398 | DeclContext *DC = method->getDeclContext(); |
| 4399 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4400 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4401 | << attr.getName() << 0; |
| 4402 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4403 | return; |
| 4404 | } |
| 4405 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4406 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4407 | << attr.getName() << 1; |
| 4408 | return; |
| 4409 | } |
| 4410 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4411 | method->addAttr(::new (S.Context) |
| 4412 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 4413 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4414 | } |
| 4415 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4416 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 4417 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
| 4418 | if (!isa<FunctionDecl>(D)) { |
| 4419 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4420 | << A.getRange() << A.getName() << ExpectedFunction; |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4421 | return; |
| 4422 | } |
| 4423 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4424 | bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4425 | |
| 4426 | // Check whether there's a conflicting attribute already present. |
| 4427 | Attr *Existing; |
| 4428 | if (IsAudited) { |
| 4429 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 4430 | } else { |
| 4431 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 4432 | } |
| 4433 | if (Existing) { |
| 4434 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 4435 | << A.getName() |
| 4436 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 4437 | << A.getRange() << Existing->getRange(); |
| 4438 | return; |
| 4439 | } |
| 4440 | |
| 4441 | // All clear; add the attribute. |
| 4442 | if (IsAudited) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4443 | D->addAttr(::new (S.Context) |
| 4444 | CFAuditedTransferAttr(A.getRange(), S.Context, |
| 4445 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4446 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4447 | D->addAttr(::new (S.Context) |
| 4448 | CFUnknownTransferAttr(A.getRange(), S.Context, |
| 4449 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4450 | } |
| 4451 | } |
| 4452 | |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4453 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4454 | const AttributeList &Attr) { |
| 4455 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4456 | if (!RD || RD->isUnion()) { |
| 4457 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4458 | << Attr.getRange() << Attr.getName() << ExpectedStruct; |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4459 | } |
| 4460 | |
| 4461 | IdentifierInfo *ParmName = Attr.getParameterName(); |
| 4462 | |
| 4463 | // In Objective-C, verify that the type names an Objective-C type. |
| 4464 | // We don't want to check this outside of ObjC because people sometimes |
| 4465 | // do crazy C declarations of Objective-C types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4466 | if (ParmName && S.getLangOpts().ObjC1) { |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4467 | // Check for an existing type with this name. |
| 4468 | LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(), |
| 4469 | Sema::LookupOrdinaryName); |
| 4470 | if (S.LookupName(R, Sc)) { |
| 4471 | NamedDecl *Target = R.getFoundDecl(); |
| 4472 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 4473 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 4474 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 4475 | } |
| 4476 | } |
| 4477 | } |
| 4478 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4479 | D->addAttr(::new (S.Context) |
| 4480 | NSBridgedAttr(Attr.getRange(), S.Context, ParmName, |
| 4481 | Attr.getAttributeSpellingListIndex())); |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4482 | } |
| 4483 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4484 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4485 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4486 | if (hasDeclarator(D)) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4487 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4488 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4489 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4490 | } |
| 4491 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4492 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4493 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4494 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4495 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4496 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4497 | return; |
| 4498 | } |
| 4499 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4500 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4501 | QualType type = vd->getType(); |
| 4502 | |
| 4503 | if (!type->isDependentType() && |
| 4504 | !type->isObjCLifetimeType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4505 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4506 | << type; |
| 4507 | return; |
| 4508 | } |
| 4509 | |
| 4510 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4511 | |
| 4512 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4513 | // going to infer. |
| 4514 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4515 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4516 | |
| 4517 | switch (lifetime) { |
| 4518 | case Qualifiers::OCL_None: |
| 4519 | assert(type->isDependentType() && |
| 4520 | "didn't infer lifetime for non-dependent type?"); |
| 4521 | break; |
| 4522 | |
| 4523 | case Qualifiers::OCL_Weak: // meaningful |
| 4524 | case Qualifiers::OCL_Strong: // meaningful |
| 4525 | break; |
| 4526 | |
| 4527 | case Qualifiers::OCL_ExplicitNone: |
| 4528 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4529 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4530 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4531 | break; |
| 4532 | } |
| 4533 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4534 | D->addAttr(::new (S.Context) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4535 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 4536 | Attr.getAttributeSpellingListIndex())); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4537 | } |
| 4538 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4539 | //===----------------------------------------------------------------------===// |
| 4540 | // Microsoft specific attribute handlers. |
| 4541 | //===----------------------------------------------------------------------===// |
| 4542 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4543 | // Check if MS extensions or some other language extensions are enabled. If |
| 4544 | // not, issue a diagnostic that the given attribute is unused. |
| 4545 | static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr, |
| 4546 | bool OtherExtension = false) { |
| 4547 | if (S.LangOpts.MicrosoftExt || OtherExtension) |
| 4548 | return true; |
| 4549 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4550 | return false; |
| 4551 | } |
| 4552 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4553 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4554 | if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland)) |
| 4555 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4556 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4557 | // check the attribute arguments. |
| 4558 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4559 | return; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4560 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4561 | Expr *Arg = Attr.getArg(0); |
| 4562 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 4563 | if (!Str || !Str->isAscii()) { |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4564 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 4565 | << Attr.getName() << AANT_ArgumentString; |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4566 | return; |
| 4567 | } |
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 | StringRef StrRef = Str->getString(); |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4570 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4571 | bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' && |
| 4572 | StrRef.back() == '}'; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4573 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4574 | // Validate GUID length. |
| 4575 | if (IsCurly && StrRef.size() != 38) { |
| 4576 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4577 | return; |
| 4578 | } |
| 4579 | if (!IsCurly && StrRef.size() != 36) { |
| 4580 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4581 | return; |
| 4582 | } |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4583 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4584 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 4585 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" |
| 4586 | StringRef::iterator I = StrRef.begin(); |
| 4587 | if (IsCurly) // Skip the optional '{' |
| 4588 | ++I; |
| 4589 | |
| 4590 | for (int i = 0; i < 36; ++i) { |
| 4591 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 4592 | if (*I != '-') { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4593 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4594 | return; |
| 4595 | } |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4596 | } else if (!isHexDigit(*I)) { |
| 4597 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4598 | return; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4599 | } |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4600 | I++; |
| 4601 | } |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4602 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4603 | D->addAttr(::new (S.Context) |
| 4604 | UuidAttr(Attr.getRange(), S.Context, Str->getString(), |
| 4605 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4606 | } |
| 4607 | |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4608 | static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4609 | if (!checkMicrosoftExt(S, Attr)) |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4610 | return; |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4611 | |
| 4612 | AttributeList::Kind Kind = Attr.getKind(); |
| 4613 | if (Kind == AttributeList::AT_SingleInheritance) |
| 4614 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4615 | ::new (S.Context) |
| 4616 | SingleInheritanceAttr(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_MultipleInheritance) |
| 4619 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4620 | ::new (S.Context) |
| 4621 | MultipleInheritanceAttr(Attr.getRange(), S.Context, |
| 4622 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4623 | else if (Kind == AttributeList::AT_VirtualInheritance) |
| 4624 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4625 | ::new (S.Context) |
| 4626 | VirtualInheritanceAttr(Attr.getRange(), S.Context, |
| 4627 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4631 | if (!checkMicrosoftExt(S, Attr)) |
| 4632 | return; |
| 4633 | |
| 4634 | AttributeList::Kind Kind = Attr.getKind(); |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4635 | if (Kind == AttributeList::AT_Win64) |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4636 | D->addAttr( |
| 4637 | ::new (S.Context) Win64Attr(Attr.getRange(), S.Context, |
| 4638 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4641 | static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4642 | if (!checkMicrosoftExt(S, Attr)) |
| 4643 | return; |
| 4644 | D->addAttr(::new (S.Context) |
| 4645 | ForceInlineAttr(Attr.getRange(), S.Context, |
| 4646 | Attr.getAttributeSpellingListIndex())); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4647 | } |
| 4648 | |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4649 | static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4650 | if (!checkMicrosoftExt(S, Attr)) |
| 4651 | return; |
| 4652 | // Check linkage after possibly merging declaratinos. See |
| 4653 | // checkAttributesAfterMerging(). |
| 4654 | D->addAttr(::new (S.Context) |
| 4655 | SelectAnyAttr(Attr.getRange(), S.Context, |
| 4656 | Attr.getAttributeSpellingListIndex())); |
| 4657 | } |
| 4658 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4659 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4660 | // Top Level Sema Entry Points |
| 4661 | //===----------------------------------------------------------------------===// |
| 4662 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4663 | static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4664 | const AttributeList &Attr) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4665 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4666 | case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break; |
| 4667 | case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break; |
| 4668 | case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break; |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4669 | default: |
| 4670 | break; |
| 4671 | } |
| 4672 | } |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4673 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4674 | static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4675 | const AttributeList &Attr) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4676 | switch (Attr.getKind()) { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4677 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 4678 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 4679 | case AttributeList::AT_IBOutletCollection: |
| 4680 | handleIBOutletCollection(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4681 | case AttributeList::AT_AddressSpace: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4682 | case AttributeList::AT_ObjCGC: |
| 4683 | case AttributeList::AT_VectorSize: |
| 4684 | case AttributeList::AT_NeonVectorType: |
| 4685 | case AttributeList::AT_NeonPolyVectorType: |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4686 | case AttributeList::AT_Ptr32: |
| 4687 | case AttributeList::AT_Ptr64: |
| 4688 | case AttributeList::AT_SPtr: |
| 4689 | case AttributeList::AT_UPtr: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4690 | // Ignore these, these are type attributes, handled by |
| 4691 | // ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4692 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4693 | case AttributeList::AT_CUDADevice: |
| 4694 | case AttributeList::AT_CUDAHost: |
| 4695 | case AttributeList::AT_Overloadable: |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4696 | // Ignore, this is a non-inheritable attribute, handled |
| 4697 | // by ProcessNonInheritableDeclAttr. |
| 4698 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4699 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 4700 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 4701 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 4702 | case AttributeList::AT_AlwaysInline: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4703 | handleAlwaysInlineAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4704 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4705 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 4706 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4707 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 4708 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 4709 | case AttributeList::AT_CarriesDependency: |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4710 | handleDependencyAttr(S, scope, D, Attr); |
| 4711 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4712 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
| 4713 | case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break; |
| 4714 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4715 | case AttributeList::AT_CXX11NoReturn: |
| 4716 | handleCXX11NoReturnAttr(S, D, Attr); |
| 4717 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4718 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4719 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4720 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4721 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 4722 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4723 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4724 | break; |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4725 | case AttributeList::AT_MinSize: |
| 4726 | handleMinSizeAttr(S, D, Attr); |
| 4727 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4728 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4729 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4730 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
| 4731 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4732 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4733 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4734 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4735 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
| 4736 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
| 4737 | case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break; |
| 4738 | case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break; |
| 4739 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4740 | case AttributeList::AT_ownership_returns: |
| 4741 | case AttributeList::AT_ownership_takes: |
| 4742 | case AttributeList::AT_ownership_holds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4743 | handleOwnershipAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4744 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4745 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
| 4746 | case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break; |
| 4747 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
| 4748 | case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break; |
| 4749 | case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break; |
| 4750 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4751 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4752 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4753 | handleObjCOwnershipAttr(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4754 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4755 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4756 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4757 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4758 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4759 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4760 | case AttributeList::AT_ObjCRequiresSuper: |
| 4761 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4762 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4763 | case AttributeList::AT_NSBridged: |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4764 | handleNSBridgedAttr(S, scope, D, Attr); break; |
| 4765 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4766 | case AttributeList::AT_CFAuditedTransfer: |
| 4767 | case AttributeList::AT_CFUnknownTransfer: |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4768 | handleCFTransferAttr(S, D, Attr); break; |
| 4769 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4770 | // Checker-specific. |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4771 | case AttributeList::AT_CFConsumed: |
| 4772 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4773 | case AttributeList::AT_NSConsumesSelf: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4774 | handleNSConsumesSelfAttr(S, D, Attr); break; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4775 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4776 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4777 | case AttributeList::AT_NSReturnsNotRetained: |
| 4778 | case AttributeList::AT_CFReturnsNotRetained: |
| 4779 | case AttributeList::AT_NSReturnsRetained: |
| 4780 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4781 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4782 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4783 | case AttributeList::AT_WorkGroupSizeHint: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4784 | case AttributeList::AT_ReqdWorkGroupSize: |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4785 | handleWorkGroupSize(S, D, Attr); break; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 4786 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4787 | case AttributeList::AT_VecTypeHint: |
| 4788 | handleVecTypeHint(S, D, Attr); break; |
| 4789 | |
Joey Gouly | 96cead5 | 2013-03-14 09:54:43 +0000 | [diff] [blame] | 4790 | case AttributeList::AT_Endian: |
| 4791 | handleEndianAttr(S, D, Attr); |
| 4792 | break; |
| 4793 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4794 | case AttributeList::AT_InitPriority: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4795 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4796 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4797 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4798 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4799 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4800 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4801 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4802 | case AttributeList::AT_ArcWeakrefUnavailable: |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 4803 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 4804 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4805 | case AttributeList::AT_ObjCRootClass: |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4806 | handleObjCRootClassAttr(S, D, Attr); |
| 4807 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4808 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 4809 | handleObjCRequiresPropertyDefsAttr (S, D, Attr); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 4810 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4811 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4812 | case AttributeList::AT_ReturnsTwice: |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 4813 | handleReturnsTwiceAttr(S, D, Attr); |
| 4814 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4815 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4816 | case AttributeList::AT_Visibility: |
| 4817 | handleVisibilityAttr(S, D, Attr, false); |
| 4818 | break; |
| 4819 | case AttributeList::AT_TypeVisibility: |
| 4820 | handleVisibilityAttr(S, D, Attr, true); |
| 4821 | break; |
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 4822 | case AttributeList::AT_WarnUnused: |
| 4823 | handleWarnUnusedAttr(S, D, Attr); |
| 4824 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4825 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4826 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4827 | case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; |
| 4828 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4829 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4830 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4831 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4832 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4833 | case AttributeList::AT_ObjCException: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4834 | handleObjCExceptionAttr(S, D, Attr); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 4835 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4836 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4837 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4838 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4839 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4840 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4841 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
| 4842 | case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break; |
| 4843 | case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break; |
| 4844 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4845 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
| 4846 | case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break; |
| 4847 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4848 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4849 | // Just ignore |
| 4850 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4851 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4852 | handleNoInstrumentFunctionAttr(S, D, Attr); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 4853 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4854 | case AttributeList::AT_StdCall: |
| 4855 | case AttributeList::AT_CDecl: |
| 4856 | case AttributeList::AT_FastCall: |
| 4857 | case AttributeList::AT_ThisCall: |
| 4858 | case AttributeList::AT_Pascal: |
| 4859 | case AttributeList::AT_Pcs: |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4860 | case AttributeList::AT_PnaclCall: |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4861 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4862 | handleCallConvAttr(S, D, Attr); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4863 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4864 | case AttributeList::AT_OpenCLKernel: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4865 | handleOpenCLKernelAttr(S, D, Attr); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4866 | break; |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4867 | case AttributeList::AT_OpenCLImageAccess: |
| 4868 | handleOpenCLImageAccessAttr(S, D, Attr); |
| 4869 | break; |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4870 | |
| 4871 | // Microsoft attributes: |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4872 | case AttributeList::AT_MsProperty: break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4873 | case AttributeList::AT_MsStruct: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4874 | handleMsStructAttr(S, D, Attr); |
| 4875 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4876 | case AttributeList::AT_Uuid: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4877 | handleUuidAttr(S, D, Attr); |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4878 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4879 | case AttributeList::AT_SingleInheritance: |
| 4880 | case AttributeList::AT_MultipleInheritance: |
| 4881 | case AttributeList::AT_VirtualInheritance: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4882 | handleInheritanceAttr(S, D, Attr); |
| 4883 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4884 | case AttributeList::AT_Win64: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4885 | handlePortabilityAttr(S, D, Attr); |
| 4886 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4887 | case AttributeList::AT_ForceInline: |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4888 | handleForceInlineAttr(S, D, Attr); |
| 4889 | break; |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4890 | case AttributeList::AT_SelectAny: |
| 4891 | handleSelectAnyAttr(S, D, Attr); |
| 4892 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4893 | |
| 4894 | // Thread safety attributes: |
DeLesley Hutchins | 5c6134f | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 4895 | case AttributeList::AT_AssertExclusiveLock: |
| 4896 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 4897 | break; |
| 4898 | case AttributeList::AT_AssertSharedLock: |
| 4899 | handleAssertSharedLockAttr(S, D, Attr); |
| 4900 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4901 | case AttributeList::AT_GuardedVar: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4902 | handleGuardedVarAttr(S, D, Attr); |
| 4903 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4904 | case AttributeList::AT_PtGuardedVar: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4905 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4906 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4907 | case AttributeList::AT_ScopedLockable: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4908 | handleScopedLockableAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4909 | break; |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4910 | case AttributeList::AT_NoSanitizeAddress: |
| 4911 | handleNoSanitizeAddressAttr(S, D, Attr); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4912 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4913 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4914 | handleNoThreadSafetyAnalysis(S, D, Attr); |
| 4915 | break; |
| 4916 | case AttributeList::AT_NoSanitizeThread: |
| 4917 | handleNoSanitizeThread(S, D, Attr); |
| 4918 | break; |
| 4919 | case AttributeList::AT_NoSanitizeMemory: |
| 4920 | handleNoSanitizeMemory(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4921 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4922 | case AttributeList::AT_Lockable: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4923 | handleLockableAttr(S, D, Attr); |
| 4924 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4925 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4926 | handleGuardedByAttr(S, D, Attr); |
| 4927 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4928 | case AttributeList::AT_PtGuardedBy: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4929 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4930 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4931 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4932 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4933 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4934 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4935 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4936 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4937 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4938 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4939 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4940 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4941 | handleLockReturnedAttr(S, D, Attr); |
| 4942 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4943 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4944 | handleLocksExcludedAttr(S, D, Attr); |
| 4945 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4946 | case AttributeList::AT_SharedLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4947 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4948 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4949 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4950 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4951 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4952 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4953 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4954 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4955 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4956 | handleUnlockFunAttr(S, D, Attr); |
| 4957 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4958 | case AttributeList::AT_AcquiredBefore: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4959 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4960 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4961 | case AttributeList::AT_AcquiredAfter: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4962 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4963 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4964 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4965 | // Type safety attributes. |
| 4966 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4967 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4968 | break; |
| 4969 | case AttributeList::AT_TypeTagForDatatype: |
| 4970 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4971 | break; |
| 4972 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4973 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4974 | // Ask target about the attribute. |
| 4975 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 4976 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4977 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 4978 | diag::warn_unhandled_ms_attribute_ignored : |
| 4979 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4980 | break; |
| 4981 | } |
| 4982 | } |
| 4983 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4984 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4985 | /// 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] | 4986 | /// silently ignore it if a GNU attribute. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4987 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4988 | const AttributeList &Attr, |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4989 | bool NonInheritable, bool Inheritable, |
| 4990 | bool IncludeCXX11Attributes) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4991 | if (Attr.isInvalid()) |
| 4992 | return; |
| 4993 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4994 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 4995 | // instead. |
| 4996 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 4997 | return; |
| 4998 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4999 | if (NonInheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5000 | ProcessNonInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5001 | |
| 5002 | if (Inheritable) |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5003 | ProcessInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5004 | } |
| 5005 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5006 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 5007 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 5008 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5009 | const AttributeList *AttrList, |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5010 | bool NonInheritable, bool Inheritable, |
| 5011 | bool IncludeCXX11Attributes) { |
| 5012 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
| 5013 | ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable, |
| 5014 | IncludeCXX11Attributes); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5015 | |
| 5016 | // GCC accepts |
| 5017 | // static int a9 __attribute__((weakref)); |
| 5018 | // but that looks really pointless. We reject it. |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5019 | if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5020 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Rafael Espindola | 4d8a33b | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 5021 | cast<NamedDecl>(D)->getNameAsString(); |
| 5022 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5023 | return; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5024 | } |
| 5025 | } |
| 5026 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 5027 | // Annotation attributes are the only attributes allowed after an access |
| 5028 | // specifier. |
| 5029 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 5030 | const AttributeList *AttrList) { |
| 5031 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5032 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 5033 | handleAnnotateAttr(*this, ASDecl, *l); |
| 5034 | } else { |
| 5035 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 5036 | return true; |
| 5037 | } |
| 5038 | } |
| 5039 | |
| 5040 | return false; |
| 5041 | } |
| 5042 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5043 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 5044 | /// contains any decl attributes that we should warn about. |
| 5045 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 5046 | for ( ; A; A = A->getNext()) { |
| 5047 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 5048 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5049 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 5050 | |
| 5051 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 5052 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 5053 | << A->getName() << A->getRange(); |
| 5054 | } else { |
| 5055 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 5056 | << A->getName() << A->getRange(); |
| 5057 | } |
| 5058 | } |
| 5059 | } |
| 5060 | |
| 5061 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 5062 | /// used to build a declaration, complain about any decl attributes |
| 5063 | /// which might be lying around on it. |
| 5064 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 5065 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 5066 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 5067 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 5068 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 5069 | } |
| 5070 | |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5071 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5072 | /// \#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] | 5073 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 5074 | SourceLocation Loc) { |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5075 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5076 | NamedDecl *NewD = 0; |
| 5077 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5078 | FunctionDecl *NewFD; |
| 5079 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 5080 | // FIXME: Mangling? |
| 5081 | // FIXME: Is the qualifier info correct? |
| 5082 | // FIXME: Is the DeclContext correct? |
| 5083 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 5084 | Loc, Loc, DeclarationName(II), |
| 5085 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5086 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5087 | FD->hasPrototype(), |
| 5088 | false/*isConstexprSpecified*/); |
| 5089 | NewD = NewFD; |
| 5090 | |
| 5091 | if (FD->getQualifier()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5092 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5093 | |
| 5094 | // Fake up parameter variables; they are declared as if this were |
| 5095 | // a typedef. |
| 5096 | QualType FDTy = FD->getType(); |
| 5097 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 5098 | SmallVector<ParmVarDecl*, 16> Params; |
| 5099 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 5100 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 5101 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 5102 | Param->setScopeInfo(0, Params.size()); |
| 5103 | Params.push_back(Param); |
| 5104 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 5105 | NewFD->setParams(Params); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5106 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5107 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 5108 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5109 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5110 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5111 | VD->getStorageClass()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5112 | if (VD->getQualifier()) { |
| 5113 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5114 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5115 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5116 | } |
| 5117 | return NewD; |
| 5118 | } |
| 5119 | |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5120 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5121 | /// applied to it, possibly with an alias. |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5122 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5123 | if (W.getUsed()) return; // only do this once |
| 5124 | W.setUsed(true); |
| 5125 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 5126 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5127 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5128 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 5129 | NDId->getName())); |
| 5130 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5131 | WeakTopLevelDecl.push_back(NewD); |
| 5132 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 5133 | // to insert Decl at TU scope, sorry. |
| 5134 | DeclContext *SavedContext = CurContext; |
| 5135 | CurContext = Context.getTranslationUnitDecl(); |
| 5136 | PushOnScopeChains(NewD, S); |
| 5137 | CurContext = SavedContext; |
| 5138 | } else { // just add weak to existing |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5139 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5140 | } |
| 5141 | } |
| 5142 | |
Rafael Espindola | 65611bf | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5143 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 5144 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 5145 | // have to do this. |
| 5146 | LoadExternalWeakUndeclaredIdentifiers(); |
| 5147 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 5148 | NamedDecl *ND = NULL; |
| 5149 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 5150 | if (VD->isExternC()) |
| 5151 | ND = VD; |
| 5152 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5153 | if (FD->isExternC()) |
| 5154 | ND = FD; |
| 5155 | if (ND) { |
| 5156 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 5157 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 5158 | = WeakUndeclaredIdentifiers.find(Id); |
| 5159 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 5160 | WeakInfo W = I->second; |
| 5161 | DeclApplyPragmaWeak(S, ND, W); |
| 5162 | WeakUndeclaredIdentifiers[Id] = W; |
| 5163 | } |
| 5164 | } |
| 5165 | } |
| 5166 | } |
| 5167 | } |
| 5168 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5169 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 5170 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 5171 | /// 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] | 5172 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD, |
| 5173 | bool NonInheritable, bool Inheritable) { |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5174 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5175 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5176 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5177 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5178 | // Walk the declarator structure, applying decl attributes that were in a type |
| 5179 | // position to the decl itself. This handles cases like: |
| 5180 | // int *__attr__(x)** D; |
| 5181 | // when X is a decl attribute. |
| 5182 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 5183 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5184 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable, |
| 5185 | /*IncludeCXX11Attributes=*/false); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5186 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5187 | // Finally, apply any attributes on the decl itself. |
| 5188 | if (const AttributeList *Attrs = PD.getAttributes()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5189 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5190 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5191 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5192 | /// Is the given declaration allowed to use a forbidden type? |
| 5193 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 5194 | // Private ivars are always okay. Unfortunately, people don't |
| 5195 | // always properly make their ivars private, even in system headers. |
| 5196 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | a6b3380 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 5197 | // Function declarations in sys headers will be marked unavailable. |
| 5198 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 5199 | !isa<FunctionDecl>(decl)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5200 | return false; |
| 5201 | |
| 5202 | // Require it to be declared in a system header. |
| 5203 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 5204 | } |
| 5205 | |
| 5206 | /// Handle a delayed forbidden-type diagnostic. |
| 5207 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 5208 | Decl *decl) { |
| 5209 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 5210 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 5211 | "this system declaration uses an unsupported type")); |
| 5212 | return; |
| 5213 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5214 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5215 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 5216 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5217 | // kind of forbidden type messages on unavailable functions. |
| 5218 | if (FD->hasAttr<UnavailableAttr>() && |
| 5219 | diag.getForbiddenTypeDiagnostic() == |
| 5220 | diag::err_arc_array_param_no_ownership) { |
| 5221 | diag.Triggered = true; |
| 5222 | return; |
| 5223 | } |
| 5224 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5225 | |
| 5226 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 5227 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 5228 | diag.Triggered = true; |
| 5229 | } |
| 5230 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5231 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 5232 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5233 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5234 | DelayedDiagnostics.popWithoutEmitting(state); |
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 | // When delaying diagnostics to run in the context of a parsed |
| 5237 | // declaration, we only want to actually emit anything if parsing |
| 5238 | // succeeds. |
| 5239 | if (!decl) return; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5240 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5241 | // We emit all the active diagnostics in this pool or any of its |
| 5242 | // parents. In general, we'll get one pool for the decl spec |
| 5243 | // and a child pool for each declarator; in a decl group like: |
| 5244 | // deprecated_typedef foo, *bar, baz(); |
| 5245 | // only the declarator pops will be passed decls. This is correct; |
| 5246 | // we really do need to consider delayed diagnostics from the decl spec |
| 5247 | // for each of the different declarations. |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5248 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5249 | do { |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5250 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5251 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 5252 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 5253 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5254 | if (diag.Triggered) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5255 | continue; |
| 5256 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5257 | switch (diag.Kind) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5258 | case DelayedDiagnostic::Deprecation: |
John McCall | e8c904f | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 5259 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 5260 | if (!decl->isInvalidDecl()) |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5261 | HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5262 | break; |
| 5263 | |
| 5264 | case DelayedDiagnostic::Access: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5265 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5266 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5267 | |
| 5268 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5269 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5270 | break; |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5271 | } |
| 5272 | } |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5273 | } while ((pool = pool->getParent())); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5274 | } |
| 5275 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5276 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 5277 | /// been delayed in the current context instead of in the given pool. |
| 5278 | /// Essentially, this just moves them to the current pool. |
| 5279 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 5280 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 5281 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 5282 | curPool->steal(pool); |
| 5283 | } |
| 5284 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5285 | static bool isDeclDeprecated(Decl *D) { |
| 5286 | do { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5287 | if (D->isDeprecated()) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5288 | return true; |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 5289 | // A category implicitly has the availability of the interface. |
| 5290 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 5291 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5292 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 5293 | return false; |
| 5294 | } |
| 5295 | |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5296 | static void |
| 5297 | DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message, |
| 5298 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5299 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5300 | const ObjCPropertyDecl *ObjCPropery) { |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5301 | DeclarationName Name = D->getDeclName(); |
| 5302 | if (!Message.empty()) { |
| 5303 | S.Diag(Loc, diag::warn_deprecated_message) << Name << Message; |
| 5304 | S.Diag(D->getLocation(), |
| 5305 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5306 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5307 | if (ObjCPropery) |
| 5308 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5309 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5310 | } else if (!UnknownObjCClass) { |
| 5311 | S.Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 5312 | S.Diag(D->getLocation(), |
| 5313 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5314 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5315 | if (ObjCPropery) |
| 5316 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5317 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5318 | } else { |
| 5319 | S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name; |
| 5320 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 5321 | } |
| 5322 | } |
| 5323 | |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 5324 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5325 | Decl *Ctx) { |
| 5326 | if (isDeclDeprecated(Ctx)) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5327 | return; |
| 5328 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5329 | DD.Triggered = true; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5330 | DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(), |
| 5331 | DD.getDeprecationMessage(), DD.Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5332 | DD.getUnknownObjCClass(), |
| 5333 | DD.getObjCProperty()); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5334 | } |
| 5335 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5336 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 5337 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5338 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5339 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5340 | // Delay if we're currently parsing a declaration. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5341 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5342 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 5343 | UnknownObjCClass, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5344 | ObjCProperty, |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5345 | Message)); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5346 | return; |
| 5347 | } |
| 5348 | |
| 5349 | // Otherwise, don't warn if our current context is deprecated. |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 5350 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5351 | return; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5352 | DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5353 | } |