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" |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 26 | #include "clang/Lex/Preprocessor.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 27 | #include "clang/Sema/DeclSpec.h" |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 28 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 29 | #include "clang/Sema/Lookup.h" |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 30 | #include "clang/Sema/Scope.h" |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 32 | using namespace clang; |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 33 | using namespace sema; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 34 | |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 35 | /// These constants match the enumerated choices of |
| 36 | /// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 37 | enum AttributeDeclKind { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 38 | ExpectedFunction, |
| 39 | ExpectedUnion, |
| 40 | ExpectedVariableOrFunction, |
| 41 | ExpectedFunctionOrMethod, |
| 42 | ExpectedParameter, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 43 | ExpectedFunctionMethodOrBlock, |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 44 | ExpectedFunctionMethodOrClass, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 45 | ExpectedFunctionMethodOrParameter, |
| 46 | ExpectedClass, |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 47 | ExpectedVariable, |
| 48 | ExpectedMethod, |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 49 | ExpectedVariableFunctionOrLabel, |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 50 | ExpectedFieldOrGlobalVar, |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 51 | ExpectedStruct, |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 52 | ExpectedVariableFunctionOrTag, |
Richard Smith | 5f838aa | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 53 | ExpectedTLSVar, |
| 54 | ExpectedVariableOrField, |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 55 | ExpectedVariableFieldOrTag, |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 56 | ExpectedTypeOrNamespace, |
Fariborz Jahanian | 937ec1d | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 57 | ExpectedObjectiveCInterface, |
| 58 | ExpectedMethodOrProperty |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 61 | //===----------------------------------------------------------------------===// |
| 62 | // Helper functions |
| 63 | //===----------------------------------------------------------------------===// |
| 64 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 65 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 66 | bool blocksToo = true) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 67 | QualType Ty; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 68 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(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 FieldDecl *decl = dyn_cast<FieldDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 71 | Ty = decl->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 72 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 73 | Ty = decl->getUnderlyingType(); |
| 74 | else |
| 75 | return 0; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 77 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 78 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 755f9d2 | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 79 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 80 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 81 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 82 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 85 | // FIXME: We should provide an abstraction around a method or function |
| 86 | // to provide the following bits of information. |
| 87 | |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 88 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 89 | /// type (function or function-typed variable). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 90 | static bool isFunction(const Decl *D) { |
| 91 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 95 | /// type (function or function-typed variable) or an Objective-C |
| 96 | /// method. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 97 | static bool isFunctionOrMethod(const Decl *D) { |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 98 | return isFunction(D) || isa<ObjCMethodDecl>(D); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 101 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 102 | /// type (function or function-typed variable) or an Objective-C |
| 103 | /// method or a block. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 104 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 105 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 106 | return true; |
| 107 | // check for block is more involved. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 108 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 109 | QualType Ty = V->getType(); |
| 110 | return Ty->isBlockPointerType(); |
| 111 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 112 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 113 | } |
| 114 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 115 | /// Return true if the given decl has a declarator that should have |
| 116 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 117 | static bool hasDeclarator(const Decl *D) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 118 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 119 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 120 | isa<ObjCPropertyDecl>(D); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 123 | /// hasFunctionProto - Return true if the given decl has a argument |
| 124 | /// information. This decl should have already passed |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 125 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 126 | static bool hasFunctionProto(const Decl *D) { |
| 127 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 128 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 129 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 130 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 131 | return true; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 136 | /// arguments. It is an error to call this on a K&R function (use |
| 137 | /// hasFunctionProto first). |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 138 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 139 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 140 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 141 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 142 | return BD->getNumParams(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 143 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 146 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 147 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 148 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 149 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 150 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 151 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 152 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 155 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 156 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 157 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 158 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 161 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 162 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 163 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 164 | return proto->isVariadic(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 165 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | db9a0ae | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 166 | return BD->isVariadic(); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 167 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 168 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 172 | static bool isInstanceMethod(const Decl *D) { |
| 173 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 174 | return MethodDecl->isInstance(); |
| 175 | return false; |
| 176 | } |
| 177 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 178 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 179 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 180 | if (!PT) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 181 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 182 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 183 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 184 | if (!Cls) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 185 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 186 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 187 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 188 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 189 | // FIXME: Should we walk the chain of classes? |
| 190 | return ClsName == &Ctx.Idents.get("NSString") || |
| 191 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 192 | } |
| 193 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 194 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 195 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 196 | if (!PT) |
| 197 | return false; |
| 198 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 199 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 200 | if (!RT) |
| 201 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 202 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 203 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 204 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 205 | return false; |
| 206 | |
| 207 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 208 | } |
| 209 | |
Fariborz Jahanian | 933a894 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 210 | static inline bool isCFRefType(TypedefNameDecl *TD, ASTContext &Ctx) { |
| 211 | StringRef TDName = TD->getIdentifier()->getName(); |
| 212 | return ((TDName.startswith("CF") || TDName.startswith("CG")) && |
| 213 | (TDName.rfind("Ref") != StringRef::npos)); |
| 214 | } |
| 215 | |
Richard Smith | ddc2a53 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 216 | static unsigned getNumAttributeArgs(const AttributeList &Attr) { |
| 217 | // FIXME: Include the type in the argument list. |
| 218 | return Attr.getNumArgs() + Attr.hasParsedType(); |
| 219 | } |
| 220 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 221 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 222 | /// output an error. |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 223 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | ddc2a53 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 224 | unsigned Num) { |
| 225 | if (getNumAttributeArgs(Attr) != Num) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 226 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 227 | << Attr.getName() << Num; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 228 | return false; |
| 229 | } |
| 230 | |
| 231 | return true; |
| 232 | } |
| 233 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 234 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 235 | /// \brief Check if the attribute has at least as many args as Num. May |
| 236 | /// output an error. |
| 237 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | ddc2a53 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 238 | unsigned Num) { |
| 239 | if (getNumAttributeArgs(Attr) < Num) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 240 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| 241 | return false; |
| 242 | } |
| 243 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 244 | return true; |
| 245 | } |
| 246 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 247 | /// \brief Check if IdxExpr is a valid argument index for a function or |
| 248 | /// instance method D. May output an error. |
| 249 | /// |
| 250 | /// \returns true if IdxExpr is a valid index. |
| 251 | static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D, |
| 252 | StringRef AttrName, |
| 253 | SourceLocation AttrLoc, |
| 254 | unsigned AttrArgNum, |
| 255 | const Expr *IdxExpr, |
| 256 | uint64_t &Idx) |
| 257 | { |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 258 | assert(isFunctionOrMethod(D)); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 259 | |
| 260 | // In C++ the implicit 'this' function parameter also counts. |
| 261 | // Parameters are counted from one. |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 262 | bool HP = hasFunctionProto(D); |
| 263 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 264 | bool IV = HP && isFunctionOrMethodVariadic(D); |
| 265 | unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) + |
| 266 | HasImplicitThisParam; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 267 | |
| 268 | llvm::APSInt IdxInt; |
| 269 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 270 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 271 | std::string Name = std::string("'") + AttrName.str() + std::string("'"); |
| 272 | S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str() |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 273 | << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 274 | return false; |
| 275 | } |
| 276 | |
| 277 | Idx = IdxInt.getLimitedValue(); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 278 | if (Idx < 1 || (!IV && Idx > NumArgs)) { |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 279 | S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds) |
| 280 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 281 | return false; |
| 282 | } |
| 283 | Idx--; // Convert to zero-based. |
| 284 | if (HasImplicitThisParam) { |
| 285 | if (Idx == 0) { |
| 286 | S.Diag(AttrLoc, |
| 287 | diag::err_attribute_invalid_implicit_this_argument) |
| 288 | << AttrName << IdxExpr->getSourceRange(); |
| 289 | return false; |
| 290 | } |
| 291 | --Idx; |
| 292 | } |
| 293 | |
| 294 | return true; |
| 295 | } |
| 296 | |
Aaron Ballman | 422ac9e | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 297 | /// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal. |
| 298 | /// If not emit an error and return false. If the argument is an identifier it |
| 299 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 300 | /// literal. |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 301 | bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr, |
| 302 | unsigned ArgNum, StringRef &Str, |
Tim Northover | 64eac85 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 303 | SourceLocation *ArgLocation) { |
Aaron Ballman | 422ac9e | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 304 | // Look for identifiers. If we have one emit a hint to fix it to a literal. |
| 305 | if (Attr.isArgIdent(ArgNum)) { |
| 306 | IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum); |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 307 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Aaron Ballman | 422ac9e | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 308 | << Attr.getName() << AANT_ArgumentString |
| 309 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 310 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 422ac9e | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 311 | Str = Loc->Ident->getName(); |
| 312 | if (ArgLocation) |
| 313 | *ArgLocation = Loc->Loc; |
| 314 | return true; |
| 315 | } |
| 316 | |
| 317 | // Now check for an actual string literal. |
| 318 | Expr *ArgExpr = Attr.getArgAsExpr(ArgNum); |
| 319 | StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
| 320 | if (ArgLocation) |
| 321 | *ArgLocation = ArgExpr->getLocStart(); |
| 322 | |
| 323 | if (!Literal || !Literal->isAscii()) { |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 324 | Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
Aaron Ballman | 422ac9e | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 325 | << Attr.getName() << AANT_ArgumentString; |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | Str = Literal->getString(); |
| 330 | return true; |
| 331 | } |
| 332 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 333 | /// |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 334 | /// \brief Check if passed in Decl is a field or potentially shared global var |
| 335 | /// \return true if the Decl is a field or potentially shared global variable |
| 336 | /// |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 337 | static bool mayBeSharedVariable(const Decl *D) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 338 | if (isa<FieldDecl>(D)) |
| 339 | return true; |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 340 | if (const VarDecl *vd = dyn_cast<VarDecl>(D)) |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 341 | return vd->hasGlobalStorage() && !vd->getTLSKind(); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 342 | |
| 343 | return false; |
| 344 | } |
| 345 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 346 | /// \brief Check if the passed-in expression is of type int or bool. |
| 347 | static bool isIntOrBool(Expr *Exp) { |
| 348 | QualType QT = Exp->getType(); |
| 349 | return QT->isBooleanType() || QT->isIntegerType(); |
| 350 | } |
| 351 | |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 352 | |
| 353 | // Check to see if the type is a smart pointer of some kind. We assume |
| 354 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 355 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 356 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 357 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 358 | if (Res1.empty()) |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 359 | return false; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 360 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 361 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 362 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 363 | if (Res2.empty()) |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 364 | return false; |
| 365 | |
| 366 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 369 | /// \brief Check if passed in Decl is a pointer type. |
| 370 | /// Note that this function may produce an error message. |
| 371 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 372 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 373 | const AttributeList &Attr) { |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 374 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 375 | QualType QT = vd->getType(); |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 376 | if (QT->isAnyPointerType()) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 377 | return true; |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 378 | |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 379 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 380 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 381 | // (We don't want to force template instantiation if we can avoid it, |
| 382 | // since that would alter the order in which templates are instantiated.) |
| 383 | if (RT->isIncompleteType()) |
| 384 | return true; |
| 385 | |
| 386 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 387 | return true; |
| 388 | } |
DeLesley Hutchins | aed9ea3 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 389 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 390 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 391 | << Attr.getName()->getName() << QT; |
| 392 | } else { |
| 393 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 394 | << Attr.getName(); |
| 395 | } |
| 396 | return false; |
| 397 | } |
| 398 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 399 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 400 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 401 | static const RecordType *getRecordType(QualType QT) { |
| 402 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 403 | return RT; |
Benjamin Kramer | 7d23b4a | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 404 | |
| 405 | // Now check if we point to record type. |
| 406 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 407 | return PT->getPointeeType()->getAs<RecordType>(); |
| 408 | |
| 409 | return 0; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 410 | } |
| 411 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 412 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 413 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 414 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 415 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
| 416 | if (RT->getDecl()->getAttr<LockableAttr>()) |
| 417 | return true; |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 422 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 423 | /// resolves to a lockable object. |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 424 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 425 | QualType Ty) { |
| 426 | const RecordType *RT = getRecordType(Ty); |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 427 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 428 | // Warn if could not get record type for this argument. |
Benjamin Kramer | d77ba89 | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 429 | if (!RT) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 430 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 431 | << Attr.getName() << Ty.getAsString(); |
| 432 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 433 | } |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 434 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 435 | // 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] | 436 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 437 | return; |
DeLesley Hutchins | 60f2024 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 438 | |
| 439 | // Allow smart pointers to be used as lockable objects. |
| 440 | // FIXME -- Check the type that the smart pointer points to. |
| 441 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 442 | return; |
| 443 | |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 444 | // Check if the type is lockable. |
| 445 | RecordDecl *RD = RT->getDecl(); |
| 446 | if (RD->getAttr<LockableAttr>()) |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 447 | return; |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 448 | |
| 449 | // Else check if any base classes are lockable. |
| 450 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 451 | CXXBasePaths BPaths(false, false); |
| 452 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 453 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 454 | } |
DeLesley Hutchins | bbba25f | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 455 | |
| 456 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 457 | << Attr.getName() << Ty.getAsString(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 460 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 461 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 462 | /// \param Sidx The attribute argument index to start checking with. |
| 463 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 464 | /// parameter list. |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 465 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 466 | const AttributeList &Attr, |
| 467 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 468 | int Sidx = 0, |
| 469 | bool ParamIdxOk = false) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 470 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 471 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 472 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 473 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 474 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 475 | Args.push_back(ArgExp); |
| 476 | continue; |
| 477 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 478 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 479 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 480 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | f37e4f2 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 481 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 482 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 483 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 484 | Args.push_back(ArgExp); |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 485 | continue; |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 486 | } |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 487 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 488 | // We allow constant strings to be used as a placeholder for expressions |
| 489 | // that are not valid C++ syntax, but warn that they are ignored. |
| 490 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 491 | Attr.getName(); |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 492 | Args.push_back(ArgExp); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 493 | continue; |
| 494 | } |
| 495 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 496 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 497 | |
DeLesley Hutchins | 79747e0 | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 498 | // A pointer to member expression of the form &MyClass::mu is treated |
| 499 | // specially -- we need to look at the type of the member. |
| 500 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 501 | if (UOp->getOpcode() == UO_AddrOf) |
| 502 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 503 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 504 | ArgTy = DRE->getDecl()->getType(); |
| 505 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 506 | // First see if we can just cast to record type, or point to record type. |
| 507 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 508 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 509 | // Now check if we index into a record type function param. |
| 510 | if(!RT && ParamIdxOk) { |
| 511 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 512 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 513 | if(FD && IL) { |
| 514 | unsigned int NumParams = FD->getNumParams(); |
| 515 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 516 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 517 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 518 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 519 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 520 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 521 | continue; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 522 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 523 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
DeLesley Hutchins | 83cad45 | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 527 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 528 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 529 | Args.push_back(ArgExp); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 530 | } |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 533 | //===----------------------------------------------------------------------===// |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 534 | // Attribute Implementations |
| 535 | //===----------------------------------------------------------------------===// |
| 536 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 537 | // FIXME: All this manual attribute parsing code is gross. At the |
| 538 | // least add some helper functions to check most argument patterns (# |
| 539 | // and types of args). |
| 540 | |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 541 | enum ThreadAttributeDeclKind { |
| 542 | ThreadExpectedFieldOrGlobalVar, |
| 543 | ThreadExpectedFunctionOrMethod, |
| 544 | ThreadExpectedClassOrStruct |
| 545 | }; |
| 546 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 547 | static bool checkGuardedVarAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 548 | const AttributeList &Attr) { |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 549 | // D must be either a member field or global (potentially shared) variable. |
| 550 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 551 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 552 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 553 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 556 | return true; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 559 | static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 560 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 561 | return; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 562 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 563 | D->addAttr(::new (S.Context) |
| 564 | GuardedVarAttr(Attr.getRange(), S.Context, |
| 565 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 568 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 569 | const AttributeList &Attr) { |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 570 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 571 | return; |
| 572 | |
| 573 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 574 | return; |
| 575 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 576 | D->addAttr(::new (S.Context) |
| 577 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 578 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 581 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 582 | const AttributeList &Attr, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 583 | Expr* &Arg) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 584 | // D must be either a member field or global (potentially shared) variable. |
| 585 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 586 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 587 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 588 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 589 | } |
| 590 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 591 | SmallVector<Expr*, 1> Args; |
| 592 | // check that all arguments are lockable objects |
| 593 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 594 | unsigned Size = Args.size(); |
| 595 | if (Size != 1) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 596 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 597 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 598 | Arg = Args[0]; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 599 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 600 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 603 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 604 | Expr *Arg = 0; |
| 605 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 606 | return; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 607 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 608 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| 609 | } |
| 610 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 611 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 612 | const AttributeList &Attr) { |
| 613 | Expr *Arg = 0; |
| 614 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 615 | return; |
| 616 | |
| 617 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 618 | return; |
| 619 | |
| 620 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| 621 | S.Context, Arg)); |
| 622 | } |
| 623 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 624 | static bool checkLockableAttrCommon(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 625 | const AttributeList &Attr) { |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 626 | // FIXME: Lockable structs for C code. |
David Blaikie | 88c4b5e | 2013-07-29 18:24:03 +0000 | [diff] [blame] | 627 | if (!isa<RecordDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 628 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 629 | << Attr.getName() << ThreadExpectedClassOrStruct; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 630 | return false; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 633 | return true; |
| 634 | } |
| 635 | |
| 636 | static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 637 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 638 | return; |
| 639 | |
| 640 | D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); |
| 641 | } |
| 642 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 643 | static void handleScopedLockableAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 644 | const AttributeList &Attr) { |
| 645 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 646 | return; |
| 647 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 648 | D->addAttr(::new (S.Context) |
| 649 | ScopedLockableAttr(Attr.getRange(), S.Context, |
| 650 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 653 | static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D, |
| 654 | const AttributeList &Attr) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 655 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 656 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 657 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 658 | return; |
| 659 | } |
| 660 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 661 | D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(), |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 662 | S.Context)); |
| 663 | } |
| 664 | |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 665 | static void handleNoSanitizeAddressAttr(Sema &S, Decl *D, |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 666 | const AttributeList &Attr) { |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 667 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 668 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 669 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 670 | return; |
| 671 | } |
| 672 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 673 | D->addAttr(::new (S.Context) |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 674 | NoSanitizeAddressAttr(Attr.getRange(), S.Context, |
| 675 | Attr.getAttributeSpellingListIndex())); |
| 676 | } |
| 677 | |
| 678 | static void handleNoSanitizeMemory(Sema &S, Decl *D, |
| 679 | const AttributeList &Attr) { |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 680 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 681 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 682 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 683 | return; |
| 684 | } |
| 685 | |
| 686 | D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(), |
| 687 | S.Context)); |
| 688 | } |
| 689 | |
| 690 | static void handleNoSanitizeThread(Sema &S, Decl *D, |
| 691 | const AttributeList &Attr) { |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 692 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 693 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 694 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 695 | return; |
| 696 | } |
| 697 | |
| 698 | D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(), |
| 699 | S.Context)); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 702 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 703 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 704 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 705 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 706 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 707 | |
| 708 | // D must be either a member field or global (potentially shared) variable. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 709 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 710 | if (!VD || !mayBeSharedVariable(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 711 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 712 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 713 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 714 | } |
| 715 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 716 | // Check that this attribute only applies to lockable types. |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 717 | QualType QT = VD->getType(); |
| 718 | if (!QT->isDependentType()) { |
| 719 | const RecordType *RT = getRecordType(QT); |
| 720 | if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 721 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 722 | << Attr.getName(); |
| 723 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 727 | // Check that all arguments are lockable objects. |
| 728 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 729 | if (Args.empty()) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 730 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 731 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 732 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 735 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 736 | const AttributeList &Attr) { |
| 737 | SmallVector<Expr*, 1> Args; |
| 738 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 739 | return; |
| 740 | |
| 741 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 742 | D->addAttr(::new (S.Context) |
| 743 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 744 | StartArg, Args.size(), |
| 745 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 748 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 749 | const AttributeList &Attr) { |
| 750 | SmallVector<Expr*, 1> Args; |
| 751 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 752 | return; |
| 753 | |
| 754 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 755 | D->addAttr(::new (S.Context) |
| 756 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 757 | StartArg, Args.size(), |
| 758 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 761 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 762 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 763 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 764 | // zero or more arguments ok |
| 765 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 766 | // check that the attribute is applied to a function |
| 767 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 768 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 769 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 770 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 773 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 774 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 775 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 776 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 779 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 780 | const AttributeList &Attr) { |
| 781 | SmallVector<Expr*, 1> Args; |
| 782 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 783 | return; |
| 784 | |
| 785 | unsigned Size = Args.size(); |
| 786 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 787 | D->addAttr(::new (S.Context) |
| 788 | SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 789 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 792 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 793 | const AttributeList &Attr) { |
| 794 | SmallVector<Expr*, 1> Args; |
| 795 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 796 | return; |
| 797 | |
| 798 | unsigned Size = Args.size(); |
| 799 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 800 | D->addAttr(::new (S.Context) |
| 801 | ExclusiveLockFunctionAttr(Attr.getRange(), S.Context, |
| 802 | StartArg, Size, |
| 803 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 804 | } |
| 805 | |
DeLesley Hutchins | 5c6134f | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 806 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 807 | const AttributeList &Attr) { |
| 808 | SmallVector<Expr*, 1> Args; |
| 809 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 810 | return; |
| 811 | |
| 812 | unsigned Size = Args.size(); |
| 813 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 814 | D->addAttr(::new (S.Context) |
| 815 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 816 | Attr.getAttributeSpellingListIndex())); |
| 817 | } |
| 818 | |
| 819 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 820 | const AttributeList &Attr) { |
| 821 | SmallVector<Expr*, 1> Args; |
| 822 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 823 | return; |
| 824 | |
| 825 | unsigned Size = Args.size(); |
| 826 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 827 | D->addAttr(::new (S.Context) |
| 828 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 829 | StartArg, Size, |
| 830 | Attr.getAttributeSpellingListIndex())); |
| 831 | } |
| 832 | |
| 833 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 834 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 835 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 836 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 837 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 838 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 839 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 840 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 841 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 842 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 843 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 846 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 847 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 848 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 849 | return false; |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 853 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 854 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 855 | return true; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 858 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 859 | const AttributeList &Attr) { |
| 860 | SmallVector<Expr*, 2> Args; |
| 861 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 862 | return; |
| 863 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 864 | D->addAttr(::new (S.Context) |
| 865 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 866 | Attr.getArgAsExpr(0), |
| 867 | Args.data(), Args.size(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 868 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 871 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 872 | const AttributeList &Attr) { |
| 873 | SmallVector<Expr*, 2> Args; |
| 874 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 875 | return; |
| 876 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 877 | D->addAttr(::new (S.Context) |
| 878 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 879 | Attr.getArgAsExpr(0), |
| 880 | Args.data(), Args.size(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 881 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 884 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 885 | const AttributeList &Attr, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 886 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 887 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 888 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 889 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 890 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 891 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 892 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 893 | return false; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 896 | // check that all arguments are lockable objects |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 897 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 898 | if (Args.empty()) |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 899 | return false; |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 900 | |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 901 | return true; |
| 902 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 903 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 904 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 905 | const AttributeList &Attr) { |
| 906 | SmallVector<Expr*, 1> Args; |
| 907 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 908 | return; |
| 909 | |
| 910 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 911 | D->addAttr(::new (S.Context) |
| 912 | ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context, |
| 913 | StartArg, Args.size(), |
| 914 | Attr.getAttributeSpellingListIndex())); |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Michael Han | f1aae3b | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 917 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 918 | const AttributeList &Attr) { |
| 919 | SmallVector<Expr*, 1> Args; |
| 920 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 921 | return; |
| 922 | |
| 923 | Expr **StartArg = &Args[0]; |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 924 | D->addAttr(::new (S.Context) |
| 925 | SharedLocksRequiredAttr(Attr.getRange(), S.Context, |
| 926 | StartArg, Args.size(), |
| 927 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 930 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 931 | const AttributeList &Attr) { |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 932 | // zero or more arguments ok |
| 933 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 934 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 935 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 936 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 937 | return; |
| 938 | } |
| 939 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 940 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 941 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 942 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 943 | unsigned Size = Args.size(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 944 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 945 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 946 | D->addAttr(::new (S.Context) |
| 947 | UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 948 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 952 | const AttributeList &Attr) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 953 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 954 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 955 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 956 | return; |
| 957 | } |
| 958 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 959 | // check that the argument is lockable object |
DeLesley Hutchins | f26efd7 | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 960 | SmallVector<Expr*, 1> Args; |
| 961 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 962 | unsigned Size = Args.size(); |
| 963 | if (Size == 0) |
| 964 | return; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 965 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 966 | D->addAttr(::new (S.Context) |
| 967 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 968 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 972 | const AttributeList &Attr) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 973 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 974 | return; |
| 975 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 976 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 0aa52aa | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 977 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 978 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 979 | return; |
| 980 | } |
| 981 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 982 | // check that all arguments are lockable objects |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 983 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 984 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 985 | unsigned Size = Args.size(); |
DeLesley Hutchins | ae519c4 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 986 | if (Size == 0) |
| 987 | return; |
| 988 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 989 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 990 | D->addAttr(::new (S.Context) |
| 991 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 992 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 993 | } |
| 994 | |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 995 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | a33ab60 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 996 | ConsumableAttr::ConsumedState DefaultState; |
| 997 | |
| 998 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 999 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 1000 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1001 | DefaultState)) { |
| 1002 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 1003 | << Attr.getName() << IL->Ident; |
David Blaikie | a33ab60 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1004 | return; |
| 1005 | } |
David Blaikie | a33ab60 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1006 | } else { |
| 1007 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1008 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 1009 | return; |
| 1010 | } |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1011 | |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1012 | if (!isa<CXXRecordDecl>(D)) { |
| 1013 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1014 | Attr.getName() << ExpectedClass; |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1015 | return; |
| 1016 | } |
| 1017 | |
| 1018 | D->addAttr(::new (S.Context) |
David Blaikie | a33ab60 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1019 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1020 | Attr.getAttributeSpellingListIndex())); |
| 1021 | } |
| 1022 | |
| 1023 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 1024 | const AttributeList &Attr) { |
| 1025 | ASTContext &CurrContext = S.getASTContext(); |
| 1026 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 1027 | |
| 1028 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 1029 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 1030 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 1031 | RD->getNameAsString(); |
| 1032 | |
| 1033 | return false; |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | return true; |
| 1038 | } |
| 1039 | |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1040 | |
DeLesley Hutchins | 6654085 | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1041 | static void handleCallableWhenAttr(Sema &S, Decl *D, |
| 1042 | const AttributeList &Attr) { |
Aaron Ballman | 9aa9261 | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1043 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1044 | return; |
DeLesley Hutchins | 6654085 | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1045 | |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1046 | if (!isa<CXXMethodDecl>(D)) { |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1047 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1048 | Attr.getName() << ExpectedMethod; |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1049 | return; |
| 1050 | } |
| 1051 | |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1052 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1053 | return; |
| 1054 | |
DeLesley Hutchins | 6654085 | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1055 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 1056 | for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) { |
| 1057 | CallableWhenAttr::ConsumedState CallableState; |
| 1058 | |
Aaron Ballman | 9025a18 | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1059 | StringRef StateString; |
| 1060 | SourceLocation Loc; |
| 1061 | if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc)) |
| 1062 | return; |
| 1063 | |
| 1064 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | d4f0e19 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1065 | CallableState)) { |
Aaron Ballman | 9025a18 | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1066 | S.Diag(Loc, diag::warn_attribute_type_not_supported) |
| 1067 | << Attr.getName() << StateString; |
DeLesley Hutchins | 6654085 | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1068 | return; |
| 1069 | } |
Aaron Ballman | 9025a18 | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1070 | |
| 1071 | States.push_back(CallableState); |
DeLesley Hutchins | 6654085 | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1074 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | 6654085 | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1075 | CallableWhenAttr(Attr.getRange(), S.Context, States.data(), |
| 1076 | States.size(), Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1079 | |
DeLesley Hutchins | d4f0e19 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1080 | static void handleParamTypestateAttr(Sema &S, Decl *D, |
| 1081 | const AttributeList &Attr) { |
| 1082 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
| 1083 | |
| 1084 | if (!isa<ParmVarDecl>(D)) { |
| 1085 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1086 | Attr.getName() << ExpectedParameter; |
| 1087 | return; |
| 1088 | } |
| 1089 | |
| 1090 | ParamTypestateAttr::ConsumedState ParamState; |
| 1091 | |
| 1092 | if (Attr.isArgIdent(0)) { |
| 1093 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1094 | StringRef StateString = Ident->Ident->getName(); |
| 1095 | |
| 1096 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 1097 | ParamState)) { |
| 1098 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1099 | << Attr.getName() << StateString; |
| 1100 | return; |
| 1101 | } |
| 1102 | } else { |
| 1103 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1104 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1105 | return; |
| 1106 | } |
| 1107 | |
| 1108 | // FIXME: This check is currently being done in the analysis. It can be |
| 1109 | // enabled here only after the parser propagates attributes at |
| 1110 | // template specialization definition, not declaration. |
| 1111 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 1112 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1113 | // |
| 1114 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1115 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1116 | // ReturnType.getAsString(); |
| 1117 | // return; |
| 1118 | //} |
| 1119 | |
| 1120 | D->addAttr(::new (S.Context) |
| 1121 | ParamTypestateAttr(Attr.getRange(), S.Context, ParamState, |
| 1122 | Attr.getAttributeSpellingListIndex())); |
| 1123 | } |
| 1124 | |
| 1125 | |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1126 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 1127 | const AttributeList &Attr) { |
DeLesley Hutchins | cd0f6d7 | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1128 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
| 1129 | |
| 1130 | if (!(isa<FunctionDecl>(D) || isa<ParmVarDecl>(D))) { |
| 1131 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1132 | Attr.getName() << ExpectedFunctionMethodOrParameter; |
| 1133 | return; |
| 1134 | } |
| 1135 | |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1136 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 1137 | |
| 1138 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1139 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 1140 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1141 | ReturnState)) { |
| 1142 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 1143 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1144 | return; |
| 1145 | } |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1146 | } else { |
| 1147 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1148 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1149 | return; |
| 1150 | } |
| 1151 | |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1152 | // FIXME: This check is currently being done in the analysis. It can be |
| 1153 | // enabled here only after the parser propagates attributes at |
| 1154 | // template specialization definition, not declaration. |
| 1155 | //QualType ReturnType; |
| 1156 | // |
DeLesley Hutchins | cd0f6d7 | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1157 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 1158 | // ReturnType = Param->getType(); |
| 1159 | // |
| 1160 | //} else if (const CXXConstructorDecl *Constructor = |
| 1161 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1162 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 1163 | // |
| 1164 | //} else { |
| 1165 | // |
| 1166 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 1167 | //} |
| 1168 | // |
| 1169 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1170 | // |
| 1171 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1172 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1173 | // ReturnType.getAsString(); |
| 1174 | // return; |
| 1175 | //} |
| 1176 | |
| 1177 | D->addAttr(::new (S.Context) |
| 1178 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 1179 | Attr.getAttributeSpellingListIndex())); |
| 1180 | } |
| 1181 | |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1182 | |
| 1183 | static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 9aa9261 | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1184 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1185 | return; |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1186 | |
| 1187 | if (!isa<CXXMethodDecl>(D)) { |
| 1188 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1189 | Attr.getName() << ExpectedMethod; |
| 1190 | return; |
| 1191 | } |
| 1192 | |
| 1193 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1194 | return; |
| 1195 | |
| 1196 | SetTypestateAttr::ConsumedState NewState; |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1197 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 2b1599c | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1198 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1199 | StringRef Param = Ident->Ident->getName(); |
| 1200 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 1201 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1202 | << Attr.getName() << Param; |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1203 | return; |
| 1204 | } |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1205 | } else { |
| 1206 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1207 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1208 | return; |
| 1209 | } |
| 1210 | |
| 1211 | D->addAttr(::new (S.Context) |
| 1212 | SetTypestateAttr(Attr.getRange(), S.Context, NewState, |
| 1213 | Attr.getAttributeSpellingListIndex())); |
| 1214 | } |
| 1215 | |
Chris Wailes | 0e429f1 | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1216 | static void handleTestTypestateAttr(Sema &S, Decl *D, |
| 1217 | const AttributeList &Attr) { |
Aaron Ballman | 9aa9261 | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1218 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1219 | return; |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1220 | |
| 1221 | if (!isa<CXXMethodDecl>(D)) { |
| 1222 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1223 | Attr.getName() << ExpectedMethod; |
| 1224 | return; |
| 1225 | } |
| 1226 | |
| 1227 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1228 | return; |
| 1229 | |
Chris Wailes | 0e429f1 | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1230 | TestTypestateAttr::ConsumedState TestState; |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1231 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 2b1599c | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1232 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1233 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 0e429f1 | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1234 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Aaron Ballman | 2b1599c | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1235 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1236 | << Attr.getName() << Param; |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1237 | return; |
| 1238 | } |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1239 | } else { |
| 1240 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1241 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1242 | return; |
| 1243 | } |
| 1244 | |
| 1245 | D->addAttr(::new (S.Context) |
Chris Wailes | 0e429f1 | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1246 | TestTypestateAttr(Attr.getRange(), S.Context, TestState, |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1247 | Attr.getAttributeSpellingListIndex())); |
| 1248 | } |
| 1249 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1250 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1251 | const AttributeList &Attr) { |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1252 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 1253 | if (TD == 0) { |
| 1254 | // __attribute__((ext_vector_type(N))) can only be applied to typedefs |
| 1255 | // and type-ids. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1256 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1257 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1258 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1259 | |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1260 | // Remember this typedef decl, we will need it later for diagnostics. |
| 1261 | S.ExtVectorDecls.push_back(TD); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1264 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1265 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1266 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1267 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1268 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1269 | // has no effect. |
Eli Friedman | b68ec6b | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1270 | if (!FD->getType()->isDependentType() && |
| 1271 | !FD->getType()->isIncompleteType() && |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1272 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1273 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1274 | << Attr.getName() << FD->getType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1275 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1276 | FD->addAttr(::new (S.Context) |
| 1277 | PackedAttr(Attr.getRange(), S.Context, |
| 1278 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1279 | } else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1280 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1283 | static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 5f608ae | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 1284 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1285 | RD->addAttr(::new (S.Context) |
| 1286 | MsStructAttr(Attr.getRange(), S.Context, |
| 1287 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | c1a0a73 | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 1288 | else |
| 1289 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1290 | } |
| 1291 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1292 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1293 | // The IBAction attributes only apply to instance methods. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1294 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1295 | if (MD->isInstanceMethod()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1296 | D->addAttr(::new (S.Context) |
| 1297 | IBActionAttr(Attr.getRange(), S.Context, |
| 1298 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1299 | return; |
| 1300 | } |
| 1301 | |
Ted Kremenek | 4ee2bb1 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 1302 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1305 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1306 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1307 | // variables or properties of Objective-C classes. The outlet must also |
| 1308 | // have an object reference type. |
| 1309 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1310 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 0bfaf06 | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1311 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1312 | << Attr.getName() << VD->getType() << 0; |
| 1313 | return false; |
| 1314 | } |
| 1315 | } |
| 1316 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1317 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1318 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1319 | << Attr.getName() << PD->getType() << 1; |
| 1320 | return false; |
| 1321 | } |
| 1322 | } |
| 1323 | else { |
| 1324 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1325 | return false; |
| 1326 | } |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1327 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1328 | return true; |
| 1329 | } |
| 1330 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1331 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1332 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1333 | return; |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1334 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1335 | D->addAttr(::new (S.Context) |
| 1336 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1337 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1340 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1341 | const AttributeList &Attr) { |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1342 | |
| 1343 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1344 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1345 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1346 | << Attr.getName() << 1; |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1347 | return; |
| 1348 | } |
| 1349 | |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1350 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1351 | return; |
Ted Kremenek | 2f041d0 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1352 | |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1353 | ParsedType PT; |
| 1354 | |
| 1355 | if (Attr.hasParsedType()) |
| 1356 | PT = Attr.getTypeArg(); |
| 1357 | else { |
| 1358 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(), |
| 1359 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1360 | if (!PT) { |
| 1361 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
| 1362 | return; |
| 1363 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1364 | } |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1365 | |
Richard Smith | ddc2a53 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1366 | TypeSourceInfo *QTLoc = 0; |
| 1367 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1368 | if (!QTLoc) |
| 1369 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc()); |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1370 | |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1371 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1372 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1373 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1374 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | f4072ae | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1375 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1376 | S.Diag(Attr.getLoc(), |
| 1377 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1378 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1379 | return; |
| 1380 | } |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1381 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1382 | D->addAttr(::new (S.Context) |
Richard Smith | ddc2a53 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1383 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1384 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1387 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1388 | if (const RecordType *UT = T->getAsUnionType()) |
| 1389 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1390 | RecordDecl *UD = UT->getDecl(); |
| 1391 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1392 | itend = UD->field_end(); it != itend; ++it) { |
| 1393 | QualType QT = it->getType(); |
| 1394 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1395 | T = QT; |
| 1396 | return; |
| 1397 | } |
| 1398 | } |
| 1399 | } |
| 1400 | } |
| 1401 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1402 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nuno Lopes | 174930d | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1403 | if (!isFunctionOrMethod(D)) { |
| 1404 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1405 | << Attr.getName() << ExpectedFunctionOrMethod; |
Nuno Lopes | 174930d | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1406 | return; |
| 1407 | } |
| 1408 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1409 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1410 | return; |
| 1411 | |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1412 | SmallVector<unsigned, 8> SizeArgs; |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1413 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1414 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1415 | uint64_t Idx; |
| 1416 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1417 | Attr.getLoc(), i + 1, Ex, Idx)) |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1418 | return; |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1419 | |
| 1420 | // check if the function argument is of an integer type |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1421 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1422 | if (!T->isIntegerType()) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 1423 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1424 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 1425 | << Ex->getSourceRange(); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1426 | return; |
| 1427 | } |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1428 | SizeArgs.push_back(Idx); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | // check if the function returns a pointer |
| 1432 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 1433 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1434 | << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1437 | D->addAttr(::new (S.Context) |
| 1438 | AllocSizeAttr(Attr.getRange(), S.Context, |
| 1439 | SizeArgs.data(), SizeArgs.size(), |
| 1440 | Attr.getAttributeSpellingListIndex())); |
Nuno Lopes | 587de5b | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1443 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1444 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1445 | // ignore it as well |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1446 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1447 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1448 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1449 | return; |
| 1450 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1451 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1452 | SmallVector<unsigned, 8> NonNullArgs; |
| 1453 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1454 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1455 | uint64_t Idx; |
| 1456 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1457 | Attr.getLoc(), i + 1, Ex, Idx)) |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1458 | return; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1459 | |
| 1460 | // Is the function argument a pointer type? |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1461 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1462 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | 68fe96a | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1463 | |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1464 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1465 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | c9ef405 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1466 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1467 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1468 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1469 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1470 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1471 | NonNullArgs.push_back(Idx); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1472 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1473 | |
| 1474 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1475 | // arguments have a nonnull attribute. |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1476 | if (NonNullArgs.empty()) { |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1477 | for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) { |
| 1478 | QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType(); |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1479 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1480 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1481 | NonNullArgs.push_back(i); |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1482 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1483 | |
Ted Kremenek | ee1c08c | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1484 | // No pointer arguments? |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1485 | if (NonNullArgs.empty()) { |
| 1486 | // Warn the trivial case only if attribute is not coming from a |
| 1487 | // macro instantiation. |
| 1488 | if (Attr.getLoc().isFileID()) |
| 1489 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1490 | return; |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1491 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1492 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1493 | |
Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1494 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1495 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1496 | llvm::array_pod_sort(start, start + size); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1497 | D->addAttr(::new (S.Context) |
| 1498 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1499 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1502 | static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) { |
| 1503 | switch (K) { |
| 1504 | case OwnershipAttr::Holds: return "'ownership_holds'"; |
| 1505 | case OwnershipAttr::Takes: return "'ownership_takes'"; |
| 1506 | case OwnershipAttr::Returns: return "'ownership_returns'"; |
| 1507 | } |
| 1508 | llvm_unreachable("unknown ownership"); |
| 1509 | } |
| 1510 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1511 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1512 | // This attribute must be applied to a function declaration. The first |
| 1513 | // argument to the attribute must be an identifier, the name of the resource, |
| 1514 | // for example: malloc. The following arguments must be argument indexes, the |
| 1515 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1516 | // The difference between Holds and Takes is that a pointer may still be used |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1517 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1518 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1519 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1520 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1521 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1522 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1523 | return; |
| 1524 | } |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1525 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1526 | // Figure out our Kind, and check arguments while we're at it. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1527 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1528 | switch (AL.getKind()) { |
| 1529 | case AttributeList::AT_ownership_takes: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1530 | K = OwnershipAttr::Takes; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1531 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1532 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1533 | return; |
| 1534 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1535 | break; |
| 1536 | case AttributeList::AT_ownership_holds: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1537 | K = OwnershipAttr::Holds; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1538 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1539 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1540 | return; |
| 1541 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1542 | break; |
| 1543 | case AttributeList::AT_ownership_returns: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1544 | K = OwnershipAttr::Returns; |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1545 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1546 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1547 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1548 | return; |
| 1549 | } |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1550 | break; |
| 1551 | default: |
| 1552 | // This should never happen given how we are called. |
| 1553 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1556 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1557 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1558 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1559 | return; |
| 1560 | } |
| 1561 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1562 | StringRef Module = AL.getArgAsIdent(0)->Ident->getName(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1563 | |
| 1564 | // Normalize the argument, __foo__ becomes foo. |
| 1565 | if (Module.startswith("__") && Module.endswith("__")) |
| 1566 | Module = Module.substr(2, Module.size() - 4); |
| 1567 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1568 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1569 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1570 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1571 | uint64_t Idx; |
| 1572 | if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(), |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1573 | AL.getLoc(), i, Ex, Idx)) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1574 | return; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1575 | |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1576 | // Is the function argument a pointer type? |
| 1577 | QualType T = getFunctionOrMethodArgType(D, Idx); |
| 1578 | int Err = -1; // No error |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1579 | switch (K) { |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1580 | case OwnershipAttr::Takes: |
| 1581 | case OwnershipAttr::Holds: |
| 1582 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1583 | Err = 0; |
| 1584 | break; |
| 1585 | case OwnershipAttr::Returns: |
| 1586 | if (!T->isIntegerType()) |
| 1587 | Err = 1; |
| 1588 | break; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1589 | } |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1590 | if (-1 != Err) { |
Fariborz Jahanian | 937ec1d | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1591 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1592 | << Ex->getSourceRange(); |
| 1593 | return; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1594 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1595 | |
| 1596 | // Check we don't have a conflict with another ownership attribute. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1597 | for (specific_attr_iterator<OwnershipAttr> |
Aaron Ballman | e1668a3 | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1598 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1599 | e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) { |
| 1600 | if ((*i)->getOwnKind() != K && (*i)->args_end() != |
| 1601 | std::find((*i)->args_begin(), (*i)->args_end(), Idx)) { |
| 1602 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1603 | << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind()); |
| 1604 | return; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1605 | } |
| 1606 | } |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1607 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | unsigned* start = OwnershipArgs.data(); |
| 1611 | unsigned size = OwnershipArgs.size(); |
| 1612 | llvm::array_pod_sort(start, start + size); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1613 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1614 | D->addAttr(::new (S.Context) |
| 1615 | OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size, |
| 1616 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1619 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1620 | // Check the attribute arguments. |
| 1621 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1622 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1623 | << Attr.getName() << 1; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1624 | return; |
| 1625 | } |
| 1626 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1627 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1628 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1629 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1630 | return; |
| 1631 | } |
| 1632 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1633 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1634 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1635 | // gcc rejects |
| 1636 | // class c { |
| 1637 | // static int a __attribute__((weakref ("v2"))); |
| 1638 | // static int b() __attribute__((weakref ("f3"))); |
| 1639 | // }; |
| 1640 | // and ignores the attributes of |
| 1641 | // void f(void) { |
| 1642 | // static int a __attribute__((weakref ("v2"))); |
| 1643 | // } |
| 1644 | // we reject them |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1645 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1646 | if (!Ctx->isFileContext()) { |
| 1647 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1648 | nd->getNameAsString(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1649 | return; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | // The GCC manual says |
| 1653 | // |
| 1654 | // At present, a declaration to which `weakref' is attached can only |
| 1655 | // be `static'. |
| 1656 | // |
| 1657 | // It also says |
| 1658 | // |
| 1659 | // Without a TARGET, |
| 1660 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1661 | // equivalent to `weak'. |
| 1662 | // |
| 1663 | // gcc 4.4.1 will accept |
| 1664 | // int a7 __attribute__((weakref)); |
| 1665 | // as |
| 1666 | // int a7 __attribute__((weak)); |
| 1667 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1668 | // it if this behaviour is actually used. |
| 1669 | |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1670 | // GCC rejects |
| 1671 | // static ((alias ("y"), weakref)). |
| 1672 | // Should we? How to check that weakref is before or after alias? |
| 1673 | |
Aaron Ballman | be116e2 | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1674 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1675 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1676 | // StringRef parameter it was given anyway. |
Aaron Ballman | 422ac9e | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1677 | StringRef Str; |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1678 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1679 | // GCC will accept anything as the argument of weakref. Should we |
| 1680 | // check for an existing decl? |
Aaron Ballman | 422ac9e | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1681 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1682 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1683 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1684 | D->addAttr(::new (S.Context) |
| 1685 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1686 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1689 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1690 | StringRef Str; |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1691 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1692 | return; |
| 1693 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1694 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | f5fe292 | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1695 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1696 | return; |
| 1697 | } |
| 1698 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1699 | // FIXME: check if target symbol exists in current file |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1700 | |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1701 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1702 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1705 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1706 | if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) { |
| 1707 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1708 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1709 | return; |
| 1710 | } |
| 1711 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1712 | D->addAttr(::new (S.Context) |
| 1713 | MinSizeAttr(Attr.getRange(), S.Context, |
| 1714 | Attr.getAttributeSpellingListIndex())); |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1717 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1718 | if (!isa<FunctionDecl>(D)) { |
| 1719 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1720 | << Attr.getName() << ExpectedFunction; |
| 1721 | return; |
| 1722 | } |
| 1723 | |
| 1724 | if (D->hasAttr<HotAttr>()) { |
| 1725 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1726 | << Attr.getName() << "hot"; |
| 1727 | return; |
| 1728 | } |
| 1729 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1730 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1731 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1735 | if (!isa<FunctionDecl>(D)) { |
| 1736 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1737 | << Attr.getName() << ExpectedFunction; |
| 1738 | return; |
| 1739 | } |
| 1740 | |
| 1741 | if (D->hasAttr<ColdAttr>()) { |
| 1742 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1743 | << Attr.getName() << "cold"; |
| 1744 | return; |
| 1745 | } |
| 1746 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1747 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1748 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | ee409a9 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1751 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1752 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1753 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1754 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1755 | return; |
| 1756 | } |
| 1757 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1758 | D->addAttr(::new (S.Context) |
| 1759 | NakedAttr(Attr.getRange(), S.Context, |
| 1760 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1763 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 1764 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1765 | if (!isa<FunctionDecl>(D)) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1766 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1767 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1768 | return; |
| 1769 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1770 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1771 | D->addAttr(::new (S.Context) |
| 1772 | AlwaysInlineAttr(Attr.getRange(), S.Context, |
| 1773 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1776 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1777 | const AttributeList &Attr) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1778 | StringRef Model; |
| 1779 | SourceLocation LiteralLoc; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1780 | // Check that it is a string. |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1781 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1782 | return; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1783 | |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 1784 | if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) { |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1785 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1786 | << Attr.getName() << ExpectedTLSVar; |
| 1787 | return; |
| 1788 | } |
| 1789 | |
| 1790 | // Check that the value. |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1791 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1792 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1793 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1794 | return; |
| 1795 | } |
| 1796 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1797 | D->addAttr(::new (S.Context) |
| 1798 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1799 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1802 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1803 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1804 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1805 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1806 | D->addAttr(::new (S.Context) |
| 1807 | MallocAttr(Attr.getRange(), S.Context, |
| 1808 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1809 | return; |
| 1810 | } |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1813 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1816 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1817 | D->addAttr(::new (S.Context) |
| 1818 | MayAliasAttr(Attr.getRange(), S.Context, |
| 1819 | Attr.getAttributeSpellingListIndex())); |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1822 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1823 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1824 | D->addAttr(::new (S.Context) |
| 1825 | NoCommonAttr(Attr.getRange(), S.Context, |
| 1826 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1827 | else |
| 1828 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1829 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1832 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 3e1aca2 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1833 | if (S.LangOpts.CPlusPlus) { |
| 1834 | S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus); |
| 1835 | return; |
| 1836 | } |
| 1837 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1838 | if (isa<VarDecl>(D)) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1839 | D->addAttr(::new (S.Context) |
| 1840 | CommonAttr(Attr.getRange(), S.Context, |
| 1841 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 722109c | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1842 | else |
| 1843 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1844 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1847 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1848 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1849 | |
| 1850 | if (S.CheckNoReturnAttr(attr)) return; |
| 1851 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1852 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1853 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1854 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1855 | return; |
| 1856 | } |
| 1857 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1858 | D->addAttr(::new (S.Context) |
| 1859 | NoReturnAttr(attr.getRange(), S.Context, |
| 1860 | attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1864 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1865 | attr.setInvalid(); |
| 1866 | return true; |
| 1867 | } |
| 1868 | |
| 1869 | return false; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1872 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1873 | const AttributeList &Attr) { |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1874 | |
| 1875 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1876 | // because 'analyzer_noreturn' does not impact the type. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1877 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1878 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1879 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1880 | && !VD->getType()->isFunctionPointerType())) { |
| 1881 | S.Diag(Attr.getLoc(), |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1882 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1883 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1884 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | b56c1cc | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1885 | return; |
| 1886 | } |
| 1887 | } |
| 1888 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1889 | D->addAttr(::new (S.Context) |
| 1890 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1891 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1894 | static void handleCXX11NoReturnAttr(Sema &S, Decl *D, |
| 1895 | const AttributeList &Attr) { |
| 1896 | // C++11 [dcl.attr.noreturn]p1: |
| 1897 | // The attribute may be applied to the declarator-id in a function |
| 1898 | // declaration. |
| 1899 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 1900 | if (!FD) { |
| 1901 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1902 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1903 | return; |
| 1904 | } |
| 1905 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1906 | D->addAttr(::new (S.Context) |
| 1907 | CXX11NoReturnAttr(Attr.getRange(), S.Context, |
| 1908 | Attr.getAttributeSpellingListIndex())); |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1911 | // PS3 PPU-specific. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1912 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1913 | /* |
| 1914 | Returning a Vector Class in Registers |
| 1915 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1916 | According to the PPU ABI specifications, a class with a single member of |
| 1917 | vector type is returned in memory when used as the return value of a function. |
| 1918 | This results in inefficient code when implementing vector classes. To return |
| 1919 | the value in a single vector register, add the vecreturn attribute to the |
| 1920 | class definition. This attribute is also applicable to struct types. |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1921 | |
| 1922 | Example: |
| 1923 | |
| 1924 | struct Vector |
| 1925 | { |
| 1926 | __vector float xyzw; |
| 1927 | } __attribute__((vecreturn)); |
| 1928 | |
| 1929 | Vector Add(Vector lhs, Vector rhs) |
| 1930 | { |
| 1931 | Vector result; |
| 1932 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1933 | return result; // This will be returned in a register |
| 1934 | } |
| 1935 | */ |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1936 | if (!isa<RecordDecl>(D)) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1937 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1938 | << Attr.getName() << ExpectedClass; |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1939 | return; |
| 1940 | } |
| 1941 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1942 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1943 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1944 | return; |
| 1945 | } |
| 1946 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1947 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1948 | int count = 0; |
| 1949 | |
| 1950 | if (!isa<CXXRecordDecl>(record)) { |
| 1951 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1952 | return; |
| 1953 | } |
| 1954 | |
| 1955 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1956 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1957 | return; |
| 1958 | } |
| 1959 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1960 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1961 | iter != record->field_end(); iter++) { |
John Thompson | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1962 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1963 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1964 | return; |
| 1965 | } |
| 1966 | count++; |
| 1967 | } |
| 1968 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1969 | D->addAttr(::new (S.Context) |
| 1970 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1971 | Attr.getAttributeSpellingListIndex())); |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1974 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1975 | const AttributeList &Attr) { |
| 1976 | if (isa<ParmVarDecl>(D)) { |
| 1977 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1978 | // parameter of a function declaration or lambda. |
| 1979 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1980 | S.Diag(Attr.getLoc(), |
| 1981 | diag::err_carries_dependency_param_not_function_decl); |
| 1982 | return; |
| 1983 | } |
| 1984 | } else if (!isa<FunctionDecl>(D)) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1985 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1986 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1987 | return; |
| 1988 | } |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1989 | |
| 1990 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1991 | Attr.getRange(), S.Context, |
| 1992 | Attr.getAttributeSpellingListIndex())); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1995 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1996 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 568eae4 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1997 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1998 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1999 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 2000 | return; |
| 2001 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2002 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2003 | D->addAttr(::new (S.Context) |
| 2004 | UnusedAttr(Attr.getRange(), S.Context, |
| 2005 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 2008 | static void handleReturnsTwiceAttr(Sema &S, Decl *D, |
| 2009 | const AttributeList &Attr) { |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 2010 | if (!isa<FunctionDecl>(D)) { |
| 2011 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2012 | << Attr.getName() << ExpectedFunction; |
| 2013 | return; |
| 2014 | } |
| 2015 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2016 | D->addAttr(::new (S.Context) |
| 2017 | ReturnsTwiceAttr(Attr.getRange(), S.Context, |
| 2018 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2021 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2022 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 29535ba | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 2023 | if (VD->hasLocalStorage()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2024 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 2025 | return; |
| 2026 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2027 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2028 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2029 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2030 | return; |
| 2031 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2032 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2033 | D->addAttr(::new (S.Context) |
| 2034 | UsedAttr(Attr.getRange(), S.Context, |
| 2035 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2038 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2039 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2040 | if (Attr.getNumArgs() > 1) { |
| 2041 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2042 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2043 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2044 | |
| 2045 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 2046 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2047 | Expr *E = Attr.getArgAsExpr(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2048 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2049 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2050 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2051 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2052 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2053 | << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2054 | return; |
| 2055 | } |
| 2056 | priority = Idx.getZExtValue(); |
| 2057 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2058 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2059 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2060 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2061 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2062 | return; |
| 2063 | } |
| 2064 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2065 | D->addAttr(::new (S.Context) |
| 2066 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 2067 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2070 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2071 | // check the attribute arguments. |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2072 | if (Attr.getNumArgs() > 1) { |
| 2073 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2074 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2075 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2076 | |
| 2077 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 2078 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2079 | Expr *E = Attr.getArgAsExpr(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2080 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2081 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2082 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2083 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2084 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2085 | << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2086 | return; |
| 2087 | } |
| 2088 | priority = Idx.getZExtValue(); |
| 2089 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2090 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2091 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2092 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2093 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2094 | return; |
| 2095 | } |
| 2096 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2097 | D->addAttr(::new (S.Context) |
| 2098 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 2099 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2102 | template <typename AttrTy> |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 2103 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 2104 | const AttributeList &Attr) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2105 | unsigned NumArgs = Attr.getNumArgs(); |
| 2106 | if (NumArgs > 1) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2107 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2108 | return; |
| 2109 | } |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2110 | |
| 2111 | // Handle the case where the attribute has a text message. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2112 | StringRef Str; |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2113 | if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2114 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2115 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2116 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 2117 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2120 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 2121 | const AttributeList &Attr) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2122 | D->addAttr(::new (S.Context) |
| 2123 | ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context, |
| 2124 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2127 | static void handleObjCRootClassAttr(Sema &S, Decl *D, |
| 2128 | const AttributeList &Attr) { |
| 2129 | if (!isa<ObjCInterfaceDecl>(D)) { |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 2130 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2131 | << Attr.getName() << ExpectedObjectiveCInterface; |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2132 | return; |
| 2133 | } |
| 2134 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2135 | D->addAttr(::new (S.Context) |
| 2136 | ObjCRootClassAttr(Attr.getRange(), S.Context, |
| 2137 | Attr.getAttributeSpellingListIndex())); |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2140 | static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, |
| 2141 | const AttributeList &Attr) { |
Fariborz Jahanian | 341b8be | 2012-01-03 22:52:32 +0000 | [diff] [blame] | 2142 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 2143 | S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis); |
| 2144 | return; |
| 2145 | } |
| 2146 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2147 | D->addAttr(::new (S.Context) |
| 2148 | ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context, |
| 2149 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
Jordy Rose | fad5de9 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2152 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2153 | IdentifierInfo *Platform, |
| 2154 | VersionTuple Introduced, |
| 2155 | VersionTuple Deprecated, |
| 2156 | VersionTuple Obsoleted) { |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2157 | StringRef PlatformName |
| 2158 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2159 | if (PlatformName.empty()) |
| 2160 | PlatformName = Platform->getName(); |
| 2161 | |
| 2162 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2163 | // of these steps are needed). |
| 2164 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2165 | !(Introduced <= Deprecated)) { |
| 2166 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2167 | << 1 << PlatformName << Deprecated.getAsString() |
| 2168 | << 0 << Introduced.getAsString(); |
| 2169 | return true; |
| 2170 | } |
| 2171 | |
| 2172 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2173 | !(Introduced <= Obsoleted)) { |
| 2174 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2175 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2176 | << 0 << Introduced.getAsString(); |
| 2177 | return true; |
| 2178 | } |
| 2179 | |
| 2180 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2181 | !(Deprecated <= Obsoleted)) { |
| 2182 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2183 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2184 | << 1 << Deprecated.getAsString(); |
| 2185 | return true; |
| 2186 | } |
| 2187 | |
| 2188 | return false; |
| 2189 | } |
| 2190 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2191 | /// \brief Check whether the two versions match. |
| 2192 | /// |
| 2193 | /// If either version tuple is empty, then they are assumed to match. If |
| 2194 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2195 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2196 | bool BeforeIsOkay) { |
| 2197 | if (X.empty() || Y.empty()) |
| 2198 | return true; |
| 2199 | |
| 2200 | if (X == Y) |
| 2201 | return true; |
| 2202 | |
| 2203 | if (BeforeIsOkay && X < Y) |
| 2204 | return true; |
| 2205 | |
| 2206 | return false; |
| 2207 | } |
| 2208 | |
Rafael Espindola | 51be6e3 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2209 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2210 | IdentifierInfo *Platform, |
| 2211 | VersionTuple Introduced, |
| 2212 | VersionTuple Deprecated, |
| 2213 | VersionTuple Obsoleted, |
| 2214 | bool IsUnavailable, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2215 | StringRef Message, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2216 | bool Override, |
| 2217 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2218 | VersionTuple MergedIntroduced = Introduced; |
| 2219 | VersionTuple MergedDeprecated = Deprecated; |
| 2220 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2221 | bool FoundAny = false; |
| 2222 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2223 | if (D->hasAttrs()) { |
| 2224 | AttrVec &Attrs = D->getAttrs(); |
| 2225 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 2226 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 2227 | if (!OldAA) { |
| 2228 | ++i; |
| 2229 | continue; |
| 2230 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2231 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2232 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2233 | if (OldPlatform != Platform) { |
| 2234 | ++i; |
| 2235 | continue; |
| 2236 | } |
| 2237 | |
| 2238 | FoundAny = true; |
| 2239 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2240 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2241 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2242 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2243 | |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2244 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 2245 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 2246 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 2247 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 72daa3f | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 2248 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2249 | if (Override) { |
| 2250 | int Which = -1; |
| 2251 | VersionTuple FirstVersion; |
| 2252 | VersionTuple SecondVersion; |
| 2253 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 2254 | Which = 0; |
| 2255 | FirstVersion = OldIntroduced; |
| 2256 | SecondVersion = Introduced; |
| 2257 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 2258 | Which = 1; |
| 2259 | FirstVersion = Deprecated; |
| 2260 | SecondVersion = OldDeprecated; |
| 2261 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 2262 | Which = 2; |
| 2263 | FirstVersion = Obsoleted; |
| 2264 | SecondVersion = OldObsoleted; |
| 2265 | } |
| 2266 | |
| 2267 | if (Which == -1) { |
| 2268 | Diag(OldAA->getLocation(), |
| 2269 | diag::warn_mismatched_availability_override_unavail) |
| 2270 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2271 | } else { |
| 2272 | Diag(OldAA->getLocation(), |
| 2273 | diag::warn_mismatched_availability_override) |
| 2274 | << Which |
| 2275 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2276 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 2277 | } |
| 2278 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2279 | } else { |
| 2280 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2281 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2282 | } |
| 2283 | |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2284 | Attrs.erase(Attrs.begin() + i); |
| 2285 | --e; |
| 2286 | continue; |
| 2287 | } |
| 2288 | |
| 2289 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2290 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2291 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2292 | |
| 2293 | if (MergedIntroduced2.empty()) |
| 2294 | MergedIntroduced2 = OldIntroduced; |
| 2295 | if (MergedDeprecated2.empty()) |
| 2296 | MergedDeprecated2 = OldDeprecated; |
| 2297 | if (MergedObsoleted2.empty()) |
| 2298 | MergedObsoleted2 = OldObsoleted; |
| 2299 | |
| 2300 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2301 | MergedIntroduced2, MergedDeprecated2, |
| 2302 | MergedObsoleted2)) { |
| 2303 | Attrs.erase(Attrs.begin() + i); |
| 2304 | --e; |
| 2305 | continue; |
| 2306 | } |
| 2307 | |
| 2308 | MergedIntroduced = MergedIntroduced2; |
| 2309 | MergedDeprecated = MergedDeprecated2; |
| 2310 | MergedObsoleted = MergedObsoleted2; |
| 2311 | ++i; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2312 | } |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | if (FoundAny && |
| 2316 | MergedIntroduced == Introduced && |
| 2317 | MergedDeprecated == Deprecated && |
| 2318 | MergedObsoleted == Obsoleted) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2319 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2320 | |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2321 | // Only create a new attribute if !Override, but we want to do |
| 2322 | // the checking. |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2323 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | cb34439 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2324 | MergedDeprecated, MergedObsoleted) && |
| 2325 | !Override) { |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2326 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 2327 | Introduced, Deprecated, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2328 | Obsoleted, IsUnavailable, Message, |
| 2329 | AttrSpellingListIndex); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2330 | } |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2331 | return NULL; |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2334 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2335 | const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2336 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 2337 | return; |
| 2338 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2339 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2340 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2341 | IdentifierInfo *II = Platform->Ident; |
| 2342 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 2343 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 2344 | << Platform->Ident; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2345 | |
Rafael Espindola | 8c4222a | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2346 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 2347 | if (!ND) { |
| 2348 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2349 | return; |
| 2350 | } |
| 2351 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2352 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2353 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2354 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2355 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2356 | StringRef Str; |
Benjamin Kramer | c561714 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 2357 | if (const StringLiteral *SE = |
| 2358 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2359 | Str = SE->getString(); |
Rafael Espindola | 3b29436 | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2360 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2361 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2362 | Introduced.Version, |
| 2363 | Deprecated.Version, |
| 2364 | Obsoleted.Version, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2365 | IsUnavailable, Str, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2366 | /*Override=*/false, |
| 2367 | Index); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2368 | if (NewAttr) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2369 | D->addAttr(NewAttr); |
Rafael Espindola | 98ae834 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2372 | template <class T> |
| 2373 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2374 | typename T::VisibilityType value, |
| 2375 | unsigned attrSpellingListIndex) { |
| 2376 | T *existingAttr = D->getAttr<T>(); |
| 2377 | if (existingAttr) { |
| 2378 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2379 | if (existingValue == value) |
| 2380 | return NULL; |
| 2381 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2382 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2383 | D->dropAttr<T>(); |
| 2384 | } |
| 2385 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2386 | } |
| 2387 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2388 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2389 | VisibilityAttr::VisibilityType Vis, |
| 2390 | unsigned AttrSpellingListIndex) { |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2391 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2392 | AttrSpellingListIndex); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2395 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2396 | TypeVisibilityAttr::VisibilityType Vis, |
| 2397 | unsigned AttrSpellingListIndex) { |
| 2398 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2399 | AttrSpellingListIndex); |
| 2400 | } |
| 2401 | |
| 2402 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2403 | bool isTypeVisibility) { |
| 2404 | // Visibility attributes don't mean anything on a typedef. |
| 2405 | if (isa<TypedefNameDecl>(D)) { |
| 2406 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2407 | << Attr.getName(); |
| 2408 | return; |
| 2409 | } |
| 2410 | |
| 2411 | // 'type_visibility' can only go on a type or namespace. |
| 2412 | if (isTypeVisibility && |
| 2413 | !(isa<TagDecl>(D) || |
| 2414 | isa<ObjCInterfaceDecl>(D) || |
| 2415 | isa<NamespaceDecl>(D))) { |
| 2416 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2417 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2418 | return; |
| 2419 | } |
| 2420 | |
Benjamin Kramer | ae3a83f | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2421 | // Check that the argument is a string literal. |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2422 | StringRef TypeStr; |
| 2423 | SourceLocation LiteralLoc; |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2424 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2425 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2426 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2427 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2428 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2429 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2430 | << Attr.getName() << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2431 | return; |
| 2432 | } |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2433 | |
| 2434 | // Complain about attempts to use protected visibility on targets |
| 2435 | // (like Darwin) that don't support it. |
| 2436 | if (type == VisibilityAttr::Protected && |
| 2437 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2438 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2439 | type = VisibilityAttr::Default; |
| 2440 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2441 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2442 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2443 | clang::Attr *newAttr; |
| 2444 | if (isTypeVisibility) { |
| 2445 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2446 | (TypeVisibilityAttr::VisibilityType) type, |
| 2447 | Index); |
| 2448 | } else { |
| 2449 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2450 | } |
| 2451 | if (newAttr) |
| 2452 | D->addAttr(newAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2455 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2456 | const AttributeList &Attr) { |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2457 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 2458 | if (!method) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2459 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2460 | << ExpectedMethod; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2461 | return; |
| 2462 | } |
| 2463 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2464 | if (!Attr.isArgIdent(0)) { |
| 2465 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2466 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2467 | return; |
| 2468 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2469 | |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2470 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2471 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2472 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 2473 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 2474 | << IL->Ident; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2475 | return; |
| 2476 | } |
| 2477 | |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2478 | if (F == ObjCMethodFamilyAttr::OMF_init && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2479 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2480 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2481 | << method->getResultType(); |
| 2482 | // Ignore the attribute. |
| 2483 | return; |
| 2484 | } |
| 2485 | |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2486 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2487 | S.Context, F)); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2490 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 2491 | const AttributeList &Attr) { |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2492 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 2493 | if (OCI == 0) { |
Aaron Ballman | 37a8953 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 2494 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2495 | << Attr.getName() << ExpectedObjectiveCInterface; |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2496 | return; |
| 2497 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2498 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2499 | D->addAttr(::new (S.Context) |
| 2500 | ObjCExceptionAttr(Attr.getRange(), S.Context, |
| 2501 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2504 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2505 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2506 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2507 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2508 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2509 | return; |
| 2510 | } |
| 2511 | } |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2512 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2513 | QualType T = PD->getType(); |
Ted Kremenek | 9af9122 | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2514 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 3427682 | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2515 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2516 | return; |
| 2517 | } |
| 2518 | } |
| 2519 | else { |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2520 | // It is okay to include this attribute on properties, e.g.: |
| 2521 | // |
| 2522 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2523 | // |
| 2524 | // In this case it follows tradition and suppresses an error in the above |
| 2525 | // case. |
Fariborz Jahanian | 9b2eb7b | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2526 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | f6e88d7 | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2527 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2528 | D->addAttr(::new (S.Context) |
| 2529 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2530 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2533 | static void |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2534 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2535 | if (!isa<FunctionDecl>(D)) { |
| 2536 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 2537 | return; |
| 2538 | } |
| 2539 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2540 | D->addAttr(::new (S.Context) |
| 2541 | OverloadableAttr(Attr.getRange(), S.Context, |
| 2542 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2543 | } |
| 2544 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2545 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2546 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2547 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2548 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2549 | return; |
| 2550 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2551 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2552 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2553 | BlocksAttr::BlockType type; |
Aaron Ballman | d068607 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2554 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2555 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2556 | << Attr.getName() << II; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2557 | return; |
| 2558 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2559 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2560 | D->addAttr(::new (S.Context) |
| 2561 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2562 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2565 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2566 | // check the attribute arguments. |
| 2567 | if (Attr.getNumArgs() > 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2568 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2569 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2572 | unsigned sentinel = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2573 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2574 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2575 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2576 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2577 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2578 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2579 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2580 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2581 | return; |
| 2582 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2583 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2584 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2585 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2586 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2587 | return; |
| 2588 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2589 | |
| 2590 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2593 | unsigned nullPos = 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2594 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2595 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2596 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2597 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2598 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2599 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2600 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2601 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2602 | return; |
| 2603 | } |
| 2604 | nullPos = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2605 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2606 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2607 | // FIXME: This error message could be improved, it would be nice |
| 2608 | // to say what the bounds actually are. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2609 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2610 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2611 | return; |
| 2612 | } |
| 2613 | } |
| 2614 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2615 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2616 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2617 | if (isa<FunctionNoProtoType>(FT)) { |
| 2618 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2619 | return; |
| 2620 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2621 | |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2622 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2623 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2624 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2625 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2626 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2627 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2628 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2629 | return; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2630 | } |
Eli Friedman | a0b2ba1 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2631 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2632 | if (!BD->isVariadic()) { |
| 2633 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2634 | return; |
| 2635 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2636 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2637 | QualType Ty = V->getType(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2638 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2639 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2640 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2641 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2642 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2643 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2644 | return; |
| 2645 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2646 | } else { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2647 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2648 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2649 | return; |
| 2650 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2651 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2652 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2653 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2654 | return; |
| 2655 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2656 | D->addAttr(::new (S.Context) |
| 2657 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2658 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2659 | } |
| 2660 | |
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 2661 | static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 2662 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
| 2663 | RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context)); |
| 2664 | else |
| 2665 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2666 | } |
| 2667 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2668 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Kaelyn Uhrain | 51ceb7b | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 2669 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2670 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Kaelyn Uhrain | d449c79 | 2012-11-13 00:18:47 +0000 | [diff] [blame] | 2671 | << Attr.getName() << ExpectedFunctionMethodOrClass; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2672 | return; |
| 2673 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2674 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2675 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2676 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2677 | << Attr.getName() << 0; |
Nuno Lopes | f857798 | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2678 | return; |
| 2679 | } |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2680 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2681 | if (MD->getResultType()->isVoidType()) { |
| 2682 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2683 | << Attr.getName() << 1; |
| 2684 | return; |
| 2685 | } |
| 2686 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2687 | D->addAttr(::new (S.Context) |
| 2688 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2689 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2692 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2693 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
Fariborz Jahanian | 13c7fcc | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 2694 | if (isa<CXXRecordDecl>(D)) { |
| 2695 | D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| 2696 | return; |
| 2697 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2698 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2699 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | f23ecd9 | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 2700 | return; |
| 2701 | } |
| 2702 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2703 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 2704 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2705 | nd->addAttr(::new (S.Context) |
| 2706 | WeakAttr(Attr.getRange(), S.Context, |
| 2707 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2708 | } |
| 2709 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2710 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2711 | // weak_import only applies to variable & function declarations. |
| 2712 | bool isDef = false; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2713 | if (!D->canBeWeakImported(isDef)) { |
| 2714 | if (isDef) |
Reid Kleckner | beba3e8 | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2715 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2716 | << "weak_import"; |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2717 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2718 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 90eed21 | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2719 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2720 | // Nothing to warn about here. |
| 2721 | } else |
Fariborz Jahanian | c034974 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2722 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2723 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2724 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2725 | return; |
| 2726 | } |
| 2727 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2728 | D->addAttr(::new (S.Context) |
| 2729 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2730 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2733 | // Handles reqd_work_group_size and work_group_size_hint. |
| 2734 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | 4ae89bc | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2735 | const AttributeList &Attr) { |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2736 | unsigned WGSize[3]; |
| 2737 | for (unsigned i = 0; i < 3; ++i) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2738 | Expr *E = Attr.getArgAsExpr(i); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2739 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2740 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2741 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 2742 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 2743 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 2744 | << E->getSourceRange(); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2745 | return; |
| 2746 | } |
| 2747 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 2748 | } |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2749 | |
| 2750 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2751 | && D->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 2752 | ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>(); |
| 2753 | if (!(A->getXDim() == WGSize[0] && |
| 2754 | A->getYDim() == WGSize[1] && |
| 2755 | A->getZDim() == WGSize[2])) { |
| 2756 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2757 | Attr.getName(); |
| 2758 | } |
| 2759 | } |
| 2760 | |
| 2761 | if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint |
| 2762 | && D->hasAttr<WorkGroupSizeHintAttr>()) { |
| 2763 | WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>(); |
| 2764 | if (!(A->getXDim() == WGSize[0] && |
| 2765 | A->getYDim() == WGSize[1] && |
| 2766 | A->getZDim() == WGSize[2])) { |
| 2767 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2768 | Attr.getName(); |
| 2769 | } |
| 2770 | } |
| 2771 | |
| 2772 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize) |
| 2773 | D->addAttr(::new (S.Context) |
| 2774 | ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2775 | WGSize[0], WGSize[1], WGSize[2], |
| 2776 | Attr.getAttributeSpellingListIndex())); |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2777 | else |
| 2778 | D->addAttr(::new (S.Context) |
| 2779 | WorkGroupSizeHintAttr(Attr.getRange(), S.Context, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2780 | WGSize[0], WGSize[1], WGSize[2], |
| 2781 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2784 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2785 | assert(Attr.getKind() == AttributeList::AT_VecTypeHint); |
| 2786 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2787 | if (!Attr.hasParsedType()) { |
| 2788 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2789 | << Attr.getName() << 1; |
| 2790 | return; |
| 2791 | } |
| 2792 | |
Richard Smith | ddc2a53 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2793 | TypeSourceInfo *ParmTSI = 0; |
| 2794 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI); |
| 2795 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2796 | |
| 2797 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2798 | (ParmType->isBooleanType() || |
| 2799 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2800 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2801 | << ParmType; |
| 2802 | return; |
| 2803 | } |
| 2804 | |
| 2805 | if (Attr.getKind() == AttributeList::AT_VecTypeHint && |
| 2806 | D->hasAttr<VecTypeHintAttr>()) { |
| 2807 | VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>(); |
Richard Smith | ddc2a53 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2808 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2809 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2810 | return; |
| 2811 | } |
| 2812 | } |
| 2813 | |
| 2814 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
Richard Smith | ddc2a53 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2815 | ParmTSI)); |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2818 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2819 | StringRef Name, |
| 2820 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2821 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2822 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2823 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2824 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2825 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2826 | return NULL; |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2827 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2828 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2829 | AttrSpellingListIndex); |
Rafael Espindola | 420efd8 | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2832 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2833 | // Make sure that there is a string literal as the sections's single |
| 2834 | // argument. |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2835 | StringRef Str; |
| 2836 | SourceLocation LiteralLoc; |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2837 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2838 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2839 | |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2840 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2841 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2842 | if (!Error.empty()) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2843 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2844 | << Error; |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2845 | return; |
| 2846 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2847 | |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2848 | // This attribute cannot be applied to local variables. |
| 2849 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2850 | S.Diag(LiteralLoc, diag::err_attribute_section_local_variable); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2851 | return; |
| 2852 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2853 | |
| 2854 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2855 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2856 | if (NewAttr) |
| 2857 | D->addAttr(NewAttr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2860 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2861 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2862 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2863 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2864 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2865 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2866 | D->addAttr(::new (S.Context) |
| 2867 | NoThrowAttr(Attr.getRange(), S.Context, |
| 2868 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2869 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2872 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2873 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2874 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2875 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2876 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2877 | D->addAttr(::new (S.Context) |
| 2878 | ConstAttr(Attr.getRange(), S.Context, |
| 2879 | Attr.getAttributeSpellingListIndex() )); |
Douglas Gregor | b30cd4a | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2880 | } |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2883 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2884 | D->addAttr(::new (S.Context) |
| 2885 | PureAttr(Attr.getRange(), S.Context, |
| 2886 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2889 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2890 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2891 | if (!VD || !VD->hasLocalStorage()) { |
Aaron Ballman | ebaee6b | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2892 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2893 | return; |
| 2894 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2895 | |
Aaron Ballman | ebaee6b | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2896 | Expr *E = Attr.getArgAsExpr(0); |
| 2897 | SourceLocation Loc = E->getExprLoc(); |
| 2898 | FunctionDecl *FD = 0; |
| 2899 | DeclarationNameInfo NI; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2900 | |
Aaron Ballman | ebaee6b | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2901 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 2902 | // will warn the user. |
| 2903 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2904 | if (DRE->hasQualifier()) |
| 2905 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 2906 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 2907 | NI = DRE->getNameInfo(); |
| 2908 | if (!FD) { |
| 2909 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 2910 | << NI.getName(); |
| 2911 | return; |
| 2912 | } |
| 2913 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2914 | if (ULE->hasExplicitTemplateArgs()) |
| 2915 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | ebaee6b | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2916 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 2917 | NI = ULE->getNameInfo(); |
Alp Toker | 8b40772 | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 2918 | if (!FD) { |
| 2919 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 2920 | << NI.getName(); |
| 2921 | if (ULE->getType() == S.Context.OverloadTy) |
| 2922 | S.NoteAllOverloadCandidates(ULE); |
| 2923 | return; |
| 2924 | } |
Aaron Ballman | ebaee6b | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2925 | } else { |
| 2926 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2927 | return; |
| 2928 | } |
| 2929 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2930 | if (FD->getNumParams() != 1) { |
Aaron Ballman | ebaee6b | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2931 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2932 | << NI.getName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2933 | return; |
| 2934 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2935 | |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2936 | // We're currently more strict than GCC about what function types we accept. |
| 2937 | // If this ever proves to be a problem it should be easy to fix. |
| 2938 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2939 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2940 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2941 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | ebaee6b | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2942 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 2943 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2944 | return; |
| 2945 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2946 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2947 | D->addAttr(::new (S.Context) |
| 2948 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2949 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2952 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2953 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2954 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2955 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2956 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2957 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2958 | return; |
| 2959 | } |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2960 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2961 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2962 | uint64_t ArgIdx; |
| 2963 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 2964 | Attr.getLoc(), 1, IdxExpr, ArgIdx)) |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2965 | return; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2966 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2967 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2968 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2969 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2970 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2971 | if (not_nsstring_type && |
| 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_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2977 | << (not_nsstring_type ? "a string type" : "an 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 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2981 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2982 | if (!isNSStringType(Ty, S.Context) && |
| 2983 | !isCFStringType(Ty, S.Context) && |
| 2984 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2985 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2986 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2987 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2988 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2989 | << IdxExpr->getSourceRange(); |
| 2990 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2993 | // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex |
| 2994 | // because that has corrected for the implicit this parameter, and is zero- |
| 2995 | // based. The attribute expects what the user wrote explicitly. |
| 2996 | llvm::APSInt Val; |
| 2997 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2998 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2999 | D->addAttr(::new (S.Context) |
Aaron Ballman | b3d7efe | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 3000 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3001 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3004 | enum FormatAttrKind { |
| 3005 | CFStringFormat, |
| 3006 | NSStringFormat, |
| 3007 | StrftimeFormat, |
| 3008 | SupportedFormat, |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3009 | IgnoredFormat, |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3010 | InvalidFormat |
| 3011 | }; |
| 3012 | |
| 3013 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 3014 | /// types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3015 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3016 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 3017 | // Check for formats that get handled specially. |
| 3018 | .Case("NSString", NSStringFormat) |
| 3019 | .Case("CFString", CFStringFormat) |
| 3020 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3021 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3022 | // Otherwise, check for supported formats. |
| 3023 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 3024 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 3025 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3026 | |
Benjamin Kramer | c51bb99 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3027 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 3028 | .Default(InvalidFormat); |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3029 | } |
| 3030 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3031 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3032 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3033 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 3034 | const AttributeList &Attr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3035 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3036 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3037 | return; |
| 3038 | } |
| 3039 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3040 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3041 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3042 | Attr.setInvalid(); |
| 3043 | return; |
| 3044 | } |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3045 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3046 | if (S.Context.getAsArrayType(T)) |
| 3047 | T = S.Context.getBaseElementType(T); |
| 3048 | if (!T->getAs<RecordType>()) { |
| 3049 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3050 | Attr.setInvalid(); |
| 3051 | return; |
| 3052 | } |
| 3053 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3054 | Expr *priorityExpr = Attr.getArgAsExpr(0); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3055 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3056 | llvm::APSInt priority(32); |
| 3057 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 3058 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 3059 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3060 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3061 | << priorityExpr->getSourceRange(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3062 | Attr.setInvalid(); |
| 3063 | return; |
| 3064 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 3065 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3066 | if (prioritynum < 101 || prioritynum > 65535) { |
| 3067 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 3068 | << priorityExpr->getSourceRange(); |
| 3069 | Attr.setInvalid(); |
| 3070 | return; |
| 3071 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3072 | D->addAttr(::new (S.Context) |
| 3073 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 3074 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3077 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 3078 | IdentifierInfo *Format, int FormatIdx, |
| 3079 | int FirstArg, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3080 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3081 | // Check whether we already have an equivalent format attribute. |
| 3082 | for (specific_attr_iterator<FormatAttr> |
| 3083 | i = D->specific_attr_begin<FormatAttr>(), |
| 3084 | e = D->specific_attr_end<FormatAttr>(); |
| 3085 | i != e ; ++i) { |
| 3086 | FormatAttr *f = *i; |
| 3087 | if (f->getType() == Format && |
| 3088 | f->getFormatIdx() == FormatIdx && |
| 3089 | f->getFirstArg() == FirstArg) { |
| 3090 | // If we don't have a valid location for this attribute, adopt the |
| 3091 | // location. |
| 3092 | if (f->getLocation().isInvalid()) |
| 3093 | f->setRange(Range); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3094 | return NULL; |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3095 | } |
| 3096 | } |
| 3097 | |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3098 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 3099 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | bf9da1f | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3102 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3103 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3104 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3105 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3106 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3107 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3108 | return; |
| 3109 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3110 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3111 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3112 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3113 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3114 | return; |
| 3115 | } |
| 3116 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3117 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3118 | // counted from one. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3119 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 3120 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3121 | unsigned FirstIdx = 1; |
| 3122 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3123 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 3124 | StringRef Format = II->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3125 | |
| 3126 | // Normalize the argument, __foo__ becomes foo. |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3127 | if (Format.startswith("__") && Format.endswith("__")) { |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3128 | Format = Format.substr(2, Format.size() - 4); |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3129 | // If we've modified the string name, we need a new identifier for it. |
| 3130 | II = &S.Context.Idents.get(Format); |
| 3131 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3132 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3133 | // Check for supported formats. |
| 3134 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3135 | |
| 3136 | if (Kind == IgnoredFormat) |
| 3137 | return; |
| 3138 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3139 | if (Kind == InvalidFormat) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3140 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3141 | << "format" << II->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3142 | return; |
| 3143 | } |
| 3144 | |
| 3145 | // checks for the 2nd argument |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3146 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3147 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3148 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 3149 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3150 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3151 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3152 | << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3153 | return; |
| 3154 | } |
| 3155 | |
| 3156 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3157 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3158 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3159 | return; |
| 3160 | } |
| 3161 | |
| 3162 | // FIXME: Do we need to bounds check? |
| 3163 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3164 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3165 | if (HasImplicitThisParam) { |
| 3166 | if (ArgIdx == 0) { |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3167 | S.Diag(Attr.getLoc(), |
| 3168 | diag::err_format_attribute_implicit_this_format_string) |
| 3169 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3170 | return; |
| 3171 | } |
| 3172 | ArgIdx--; |
| 3173 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3174 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3175 | // make sure the format string is really a string |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3176 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3177 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3178 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3179 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3180 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3181 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3182 | return; |
| 3183 | } |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3184 | } else if (Kind == NSStringFormat) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3185 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3186 | // semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3187 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3188 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3189 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3190 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3191 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3192 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3193 | } else if (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3194 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3195 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3196 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3197 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3198 | return; |
| 3199 | } |
| 3200 | |
| 3201 | // check the 3rd argument |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3202 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3203 | llvm::APSInt FirstArg(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3204 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 3205 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3206 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3207 | << Attr.getName() << 3 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3208 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3209 | return; |
| 3210 | } |
| 3211 | |
| 3212 | // check if the function is variadic if the 3rd argument non-zero |
| 3213 | if (FirstArg != 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3214 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3215 | ++NumArgs; // +1 for ... |
| 3216 | } else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3217 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3218 | return; |
| 3219 | } |
| 3220 | } |
| 3221 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3222 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3223 | // variable the input is just the current time + the format string. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3224 | if (Kind == StrftimeFormat) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3225 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3226 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 3227 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3228 | return; |
| 3229 | } |
| 3230 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3231 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3232 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3233 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3234 | return; |
| 3235 | } |
| 3236 | |
Aaron Ballman | caa5ab2 | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3237 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3238 | Idx.getZExtValue(), |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3239 | FirstArg.getZExtValue(), |
| 3240 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | 599f1b7 | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3241 | if (NewAttr) |
| 3242 | D->addAttr(NewAttr); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3245 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 3246 | const AttributeList &Attr) { |
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 | // Make sure that there is a string literal as the annotation's single |
| 3311 | // argument. |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3312 | StringRef Str; |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3313 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3314 | return; |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3315 | |
| 3316 | // Don't duplicate annotations that are already set. |
| 3317 | for (specific_attr_iterator<AnnotateAttr> |
| 3318 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 3319 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3320 | if ((*i)->getAnnotation() == Str) |
| 3321 | return; |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3322 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3323 | |
| 3324 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3325 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3326 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3329 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3330 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3331 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3332 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3333 | << Attr.getName() << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3334 | return; |
| 3335 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3336 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3337 | if (Attr.getNumArgs() == 0) { |
| 3338 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 3339 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 3340 | return; |
| 3341 | } |
| 3342 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3343 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3344 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3345 | S.Diag(Attr.getEllipsisLoc(), |
| 3346 | diag::err_pack_expansion_without_parameter_packs); |
| 3347 | return; |
| 3348 | } |
| 3349 | |
| 3350 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 3351 | return; |
| 3352 | |
| 3353 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 3354 | Attr.isPackExpansion()); |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
| 3357 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3358 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3359 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3360 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3361 | |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3362 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3363 | if (TmpAttr.isAlignas()) { |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3364 | // C++11 [dcl.align]p1: |
| 3365 | // An alignment-specifier may be applied to a variable or to a class |
| 3366 | // data member, but it shall not be applied to a bit-field, a function |
| 3367 | // parameter, the formal parameter of a catch clause, or a variable |
| 3368 | // declared with the register storage class specifier. An |
| 3369 | // alignment-specifier may also be applied to the declaration of a class |
| 3370 | // or enumeration type. |
| 3371 | // C11 6.7.5/2: |
| 3372 | // An alignment attribute shall not be specified in a declaration of |
| 3373 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3374 | // object declared with the register storage-class specifier. |
| 3375 | int DiagKind = -1; |
| 3376 | if (isa<ParmVarDecl>(D)) { |
| 3377 | DiagKind = 0; |
| 3378 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3379 | if (VD->getStorageClass() == SC_Register) |
| 3380 | DiagKind = 1; |
| 3381 | if (VD->isExceptionVariable()) |
| 3382 | DiagKind = 2; |
| 3383 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 3384 | if (FD->isBitField()) |
| 3385 | DiagKind = 3; |
| 3386 | } else if (!isa<TagDecl>(D)) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3387 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) |
| 3388 | << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'") |
Richard Smith | 5f838aa | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3389 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3390 | : ExpectedVariableFieldOrTag); |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3391 | return; |
| 3392 | } |
| 3393 | if (DiagKind != -1) { |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3394 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Richard Smith | 671b321 | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 3395 | << TmpAttr.isC11() << DiagKind; |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3396 | return; |
| 3397 | } |
| 3398 | } |
| 3399 | |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3400 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3401 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3402 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3403 | AA->setPackExpansion(IsPackExpansion); |
| 3404 | D->addAttr(AA); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3405 | return; |
| 3406 | } |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3407 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3408 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3409 | llvm::APSInt Alignment(32); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3410 | ExprResult ICE |
| 3411 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3412 | diag::err_aligned_attribute_argument_not_int, |
| 3413 | /*AllowFold*/ false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3414 | if (ICE.isInvalid()) |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3415 | return; |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3416 | |
| 3417 | // C++11 [dcl.align]p2: |
| 3418 | // -- if the constant expression evaluates to zero, the alignment |
| 3419 | // specifier shall have no effect |
| 3420 | // C11 6.7.5p6: |
| 3421 | // An alignment specification of zero has no effect. |
| 3422 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 3423 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3424 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 3425 | << E->getSourceRange(); |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3426 | return; |
| 3427 | } |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3428 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3429 | if (TmpAttr.isDeclspec()) { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3430 | // We've already verified it's a power of 2, now let's make sure it's |
| 3431 | // 8192 or less. |
| 3432 | if (Alignment.getZExtValue() > 8192) { |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3433 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3434 | << E->getSourceRange(); |
| 3435 | return; |
| 3436 | } |
| 3437 | } |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3438 | |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3439 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 3440 | ICE.take(), SpellingListIndex); |
| 3441 | AA->setPackExpansion(IsPackExpansion); |
| 3442 | D->addAttr(AA); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
Michael Han | a31f65b | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3445 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3446 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3447 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3448 | // FIXME: Perform checking of type validity |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3449 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3450 | SpellingListIndex); |
| 3451 | AA->setPackExpansion(IsPackExpansion); |
| 3452 | D->addAttr(AA); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3453 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3454 | |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3455 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3456 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3457 | |
| 3458 | QualType Ty; |
| 3459 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3460 | Ty = VD->getType(); |
| 3461 | else |
| 3462 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
Richard Smith | 4da0903 | 2013-02-22 09:21:42 +0000 | [diff] [blame] | 3463 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
Richard Smith | be507b6 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3464 | return; |
| 3465 | |
| 3466 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3467 | // The combined effect of all alignment attributes in a declaration shall |
| 3468 | // not specify an alignment that is less strict than the alignment that |
| 3469 | // would otherwise be required for the entity being declared. |
| 3470 | AlignedAttr *AlignasAttr = 0; |
| 3471 | unsigned Align = 0; |
| 3472 | for (specific_attr_iterator<AlignedAttr> |
| 3473 | I = D->specific_attr_begin<AlignedAttr>(), |
| 3474 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 3475 | if (I->isAlignmentDependent()) |
| 3476 | return; |
| 3477 | if (I->isAlignas()) |
| 3478 | AlignasAttr = *I; |
| 3479 | Align = std::max(Align, I->getAlignment(Context)); |
| 3480 | } |
| 3481 | |
| 3482 | if (AlignasAttr && Align) { |
| 3483 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 3484 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 3485 | if (NaturalAlign > RequestedAlign) |
| 3486 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 3487 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 3488 | } |
| 3489 | } |
| 3490 | |
Chandler Carruth | d309c81 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3491 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3492 | /// type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3493 | /// |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3494 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3495 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3496 | /// HImode, not an intermediate pointer. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3497 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3498 | // This attribute isn't documented, but glibc uses it. It changes |
| 3499 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3500 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 750f73a | 2013-07-30 14:29:12 +0000 | [diff] [blame] | 3501 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3502 | << AANT_ArgumentIdentifier; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3503 | return; |
| 3504 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3505 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3506 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
| 3507 | StringRef Str = Name->getName(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3508 | |
| 3509 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3510 | if (Str.startswith("__") && Str.endswith("__")) |
| 3511 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3512 | |
| 3513 | unsigned DestWidth = 0; |
| 3514 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3515 | bool ComplexMode = false; |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3516 | switch (Str.size()) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3517 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3518 | switch (Str[0]) { |
| 3519 | case 'Q': DestWidth = 8; break; |
| 3520 | case 'H': DestWidth = 16; break; |
| 3521 | case 'S': DestWidth = 32; break; |
| 3522 | case 'D': DestWidth = 64; break; |
| 3523 | case 'X': DestWidth = 96; break; |
| 3524 | case 'T': DestWidth = 128; break; |
| 3525 | } |
| 3526 | if (Str[1] == 'F') { |
| 3527 | IntegerMode = false; |
| 3528 | } else if (Str[1] == 'C') { |
| 3529 | IntegerMode = false; |
| 3530 | ComplexMode = true; |
| 3531 | } else if (Str[1] != 'I') { |
| 3532 | DestWidth = 0; |
| 3533 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3534 | break; |
| 3535 | case 4: |
| 3536 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3537 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3538 | if (Str == "word") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3539 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3540 | else if (Str == "byte") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3541 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3542 | break; |
| 3543 | case 7: |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3544 | if (Str == "pointer") |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3545 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3546 | break; |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3547 | case 11: |
| 3548 | if (Str == "unwind_word") |
Rafael Espindola | 0b1de54 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3549 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | 8e721b7 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3550 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3551 | } |
| 3552 | |
| 3553 | QualType OldTy; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3554 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3555 | OldTy = TD->getUnderlyingType(); |
| 3556 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3557 | OldTy = VD->getType(); |
| 3558 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3559 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3560 | << "mode" << Attr.getRange(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3561 | return; |
| 3562 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3563 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3564 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3565 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3566 | else if (IntegerMode) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3567 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3568 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3569 | } else if (ComplexMode) { |
| 3570 | if (!OldTy->isComplexType()) |
| 3571 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3572 | } else { |
| 3573 | if (!OldTy->isFloatingType()) |
| 3574 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3575 | } |
| 3576 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3577 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3578 | // and friends, at least with glibc. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3579 | // FIXME: Make sure floating-point mappings are accurate |
| 3580 | // FIXME: Support XF and TF types |
Stepan Dyatkovskiy | d569295 | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3581 | if (!DestWidth) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3582 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3583 | return; |
Stepan Dyatkovskiy | d569295 | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | QualType NewTy; |
| 3587 | |
| 3588 | if (IntegerMode) |
| 3589 | NewTy = S.Context.getIntTypeForBitwidth(DestWidth, |
| 3590 | OldTy->isSignedIntegerType()); |
| 3591 | else |
| 3592 | NewTy = S.Context.getRealTypeForBitwidth(DestWidth); |
| 3593 | |
| 3594 | if (NewTy.isNull()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3595 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3596 | return; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3597 | } |
| 3598 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3599 | if (ComplexMode) { |
| 3600 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
| 3603 | // Install the new type. |
Enea Zaffanella | c2fa6b6 | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3604 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3605 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 3606 | else |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3607 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | c2fa6b6 | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3608 | |
| 3609 | D->addAttr(::new (S.Context) |
| 3610 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 3611 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3612 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3613 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3614 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3615 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3616 | if (!VD->hasGlobalStorage()) |
| 3617 | S.Diag(Attr.getLoc(), |
| 3618 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3619 | << Attr.getName(); |
| 3620 | } else if (!isFunctionOrMethod(D)) { |
| 3621 | S.Diag(Attr.getLoc(), |
| 3622 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3623 | << Attr.getName(); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3624 | return; |
| 3625 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3626 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3627 | D->addAttr(::new (S.Context) |
| 3628 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3629 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3630 | } |
| 3631 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3632 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3633 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3634 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3635 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3636 | return; |
| 3637 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3638 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3639 | D->addAttr(::new (S.Context) |
| 3640 | NoInlineAttr(Attr.getRange(), S.Context, |
| 3641 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3642 | } |
| 3643 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3644 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 3645 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3646 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3647 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3648 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3649 | return; |
| 3650 | } |
| 3651 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3652 | D->addAttr(::new (S.Context) |
| 3653 | NoInstrumentFunctionAttr(Attr.getRange(), S.Context, |
| 3654 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3655 | } |
| 3656 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3657 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3658 | if (S.LangOpts.CUDA) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3659 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3660 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3661 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3662 | return; |
| 3663 | } |
| 3664 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3665 | D->addAttr(::new (S.Context) |
| 3666 | CUDAConstantAttr(Attr.getRange(), S.Context, |
| 3667 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3668 | } else { |
| 3669 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 3670 | } |
| 3671 | } |
| 3672 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3673 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3674 | if (S.LangOpts.CUDA) { |
| 3675 | // check the attribute arguments. |
| 3676 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | baec778 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3677 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3678 | << Attr.getName() << 0; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3679 | return; |
| 3680 | } |
| 3681 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3682 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3683 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3684 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3685 | return; |
| 3686 | } |
| 3687 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3688 | D->addAttr(::new (S.Context) |
| 3689 | CUDADeviceAttr(Attr.getRange(), S.Context, |
| 3690 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3691 | } else { |
| 3692 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 3693 | } |
| 3694 | } |
| 3695 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3696 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3697 | if (S.LangOpts.CUDA) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3698 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3699 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3700 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3701 | return; |
| 3702 | } |
| 3703 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3704 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3705 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 3706 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3707 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3708 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3709 | << FD->getType() |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3710 | << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(), |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3711 | "void"); |
| 3712 | } else { |
| 3713 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3714 | << FD->getType(); |
| 3715 | } |
| 3716 | return; |
| 3717 | } |
| 3718 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3719 | D->addAttr(::new (S.Context) |
| 3720 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
| 3721 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3722 | } else { |
| 3723 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 3724 | } |
| 3725 | } |
| 3726 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3727 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3728 | if (S.LangOpts.CUDA) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3729 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3730 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3731 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3732 | return; |
| 3733 | } |
| 3734 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3735 | D->addAttr(::new (S.Context) |
| 3736 | CUDAHostAttr(Attr.getRange(), S.Context, |
| 3737 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3738 | } else { |
| 3739 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 3740 | } |
| 3741 | } |
| 3742 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3743 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3744 | if (S.LangOpts.CUDA) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3745 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3746 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3747 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3748 | return; |
| 3749 | } |
| 3750 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3751 | D->addAttr(::new (S.Context) |
| 3752 | CUDASharedAttr(Attr.getRange(), S.Context, |
| 3753 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3754 | } else { |
| 3755 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 3756 | } |
| 3757 | } |
| 3758 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3759 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3760 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3761 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3762 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3763 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3764 | return; |
| 3765 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3766 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3767 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3768 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3769 | return; |
| 3770 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3771 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3772 | D->addAttr(::new (S.Context) |
| 3773 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3774 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3777 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3778 | if (hasDeclarator(D)) return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3779 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3780 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3781 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3782 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3783 | CallingConv CC; |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3784 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3785 | return; |
| 3786 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3787 | if (!isa<ObjCMethodDecl>(D)) { |
| 3788 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3789 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3790 | return; |
| 3791 | } |
| 3792 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3793 | switch (Attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3794 | case AttributeList::AT_FastCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3795 | D->addAttr(::new (S.Context) |
| 3796 | FastCallAttr(Attr.getRange(), S.Context, |
| 3797 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3798 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3799 | case AttributeList::AT_StdCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3800 | D->addAttr(::new (S.Context) |
| 3801 | StdCallAttr(Attr.getRange(), S.Context, |
| 3802 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3803 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3804 | case AttributeList::AT_ThisCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3805 | D->addAttr(::new (S.Context) |
| 3806 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3807 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 04633eb | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3808 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3809 | case AttributeList::AT_CDecl: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3810 | D->addAttr(::new (S.Context) |
| 3811 | CDeclAttr(Attr.getRange(), S.Context, |
| 3812 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3813 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3814 | case AttributeList::AT_Pascal: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3815 | D->addAttr(::new (S.Context) |
| 3816 | PascalAttr(Attr.getRange(), S.Context, |
| 3817 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3818 | return; |
Charles Davis | e8519c3 | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3819 | case AttributeList::AT_MSABI: |
| 3820 | D->addAttr(::new (S.Context) |
| 3821 | MSABIAttr(Attr.getRange(), S.Context, |
| 3822 | Attr.getAttributeSpellingListIndex())); |
| 3823 | return; |
| 3824 | case AttributeList::AT_SysVABI: |
| 3825 | D->addAttr(::new (S.Context) |
| 3826 | SysVABIAttr(Attr.getRange(), S.Context, |
| 3827 | Attr.getAttributeSpellingListIndex())); |
| 3828 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3829 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3830 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3831 | switch (CC) { |
| 3832 | case CC_AAPCS: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3833 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3834 | break; |
| 3835 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3836 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 9071def | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3837 | break; |
| 3838 | default: |
| 3839 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3840 | } |
| 3841 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3842 | D->addAttr(::new (S.Context) |
| 3843 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3844 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3845 | return; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3846 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3847 | case AttributeList::AT_PnaclCall: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3848 | D->addAttr(::new (S.Context) |
| 3849 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 3850 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3851 | return; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3852 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3853 | D->addAttr(::new (S.Context) |
| 3854 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3855 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3856 | return; |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3857 | |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3858 | default: |
| 3859 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3860 | } |
| 3861 | } |
| 3862 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3863 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3864 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3867 | static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3868 | Expr *E = Attr.getArgAsExpr(0); |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3869 | llvm::APSInt ArgNum(32); |
| 3870 | if (E->isTypeDependent() || E->isValueDependent() || |
| 3871 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 3872 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3873 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3874 | << E->getSourceRange(); |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3875 | return; |
| 3876 | } |
| 3877 | |
| 3878 | D->addAttr(::new (S.Context) OpenCLImageAccessAttr( |
| 3879 | Attr.getRange(), S.Context, ArgNum.getZExtValue())); |
| 3880 | } |
| 3881 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3882 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3883 | const FunctionDecl *FD) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3884 | if (attr.isInvalid()) |
| 3885 | return true; |
| 3886 | |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3887 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3888 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3889 | attr.setInvalid(); |
| 3890 | return true; |
| 3891 | } |
| 3892 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3893 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 3894 | // move to TargetAttributesSema one day. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3895 | switch (attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3896 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3897 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3898 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3899 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3900 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
Charles Davis | e8519c3 | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3901 | case AttributeList::AT_MSABI: |
| 3902 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 3903 | CC_X86_64Win64; |
| 3904 | break; |
| 3905 | case AttributeList::AT_SysVABI: |
| 3906 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 3907 | CC_C; |
| 3908 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3909 | case AttributeList::AT_Pcs: { |
Aaron Ballman | fbf6f5c | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 3910 | StringRef StrRef; |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3911 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3912 | attr.setInvalid(); |
| 3913 | return true; |
| 3914 | } |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3915 | if (StrRef == "aapcs") { |
| 3916 | CC = CC_AAPCS; |
| 3917 | break; |
| 3918 | } else if (StrRef == "aapcs-vfp") { |
| 3919 | CC = CC_AAPCS_VFP; |
| 3920 | break; |
| 3921 | } |
Benjamin Kramer | fac8e43 | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3922 | |
| 3923 | attr.setInvalid(); |
| 3924 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3925 | return true; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3926 | } |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3927 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3928 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3929 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3930 | } |
| 3931 | |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3932 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3933 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 3934 | if (A == TargetInfo::CCCR_Warning) { |
| 3935 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3936 | |
| 3937 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3938 | if (FD) |
| 3939 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3940 | TargetInfo::CCMT_NonMember; |
| 3941 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3942 | } |
| 3943 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3944 | return false; |
| 3945 | } |
| 3946 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3947 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3948 | if (hasDeclarator(D)) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3949 | |
| 3950 | unsigned numParams; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3951 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3952 | return; |
| 3953 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3954 | if (!isa<ObjCMethodDecl>(D)) { |
| 3955 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3956 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3957 | return; |
| 3958 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3959 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3960 | D->addAttr(::new (S.Context) |
| 3961 | RegparmAttr(Attr.getRange(), S.Context, numParams, |
| 3962 | Attr.getAttributeSpellingListIndex())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
| 3965 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3966 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3967 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3968 | if (Attr.isInvalid()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3969 | return true; |
| 3970 | |
Aaron Ballman | ffa9d57 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3971 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3972 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3973 | return true; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3974 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3975 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3976 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3977 | llvm::APSInt NumParams(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3978 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3979 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
Aaron Ballman | 9f939f7 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 3980 | Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3981 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3982 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3983 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3984 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3985 | } |
| 3986 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3987 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3988 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3989 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3990 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3991 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3992 | } |
| 3993 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3994 | numParams = NumParams.getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3995 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3996 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3997 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3998 | Attr.setInvalid(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3999 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4000 | } |
| 4001 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4002 | return false; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4005 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4006 | if (S.LangOpts.CUDA) { |
| 4007 | // check the attribute arguments. |
| 4008 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 4009 | // FIXME: 0 is not okay. |
| 4010 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4011 | return; |
| 4012 | } |
| 4013 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4014 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4015 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4016 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4017 | return; |
| 4018 | } |
| 4019 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4020 | Expr *MaxThreadsExpr = Attr.getArgAsExpr(0); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4021 | llvm::APSInt MaxThreads(32); |
| 4022 | if (MaxThreadsExpr->isTypeDependent() || |
| 4023 | MaxThreadsExpr->isValueDependent() || |
| 4024 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4025 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4026 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4027 | << MaxThreadsExpr->getSourceRange(); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4028 | return; |
| 4029 | } |
| 4030 | |
| 4031 | llvm::APSInt MinBlocks(32); |
| 4032 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4033 | Expr *MinBlocksExpr = Attr.getArgAsExpr(1); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4034 | if (MinBlocksExpr->isTypeDependent() || |
| 4035 | MinBlocksExpr->isValueDependent() || |
| 4036 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4037 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4038 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4039 | << MinBlocksExpr->getSourceRange(); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4040 | return; |
| 4041 | } |
| 4042 | } |
| 4043 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4044 | D->addAttr(::new (S.Context) |
| 4045 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 4046 | MaxThreads.getZExtValue(), |
| 4047 | MinBlocks.getZExtValue(), |
| 4048 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4049 | } else { |
| 4050 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 4051 | } |
| 4052 | } |
| 4053 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4054 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 4055 | const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4056 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4057 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4058 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4059 | return; |
| 4060 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4061 | |
| 4062 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4063 | return; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4064 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4065 | StringRef AttrName = Attr.getName()->getName(); |
| 4066 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4067 | |
| 4068 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 4069 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4070 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 4071 | return; |
| 4072 | } |
| 4073 | |
| 4074 | uint64_t ArgumentIdx; |
| 4075 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4076 | Attr.getLoc(), 2, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4077 | Attr.getArgAsExpr(1), ArgumentIdx)) |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4078 | return; |
| 4079 | |
| 4080 | uint64_t TypeTagIdx; |
| 4081 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4082 | Attr.getLoc(), 3, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4083 | Attr.getArgAsExpr(2), TypeTagIdx)) |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4084 | return; |
| 4085 | |
| 4086 | bool IsPointer = (AttrName == "pointer_with_type_tag"); |
| 4087 | if (IsPointer) { |
| 4088 | // Ensure that buffer has a pointer type. |
| 4089 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 4090 | if (!BufferTy->isPointerType()) { |
| 4091 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4092 | << Attr.getName(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4093 | } |
| 4094 | } |
| 4095 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4096 | D->addAttr(::new (S.Context) |
| 4097 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 4098 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 4099 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4100 | } |
| 4101 | |
| 4102 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 4103 | const AttributeList &Attr) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4104 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 437d43f | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4105 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3cd6feb | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4106 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4107 | return; |
| 4108 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4109 | |
| 4110 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4111 | return; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4112 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4113 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Richard Smith | ddc2a53 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4114 | TypeSourceInfo *MatchingCTypeLoc = 0; |
| 4115 | S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc); |
| 4116 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4117 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4118 | D->addAttr(::new (S.Context) |
| 4119 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
Richard Smith | ddc2a53 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4120 | MatchingCTypeLoc, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4121 | Attr.getLayoutCompatible(), |
| 4122 | Attr.getMustBeNull(), |
| 4123 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4126 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4127 | // Checker-specific attribute handlers. |
| 4128 | //===----------------------------------------------------------------------===// |
| 4129 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4130 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4131 | return type->isDependentType() || |
| 4132 | type->isObjCObjectPointerType() || |
| 4133 | S.Context.isObjCNSObjectType(type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4134 | } |
| 4135 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | 6c73a29 | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4136 | return type->isDependentType() || |
| 4137 | type->isPointerType() || |
| 4138 | isValidSubjectOfNSAttribute(S, type); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4141 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4142 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4143 | if (!param) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4144 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4145 | << Attr.getRange() << Attr.getName() << ExpectedParameter; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4146 | return; |
| 4147 | } |
| 4148 | |
| 4149 | bool typeOK, cf; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4150 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4151 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 4152 | cf = false; |
| 4153 | } else { |
| 4154 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 4155 | cf = true; |
| 4156 | } |
| 4157 | |
| 4158 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4159 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4160 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4161 | return; |
| 4162 | } |
| 4163 | |
| 4164 | if (cf) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4165 | param->addAttr(::new (S.Context) |
| 4166 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 4167 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4168 | else |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4169 | param->addAttr(::new (S.Context) |
| 4170 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 4171 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4172 | } |
| 4173 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4174 | static void handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 4175 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4176 | if (!isa<ObjCMethodDecl>(D)) { |
| 4177 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4178 | << Attr.getRange() << Attr.getName() << ExpectedMethod; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4179 | return; |
| 4180 | } |
| 4181 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4182 | D->addAttr(::new (S.Context) |
| 4183 | NSConsumesSelfAttr(Attr.getRange(), S.Context, |
| 4184 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4187 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 4188 | const AttributeList &Attr) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4189 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4190 | QualType returnType; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4191 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4192 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4193 | returnType = MD->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4194 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4195 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4196 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | a23bd4c | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 4197 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4198 | returnType = PD->getType(); |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4199 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4200 | returnType = FD->getResultType(); |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4201 | else { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4202 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4203 | << Attr.getRange() << Attr.getName() |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4204 | << ExpectedFunctionOrMethod; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4205 | return; |
| 4206 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4207 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4208 | bool typeOK; |
| 4209 | bool cf; |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4210 | switch (Attr.getKind()) { |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4211 | default: llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4212 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4213 | case AttributeList::AT_NSReturnsRetained: |
| 4214 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4215 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 4216 | cf = false; |
| 4217 | break; |
| 4218 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4219 | case AttributeList::AT_CFReturnsRetained: |
| 4220 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4221 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 4222 | cf = true; |
| 4223 | break; |
| 4224 | } |
| 4225 | |
| 4226 | if (!typeOK) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4227 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4228 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4229 | return; |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4230 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4231 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4232 | switch (Attr.getKind()) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4233 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4234 | llvm_unreachable("invalid ownership attribute"); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4235 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4236 | D->addAttr(::new (S.Context) |
| 4237 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 4238 | Attr.getAttributeSpellingListIndex())); |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4239 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4240 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4241 | D->addAttr(::new (S.Context) |
| 4242 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4243 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4244 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4245 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4246 | D->addAttr(::new (S.Context) |
| 4247 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4248 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4249 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4250 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4251 | D->addAttr(::new (S.Context) |
| 4252 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4253 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4254 | return; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4255 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4256 | D->addAttr(::new (S.Context) |
| 4257 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4258 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4259 | return; |
| 4260 | }; |
| 4261 | } |
| 4262 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4263 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4264 | const AttributeList &attr) { |
Fariborz Jahanian | af1d28f | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 4265 | const int EP_ObjCMethod = 1; |
| 4266 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | f35a356 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 4267 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4268 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 937ec1d | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4269 | QualType resultType; |
| 4270 | |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4271 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4272 | |
Fariborz Jahanian | 94d55d7 | 2012-04-21 17:51:44 +0000 | [diff] [blame] | 4273 | if (!method) { |
Fariborz Jahanian | 937ec1d | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4274 | ObjCPropertyDecl *property = dyn_cast<ObjCPropertyDecl>(D); |
| 4275 | if (!property) { |
| 4276 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 4277 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethodOrProperty; |
| 4278 | return; |
| 4279 | } |
| 4280 | resultType = property->getType(); |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4281 | } |
Fariborz Jahanian | 937ec1d | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4282 | else |
| 4283 | // Check that the method returns a normal pointer. |
| 4284 | resultType = method->getResultType(); |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4285 | |
Fariborz Jahanian | f2e5945 | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4286 | if (!resultType->isReferenceType() && |
| 4287 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 937ec1d | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4288 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4289 | << SourceRange(loc) |
Fariborz Jahanian | f35a356 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 4290 | << attr.getName() << (method ? EP_ObjCMethod : EP_ObjCProperty) |
| 4291 | << /*non-retainable pointer*/ 2; |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4292 | |
| 4293 | // Drop the attribute. |
| 4294 | return; |
| 4295 | } |
| 4296 | |
Fariborz Jahanian | 937ec1d | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4297 | D->addAttr(::new (S.Context) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4298 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 4299 | attr.getAttributeSpellingListIndex())); |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4300 | } |
| 4301 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4302 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4303 | const AttributeList &attr) { |
| 4304 | SourceLocation loc = attr.getLoc(); |
| 4305 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4306 | |
| 4307 | if (!method) { |
| 4308 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 4309 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
| 4310 | return; |
| 4311 | } |
| 4312 | DeclContext *DC = method->getDeclContext(); |
| 4313 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4314 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4315 | << attr.getName() << 0; |
| 4316 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4317 | return; |
| 4318 | } |
| 4319 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4320 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4321 | << attr.getName() << 1; |
| 4322 | return; |
| 4323 | } |
| 4324 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4325 | method->addAttr(::new (S.Context) |
| 4326 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 4327 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4330 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 4331 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
| 4332 | if (!isa<FunctionDecl>(D)) { |
| 4333 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4334 | << A.getRange() << A.getName() << ExpectedFunction; |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4335 | return; |
| 4336 | } |
| 4337 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4338 | bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4339 | |
| 4340 | // Check whether there's a conflicting attribute already present. |
| 4341 | Attr *Existing; |
| 4342 | if (IsAudited) { |
| 4343 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 4344 | } else { |
| 4345 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 4346 | } |
| 4347 | if (Existing) { |
| 4348 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 4349 | << A.getName() |
| 4350 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 4351 | << A.getRange() << Existing->getRange(); |
| 4352 | return; |
| 4353 | } |
| 4354 | |
| 4355 | // All clear; add the attribute. |
| 4356 | if (IsAudited) { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4357 | D->addAttr(::new (S.Context) |
| 4358 | CFAuditedTransferAttr(A.getRange(), S.Context, |
| 4359 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4360 | } else { |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4361 | D->addAttr(::new (S.Context) |
| 4362 | CFUnknownTransferAttr(A.getRange(), S.Context, |
| 4363 | A.getAttributeSpellingListIndex())); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4364 | } |
| 4365 | } |
| 4366 | |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4367 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4368 | const AttributeList &Attr) { |
| 4369 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4370 | if (!RD || RD->isUnion()) { |
| 4371 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4372 | << Attr.getRange() << Attr.getName() << ExpectedStruct; |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4373 | } |
| 4374 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4375 | IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4376 | |
| 4377 | // In Objective-C, verify that the type names an Objective-C type. |
| 4378 | // We don't want to check this outside of ObjC because people sometimes |
| 4379 | // do crazy C declarations of Objective-C types. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4380 | if (Parm && S.getLangOpts().ObjC1) { |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4381 | // Check for an existing type with this name. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4382 | LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc, |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4383 | Sema::LookupOrdinaryName); |
| 4384 | if (S.LookupName(R, Sc)) { |
| 4385 | NamedDecl *Target = R.getFoundDecl(); |
| 4386 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 4387 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 4388 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 4389 | } |
| 4390 | } |
| 4391 | } |
| 4392 | |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4393 | D->addAttr(::new (S.Context) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4394 | NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0, |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4395 | Attr.getAttributeSpellingListIndex())); |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4396 | } |
| 4397 | |
Fariborz Jahanian | 933a894 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4398 | static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D, |
| 4399 | const AttributeList &Attr) { |
| 4400 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 4401 | QualType T = TD->getUnderlyingType(); |
| 4402 | if (T->isPointerType()) { |
| 4403 | T = T->getPointeeType(); |
| 4404 | if (T->isRecordType()) { |
| 4405 | RecordDecl *RD = T->getAs<RecordType>()->getDecl(); |
| 4406 | if (!RD || RD->isUnion()) { |
| 4407 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 4408 | << Attr.getRange() << Attr.getName() << ExpectedStruct; |
| 4409 | return; |
| 4410 | } |
| 4411 | } |
| 4412 | } else { |
| 4413 | S.Diag(TD->getLocStart(), diag::err_objc_bridge_not_pointertype); |
| 4414 | return; |
| 4415 | } |
| 4416 | // Check for T being a CFType goes here. |
| 4417 | if (!isCFRefType(TD, S.Context)) { |
| 4418 | S.Diag(TD->getLocStart(), diag::err_objc_bridge_not_cftype); |
| 4419 | return; |
| 4420 | } |
| 4421 | } |
| 4422 | else { |
| 4423 | S.Diag(D->getLocStart(), diag::err_objc_bridge_attribute); |
| 4424 | return; |
| 4425 | } |
| 4426 | |
| 4427 | if (Attr.getNumArgs() != 1) { |
| 4428 | S.Diag(D->getLocStart(), diag::err_objc_bridge_not_id); |
| 4429 | return; |
| 4430 | } |
| 4431 | IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
| 4432 | if (!Parm) { |
| 4433 | S.Diag(D->getLocStart(), diag::err_objc_bridge_not_id); |
| 4434 | return; |
| 4435 | } |
| 4436 | |
| 4437 | D->addAttr(::new (S.Context) |
| 4438 | ObjCBridgeAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0, |
| 4439 | Attr.getAttributeSpellingListIndex())); |
| 4440 | } |
| 4441 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4442 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4443 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4444 | if (hasDeclarator(D)) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4445 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4446 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4447 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4448 | } |
| 4449 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4450 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4451 | const AttributeList &Attr) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4452 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4453 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | f6b8b58 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4454 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4455 | return; |
| 4456 | } |
| 4457 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4458 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4459 | QualType type = vd->getType(); |
| 4460 | |
| 4461 | if (!type->isDependentType() && |
| 4462 | !type->isObjCLifetimeType()) { |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4463 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4464 | << type; |
| 4465 | return; |
| 4466 | } |
| 4467 | |
| 4468 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4469 | |
| 4470 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4471 | // going to infer. |
| 4472 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4473 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4474 | |
| 4475 | switch (lifetime) { |
| 4476 | case Qualifiers::OCL_None: |
| 4477 | assert(type->isDependentType() && |
| 4478 | "didn't infer lifetime for non-dependent type?"); |
| 4479 | break; |
| 4480 | |
| 4481 | case Qualifiers::OCL_Weak: // meaningful |
| 4482 | case Qualifiers::OCL_Strong: // meaningful |
| 4483 | break; |
| 4484 | |
| 4485 | case Qualifiers::OCL_ExplicitNone: |
| 4486 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4487 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4488 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4489 | break; |
| 4490 | } |
| 4491 | |
Chandler Carruth | 87c4460 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4492 | D->addAttr(::new (S.Context) |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4493 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 4494 | Attr.getAttributeSpellingListIndex())); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4497 | //===----------------------------------------------------------------------===// |
| 4498 | // Microsoft specific attribute handlers. |
| 4499 | //===----------------------------------------------------------------------===// |
| 4500 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4501 | // Check if MS extensions or some other language extensions are enabled. If |
| 4502 | // not, issue a diagnostic that the given attribute is unused. |
| 4503 | static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr, |
| 4504 | bool OtherExtension = false) { |
| 4505 | if (S.LangOpts.MicrosoftExt || OtherExtension) |
| 4506 | return true; |
| 4507 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4508 | return false; |
| 4509 | } |
| 4510 | |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4511 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4512 | if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland)) |
| 4513 | return; |
Chandler Carruth | 1731e20 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4514 | |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4515 | StringRef StrRef; |
| 4516 | SourceLocation LiteralLoc; |
Tim Northover | f8aebef | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 4517 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4518 | return; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4519 | |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4520 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 4521 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4522 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 4523 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4524 | |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4525 | // Validate GUID length. |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4526 | if (StrRef.size() != 36) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4527 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4528 | return; |
| 4529 | } |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4530 | |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4531 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4532 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4533 | if (StrRef[i] != '-') { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4534 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4535 | return; |
| 4536 | } |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4537 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 1a343e2 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4538 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4539 | return; |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4540 | } |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4541 | } |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4542 | |
David Majnemer | bb0ed29 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4543 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef, |
| 4544 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4545 | } |
| 4546 | |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4547 | static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4548 | if (!checkMicrosoftExt(S, Attr)) |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4549 | return; |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4550 | |
| 4551 | AttributeList::Kind Kind = Attr.getKind(); |
| 4552 | if (Kind == AttributeList::AT_SingleInheritance) |
| 4553 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4554 | ::new (S.Context) |
| 4555 | SingleInheritanceAttr(Attr.getRange(), S.Context, |
| 4556 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4557 | else if (Kind == AttributeList::AT_MultipleInheritance) |
| 4558 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4559 | ::new (S.Context) |
| 4560 | MultipleInheritanceAttr(Attr.getRange(), S.Context, |
| 4561 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | 7b89ab7 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4562 | else if (Kind == AttributeList::AT_VirtualInheritance) |
| 4563 | D->addAttr( |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4564 | ::new (S.Context) |
| 4565 | VirtualInheritanceAttr(Attr.getRange(), S.Context, |
| 4566 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4567 | } |
| 4568 | |
| 4569 | static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4570 | if (!checkMicrosoftExt(S, Attr)) |
| 4571 | return; |
| 4572 | |
| 4573 | AttributeList::Kind Kind = Attr.getKind(); |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4574 | if (Kind == AttributeList::AT_Win64) |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4575 | D->addAttr( |
| 4576 | ::new (S.Context) Win64Attr(Attr.getRange(), S.Context, |
| 4577 | Attr.getAttributeSpellingListIndex())); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4578 | } |
| 4579 | |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4580 | static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 8fbda8e | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4581 | if (!checkMicrosoftExt(S, Attr)) |
| 4582 | return; |
| 4583 | D->addAttr(::new (S.Context) |
| 4584 | ForceInlineAttr(Attr.getRange(), S.Context, |
| 4585 | Attr.getAttributeSpellingListIndex())); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4586 | } |
| 4587 | |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4588 | static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4589 | if (!checkMicrosoftExt(S, Attr)) |
| 4590 | return; |
| 4591 | // Check linkage after possibly merging declaratinos. See |
| 4592 | // checkAttributesAfterMerging(). |
| 4593 | D->addAttr(::new (S.Context) |
| 4594 | SelectAnyAttr(Attr.getRange(), S.Context, |
| 4595 | Attr.getAttributeSpellingListIndex())); |
| 4596 | } |
| 4597 | |
Aaron Ballman | bbb3b32 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4598 | /// Handles semantic checking for features that are common to all attributes, |
| 4599 | /// such as checking whether a parameter was properly specified, or the correct |
| 4600 | /// number of arguments were passed, etc. |
| 4601 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 4602 | const AttributeList &Attr) { |
| 4603 | // Several attributes carry different semantics than the parsing requires, so |
| 4604 | // those are opted out of the common handling. |
| 4605 | // |
| 4606 | // We also bail on unknown and ignored attributes because those are handled |
| 4607 | // as part of the target-specific handling logic. |
| 4608 | if (Attr.hasCustomParsing() || |
| 4609 | Attr.getKind() == AttributeList::UnknownAttribute || |
| 4610 | Attr.getKind() == AttributeList::IgnoredAttribute) |
| 4611 | return false; |
| 4612 | |
| 4613 | // If there are no optional arguments, then checking for the argument count |
| 4614 | // is trivial. |
| 4615 | if (Attr.getMinArgs() == Attr.getMaxArgs() && |
| 4616 | !checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 4617 | return true; |
| 4618 | return false; |
| 4619 | } |
| 4620 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4621 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4622 | // Top Level Sema Entry Points |
| 4623 | //===----------------------------------------------------------------------===// |
| 4624 | |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4625 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4626 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 4627 | /// silently ignore it if a GNU attribute. |
| 4628 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4629 | const AttributeList &Attr, |
| 4630 | bool IncludeCXX11Attributes) { |
| 4631 | if (Attr.isInvalid()) |
| 4632 | return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4633 | |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4634 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 4635 | // instead. |
| 4636 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 4637 | return; |
| 4638 | |
Aaron Ballman | bbb3b32 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4639 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 4640 | return; |
| 4641 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4642 | switch (Attr.getKind()) { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4643 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 4644 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 4645 | case AttributeList::AT_IBOutletCollection: |
| 4646 | handleIBOutletCollection(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4647 | case AttributeList::AT_AddressSpace: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4648 | case AttributeList::AT_ObjCGC: |
| 4649 | case AttributeList::AT_VectorSize: |
| 4650 | case AttributeList::AT_NeonVectorType: |
| 4651 | case AttributeList::AT_NeonPolyVectorType: |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4652 | case AttributeList::AT_Ptr32: |
| 4653 | case AttributeList::AT_Ptr64: |
| 4654 | case AttributeList::AT_SPtr: |
| 4655 | case AttributeList::AT_UPtr: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4656 | // Ignore these, these are type attributes, handled by |
| 4657 | // ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4658 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4659 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 4660 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 4661 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 4662 | case AttributeList::AT_AlwaysInline: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4663 | handleAlwaysInlineAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4664 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4665 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | 5e2d5de | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 4666 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4667 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 4668 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 4669 | case AttributeList::AT_CarriesDependency: |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4670 | handleDependencyAttr(S, scope, D, Attr); |
| 4671 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4672 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
| 4673 | case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break; |
| 4674 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4675 | case AttributeList::AT_CXX11NoReturn: |
| 4676 | handleCXX11NoReturnAttr(S, D, Attr); |
| 4677 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4678 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4679 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4680 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4681 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 4682 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4683 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4684 | break; |
Quentin Colombet | aee56fa | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4685 | case AttributeList::AT_MinSize: |
| 4686 | handleMinSizeAttr(S, D, Attr); |
| 4687 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4688 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4689 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4690 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
Aaron Ballman | 1951323 | 2013-08-28 23:13:26 +0000 | [diff] [blame] | 4691 | case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break; |
| 4692 | case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4693 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4694 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4695 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4696 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4697 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
| 4698 | case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break; |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4699 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4700 | case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break; |
| 4701 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4702 | case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4703 | case AttributeList::AT_ownership_returns: |
| 4704 | case AttributeList::AT_ownership_takes: |
| 4705 | case AttributeList::AT_ownership_holds: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4706 | handleOwnershipAttr (S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4707 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4708 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
| 4709 | case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break; |
| 4710 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
| 4711 | case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break; |
| 4712 | case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break; |
| 4713 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4714 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4715 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4716 | handleObjCOwnershipAttr(S, D, Attr); break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4717 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4718 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4719 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4720 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | dc7c5ad | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4721 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4722 | |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4723 | case AttributeList::AT_ObjCRequiresSuper: |
| 4724 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4725 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4726 | case AttributeList::AT_NSBridged: |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4727 | handleNSBridgedAttr(S, scope, D, Attr); break; |
Fariborz Jahanian | 933a894 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4728 | |
| 4729 | case AttributeList::AT_ObjCBridge: |
| 4730 | handleObjCBridgeAttr(S, scope, D, Attr); break; |
John McCall | fe98da0 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4731 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4732 | case AttributeList::AT_CFAuditedTransfer: |
| 4733 | case AttributeList::AT_CFUnknownTransfer: |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4734 | handleCFTransferAttr(S, D, Attr); break; |
| 4735 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4736 | // Checker-specific. |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4737 | case AttributeList::AT_CFConsumed: |
| 4738 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4739 | case AttributeList::AT_NSConsumesSelf: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4740 | handleNSConsumesSelfAttr(S, D, Attr); break; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4741 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4742 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4743 | case AttributeList::AT_NSReturnsNotRetained: |
| 4744 | case AttributeList::AT_CFReturnsNotRetained: |
| 4745 | case AttributeList::AT_NSReturnsRetained: |
| 4746 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4747 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4748 | |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4749 | case AttributeList::AT_WorkGroupSizeHint: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4750 | case AttributeList::AT_ReqdWorkGroupSize: |
Tanya Lattner | 0df579e | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4751 | handleWorkGroupSize(S, D, Attr); break; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 4752 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4753 | case AttributeList::AT_VecTypeHint: |
| 4754 | handleVecTypeHint(S, D, Attr); break; |
| 4755 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4756 | case AttributeList::AT_InitPriority: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4757 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4758 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4759 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4760 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4761 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 2dbdef2 | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4762 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | bc3260d | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4763 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4764 | case AttributeList::AT_ArcWeakrefUnavailable: |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 4765 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 4766 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4767 | case AttributeList::AT_ObjCRootClass: |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4768 | handleObjCRootClassAttr(S, D, Attr); |
| 4769 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4770 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 4771 | handleObjCRequiresPropertyDefsAttr (S, D, Attr); |
Fariborz Jahanian | e23dcf3 | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 4772 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4773 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4774 | case AttributeList::AT_ReturnsTwice: |
Rafael Espindola | f87cced | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 4775 | handleReturnsTwiceAttr(S, D, Attr); |
| 4776 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4777 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4778 | case AttributeList::AT_Visibility: |
| 4779 | handleVisibilityAttr(S, D, Attr, false); |
| 4780 | break; |
| 4781 | case AttributeList::AT_TypeVisibility: |
| 4782 | handleVisibilityAttr(S, D, Attr, true); |
| 4783 | break; |
Lubos Lunak | 1d3ce65 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 4784 | case AttributeList::AT_WarnUnused: |
| 4785 | handleWarnUnusedAttr(S, D, Attr); |
| 4786 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4787 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4788 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4789 | case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; |
| 4790 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4791 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4792 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4793 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4794 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4795 | case AttributeList::AT_ObjCException: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4796 | handleObjCExceptionAttr(S, D, Attr); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 4797 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4798 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4799 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4800 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4801 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4802 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4803 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
| 4804 | case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break; |
| 4805 | case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break; |
| 4806 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4807 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
| 4808 | case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break; |
| 4809 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4810 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4811 | // Just ignore |
| 4812 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4813 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4814 | handleNoInstrumentFunctionAttr(S, D, Attr); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 4815 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4816 | case AttributeList::AT_StdCall: |
| 4817 | case AttributeList::AT_CDecl: |
| 4818 | case AttributeList::AT_FastCall: |
| 4819 | case AttributeList::AT_ThisCall: |
| 4820 | case AttributeList::AT_Pascal: |
Charles Davis | e8519c3 | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4821 | case AttributeList::AT_MSABI: |
| 4822 | case AttributeList::AT_SysVABI: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4823 | case AttributeList::AT_Pcs: |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4824 | case AttributeList::AT_PnaclCall: |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4825 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4826 | handleCallConvAttr(S, D, Attr); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4827 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4828 | case AttributeList::AT_OpenCLKernel: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4829 | handleOpenCLKernelAttr(S, D, Attr); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4830 | break; |
Guy Benyei | 1db7040 | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4831 | case AttributeList::AT_OpenCLImageAccess: |
| 4832 | handleOpenCLImageAccessAttr(S, D, Attr); |
| 4833 | break; |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4834 | |
| 4835 | // Microsoft attributes: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4836 | case AttributeList::AT_MsStruct: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4837 | handleMsStructAttr(S, D, Attr); |
| 4838 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4839 | case AttributeList::AT_Uuid: |
Chandler Carruth | 1b03c87 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4840 | handleUuidAttr(S, D, Attr); |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4841 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4842 | case AttributeList::AT_SingleInheritance: |
| 4843 | case AttributeList::AT_MultipleInheritance: |
| 4844 | case AttributeList::AT_VirtualInheritance: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4845 | handleInheritanceAttr(S, D, Attr); |
| 4846 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4847 | case AttributeList::AT_Win64: |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4848 | handlePortabilityAttr(S, D, Attr); |
| 4849 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4850 | case AttributeList::AT_ForceInline: |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4851 | handleForceInlineAttr(S, D, Attr); |
| 4852 | break; |
Reid Kleckner | a722534 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4853 | case AttributeList::AT_SelectAny: |
| 4854 | handleSelectAnyAttr(S, D, Attr); |
| 4855 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4856 | |
| 4857 | // Thread safety attributes: |
DeLesley Hutchins | 5c6134f | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 4858 | case AttributeList::AT_AssertExclusiveLock: |
| 4859 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 4860 | break; |
| 4861 | case AttributeList::AT_AssertSharedLock: |
| 4862 | handleAssertSharedLockAttr(S, D, Attr); |
| 4863 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4864 | case AttributeList::AT_GuardedVar: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4865 | handleGuardedVarAttr(S, D, Attr); |
| 4866 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4867 | case AttributeList::AT_PtGuardedVar: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4868 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4869 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4870 | case AttributeList::AT_ScopedLockable: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4871 | handleScopedLockableAttr(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4872 | break; |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4873 | case AttributeList::AT_NoSanitizeAddress: |
| 4874 | handleNoSanitizeAddressAttr(S, D, Attr); |
Kostya Serebryany | 71efba0 | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4875 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4876 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4877 | handleNoThreadSafetyAnalysis(S, D, Attr); |
| 4878 | break; |
| 4879 | case AttributeList::AT_NoSanitizeThread: |
| 4880 | handleNoSanitizeThread(S, D, Attr); |
| 4881 | break; |
| 4882 | case AttributeList::AT_NoSanitizeMemory: |
| 4883 | handleNoSanitizeMemory(S, D, Attr); |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4884 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4885 | case AttributeList::AT_Lockable: |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4886 | handleLockableAttr(S, D, Attr); |
| 4887 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4888 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4889 | handleGuardedByAttr(S, D, Attr); |
| 4890 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4891 | case AttributeList::AT_PtGuardedBy: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4892 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4893 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4894 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4895 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4896 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4897 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4898 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4899 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4900 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4901 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4902 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4903 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4904 | handleLockReturnedAttr(S, D, Attr); |
| 4905 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4906 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4907 | handleLocksExcludedAttr(S, D, Attr); |
| 4908 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4909 | case AttributeList::AT_SharedLockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4910 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4911 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4912 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4913 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4914 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4915 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4916 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4917 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4918 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4919 | handleUnlockFunAttr(S, D, Attr); |
| 4920 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4921 | case AttributeList::AT_AcquiredBefore: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4922 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4923 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4924 | case AttributeList::AT_AcquiredAfter: |
Michael Han | dc69157 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4925 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | db33e14 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4926 | break; |
Caitlin Sadowski | fdde9e7 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4927 | |
DeLesley Hutchins | 1bf6343 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 4928 | // Consumed analysis attributes. |
DeLesley Hutchins | c55bee6 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 4929 | case AttributeList::AT_Consumable: |
| 4930 | handleConsumableAttr(S, D, Attr); |
| 4931 | break; |
DeLesley Hutchins | 6654085 | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 4932 | case AttributeList::AT_CallableWhen: |
| 4933 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4934 | break; |
DeLesley Hutchins | d4f0e19 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 4935 | case AttributeList::AT_ParamTypestate: |
| 4936 | handleParamTypestateAttr(S, D, Attr); |
| 4937 | break; |
DeLesley Hutchins | 0e8534e | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 4938 | case AttributeList::AT_ReturnTypestate: |
| 4939 | handleReturnTypestateAttr(S, D, Attr); |
| 4940 | break; |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4941 | case AttributeList::AT_SetTypestate: |
| 4942 | handleSetTypestateAttr(S, D, Attr); |
| 4943 | break; |
Chris Wailes | 0e429f1 | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 4944 | case AttributeList::AT_TestTypestate: |
| 4945 | handleTestTypestateAttr(S, D, Attr); |
DeLesley Hutchins | f30e194 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4946 | break; |
DeLesley Hutchins | df7bef0 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4947 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4948 | // Type safety attributes. |
| 4949 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4950 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4951 | break; |
| 4952 | case AttributeList::AT_TypeTagForDatatype: |
| 4953 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4954 | break; |
| 4955 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4956 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4957 | // Ask target about the attribute. |
| 4958 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 4959 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4960 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 4961 | diag::warn_unhandled_ms_attribute_ignored : |
| 4962 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4963 | break; |
| 4964 | } |
| 4965 | } |
| 4966 | |
| 4967 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4968 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4969 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4970 | const AttributeList *AttrList, |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4971 | bool IncludeCXX11Attributes) { |
| 4972 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4973 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4974 | |
| 4975 | // GCC accepts |
| 4976 | // static int a9 __attribute__((weakref)); |
| 4977 | // but that looks really pointless. We reject it. |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4978 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4979 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Rafael Espindola | 4d8a33b | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4980 | cast<NamedDecl>(D)->getNameAsString(); |
| 4981 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4982 | return; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4983 | } |
| 4984 | } |
| 4985 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4986 | // Annotation attributes are the only attributes allowed after an access |
| 4987 | // specifier. |
| 4988 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4989 | const AttributeList *AttrList) { |
| 4990 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4991 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4992 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4993 | } else { |
| 4994 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4995 | return true; |
| 4996 | } |
| 4997 | } |
| 4998 | |
| 4999 | return false; |
| 5000 | } |
| 5001 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5002 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 5003 | /// contains any decl attributes that we should warn about. |
| 5004 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 5005 | for ( ; A; A = A->getNext()) { |
| 5006 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 5007 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5008 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 5009 | |
| 5010 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 5011 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 5012 | << A->getName() << A->getRange(); |
| 5013 | } else { |
| 5014 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 5015 | << A->getName() << A->getRange(); |
| 5016 | } |
| 5017 | } |
| 5018 | } |
| 5019 | |
| 5020 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 5021 | /// used to build a declaration, complain about any decl attributes |
| 5022 | /// which might be lying around on it. |
| 5023 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 5024 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 5025 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 5026 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 5027 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 5028 | } |
| 5029 | |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5030 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5031 | /// \#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] | 5032 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 5033 | SourceLocation Loc) { |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5034 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5035 | NamedDecl *NewD = 0; |
| 5036 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5037 | FunctionDecl *NewFD; |
| 5038 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 5039 | // FIXME: Mangling? |
| 5040 | // FIXME: Is the qualifier info correct? |
| 5041 | // FIXME: Is the DeclContext correct? |
| 5042 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 5043 | Loc, Loc, DeclarationName(II), |
| 5044 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5045 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5046 | FD->hasPrototype(), |
| 5047 | false/*isConstexprSpecified*/); |
| 5048 | NewD = NewFD; |
| 5049 | |
| 5050 | if (FD->getQualifier()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5051 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5052 | |
| 5053 | // Fake up parameter variables; they are declared as if this were |
| 5054 | // a typedef. |
| 5055 | QualType FDTy = FD->getType(); |
| 5056 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 5057 | SmallVector<ParmVarDecl*, 16> Params; |
| 5058 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 5059 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 5060 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 5061 | Param->setScopeInfo(0, Params.size()); |
| 5062 | Params.push_back(Param); |
| 5063 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 5064 | NewFD->setParams(Params); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5065 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5066 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 5067 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5068 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5069 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5070 | VD->getStorageClass()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5071 | if (VD->getQualifier()) { |
| 5072 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5073 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5074 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5075 | } |
| 5076 | return NewD; |
| 5077 | } |
| 5078 | |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5079 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5080 | /// applied to it, possibly with an alias. |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5081 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5082 | if (W.getUsed()) return; // only do this once |
| 5083 | W.setUsed(true); |
| 5084 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 5085 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | 900693b | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5086 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5087 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 5088 | NDId->getName())); |
| 5089 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5090 | WeakTopLevelDecl.push_back(NewD); |
| 5091 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 5092 | // to insert Decl at TU scope, sorry. |
| 5093 | DeclContext *SavedContext = CurContext; |
| 5094 | CurContext = Context.getTranslationUnitDecl(); |
| 5095 | PushOnScopeChains(NewD, S); |
| 5096 | CurContext = SavedContext; |
| 5097 | } else { // just add weak to existing |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5098 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5099 | } |
| 5100 | } |
| 5101 | |
Rafael Espindola | 65611bf | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5102 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 5103 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 5104 | // have to do this. |
| 5105 | LoadExternalWeakUndeclaredIdentifiers(); |
| 5106 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 5107 | NamedDecl *ND = NULL; |
| 5108 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 5109 | if (VD->isExternC()) |
| 5110 | ND = VD; |
| 5111 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5112 | if (FD->isExternC()) |
| 5113 | ND = FD; |
| 5114 | if (ND) { |
| 5115 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 5116 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 5117 | = WeakUndeclaredIdentifiers.find(Id); |
| 5118 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 5119 | WeakInfo W = I->second; |
| 5120 | DeclApplyPragmaWeak(S, ND, W); |
| 5121 | WeakUndeclaredIdentifiers[Id] = W; |
| 5122 | } |
| 5123 | } |
| 5124 | } |
| 5125 | } |
| 5126 | } |
| 5127 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5128 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 5129 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 5130 | /// specified in many different places, and we need to find and apply them all. |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5131 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5132 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5133 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5134 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5135 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5136 | // Walk the declarator structure, applying decl attributes that were in a type |
| 5137 | // position to the decl itself. This handles cases like: |
| 5138 | // int *__attr__(x)** D; |
| 5139 | // when X is a decl attribute. |
| 5140 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 5141 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5142 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5143 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5144 | // Finally, apply any attributes on the decl itself. |
| 5145 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5146 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5147 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5148 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5149 | /// Is the given declaration allowed to use a forbidden type? |
| 5150 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 5151 | // Private ivars are always okay. Unfortunately, people don't |
| 5152 | // always properly make their ivars private, even in system headers. |
| 5153 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | a6b3380 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 5154 | // Function declarations in sys headers will be marked unavailable. |
| 5155 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 5156 | !isa<FunctionDecl>(decl)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5157 | return false; |
| 5158 | |
| 5159 | // Require it to be declared in a system header. |
| 5160 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 5161 | } |
| 5162 | |
| 5163 | /// Handle a delayed forbidden-type diagnostic. |
| 5164 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 5165 | Decl *decl) { |
| 5166 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 5167 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 5168 | "this system declaration uses an unsupported type")); |
| 5169 | return; |
| 5170 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5171 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5172 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 5173 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5174 | // kind of forbidden type messages on unavailable functions. |
| 5175 | if (FD->hasAttr<UnavailableAttr>() && |
| 5176 | diag.getForbiddenTypeDiagnostic() == |
| 5177 | diag::err_arc_array_param_no_ownership) { |
| 5178 | diag.Triggered = true; |
| 5179 | return; |
| 5180 | } |
| 5181 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5182 | |
| 5183 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 5184 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 5185 | diag.Triggered = true; |
| 5186 | } |
| 5187 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5188 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 5189 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5190 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5191 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5192 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5193 | // When delaying diagnostics to run in the context of a parsed |
| 5194 | // declaration, we only want to actually emit anything if parsing |
| 5195 | // succeeds. |
| 5196 | if (!decl) return; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5197 | |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5198 | // We emit all the active diagnostics in this pool or any of its |
| 5199 | // parents. In general, we'll get one pool for the decl spec |
| 5200 | // and a child pool for each declarator; in a decl group like: |
| 5201 | // deprecated_typedef foo, *bar, baz(); |
| 5202 | // only the declarator pops will be passed decls. This is correct; |
| 5203 | // we really do need to consider delayed diagnostics from the decl spec |
| 5204 | // for each of the different declarations. |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5205 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5206 | do { |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5207 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5208 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 5209 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 5210 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5211 | if (diag.Triggered) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5212 | continue; |
| 5213 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5214 | switch (diag.Kind) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5215 | case DelayedDiagnostic::Deprecation: |
John McCall | e8c904f | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 5216 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 5217 | if (!decl->isInvalidDecl()) |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5218 | HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5219 | break; |
| 5220 | |
| 5221 | case DelayedDiagnostic::Access: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5222 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5223 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5224 | |
| 5225 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5226 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5227 | break; |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5228 | } |
| 5229 | } |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5230 | } while ((pool = pool->getParent())); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5231 | } |
| 5232 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5233 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 5234 | /// been delayed in the current context instead of in the given pool. |
| 5235 | /// Essentially, this just moves them to the current pool. |
| 5236 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 5237 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 5238 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 5239 | curPool->steal(pool); |
| 5240 | } |
| 5241 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5242 | static bool isDeclDeprecated(Decl *D) { |
| 5243 | do { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5244 | if (D->isDeprecated()) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5245 | return true; |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 5246 | // A category implicitly has the availability of the interface. |
| 5247 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 5248 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5249 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 5250 | return false; |
| 5251 | } |
| 5252 | |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5253 | static void |
| 5254 | DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message, |
| 5255 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5256 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5257 | const ObjCPropertyDecl *ObjCPropery) { |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5258 | DeclarationName Name = D->getDeclName(); |
| 5259 | if (!Message.empty()) { |
| 5260 | S.Diag(Loc, diag::warn_deprecated_message) << Name << Message; |
| 5261 | S.Diag(D->getLocation(), |
| 5262 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5263 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5264 | if (ObjCPropery) |
| 5265 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5266 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5267 | } else if (!UnknownObjCClass) { |
| 5268 | S.Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 5269 | S.Diag(D->getLocation(), |
| 5270 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5271 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5272 | if (ObjCPropery) |
| 5273 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5274 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5275 | } else { |
| 5276 | S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name; |
| 5277 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 5278 | } |
| 5279 | } |
| 5280 | |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 5281 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5282 | Decl *Ctx) { |
| 5283 | if (isDeclDeprecated(Ctx)) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5284 | return; |
| 5285 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5286 | DD.Triggered = true; |
Eli Friedman | c3b2308 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5287 | DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(), |
| 5288 | DD.getDeprecationMessage(), DD.Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5289 | DD.getUnknownObjCClass(), |
| 5290 | DD.getObjCProperty()); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5291 | } |
| 5292 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5293 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 5294 | SourceLocation Loc, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5295 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5296 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5297 | // Delay if we're currently parsing a declaration. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5298 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5299 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 5300 | UnknownObjCClass, |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5301 | ObjCProperty, |
Fariborz Jahanian | b0a6615 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5302 | Message)); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5303 | return; |
| 5304 | } |
| 5305 | |
| 5306 | // Otherwise, don't warn if our current context is deprecated. |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 5307 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5308 | return; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5309 | DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5310 | } |